1 # Copyright 2016 Cisco Systems, Inc. All rights reserved.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
17 class Interface(object):
18 """A class to hold the RX and TX counters for a virtual or physical interface."""
20 def __init__(self, name, device, tx_packets, rx_packets):
21 """Create a new interface instance."""
29 def set_packets(self, tx, rx):
30 """Set tx and rx counters for this interface."""
36 def set_packets_diff(self, tx, rx):
37 """Subtract current counters from new set of counters and update with results."""
39 'tx': tx - self.packets['tx'],
40 'rx': rx - self.packets['rx'],
44 """Check if this interface is a no-opn interface."""
45 return self.name is None
47 def get_packet_count(self, traffic_type):
48 """Get packet count for given direction."""
49 return self.packets.get(traffic_type, 0)
53 """Return an interface that doe snot pass any traffic."""
54 return Interface(None, None, 0, 0)
57 class Network(object):
58 """This class holds all interfaces that make up a logical neutron network.
60 A loopback packet path has exactly 2 networks.
61 The first interface is always one of the 2 traffic gen interface.
62 Subsequent interfaces are sorted along the path from the TG to the loopback point
63 which could be interfaces in a switch, a vswitch or a VM.
66 def __init__(self, interfaces=None, reverse=False):
67 """Create a network with initial interface list and direction.
69 :param interfaces: initial interface list
70 :param reverse: specifies the order of interfaces returned by get_interfaces
72 if interfaces is None:
74 self.interfaces = interfaces
75 self.reverse = reverse
77 def add_interface(self, interface):
78 """Add one more interface to this network.
80 Order if important as interfaces must be added from traffic generator ports towards then
83 self.interfaces.append(interface)
85 def get_interfaces(self):
86 """Get interfaces associated to this network.
88 Returned interface list is ordered from traffic generator port towards looping device if
89 reverse is false. Else returms the list in the reverse order.
91 return self.interfaces[::-1] if self.reverse else self.interfaces