| 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.vlan import types |
| 19 |
|
| 20 |
|
| 21 |
class TrunkVlans(abc.Collection): |
| 22 |
|
| 23 |
def __init__(self): |
| 24 |
self._trunk_vlans = [] |
| 25 |
|
| 26 |
def __iter__(self): |
| 27 |
return iter(self._trunk_vlans) |
| 28 |
|
| 29 |
def __len__(self): |
| 30 |
return len(self._trunk_vlans) |
| 31 |
|
| 32 |
def __contains__(self, item): |
| 33 |
return item in self._trunk_vlans |
| 34 |
|
| 35 |
def add(self, value): |
| 36 |
"""Add vlan or range of vlans (range: 100..200)""" |
| 37 |
try: |
| 38 |
value = int(value) |
| 39 |
if value not in self._trunk_vlans: |
| 40 |
self._trunk_vlans.append(types.VlanId(value).vlan_id) |
| 41 |
except ValueError: |
| 42 |
if value not in self._trunk_vlans: |
| 43 |
self._trunk_vlans.append(types.VlanRange(value).vlan_range) |
| 44 |
|
| 45 |
|
| 46 |
class VlanSwitchedConfig: |
| 47 |
"""Ethernet interface VLAN config |
| 48 |
|
| 49 |
VLAN related configuration that is part of the physical |
| 50 |
Ethernet interface. |
| 51 |
""" |
| 52 |
NAMESPACE = 'http://openconfig.net/yang/vlan' |
| 53 |
PARENT = 'switched-vlan' |
| 54 |
TAG = 'config' |
| 55 |
|
| 56 |
def __init__(self, |
| 57 |
operation: str = constants.NetconfEditConfigOperation.MERGE, |
| 58 |
interface_mode: Optional[str] = None, |
| 59 |
native_vlan: Optional[int] = None, |
| 60 |
access_vlan: Optional[int] = None): |
| 61 |
|
| 62 |
self.operation = operation |
| 63 |
self._interface_mode = None |
| 64 |
self._native_vlan = None |
| 65 |
self._access_vlan = None |
| 66 |
self._trunk_vlans = TrunkVlans() |
| 67 |
if interface_mode: |
| 68 |
self.interface_mode = interface_mode |
| 69 |
if native_vlan: |
| 70 |
self.native_vlan = native_vlan |
| 71 |
if access_vlan: |
| 72 |
self.access_vlan = access_vlan |
| 73 |
|
| 74 |
@property |
| 75 |
def operation(self): |
| 76 |
"""RFC 6241 - <edit-config> operation attribute""" |
| 77 |
return self._operation.value if self._operation else None |
| 78 |
|
| 79 |
@operation.setter |
| 80 |
def operation(self, value): |
| 81 |
"""RFC 6241 - <edit-config> operation attribute""" |
| 82 |
if isinstance(value, constants.NetconfEditConfigOperation): |
| 83 |
self._operation = value |
| 84 |
elif isinstance(value, str): |
| 85 |
self._operation = constants.NetconfEditConfigOperation(value) |
| 86 |
else: |
| 87 |
raise TypeError('Invalid type {} for config operation attribute.' |
| 88 |
.format(type(value))) |
| 89 |
|
| 90 |
@operation.deleter |
| 91 |
def operation(self): |
| 92 |
self._operation = None |
| 93 |
|
| 94 |
@property |
| 95 |
def interface_mode(self): |
| 96 |
"""Get the interface to access or trunk mode for VLANs""" |
| 97 |
return self._interface_mode.value if self._interface_mode else None |
| 98 |
|
| 99 |
@interface_mode.setter |
| 100 |
def interface_mode(self, value): |
| 101 |
"""Set the interface to access or trunk mode for VLANs""" |
| 102 |
self._interface_mode = types.VlanInterfaceMode(value) |
| 103 |
|
| 104 |
@interface_mode.deleter |
| 105 |
def interface_mode(self): |
| 106 |
"""Delete the interface to access or trunk mode for VLANs""" |
| 107 |
self._interface_mode = None |
| 108 |
|
| 109 |
@property |
| 110 |
def native_vlan(self): |
| 111 |
"""Native VLAN |
| 112 |
|
| 113 |
is valid for trunk mode interfaces |
| 114 |
""" |
| 115 |
return self._native_vlan.vlan_id if self._native_vlan else None |
| 116 |
|
| 117 |
# TODO(hjensas): Only allow if interface_mode == trunk |
| 118 |
@native_vlan.setter |
| 119 |
def native_vlan(self, value: int): |
| 120 |
"""Set native VLAN |
| 121 |
|
| 122 |
is valid for trunk mode interfaces |
| 123 |
""" |
| 124 |
self._native_vlan = types.VlanId(value) |
| 125 |
|
| 126 |
@native_vlan.deleter |
| 127 |
def native_vlan(self): |
| 128 |
"""Delete native VLAN""" |
| 129 |
self._native_vlan = None |
| 130 |
|
| 131 |
# TODO(hjensas): Only allow if interface_mode == access |
| 132 |
@property |
| 133 |
def access_vlan(self): |
| 134 |
"""Access VLAN assigned to the interfaces""" |
| 135 |
return self._access_vlan.vlan_id if self._access_vlan else None |
| 136 |
|
| 137 |
@access_vlan.setter |
| 138 |
def access_vlan(self, value: int): |
| 139 |
"""Set access VLAN assigned to the interfaces""" |
| 140 |
self._access_vlan = types.VlanId(value) |
| 141 |
|
| 142 |
@access_vlan.deleter |
| 143 |
def access_vlan(self): |
| 144 |
"""Unset access VLAN assigned to the interfaces""" |
| 145 |
self._access_vlan = None |
| 146 |
|
| 147 |
@property |
| 148 |
def trunk_vlans(self): |
| 149 |
"""Allowed VLANs may be specified for trunk mode interfaces""" |
| 150 |
return self._trunk_vlans |
| 151 |
|
| 152 |
# TODO(hjensas): Only allow if interface_mode == trunk |
| 153 |
@trunk_vlans.setter |
| 154 |
def trunk_vlans(self, value: str): |
| 155 |
"""Set allowed VLANs may be specified for trunk mode interfaces""" |
| 156 |
self._trunk_vlans.add(value) |
| 157 |
|
| 158 |
@trunk_vlans.deleter |
| 159 |
def trunk_vlans(self): |
| 160 |
self._trunk_vlans = TrunkVlans() |
| 161 |
|
| 162 |
def to_xml_element(self): |
| 163 |
"""Create XML Element |
| 164 |
|
| 165 |
:return: ElementTree Element with SubElements |
| 166 |
""" |
| 167 |
elem = ElementTree.Element(self.TAG) |
| 168 |
if self.operation: |
| 169 |
elem.set('operation', self.operation) |
| 170 |
if self.interface_mode: |
| 171 |
common.txt_subelement(elem, 'interface-mode', |
| 172 |
self.interface_mode) |
| 173 |
if self.access_vlan is not None: |
| 174 |
common.txt_subelement(elem, 'access-vlan', str(self.access_vlan)) |
| 175 |
if self.native_vlan is not None: |
| 176 |
common.txt_subelement(elem, 'native-vlan', str(self.native_vlan)) |
| 177 |
if self.trunk_vlans is not None: |
| 178 |
for item in self.trunk_vlans: |
| 179 |
common.txt_subelement(elem, 'trunk-vlans', str(item)) |
| 180 |
return elem |
| 181 |
|
| 182 |
|
| 183 |
class VlanSwitchedVlan: |
| 184 |
"""VLAN interface-specific data on Ethernet interfaces. |
| 185 |
|
| 186 |
Enclosing container for VLAN interface-specific |
| 187 |
data on Ethernet interfaces. These are for standard |
| 188 |
L2, switched-style VLANs. |
| 189 |
""" |
| 190 |
NAMESPACE = 'http://openconfig.net/yang/vlan' |
| 191 |
PARENT = 'ethernet' |
| 192 |
TAG = 'switched-vlan' |
| 193 |
|
| 194 |
def __init__(self): |
| 195 |
self._config = VlanSwitchedConfig() |
| 196 |
|
| 197 |
@property |
| 198 |
def config(self): |
| 199 |
"""Configuration parameters for VLANs""" |
| 200 |
return self._config |
| 201 |
|
| 202 |
@config.setter |
| 203 |
def config(self, value): |
| 204 |
if not isinstance(value, VlanSwitchedConfig): |
| 205 |
raise TypeError('config must be VlanSwitchedConfig, got {}' |
| 206 |
.format(type(value))) |
| 207 |
self._config = value |
| 208 |
|
| 209 |
@config.deleter |
| 210 |
def config(self): |
| 211 |
self._config = None |
| 212 |
|
| 213 |
def to_xml_element(self): |
| 214 |
"""Create XML Element |
| 215 |
|
| 216 |
:return: ElementTree Element with SubElements |
| 217 |
""" |
| 218 |
elem = ElementTree.Element(self.TAG) |
| 219 |
elem.set('xmlns', self.NAMESPACE) |
| 220 |
if self.config: |
| 221 |
elem.append(self.config.to_xml_element()) |
| 222 |
return elem |
| 223 |
|
| 224 |
|
| 225 |
class VlanConfig: |
| 226 |
"""OpenConfig VLAN configuration""" |
| 227 |
|
| 228 |
NAMESPACE = 'http://openconfig.net/yang/vlan' |
| 229 |
PARENT = 'vlan' |
| 230 |
TAG = 'config' |
| 231 |
|
| 232 |
def __init__(self, |
| 233 |
operation=constants.NetconfEditConfigOperation.MERGE, |
| 234 |
vlan_id: int = None, |
| 235 |
name: str = None, |
| 236 |
status: str = None): |
| 237 |
self.operation = operation |
| 238 |
self._vlan_id = None |
| 239 |
self._name = None |
| 240 |
self._status = None |
| 241 |
if vlan_id: |
| 242 |
self.vlan_id = vlan_id |
| 243 |
if name: |
| 244 |
self.name = name |
| 245 |
if status: |
| 246 |
self.status = status |
| 247 |
|
| 248 |
@property |
| 249 |
def operation(self): |
| 250 |
"""RFC 6241 - <edit-config> operation attribute""" |
| 251 |
return self._operation.value |
| 252 |
|
| 253 |
@operation.setter |
| 254 |
def operation(self, value): |
| 255 |
"""RFC 6241 - <edit-config> operation attribute""" |
| 256 |
if isinstance(value, constants.NetconfEditConfigOperation): |
| 257 |
self._operation = value |
| 258 |
elif isinstance(value, str): |
| 259 |
self._operation = constants.NetconfEditConfigOperation(value) |
| 260 |
else: |
| 261 |
raise TypeError('Invalid type {} for config operation attribute.' |
| 262 |
.format(type(value))) |
| 263 |
|
| 264 |
@property |
| 265 |
def vlan_id(self): |
| 266 |
"""The id of the VLAN""" |
| 267 |
return self._vlan_id.vlan_id if self._vlan_id else None |
| 268 |
|
| 269 |
@vlan_id.setter |
| 270 |
def vlan_id(self, value: int): |
| 271 |
self._vlan_id = types.VlanId(value) |
| 272 |
|
| 273 |
@vlan_id.deleter |
| 274 |
def vlan_id(self): |
| 275 |
self._vlan_id = None |
| 276 |
|
| 277 |
@property |
| 278 |
def name(self): |
| 279 |
"""Interface VLAN name.""" |
| 280 |
return self._name |
| 281 |
|
| 282 |
@name.setter |
| 283 |
def name(self, value: str): |
| 284 |
if not isinstance(value, str): |
| 285 |
raise TypeError('name must be string, got {}' |
| 286 |
.format(type(value))) |
| 287 |
self._name = value |
| 288 |
|
| 289 |
@name.deleter |
| 290 |
def name(self): |
| 291 |
self._name = None |
| 292 |
|
| 293 |
@property |
| 294 |
def status(self): |
| 295 |
"""Admin state of the VLAN""" |
| 296 |
return self._status.value if self._status else None |
| 297 |
|
| 298 |
@status.setter |
| 299 |
def status(self, value: str): |
| 300 |
"""Admin state of the VLAN""" |
| 301 |
self._status = types.VlanStatus(value) |
| 302 |
|
| 303 |
@status.deleter |
| 304 |
def status(self): |
| 305 |
self._status = None |
| 306 |
|
| 307 |
def to_xml_element(self): |
| 308 |
"""Create XML Element |
| 309 |
|
| 310 |
:return: ElementTree Element with SubElements |
| 311 |
""" |
| 312 |
elem = ElementTree.Element(self.TAG) |
| 313 |
elem.set('operation', self.operation) |
| 314 |
if self.vlan_id is not None: |
| 315 |
common.txt_subelement(elem, 'vlan-id', str(self.vlan_id)) |
| 316 |
if self.name is not None: |
| 317 |
common.txt_subelement(elem, 'name', self.name) |
| 318 |
if self.status is not None: |
| 319 |
common.txt_subelement(elem, 'status', self.status) |
| 320 |
return elem |
| 321 |
|
| 322 |
|
| 323 |
class Vlan: |
| 324 |
"""Base vlan""" |
| 325 |
|
| 326 |
NAMESPACE = 'http://openconfig.net/yang/vlan' |
| 327 |
PARENT = 'vlans' |
| 328 |
TAG = 'vlan' |
| 329 |
|
| 330 |
def __init__(self, vlan_id: int, |
| 331 |
operation=constants.NetconfEditConfigOperation.MERGE): |
| 332 |
self.operation = operation |
| 333 |
self.vlan_id = vlan_id |
| 334 |
self._config = VlanConfig(vlan_id=self.vlan_id) |
| 335 |
|
| 336 |
@property |
| 337 |
def operation(self): |
| 338 |
"""RFC 6241 - <edit-config> operation attribute""" |
| 339 |
return self._operation.value |
| 340 |
|
| 341 |
@operation.setter |
| 342 |
def operation(self, value): |
| 343 |
"""RFC 6241 - <edit-config> operation attribute""" |
| 344 |
if isinstance(value, constants.NetconfEditConfigOperation): |
| 345 |
self._operation = value |
| 346 |
elif isinstance(value, str): |
| 347 |
self._operation = constants.NetconfEditConfigOperation(value) |
| 348 |
else: |
| 349 |
raise TypeError('Invalid type {} for config operation ' |
| 350 |
'attribute.'.format(type(value))) |
| 351 |
|
| 352 |
@property |
| 353 |
def vlan_id(self): |
| 354 |
"""The id of the VLAN""" |
| 355 |
return self._vlan_id.vlan_id |
| 356 |
|
| 357 |
@vlan_id.setter |
| 358 |
def vlan_id(self, value: int): |
| 359 |
if not isinstance(value, int): |
| 360 |
raise TypeError(f'vlan_id must be integer, got {type(value)}') |
| 361 |
self._vlan_id = types.VlanId(value) |
| 362 |
|
| 363 |
@vlan_id.deleter |
| 364 |
def vlan_id(self): |
| 365 |
self._vlan_id = None |
| 366 |
|
| 367 |
@property |
| 368 |
def config(self): |
| 369 |
"""Configuration parameters for VLAN""" |
| 370 |
return self._config |
| 371 |
|
| 372 |
@config.setter |
| 373 |
def config(self, value): |
| 374 |
"""Configuration parameters for VLAN""" |
| 375 |
if not isinstance(value, VlanConfig): |
| 376 |
raise TypeError('config must be VlanConfig, got {}' |
| 377 |
.format(type(value))) |
| 378 |
self._config = value |
| 379 |
if self.vlan_id: |
| 380 |
self.config.vlan_id = self.vlan_id |
| 381 |
|
| 382 |
@config.deleter |
| 383 |
def config(self): |
| 384 |
self._config = None |
| 385 |
|
| 386 |
def to_xml_element(self): |
| 387 |
"""Create XML Element |
| 388 |
|
| 389 |
:return: ElementTree Element with SubElements |
| 390 |
""" |
| 391 |
elem = ElementTree.Element(self.TAG) |
| 392 |
if self.vlan_id: |
| 393 |
common.txt_subelement(elem, 'vlan-id', str(self.vlan_id), |
| 394 |
attrib={'operation': self.operation}) |
| 395 |
if self.config: |
| 396 |
elem.append(self.config.to_xml_element()) |
| 397 |
return elem |
| 398 |
|
| 399 |
|
| 400 |
class Vlans(abc.Collection): |
| 401 |
"""Group/List of VLANs""" |
| 402 |
|
| 403 |
NAMESPACE = 'http://openconfig.net/yang/vlan' |
| 404 |
TAG = 'vlans' |
| 405 |
|
| 406 |
def __init__(self): |
| 407 |
# List of vlans of type OpenconfigVlan |
| 408 |
self._vlans = list() |
| 409 |
|
| 410 |
def __iter__(self): |
| 411 |
return iter(self._vlans) |
| 412 |
|
| 413 |
def __len__(self): |
| 414 |
return len(self._vlans) |
| 415 |
|
| 416 |
def __contains__(self, item): |
| 417 |
return item in self._vlans |
| 418 |
|
| 419 |
@property |
| 420 |
def vlans(self): |
| 421 |
"""List of VLANs""" |
| 422 |
return self._vlans |
| 423 |
|
| 424 |
def add(self, vlan_id: int): |
| 425 |
"""Add VLAN |
| 426 |
|
| 427 |
:param vlan_id: VLAN ID |
| 428 |
:type: int |
| 429 |
:Keyword arguments: VLAN configuration |
| 430 |
""" |
| 431 |
vlan = Vlan(vlan_id) |
| 432 |
self._vlans.append(vlan) |
| 433 |
return vlan |
| 434 |
|
| 435 |
def remove(self, vlan_id: int): |
| 436 |
"""Remove VLAN |
| 437 |
|
| 438 |
:param vlan_id: VLAN ID |
| 439 |
:type: int |
| 440 |
""" |
| 441 |
vlan = Vlan(vlan_id) |
| 442 |
vlan.operation = constants.NetconfEditConfigOperation.REMOVE |
| 443 |
self._vlans.append(vlan) |
| 444 |
return vlan |
| 445 |
|
| 446 |
def to_xml_element(self): |
| 447 |
"""Create XML Element |
| 448 |
|
| 449 |
:return: ElementTree Element with SubElements |
| 450 |
""" |
| 451 |
elem = ElementTree.Element(self.TAG) |
| 452 |
elem.set('xmlns', self.NAMESPACE) |
| 453 |
for vlan in self.vlans: |
| 454 |
elem.append(vlan.to_xml_element()) |
| 455 |
return elem |