Debian Codegraph @

networking_baremetal/openconfig/lacp/lacp.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 import constants
17 from networking_baremetal.openconfig.lacp import types
18
19
20 class LACP:
21 """LACP Top level
22
23 LACP configuration and state variable containers
24 """
25
26 NAMESPACE = 'http://openconfig.net/yang/lacp'
27 TAG = 'lacp'
28
29 def __init__(self):
30 self._config = None
31 self._interfaces = LACPInterfaces()
32
33 @property
34 def interfaces(self):
35 return self._interfaces
36
37 @interfaces.setter
38 def interfaces(self, value):
39 if not isinstance(value, LACPInterfaces):
40 raise TypeError('interfaces must be OpenConfigLACPInterfaces,'
41 'got {}'.format(type(value)))
42 self._interfaces = value
43
44 @interfaces.deleter
45 def interfaces(self):
46 self._interfaces = None
47
48 def to_xml_element(self):
49 """Create XML Element
50
51 :return: ElementTree Element with SubElements
52 """
53 elem = ElementTree.Element(self.TAG)
54 elem.set('xmlns', self.NAMESPACE)
55 if self.interfaces:
56 elem.append(self.interfaces.to_xml_element())
57 return elem
58
59
60 class LACPInterfaces(abc.Collection):
61 """Top-level grouping for LACP-enabled interfaces"""
62
63 NAMESPACE = 'http://openconfig.net/yang/lacp'
64 PARENT = 'lacp'
65 TAG = 'interfaces'
66
67 def __init__(self):
68 # List of interfaces of type OpenconfigInterface
69 self._interfaces = list()
70
71 def __iter__(self):
72 return iter(self._interfaces)
73
74 def __len__(self):
75 return len(self._interfaces)
76
77 def __contains__(self, item):
78 return item in self._interfaces
79
80 @property
81 def interfaces(self):
82 """List of interfaces"""
83 return self._interfaces
84
85 def add(self, name: str):
86 """Add interface
87
88 :param name: Interface name
89 :type: str
90 """
91 interface = LACPInterface(name)
92 self._interfaces.append(interface)
93 return interface
94
95 def to_xml_element(self):
96 """Create XML Element
97
98 :return: ElementTree Element with SubElements
99 """
100 elem = ElementTree.Element(self.TAG)
101 for interface in self.interfaces:
102 elem.append(interface.to_xml_element())
103 return elem
104
105
106 class LACPInterface:
107 """Base LACP aggregate interface"""
108
109 NAMESPACE = 'http://openconfig.net/yang/lacp'
110 PARENT = 'interfaces'
111 TAG = 'interface'
112
113 def __init__(self, name: str,
114 operation=constants.NetconfEditConfigOperation.MERGE):
115 self.operation = operation
116 self._config = LACPInterfaceConfig(name)
117 self.name = name
118
119 @property
120 def operation(self):
121 """RFC 6241 - <edit-config> operation attribute"""
122 return self._operation.value
123
124 @operation.setter
125 def operation(self, value):
126 """RFC 6241 - <edit-config> operation attribute"""
127 if isinstance(value, constants.NetconfEditConfigOperation):
128 self._operation = value
129 elif isinstance(value, str):
130 self._operation = constants.NetconfEditConfigOperation(value)
131 else:
132 raise TypeError('Invalid type {} for config operation attribute.'
133 .format(type(value)))
134
135 @property
136 def name(self):
137 """The name of the LACP aggregate interface."""
138 return self._name
139
140 @name.setter
141 def name(self, value: str):
142 """The name of the LACP aggregate interface."""
143 if not isinstance(value, str):
144 raise TypeError('name must be string, got {}'.format(type(value)))
145 self._name = value
146 # 'name' in the configuration is leaf-ref, should match.
147 if self.config is not None:
148 self.config.name = self.name
149
150 @name.deleter
151 def name(self):
152 self._name = None
153
154 @property
155 def config(self):
156 """Configuration data for each LACP aggregate interface"""
157 return self._config
158
159 @config.setter
160 def config(self, value):
161 if not isinstance(value, LACPInterfaceConfig):
162 raise TypeError('config must be LACPInterfaceConfig,'
163 'got {}'.format(type(value)))
164 self._config = value
165
166 @config.deleter
167 def config(self):
168 self._config = None
169
170 def to_xml_element(self):
171 """Create XML Element
172
173 :return: ElementTree Element with SubElements
174 """
175 elem = ElementTree.Element(self.TAG)
176 elem.set('operation', self.operation)
177 if self.name:
178 common.txt_subelement(elem, 'name', self.name)
179 if self.config:
180 elem.append(self.config.to_xml_element())
181 return elem
182
183
184 class LACPInterfaceConfig:
185 """OpenConfig LACP aggregate interface configuration"""
186
187 NAMESPACE = 'http://openconfig.net/yang/lacp'
188 PARENT = 'interface'
189 TAG = 'config'
190
191 def __init__(self, name: str,
192 operation=constants.NetconfEditConfigOperation.MERGE,
193 interval=types.LACPPeriod.SLOW,
194 lacp_mode=types.LACPActivity.ACTIVE):
195 self._name = name
196 self.operation = operation
197 self.interval = interval
198 self.lacp_mode = lacp_mode
199
200 @property
201 def operation(self):
202 """RFC 6241 - <edit-config> operation attribute"""
203 return self._operation.value
204
205 @operation.setter
206 def operation(self, value):
207 """RFC 6241 - <edit-config> operation attribute"""
208 if isinstance(value, constants.NetconfEditConfigOperation):
209 self._operation = value
210 elif isinstance(value, str):
211 self._operation = constants.NetconfEditConfigOperation(value)
212 else:
213 raise TypeError('Invalid type {} for config operation attribute.'
214 .format(type(value)))
215
216 @property
217 def name(self):
218 """The name of the interface."""
219 return self._name
220
221 @name.setter
222 def name(self, value: str):
223 if not isinstance(value, str):
224 raise TypeError('name must be string, got {}'.format(type(value)))
225 self._name = value
226
227 @name.deleter
228 def name(self):
229 self._name = None
230
231 @property
232 def interval(self):
233 """The period between LACP messages"""
234 return self._interval.value if self._interval else None
235
236 @interval.setter
237 def interval(self, value: str):
238 """Set the period between LACP messages (SLOW or FAST)"""
239 if isinstance(value, types.LACPPeriod):
240 self._interval = value
241 elif isinstance(value, str):
242 self._interval = types.LACPPeriod(value)
243 else:
244 raise TypeError('Invalid type {} for LACP interface interval.'
245 .format(type(value)))
246
247 @interval.deleter
248 def interval(self):
249 self._interval = None
250
251 @property
252 def lacp_mode(self):
253 """The LACP mode if the aggregate interface"""
254 return self._lacp_mode.value
255
256 @lacp_mode.setter
257 def lacp_mode(self, value: str):
258 """Set the LACP mode if the aggregate interface
259
260 ACTIVE: is to initiate the transmission of LACP packets.
261 PASSIVE: is to wait for peer to initiate the transmission of
262 LACP packets
263 """
264 if isinstance(value, types.LACPActivity):
265 self._lacp_mode = value
266 elif isinstance(value, str):
267 self._lacp_mode = types.LACPActivity(value)
268 else:
269 raise TypeError('Invalid type {} for LACP interface mode.'
270 .format(type(value)))
271
272 @lacp_mode.deleter
273 def lacp_mode(self):
274 self._lacp_mode = None
275
276 def to_xml_element(self):
277 """Create XML Element
278
279 :return: ElementTree Element with SubElements
280 """
281 elem = ElementTree.Element(self.TAG)
282 elem.set('operation', self.operation)
283 if self.name is not None:
284 common.txt_subelement(elem, 'name', self.name)
285 if self.interval is not None:
286 common.txt_subelement(elem, 'interval', self.interval)
287 if self.lacp_mode is not None:
288 common.txt_subelement(elem, 'lacp-mode', self.lacp_mode)
289 return elem