Initial code drop from Cisco
[nfvbench.git] / nfvbench / chain_runner.py
1 #!/usr/bin/env python
2 # Copyright 2016 Cisco Systems, Inc.  All rights reserved.
3 #
4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
5 #    not use this file except in compliance with the License. You may obtain
6 #    a copy of the License at
7 #
8 #         http://www.apache.org/licenses/LICENSE-2.0
9 #
10 #    Unless required by applicable law or agreed to in writing, software
11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 #    License for the specific language governing permissions and limitations
14 #    under the License.
15 #
16
17 from log import LOG
18 from service_chain import ServiceChain
19 import traceback
20 from traffic_client import TrafficClient
21
22
23 class ChainRunner(object):
24     """Run selected chain, collect results and analyse them."""
25
26     def __init__(self, config, clients, cred, specs, factory, notifier=None):
27         self.config = config
28         self.clients = clients
29         self.specs = specs
30         self.factory = factory
31         self.chain_name = self.config.service_chain
32
33         try:
34             TORClass = factory.get_tor_class(self.config.tor.type, self.config.no_tor_access)
35         except AttributeError:
36             raise Exception("Requested TOR class '{}' was not found.".format(self.config.tor.type))
37
38         self.clients['tor'] = TORClass(self.config.tor.switches)
39         self.clients['traffic'] = TrafficClient(config, notifier)
40         self.chain = ServiceChain(config, clients, cred, specs, factory, notifier)
41
42         LOG.info('ChainRunner initialized.')
43
44     def run(self):
45         """
46         Run a chain, collect and analyse results.
47
48         :return: dictionary
49         """
50         self.clients['traffic'].start_traffic_generator()
51         self.clients['traffic'].set_macs()
52
53         return self.chain.run()
54
55     def close(self):
56         try:
57             if not self.config.no_cleanup:
58                 LOG.info('Cleaning up...')
59             else:
60                 LOG.info('Clean up skipped.')
61
62             for client in ['traffic', 'tor']:
63                 try:
64                     self.clients[client].close()
65                 except Exception as e:
66                     traceback.print_exc()
67                     LOG.error(e)
68
69             self.chain.close()
70         except Exception:
71             traceback.print_exc()
72             LOG.error('Cleanup not finished.')
73
74     def get_version(self):
75         versions = {
76             'Traffic Generator': self.clients['traffic'].get_version(),
77             'TOR': self.clients['tor'].get_version(),
78         }
79
80         versions.update(self.chain.get_version())
81
82         return versions