Debian Codegraph @

networking_baremetal/openconfig/interfaces/ethernet.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 xml.etree import ElementTree
13
14 from networking_baremetal import common
15 from networking_baremetal import constants
16 from networking_baremetal.openconfig.vlan import vlan
17
18
19 class InterfacesEthernetConfig:
20 """OpenConfig interface ethernet configuration"""
21
22 NAMESPACE = 'http://openconfig.net/yang/interfaces'
23 PARENT = 'interface'
24 TAG = 'config'
25
26 def __init__(self, operation=constants.NetconfEditConfigOperation.MERGE):
27 self.operation = operation
28 self._aggregate_id = None
29 self._aggregate_id_namespace = (
30 'http://openconfig.net/yang/interfaces/aggregate')
31
32 @property
33 def operation(self):
34 """RFC 6241 - <edit-config> operation attribute"""
35 return self._operation.value if self._operation else None
36
37 @operation.setter
38 def operation(self, value):
39 """RFC 6241 - <edit-config> operation attribute"""
40 if isinstance(value, constants.NetconfEditConfigOperation):
41 self._operation = value
42 elif isinstance(value, str):
43 self._operation = constants.NetconfEditConfigOperation(value)
44 else:
45 raise TypeError('Invalid type {} for config operation attribute.'
46 .format(type(value)))
47
48 @operation.deleter
49 def operation(self):
50 self._operation = None
51
52 @property
53 def aggregate_id(self):
54 """Logical aggregate interface for interface"""
55 return self._aggregate_id
56
57 @aggregate_id.setter
58 def aggregate_id(self, value: str):
59 """Set logical aggregate interface for interface"""
60 if not isinstance(value, str):
61 raise TypeError('aggregate_id must be string, got {}'
62 .format(type(value)))
63 self._aggregate_id = value
64
65 @aggregate_id.deleter
66 def aggregate_id(self):
67 self._aggregate_id = None
68
69 def to_xml_element(self):
70 """Create XML Element
71
72 :return: ElementTree Element with SubElements
73 """
74 element = ElementTree.Element(self.TAG)
75 if self.operation:
76 element.set('operation', self.operation)
77 if self.aggregate_id is not None:
78 common.txt_subelement(element, 'aggregate-id', self.aggregate_id,
79 xmlns=self._aggregate_id_namespace)
80 return element
81
82
83 class InterfacesEthernet:
84 """Ethernet configuration and state"""
85 NAMESPACE = 'http://openconfig.net/yang/interfaces/ethernet'
86 PARENT = 'interface'
87 TAG = 'ethernet'
88
89 def __init__(self):
90 self._switched_vlan = vlan.VlanSwitchedVlan()
91 self._config = InterfacesEthernetConfig()
92
93 @property
94 def switched_vlan(self):
95 return self._switched_vlan
96
97 @switched_vlan.setter
98 def switched_vlan(self, value):
99 if not isinstance(value, vlan.VlanSwitchedVlan):
100 raise TypeError('switched_vlan must be VlanSwitchedVlan, got {}'
101 .format(type(value)))
102 self._switched_vlan = value
103
104 @switched_vlan.deleter
105 def switched_vlan(self):
106 self._switched_vlan = None
107
108 @property
109 def config(self):
110 """Configuration parameters for interface"""
111 return self._config
112
113 @config.setter
114 def config(self, value):
115 if not isinstance(value, InterfacesEthernetConfig):
116 raise TypeError('config must be InterfacesEthernetConfig, got {}'
117 .format(type(value)))
118 self._config = value
119
120 @config.deleter
121 def config(self):
122 self._config = None
123
124 def to_xml_element(self):
125 """Create XML Element
126
127 :return: ElementTree Element with SubElements
128 """
129 elem = ElementTree.Element(self.TAG)
130 elem.set('xmlns', self.NAMESPACE)
131 if self.config:
132 elem.append(self.config.to_xml_element())
133 if self.switched_vlan:
134 elem.append(self.switched_vlan.to_xml_element())
135 return elem