-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtutorial_translator.py
More file actions
212 lines (167 loc) · 7.13 KB
/
Copy pathtutorial_translator.py
File metadata and controls
212 lines (167 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from typing import Any, Dict, Optional
from yangify.translator import Translator, TranslatorData, unneeded
from yangson.instance import NonexistentInstance
class SubinterfaceConfig(Translator):
"""
Implements openconfig-interfaces:interfaces/interface/subinterfaces/subinterface/config
"""
index = unneeded
def description(self, value: Optional[str]) -> None:
if value:
self.yy.result.add_command(f" description {value}")
else:
self.yy.result.add_command(f" no description")
class Subinterface(Translator):
"""
Implements openconfig-interfaces:interfaces/interface/subinterfaces/subinterface
"""
class Yangify(TranslatorData):
def pre_process_list(self) -> None:
"""
If we need to remove itnerfaces we do it here. However, will need to
get the key of the parent interface first as we will need it
to remove the subinterfaces. Remember that subinterfaces in openconfig
are referenced by their index and don't have a fully qualified name
"""
parent_key = self.keys["/openconfig-interfaces:interfaces/interface"]
for element in self.to_remove:
iface_name = f"{parent_key}.{element['index']}"
self.root_result.add_command(f"no interface {iface_name}")
def pre_process(self) -> None:
"""
We create a placeholder for our configuration in self.result, we attach it to
self.root_result and also default the subinterface if we are in replace mode
"""
parent_key = self.keys["/openconfig-interfaces:interfaces/interface"]
self.keys["subinterface_key"] = f"{parent_key}.{self.key}"
path = f"interface {self.keys['subinterface_key']}"
if self.replace:
self.root_result.add_command(
f"no interface {self.keys['subinterface_key']}"
)
self.result = self.root_result.new_section(path)
def post_process(self) -> None:
"""
After we are doing processing the interface we can either
remove entirely the interface from the configuration if it's empty
or add_command exit\n! to the commands
"""
path = f"interface {self.keys['subinterface_key']}"
if not self.result:
self.root_result.pop_section(path)
else:
self.result.add_command(" exit\n!")
index = unneeded
config = SubinterfaceConfig
class Subinterfaces(Translator):
"""
Implements openconfig-interfaces:interfaces/interface/subinterfaces
"""
subinterface = Subinterface
class InterfaceConfig(Translator):
"""
Implements openconfig-interfaces:interfaces/interface/config
"""
name = unneeded
type = unneeded
def description(self, value: Optional[str]) -> None:
if value:
self.yy.result.add_command(f" description {value}")
else:
self.yy.result.add_command(f" no description")
def enabled(self, value: Optional[bool]) -> None:
if value:
self.yy.result.add_command(f" no shutdown")
else:
self.yy.result.add_command(f" shutdown")
class Interface(Translator):
"""
Implements openconfig-interfaces:interfaces/interface
"""
class Yangify(TranslatorData):
def _remove_subinterfaces(self, interface: Dict[str, Any]) -> None:
"""
A helper function to remove subinterfaces.
"""
subifaces = interface.get("subinterfaces", {}).get("subinterface", [])
for subiface in subifaces:
self.root_result.add_command(
f"no interface {self.key}.{subiface['index']}"
)
def pre_process_list(self) -> None:
"""
If we have interfaces to remove we do so before processing the list of interfaces.
"""
for element in self.to_remove:
self.result.add_command(f"default interface {element['name']}")
self._remove_subinterfaces(element)
def pre_process(self) -> None:
"""
Before processing a given interface we are going to do a couple of things:
1. if we are replacing the configuration we default the interface and its
subinterfaces
2. We create a placeholder for the interface configuration and we set it
in self.result
"""
if self.replace:
self.root_result.add_command(f"default interface {self.key}")
if self.running is not None:
# self.running.goto(self.path).value is going to return the running
# value of the current interface
try:
self._remove_subinterfaces(self.running.goto(self.path).value)
except NonexistentInstance:
# if it's a candidate interface self.running.goto(self.path) will
# raise this exception
pass
path = f"interface {self.key}"
# we insert it as soon as possible to maintain order
self.result = self.root_result.new_section(path)
def post_process(self) -> None:
"""
After we are doing processing the interface we can either
remove entirely the interface from the configuration if it's empty
or add_command exit\n! to the commands
"""
path = f"interface {self.key}"
if not self.result:
self.root_result.pop_section(path)
else:
self.result.add_command(" exit\n!")
name = unneeded
subinterfaces = Subinterfaces
config = InterfaceConfig
class Interfaces(Translator):
"""
Implements openconfig-interfaces:interfaces
Using a :obj:`yangify.translator.config_tree.ConfigTree` object for the result
"""
interface = Interface
class VlanConfig(Translator):
vlan_id = unneeded
def name(self, value: Optional[str]) -> None:
if value:
self.yy.result.add_command(f" name {value}")
else:
self.yy.result.add_command(f" no name")
def status(self, value: Optional[str]) -> None:
if value == "SUSPENDED":
self.yy.result.add_command(f" shutdown")
else:
self.yy.result.add_command(f" no shutdown")
class Vlan(Translator):
class Yangify(TranslatorData):
def pre_process_list(self) -> None:
if self.to_remove:
for element in self.to_remove:
self.result.add_command(f"no vlan {element['vlan-id']}")
def pre_process(self) -> None:
if self.replace:
self.root_result.add_command(f"no vlan {self.key}")
self.result = self.result.new_section(f"vlan {self.key}")
def post_process(self) -> None:
self.result.add_command(" exit\n!")
vlan_id = unneeded
config = VlanConfig
class Vlans(Translator):
vlan = Vlan