| 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 typing import Optional |
| 14 |
from xml.etree import ElementTree |
| 15 |
|
| 16 |
from networking_baremetal import common |
| 17 |
from networking_baremetal import constants |
| 18 |
from networking_baremetal.openconfig.interfaces import aggregate |
| 19 |
from networking_baremetal.openconfig.interfaces import ethernet |
| 20 |
|
| 21 |
|
| 22 |
class InterfaceConfig: |
| 23 |
"""OpenConfig interface configuration""" |
| 24 |
|
| 25 |
NAMESPACE = 'http://openconfig.net/yang/interfaces' |
| 26 |
PARENT = 'interface' |
| 27 |
TAG = 'config' |
| 28 |
|
| 29 |
def __init__(self, |
| 30 |
operation=constants.NetconfEditConfigOperation.MERGE, |
| 31 |
name: Optional[str] = None, |
| 32 |
description: Optional[str] = None, |
| 33 |
enabled: Optional[bool] = None, |
| 34 |
mtu: Optional[int] = None): |
| 35 |
self.operation = operation |
| 36 |
self._name = name |
| 37 |
self._description = None |
| 38 |
self._enabled = None |
| 39 |
self._mtu = None |
| 40 |
if description: |
| 41 |
self.description = description |
| 42 |
if enabled is not None: |
| 43 |
self.enabled = enabled |
| 44 |
if mtu: |
| 45 |
self.mtu = mtu |
| 46 |
|
| 47 |
@property |
| 48 |
def operation(self): |
| 49 |
"""RFC 6241 - <edit-config> operation attribute""" |
| 50 |
return self._operation.value if self._operation else None |
| 51 |
|
| 52 |
@operation.setter |
| 53 |
def operation(self, value): |
| 54 |
"""RFC 6241 - <edit-config> operation attribute""" |
| 55 |
if isinstance(value, constants.NetconfEditConfigOperation): |
| 56 |
self._operation = value |
| 57 |
elif isinstance(value, str): |
| 58 |
self._operation = constants.NetconfEditConfigOperation(value) |
| 59 |
else: |
| 60 |
raise TypeError('Invalid type {} for config operation attribute.' |
| 61 |
.format(type(value))) |
| 62 |
|
| 63 |
@operation.deleter |
| 64 |
def operation(self): |
| 65 |
"""RFC 6241 - <edit-config> operation attribute""" |
| 66 |
self._operation = None |
| 67 |
|
| 68 |
@property |
| 69 |
def name(self): |
| 70 |
"""The name of the interface.""" |
| 71 |
return self._name |
| 72 |
|
| 73 |
@name.setter |
| 74 |
def name(self, value: str): |
| 75 |
"""The name of the interface.""" |
| 76 |
if not isinstance(value, str): |
| 77 |
raise TypeError('name must be string, got {}'.format(type(value))) |
| 78 |
self._name = value |
| 79 |
|
| 80 |
@name.deleter |
| 81 |
def name(self): |
| 82 |
"""The name of the interface.""" |
| 83 |
self._name = None |
| 84 |
|
| 85 |
@property |
| 86 |
def description(self): |
| 87 |
"""A textual description of the interface""" |
| 88 |
return self._description |
| 89 |
|
| 90 |
@description.setter |
| 91 |
def description(self, value: str): |
| 92 |
"""A textual description of the interface""" |
| 93 |
if not isinstance(value, str): |
| 94 |
raise TypeError('description must be string, got {}' |
| 95 |
.format(type(value))) |
| 96 |
self._description = value |
| 97 |
|
| 98 |
@description.deleter |
| 99 |
def description(self): |
| 100 |
self._description = None |
| 101 |
|
| 102 |
@property |
| 103 |
def enabled(self): |
| 104 |
"""The configured, desired state of the interface""" |
| 105 |
return self._enabled |
| 106 |
|
| 107 |
@enabled.setter |
| 108 |
def enabled(self, value: bool): |
| 109 |
"""The configured, desired state of the interface""" |
| 110 |
if not isinstance(value, bool): |
| 111 |
raise TypeError('enabled must be boolean, got {}' |
| 112 |
.format(type(value))) |
| 113 |
self._enabled = value |
| 114 |
|
| 115 |
@enabled.deleter |
| 116 |
def enabled(self): |
| 117 |
self._enabled = None |
| 118 |
|
| 119 |
@property |
| 120 |
def mtu(self): |
| 121 |
"""The max transmission unit size in octets""" |
| 122 |
return self._mtu |
| 123 |
|
| 124 |
@mtu.setter |
| 125 |
def mtu(self, value: int): |
| 126 |
"""Set the max transmission unit size in octets""" |
| 127 |
if not isinstance(value, int): |
| 128 |
raise TypeError(f'mtu must be integer, got {type(value)}') |
| 129 |
self._mtu = value |
| 130 |
|
| 131 |
@mtu.deleter |
| 132 |
def mtu(self): |
| 133 |
self._mtu = None |
| 134 |
|
| 135 |
def to_xml_element(self): |
| 136 |
"""Create XML Element |
| 137 |
|
| 138 |
:return: ElementTree Element with SubElements |
| 139 |
""" |
| 140 |
elem = ElementTree.Element(self.TAG) |
| 141 |
if self.name is not None: |
| 142 |
common.txt_subelement(elem, 'name', self.name, |
| 143 |
attrib={'operation': self.operation}) |
| 144 |
if self.description is not None: |
| 145 |
common.txt_subelement(elem, 'description', self.description, |
| 146 |
attrib={'operation': self.operation}) |
| 147 |
if self.enabled is not None: |
| 148 |
common.txt_subelement(elem, 'enabled', str(self.enabled).lower(), |
| 149 |
attrib={'operation': self.operation}) |
| 150 |
if self.mtu is not None: |
| 151 |
common.txt_subelement(elem, 'mtu', str(self.mtu), |
| 152 |
attrib={'operation': self.operation}) |
| 153 |
return elem |
| 154 |
|
| 155 |
|
| 156 |
class BaseInterface: |
| 157 |
"""Base interface""" |
| 158 |
|
| 159 |
NAMESPACE = 'http://openconfig.net/yang/interfaces' |
| 160 |
PARENT = 'interfaces' |
| 161 |
TAG = 'interface' |
| 162 |
|
| 163 |
def __init__(self, name: str): |
| 164 |
self.name = name |
| 165 |
self._config = InterfaceConfig() |
| 166 |
|
| 167 |
@property |
| 168 |
def name(self): |
| 169 |
"""The name of the interface.""" |
| 170 |
return self._name |
| 171 |
|
| 172 |
@name.setter |
| 173 |
def name(self, value: str): |
| 174 |
"""The name of the interface.""" |
| 175 |
if not isinstance(value, str): |
| 176 |
raise TypeError('name must be string, got {}'.format(type(value))) |
| 177 |
self._name = value |
| 178 |
|
| 179 |
@name.deleter |
| 180 |
def name(self): |
| 181 |
self._name = None |
| 182 |
|
| 183 |
@property |
| 184 |
def config(self): |
| 185 |
"""Configuration parameters for interface""" |
| 186 |
return self._config |
| 187 |
|
| 188 |
@config.setter |
| 189 |
def config(self, value): |
| 190 |
if not isinstance(value, InterfaceConfig): |
| 191 |
raise TypeError('config must be InterfaceConfig, got {}' |
| 192 |
.format(type(value))) |
| 193 |
self._config = value |
| 194 |
|
| 195 |
@config.deleter |
| 196 |
def config(self): |
| 197 |
self._config = None |
| 198 |
|
| 199 |
def to_xml_element(self): |
| 200 |
"""Create XML Element |
| 201 |
|
| 202 |
:return: ElementTree Element with SubElements |
| 203 |
""" |
| 204 |
elem = ElementTree.Element(self.TAG) |
| 205 |
common.txt_subelement(elem, 'name', self.name) |
| 206 |
if self._config: |
| 207 |
elem.append(self.config.to_xml_element()) |
| 208 |
return elem |
| 209 |
|
| 210 |
|
| 211 |
class InterfaceEthernet(BaseInterface): |
| 212 |
|
| 213 |
def __init__(self, name: str): |
| 214 |
super(InterfaceEthernet, self).__init__(name) |
| 215 |
self._ethernet = ethernet.InterfacesEthernet() |
| 216 |
|
| 217 |
@property |
| 218 |
def ethernet(self): |
| 219 |
"""Ethernet configuration and state""" |
| 220 |
return self._ethernet |
| 221 |
|
| 222 |
@ethernet.setter |
| 223 |
def ethernet(self, value): |
| 224 |
if not isinstance(value, ethernet.InterfacesEthernet): |
| 225 |
raise TypeError('ethernet must be InterfacesEthernet, got {}' |
| 226 |
.format(type(value))) |
| 227 |
self._ethernet = value |
| 228 |
|
| 229 |
@ethernet.deleter |
| 230 |
def ethernet(self): |
| 231 |
self._ethernet = None |
| 232 |
|
| 233 |
def to_xml_element(self): |
| 234 |
"""Create XML Element |
| 235 |
|
| 236 |
:return: ElementTree Element with SubElements |
| 237 |
""" |
| 238 |
elem = ElementTree.Element(self.TAG) |
| 239 |
common.txt_subelement(elem, 'name', self.name) |
| 240 |
if self.config: |
| 241 |
elem.append(self.config.to_xml_element()) |
| 242 |
if self.ethernet: |
| 243 |
elem.append(self.ethernet.to_xml_element()) |
| 244 |
return elem |
| 245 |
|
| 246 |
|
| 247 |
class InterfaceAggregate(BaseInterface): |
| 248 |
|
| 249 |
def __init__(self, name: str, |
| 250 |
operation: str = constants.NetconfEditConfigOperation.MERGE): |
| 251 |
super(InterfaceAggregate, self).__init__(name) |
| 252 |
self.operation = operation |
| 253 |
self._aggregation = aggregate.InterfacesAggregation() |
| 254 |
|
| 255 |
@property |
| 256 |
def operation(self): |
| 257 |
"""RFC 6241 - <edit-config> operation attribute""" |
| 258 |
return self._operation.value if self._operation else None |
| 259 |
|
| 260 |
@operation.setter |
| 261 |
def operation(self, value): |
| 262 |
"""RFC 6241 - <edit-config> operation attribute""" |
| 263 |
if isinstance(value, constants.NetconfEditConfigOperation): |
| 264 |
self._operation = value |
| 265 |
elif isinstance(value, str): |
| 266 |
self._operation = constants.NetconfEditConfigOperation(value) |
| 267 |
else: |
| 268 |
raise TypeError('Invalid type {} for config operation attribute.' |
| 269 |
.format(type(value))) |
| 270 |
|
| 271 |
@operation.deleter |
| 272 |
def operation(self): |
| 273 |
self._operation = None |
| 274 |
|
| 275 |
@property |
| 276 |
def aggregation(self): |
| 277 |
"""Ethernet configuration and state""" |
| 278 |
return self._aggregation |
| 279 |
|
| 280 |
@aggregation.setter |
| 281 |
def aggregation(self, value): |
| 282 |
if not isinstance(value, aggregate.InterfacesAggregation): |
| 283 |
raise TypeError('ethernet must be OpenConfigInterfacesAggregation,' |
| 284 |
'got {}'.format(type(value))) |
| 285 |
self._aggregation = value |
| 286 |
|
| 287 |
@aggregation.deleter |
| 288 |
def aggregation(self): |
| 289 |
self._aggregation = None |
| 290 |
|
| 291 |
def to_xml_element(self): |
| 292 |
"""Create XML Element |
| 293 |
|
| 294 |
:return: ElementTree Element with SubElements |
| 295 |
""" |
| 296 |
elem = ElementTree.Element(self.TAG) |
| 297 |
if self.operation: |
| 298 |
elem.set('operation', self.operation) |
| 299 |
common.txt_subelement(elem, 'name', self.name) |
| 300 |
if self.config: |
| 301 |
elem.append(self.config.to_xml_element()) |
| 302 |
if self.aggregation: |
| 303 |
elem.append(self.aggregation.to_xml_element()) |
| 304 |
return elem |
| 305 |
|
| 306 |
|
| 307 |
class Interfaces(abc.Collection): |
| 308 |
"""Group/List of interfaces""" |
| 309 |
|
| 310 |
NAMESPACE = 'http://openconfig.net/yang/interfaces' |
| 311 |
TAG = 'interfaces' |
| 312 |
|
| 313 |
def __init__(self): |
| 314 |
# List of interfaces of type Interface |
| 315 |
self._interfaces = list() |
| 316 |
|
| 317 |
def __iter__(self): |
| 318 |
return iter(self._interfaces) |
| 319 |
|
| 320 |
def __len__(self): |
| 321 |
return len(self._interfaces) |
| 322 |
|
| 323 |
def __contains__(self, item): |
| 324 |
return item in self._interfaces |
| 325 |
|
| 326 |
@property |
| 327 |
def interfaces(self): |
| 328 |
"""List of interfaces""" |
| 329 |
return self._interfaces |
| 330 |
|
| 331 |
def add(self, name: str, |
| 332 |
interface_type: str = constants.IFACE_TYPE_ETHERNET): |
| 333 |
"""Add interface |
| 334 |
|
| 335 |
:param name: Interface name |
| 336 |
:type: str |
| 337 |
:param interface_type: Interface type ('ethernet', 'aggregate', 'base') |
| 338 |
:type: str |
| 339 |
""" |
| 340 |
if interface_type == constants.IFACE_TYPE_ETHERNET: |
| 341 |
interface = InterfaceEthernet(name) |
| 342 |
elif interface_type == constants.IFACE_TYPE_AGGREGATE: |
| 343 |
interface = InterfaceAggregate(name) |
| 344 |
elif interface_type == constants.IFACE_TYPE_BASE: |
| 345 |
interface = BaseInterface(name) |
| 346 |
else: |
| 347 |
raise ValueError('Invalid interface type {}'.format(type(name))) |
| 348 |
self._interfaces.append(interface) |
| 349 |
return interface |
| 350 |
|
| 351 |
def to_xml_element(self): |
| 352 |
"""Create XML Element |
| 353 |
|
| 354 |
:return: ElementTree Element with SubElements |
| 355 |
""" |
| 356 |
eleme = ElementTree.Element(self.TAG) |
| 357 |
eleme.set('xmlns', self.NAMESPACE) |
| 358 |
for interface in self.interfaces: |
| 359 |
eleme.append(interface.to_xml_element()) |
| 360 |
return eleme |