-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathtutorial_parser.py
More file actions
152 lines (114 loc) · 4.53 KB
/
Copy pathtutorial_parser.py
File metadata and controls
152 lines (114 loc) · 4.53 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
from typing import Any, Dict, Iterator, Optional, Tuple, cast
from yangify.parser import Parser, ParserData
class SubinterfaceConfig(Parser):
"""
Implements openconfig-interfaces:interfaces/interface/subinterfaces/subinterface/config
"""
def description(self) -> Optional[str]:
return cast(Optional[str], self.yy.native.get("description", {}).get("#text"))
def index(self) -> int:
return int(self.yy.key.split(".")[-1])
class Subinterface(Parser):
"""
Implements openconfig-interfaces:interfaces/interface/subinterfaces/subinterface
"""
class Yangify(ParserData):
def extract_elements(self) -> Iterator[Tuple[str, Dict[str, Any]]]:
"""
IOS subinterfaces are in the root of the configuration and named following
the format ``$parent_interface.$index``. The model specifies the key is
the $index, which is just a number. These means we will need to:
1. Iterate over all the interfaces
2. Filter the ones that don't start by `$parent_interface.`
3. Extract the $index and return it
"""
# self.keys keeps a record of all the keys found so far in the current
# object. To access them you can use the full YANG path
parent_key = self.keys["/openconfig-interfaces:interfaces/interface"]
for k, v in self.root_native["interface"].items():
if k.startswith(f"{parent_key}."):
yield k, v
config = SubinterfaceConfig
def index(self) -> int:
return int(self.yy.key.split(".")[-1])
class Subinterfaces(Parser):
"""
Implements openconfig-interfaces:interfaces/interface/subinterfaces
"""
subinterface = Subinterface
class InterfaceConfig(Parser):
"""
Implements openconfig-interfaces:interfaces/interface/config
"""
def description(self) -> Optional[str]:
return cast(Optional[str], self.yy.native.get("description", {}).get("#text"))
def enabled(self) -> bool:
shutdown = self.yy.native.get("shutdown", {}).get("#standalone")
if shutdown:
return False
else:
return True
def name(self) -> str:
return self.yy.key
def type(self) -> str:
if "Ethernet" in self.yy.key:
return "iana-if-type:ethernetCsmacd"
elif "Loopback" in self.yy.key:
return "iana-if-type:softwareLoopback"
else:
raise ValueError(f"don't know type for interface {self.yy.key}")
class Interface(Parser):
"""
Implements openconfig-interfaces:interfaces/interface
"""
class Yangify(ParserData):
def extract_elements(self) -> Iterator[Tuple[str, Dict[str, Any]]]:
"""
IOS interfaces are in the root of the configuration. However,
subinterfaces are also in the root of the configuration. For
that reason we will have to make sure that we filter the
subinterfaces as we iterate over all the interfaces in the root
of the configuration. That's should be as easy as checking that
the interface name has not dot in it.
"""
for k, v in self.native["interface"].items():
# k == "#text" is due to a harmless artifact in the
# parse_indented_config function that needs to be addressed
if k == "#text" or "." in k:
continue
yield k, v
config = InterfaceConfig
subinterfaces = Subinterfaces
def name(self) -> str:
return self.yy.key
class Interfaces(Parser):
"""
Implements openconfig-interfaces:interfaces
"""
interface = Interface
class VlanConfig(Parser):
def vlan_id(self) -> int:
return int(self.yy.key)
def name(self) -> Optional[str]:
v = self.yy.native.get("name", {}).get("#text")
if v is not None:
return str(v)
else:
return None
def status(self) -> str:
if self.yy.native.get("shutdown", {}).get("#standalone"):
return "SUSPENDED"
else:
return "ACTIVE"
class Vlan(Parser):
class Yangify(ParserData):
def extract_elements(self) -> Iterator[Tuple[str, Dict[str, Any]]]:
for k, v in self.native["vlan"].items():
if k == "#text":
continue
yield k, cast(Dict[str, Any], v)
config = VlanConfig
def vlan_id(self) -> int:
return int(self.yy.key)
class Vlans(Parser):
vlan = Vlan