Debian Codegraph @

networking_baremetal/openconfig/network_instance/network_instance.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12 from collections import abc
13 from xml.etree import ElementTree
14
15 from networking_baremetal import common
16 from networking_baremetal.openconfig.vlan import vlan
17
18
19 class NetworkInstances(abc.Collection):
20 """Top-level grouping containing a list of network instances."""
21
22 NAMESPACE = 'http://openconfig.net/yang/network-instance'
23 TAG = 'network-instances'
24
25 def __init__(self):
26 self._network_instances = list()
27
28 def __iter__(self):
29 return iter(self._network_instances)
30
31 def __len__(self):
32 return len(self._network_instances)
33
34 def __contains__(self, item):
35 return item in self._network_instances
36
37 @property
38 def network_instances(self):
39 return self._network_instances
40
41 def add(self, name: str):
42 """Add network instance
43
44 :param name: A unique name identifying the network instance
45 :type: str
46 :Keyword arguments: Network instance arguments
47 """
48 network_instance = NetworkInstance(name)
49 self._network_instances.append(network_instance)
50 return network_instance
51
52 def to_xml_element(self):
53 """Create XML Element
54
55 :return: ElementTree Element with SubElements
56 """
57 elem = ElementTree.Element(self.TAG)
58 elem.set('xmlns', self.NAMESPACE)
59 for instance in self.network_instances:
60 elem.append(instance.to_xml_element())
61 return elem
62
63
64 class NetworkInstance:
65 """An OpenConfig description of a network_instance.
66
67 This may be a Layer 3 forwarding construct such as a virtual
68 routing and forwarding (VRF) instance, or a Layer 2 instance
69 such as a virtual switch instance (VSI). Mixed Layer 2 and
70 Layer 3 instances are also supported.
71 """
72 NAMESPACE = 'http://openconfig.net/yang/network-instance'
73 TAG = 'network-instance'
74
75 def __init__(self, name):
76 self.name = name
77 self._vlans = vlan.Vlans()
78
79 @property
80 def name(self):
81 """A unique name identifying the network instance"""
82 return self._name
83
84 @name.setter
85 def name(self, value: str):
86 """A unique name identifying the network instance"""
87 if not isinstance(value, str):
88 raise TypeError('name must be string, got {}'.format(type(value)))
89 self._name = value
90
91 @name.deleter
92 def name(self):
93 self._name = None
94
95 @property
96 def vlans(self):
97 """Group/List of VLANs - keyed by id"""
98 return self._vlans
99
100 @vlans.setter
101 def vlans(self, value):
102 if not isinstance(value, vlan.Vlans):
103 raise TypeError('vlans must be Vlans, got {}'.format(type(value)))
104 self._vlans = value
105
106 @vlans.deleter
107 def vlans(self):
108 self._vlans = None
109
110 def to_xml_element(self):
111 """Create XML Element
112
113 :return: ElementTree Element with SubElements
114 """
115 elem = ElementTree.Element(self.TAG)
116 if self.name:
117 common.txt_subelement(elem, 'name', self.name)
118 if self.vlans:
119 elem.append(self.vlans.to_xml_element())
120 return elem