NFVBENCH-40 Add pylint to tox
[nfvbench.git] / nfvbench / service_chain.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 collections import OrderedDict
18 import time
19
20 from chain_managers import StageManager
21 from log import LOG
22 from specs import ChainType
23
24
25 class ServiceChain(object):
26
27     def __init__(self, config, clients, cred, specs, factory, notifier=None):
28         self.config = config
29         self.clients = clients
30         self.cred = cred
31         self.specs = specs
32         self.factory = factory
33         self.notifier = notifier
34         self.chain_name = self.config.service_chain
35         self.vlans = None
36         self.stage_manager = None
37         self.stats_manager = None
38         LOG.info('ServiceChain initialized.')
39
40     def __set_helpers(self):
41         self.stage_manager = StageManager(self.config, self.cred, self.factory)
42         self.clients['vm'] = self.stage_manager
43         self.vlans = self.stage_manager.get_vlans()
44
45         STATS_CLASS = self.factory.get_stats_class(self.config.service_chain)
46         self.stats_manager = STATS_CLASS(self.config,
47                                          self.clients,
48                                          self.specs,
49                                          self.factory,
50                                          self.vlans,
51                                          self.notifier)
52
53     def __set_vlan_tags(self):
54         if self.config.vlan_tagging:
55             # override with user-specified vlans if configured
56             vlans = self.config.vlans if self.config.vlans else self.vlans[:2]
57             for vlan, device in zip(vlans, self.config.generator_config.devices):
58                 self.stats_manager.set_vlan_tag(device, vlan)
59
60     def __get_result_per_frame_size(self, frame_size, bidirectional):
61         start_time = time.time()
62         traffic_result = {
63             frame_size: {}
64         }
65         result = {}
66         if not self.config.no_traffic:
67             self.clients['traffic'].set_traffic(frame_size, bidirectional)
68
69             if self.config.single_run:
70                 result = self.stats_manager.run()
71             else:
72                 results = self.clients['traffic'].get_ndr_and_pdr()
73
74                 for dr in ['pdr', 'ndr']:
75                     if dr in results:
76                         traffic_result[frame_size][dr] = results[dr]
77                         if 'warning' in results[dr]['stats'] and results[dr]['stats']['warning']:
78                             traffic_result['warning'] = results[dr]['stats']['warning']
79                 traffic_result[frame_size]['iteration_stats'] = results['iteration_stats']
80
81             result['analysis_duration_sec'] = time.time() - start_time
82             if self.config.single_run:
83                 result['run_config'] = self.clients['traffic'].get_run_config(result)
84                 required = result['run_config']['direction-total']['orig']['rate_pps']
85                 actual = result['stats']['total_tx_rate']
86                 warning = self.clients['traffic'].compare_tx_rates(required, actual)
87                 if warning is not None:
88                     result['run_config']['warning'] = warning
89
90         traffic_result[frame_size].update(result)
91         return traffic_result
92
93     def __get_chain_result(self):
94         result = OrderedDict()
95         for fs in self.config.frame_sizes:
96             result.update(self.__get_result_per_frame_size(fs, self.config.traffic.bidirectional))
97
98         chain_result = {
99             'flow_count': self.config.flow_count,
100             'service_chain_count': self.config.service_chain_count,
101             'bidirectional': self.config.traffic.bidirectional,
102             'profile': self.config.traffic.profile,
103             'compute_nodes': self.stats_manager.get_compute_nodes_bios(),
104             'result': result
105         }
106
107         return chain_result
108
109     def __setup_traffic(self):
110         self.clients['traffic'].setup()
111         if not self.config.no_traffic:
112             if self.config.service_chain == ChainType.EXT and not self.config.no_arp:
113                 self.clients['traffic'].ensure_arp_successful()
114             self.clients['traffic'].ensure_end_to_end()
115
116     def run(self):
117         LOG.info('Starting %s chain...', self.chain_name)
118         LOG.info('Dry run: %s', self.config.no_traffic)
119         results = {}
120
121         self.__set_helpers()
122         self.__set_vlan_tags()
123         self.stage_manager.set_vm_macs()
124         self.__setup_traffic()
125         results[self.chain_name] = {'result': self.__get_chain_result()}
126
127         if self.config.service_chain == ChainType.PVVP:
128             results[self.chain_name]['mode'] = 'inter-node' \
129                 if self.config.inter_node else 'intra-node'
130
131         LOG.info("Service chain '%s' run completed.", self.chain_name)
132         return results
133
134     def get_version(self):
135         return self.stats_manager.get_version()
136
137     def close(self):
138         if self.stage_manager:
139             self.stage_manager.close()
140         if self.stats_manager:
141             self.stats_manager.close()