forked from robshakir/pyangbind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_route_example.py
More file actions
111 lines (93 loc) · 3.58 KB
/
static_route_example.py
File metadata and controls
111 lines (93 loc) · 3.58 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
#!/usr/bin/env python
from __future__ import print_function, unicode_literals
from binding import openconfig_network_instance
import pyangbind.lib.pybindJSON as pybindJSON
import os
# Instantiate a copy of the pyangbind-kettle module and add a network instance.
ocni = openconfig_network_instance()
ocni.network_instances.network_instance.add("a")
ocni.network_instances.network_instance["a"].protocols.protocol.add(identifier="STATIC", name="DEFAULT")
# Add an entry to the static route list
rt = (
ocni.network_instances.network_instance["a"]
.protocols.protocol["STATIC DEFAULT"]
.static_routes.static.add("192.0.2.1/32")
)
# Set a tag for the route
rt.config.set_tag = 42
# Retrieve the tag value
print(rt.config.set_tag)
# Retrieve the tag value through the original object
print(
ocni.network_instances.network_instance["a"]
.protocols.protocol["STATIC DEFAULT"]
.static_routes.static["192.0.2.1/32"]
.config.set_tag
)
# Use the get() method to see the content of the classes
# using the filter=True keyword to get only elements that
# are not empty or the default
print(
ocni.network_instances.network_instance["a"]
.protocols.protocol["STATIC DEFAULT"]
.static_routes.static["192.0.2.1/32"]
.get(filter=True)
)
# Add a set of next_hops
for nhop in [(0, "192.168.0.1"), (1, "10.0.0.1")]:
nh = rt.next_hops.next_hop.add(nhop[0])
nh.config.next_hop = nhop[1]
# Iterate through the next-hops added
for index, nh in rt.next_hops.next_hop.items():
print("%s: %s" % (index, nh.config.next_hop))
# Try and set an invalid tag type
try:
rt.config.set_tag = "INVALID-TAG"
except ValueError as m:
print("Cannot set tag: %s" % m)
# Dump the entire instance as JSON in PyangBind format
print(pybindJSON.dumps(ocni, indent=2))
# Dump the static routes instance as JSON in IETF format
print(
pybindJSON.dumps(
ocni.network_instances.network_instance["a"].protocols.protocol["STATIC DEFAULT"], mode="ietf", indent=2
)
)
# Load the "json/oc-ni.json" file into a new instance of
# "openconfig_network_instance". We import the module here, such that a new
# instance of the class can be created by the deserialisation code.
# Note that you may need to provide the absolute path to oc-ni.json.
import binding
new_ocni = pybindJSON.load(os.path.join("json", "oc-ni.json"), binding, "openconfig_network_instance")
# Manipulate the data loaded
print(
"Current tag: %d"
% new_ocni.network_instances.network_instance["a"]
.protocols.protocol["STATIC DEFAULT"]
.static_routes.static["192.0.2.1/32"]
.config.set_tag
)
new_ocni.network_instances.network_instance["a"].protocols.protocol["STATIC DEFAULT"].static_routes.static[
"192.0.2.1/32"
].config.set_tag += 1
print(
"New tag: %d"
% new_ocni.network_instances.network_instance["a"]
.protocols.protocol["STATIC DEFAULT"]
.static_routes.static["192.0.2.1/32"]
.config.set_tag
)
# Load JSON into an existing class structure
from pyangbind.lib.serialise import pybindJSONDecoder
import json
# Provide absolute path to oc-ni_ietf.json if needed.
ietf_json = json.load(open(os.path.join("json", "oc-ni_ietf.json"), "r"))
pybindJSONDecoder.load_ietf_json(
ietf_json, None, None, obj=new_ocni.network_instances.network_instance["a"].protocols.protocol["STATIC DEFAULT"]
)
# Iterate through the classes - both the 192.0.2.1/32 prefix and 192.0.2.2/32
# prefix are now in the objects
for prefix, route in (
new_ocni.network_instances.network_instance["a"].protocols.protocol["STATIC DEFAULT"].static_routes.static.items()
):
print("Prefix: %s, tag: %d" % (prefix, route.config.set_tag))