forked from napalm-automation/napalm-yang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_templates.py
111 lines (84 loc) · 3.15 KB
/
generate_templates.py
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
from collections import defaultdict
import os
import napalm_yang
import yaml
models = [
napalm_yang.models.openconfig_network_instance,
napalm_yang.models.openconfig_interfaces,
napalm_yang.models.openconfig_platform,
napalm_yang.models.openconfig_vlan,
napalm_yang.models.openconfig_system,
]
profile = "dummy"
def nested_dd():
return defaultdict(nested_dd)
def process(model, r_config, r_state):
if model._yang_type in ("container",):
ctr = model
elif model._yang_type in ("list", None):
ctr = model._contained_class()
else:
if model._is_config or model._yang_name == "config":
r_config[model._yang_name] = {"_process": "not_implemented"}
else:
r_state[model._yang_name] = {"_process": "not_implemented"}
return
if model._yang_name == "config":
r_config = r_config[ctr._yang_name]
r_config["_process"] = "not_implemented"
elif model._is_config:
r_config = r_config[ctr._yang_name]
r_config["_process"] = "not_implemented"
r_state = r_state[ctr._yang_name]
r_state["_process"] = "not_implemented"
else:
r_state = r_state[ctr._yang_name]
r_state["_process"] = "not_implemented"
for k, v in ctr:
process_module(v, model._defining_module, r_config, r_state)
def process_module(model, module, r_config=None, r_state=None):
if model._defining_module != module:
r_config = result["config"][
os.path.join(model._defining_module, "{}.yaml".format(model._yang_name))
]
r_state = result["state"][
os.path.join(model._defining_module, "{}.yaml".format(model._yang_name))
]
process(model, r_config, r_state)
def ddict2dict(d):
for k, v in d.items():
if isinstance(v, dict):
d[k] = ddict2dict(v)
return dict(d)
def main():
global result
result = nested_dd()
for model in models:
m = model()
for _, v in m:
process_module(v, None, result["config"], result["state"])
for mode, data in result.items():
for module, model in data.items():
module, filename = module.split("/")
for action in ["parsers", "translators"]:
if action == "translators" and mode == "state":
continue
elif action == "translators":
directory = os.path.join(
"napalm_yang", "mappings", "dummy", action, module
)
else:
directory = os.path.join(
"napalm_yang", "mappings", "dummy", action, mode, module
)
if not os.path.exists(directory):
os.makedirs(directory)
model = ddict2dict(model)
metadata = "---\nmetadata:\n processor: unset\n\n"
with open(os.path.join(directory, filename), "w+") as f:
f.write(
metadata
+ yaml.safe_dump(model, indent=4, default_flow_style=False)
)
if __name__ == "__main__":
main()