Browse Source

Add generic InterfaceInfo and RouterConfig class

RouterConfig is a wrapper around raw filesystem like
Vyatta router configuration.

Change-Id: I2781d38d050146c0beaedb78c50ce23d2720f756
Harkirat Singh 3 years ago
parent
commit
fa8140f680

+ 20
- 0
networking_brocade/vyatta/common/utils.py View File

@@ -16,6 +16,7 @@
16 16
 import collections
17 17
 
18 18
 from eventlet import greenthread
19
+import netaddr
19 20
 import six
20 21
 
21 22
 
@@ -107,3 +108,22 @@ class MultiDict(collections.MutableMapping):
107 108
 
108 109
     def listvalues(self):
109 110
         return six.itervalues(self._items)
111
+
112
+
113
+class InterfaceInfo(object):
114
+
115
+    def __init__(self, name=None, ip_addresses=None,
116
+                 mac_address=None, gateway=None):
117
+
118
+        self.name = name
119
+        self.mac_address = mac_address
120
+        self.gateway = gateway
121
+
122
+        self.ip_addresses = []
123
+        for addr in ip_addresses:
124
+            if isinstance(addr, str):
125
+                addr = netaddr.IPNetwork(addr)
126
+            elif not isinstance(addr, netaddr.IPNetwork):
127
+                raise TypeError('IP address {0!r} should be '
128
+                                'str or netaddr.IPNetwork')
129
+            self.ip_addresses.append(addr)

+ 42
- 0
networking_brocade/vyatta/common/vrouter_config.py View File

@@ -13,8 +13,12 @@
13 13
 #    License for the specific language governing permissions and limitations
14 14
 #    under the License.
15 15
 
16
+import six
17
+
16 18
 from networking_brocade.vyatta.common import utils
17 19
 
20
+
21
+# TODO(asaprykin): Move to parsers
18 22
 TOKEN_GROUP = 'group'
19 23
 TOKEN_PARAM = 'param'
20 24
 TOKEN_END = 'end'
@@ -64,3 +68,41 @@ def parse_group(lines):
64 68
 def parse_config(config):
65 69
     lines = config_iter(config)
66 70
     return parse_group(lines)
71
+
72
+
73
+class RouterConfig(object):
74
+
75
+    EXTERNAL_GATEWAY = 'External_Gateway'
76
+    ROUTER_INTERFACE = 'Router_Interface'
77
+
78
+    def __init__(self, data):
79
+        self.data = data
80
+
81
+    def interfaces(self):
82
+        try:
83
+            ifaces = self.data['interfaces']
84
+        except KeyError:
85
+            return
86
+
87
+        for key, params in six.iteritems(ifaces):
88
+            type_, name = key.split()
89
+
90
+            info = utils.MultiDict(params)
91
+            info['name'] = name
92
+            info['type'] = type_
93
+
94
+            yield info
95
+
96
+    def find_interface(self, name):
97
+        for interface in self.interfaces():
98
+            if interface['name'] == name:
99
+                return interface
100
+
101
+    def router_interfaces(self):
102
+        for iface in self.interfaces():
103
+            if iface['descr'] == self.ROUTER_INTERFACE:
104
+                yield iface
105
+
106
+    @classmethod
107
+    def from_string(cls, string):
108
+        return cls(parse_config(string))

Loading…
Cancel
Save