63cc48f0346b06184c536a49c104f63e014810f2
[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 import traceback
18
19 from log import LOG
20 from service_chain import ServiceChain
21 from traffic_client import TrafficClient
22
23
24 class ChainRunner(object):
25     """Run selected chain, collect results and analyse them."""
26
27     def __init__(self, config, clients, cred, specs, factory, notifier=None):
28         self.config = config
29         self.clients = clients
30         self.specs = specs
31         self.factory = factory
32         self.chain_name = self.config.service_chain
33
34         try:
35             TORClass = factory.get_tor_class(self.config.tor.type, self.config.no_tor_access)
36         except AttributeError:
37             raise Exception("Requested TOR class '{}' was not found.".format(self.config.tor.type))
38
39         self.clients['tor'] = TORClass(self.config.tor.switches)
40         self.clients['traffic'] = TrafficClient(config, notifier)
41         self.chain = ServiceChain(config, clients, cred, specs, factory, notifier)
42
43         LOG.info('ChainRunner initialized.')
44
45     def run(self):
46         """
47         Run a chain, collect and analyse results.
48
49         :return: dictionary
50         """
51         self.clients['traffic'].start_traffic_generator()
52         self.clients['traffic'].set_macs()
53
54         return self.chain.run()
55
56     def close(self):
57         try:
58             if not self.config.no_cleanup:
59                 LOG.info('Cleaning up...')
60             else:
61                 LOG.info('Clean up skipped.')
62
63             for client in ['traffic', 'tor']:
64                 try:
65                     self.clients[client].close()
66                 except Exception as e:
67                     traceback.print_exc()
68                     LOG.error(e)
69
70             self.chain.close()
71         except Exception:
72             traceback.print_exc()
73             LOG.error('Cleanup not finished.')
74
75     def get_version(self):
76         versions = {
77             'Traffic Generator': self.clients['traffic'].get_version(),
78             'TOR': self.clients['tor'].get_version(),
79         }
80
81         versions.update(self.chain.get_version())
82
83         return versions