Initial code drop from Cisco
[nfvbench.git] / nfvbench / network.py
1 # Copyright 2016 Cisco Systems, Inc.  All rights reserved.
2 #
3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
4 #    not use this file except in compliance with the License. You may obtain
5 #    a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #    Unless required by applicable law or agreed to in writing, software
10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 #    License for the specific language governing permissions and limitations
13 #    under the License.
14 #
15
16
17 class Interface(object):
18
19     def __init__(self, name, device, tx_packets, rx_packets):
20         self.name = name
21         self.device = device
22         self.packets = {
23             'tx': tx_packets,
24             'rx': rx_packets
25         }
26
27     def set_packets(self, tx, rx):
28         self.packets = {
29             'tx': tx,
30             'rx': rx
31         }
32
33     def set_packets_diff(self, tx, rx):
34         self.packets = {
35             'tx': tx - self.packets['tx'],
36             'rx': rx - self.packets['rx'],
37         }
38
39     def is_no_op(self):
40         return self.name is None
41
42     def get_packet_count(self, traffic_type):
43         return self.packets.get(traffic_type, 0)
44
45     @staticmethod
46     def no_op():
47         return Interface(None, None, 0, 0)
48
49
50 class Network(object):
51
52     def __init__(self, interfaces=None, reverse=False):
53         if interfaces is None:
54             interfaces = []
55         self.interfaces = interfaces
56         self.reverse = reverse
57
58     def add_interface(self, interface):
59         self.interfaces.append(interface)
60
61     def get_interfaces(self):
62         return self.interfaces[::-1] if self.reverse else self.interfaces