6
6
4. It needs to use templating to create new policies and apply to upload them in the end.
7
7
"""
8
8
import json
9
- from collections import OrderedDict
9
+ try :
10
+ from collections import OrderedDict
11
+ except ImportError :
12
+ from ordereddict import OrderedDict
10
13
import sys
11
14
sys .path .insert (0 , '../' )
12
15
from lib .cisco_grpc_client import CiscoGRPCClient
@@ -36,7 +39,7 @@ def get_policies(self):
36
39
path = '{"openconfig-routing-policy:routing-policy": [null]}'
37
40
err , result = self .client .getconfig (path )
38
41
if err :
39
- print err
42
+ print ( err )
40
43
policies = json .loads (result , object_pairs_hook = OrderedDict )
41
44
return policies
42
45
@@ -45,9 +48,9 @@ def list_policies(self):
45
48
"""
46
49
bgp_policies = self .get_policies ()
47
50
policy_definitions = bgp_policies ['openconfig-routing-policy:routing-policy' ]['policy-definitions' ]['policy-definition' ]
48
- print '\n Policy Names:\n '
51
+ print ( '\n Policy Names:\n ' )
49
52
for policy in policy_definitions :
50
- print 'Name: %s' % policy ['name' ]
53
+ print ( 'Name: %s' % policy ['name' ])
51
54
52
55
def detail_policy (self , policy_name ):
53
56
"""Prints the full json of a policy in Openconfig and returns it
@@ -60,7 +63,7 @@ def detail_policy(self, policy_name):
60
63
policy_definitions = bgp_policies ['openconfig-routing-policy:routing-policy' ]['policy-definitions' ]['policy-definition' ]
61
64
for policy in policy_definitions :
62
65
if policy_name == policy ['name' ]:
63
- print json .dumps (policy , indent = 4 , separators = (',' , ': ' ))
66
+ print ( json .dumps (policy , indent = 4 , separators = (',' , ': ' ) ))
64
67
inner = json .dumps (policy )
65
68
template = '{"openconfig-routing-policy:routing-policy": {"policy-definitions": {"policy-definition": [%s]}}}' % inner
66
69
return json .dumps (json .loads (template , object_pairs_hook = OrderedDict ), indent = 4 , separators = (',' , ': ' ))
@@ -73,7 +76,7 @@ def get_neighbors(self):
73
76
path = '{"openconfig-bgp:bgp": [null]}'
74
77
err , result = self .client .getconfig (path )
75
78
if err :
76
- print err
79
+ print ( err )
77
80
bgp = json .loads (result , object_pairs_hook = OrderedDict )
78
81
return bgp
79
82
@@ -82,9 +85,9 @@ def list_neighbors(self):
82
85
"""
83
86
bgp = self .get_neighbors ()
84
87
bgp_neighbors = bgp ['openconfig-bgp:bgp' ]['neighbors' ]['neighbor' ]
85
- print "\n Neighbor's\n "
88
+ print ( "\n Neighbor's\n " )
86
89
for neighbor in bgp_neighbors :
87
- print 'Neighbor: %s AS: %s' % (neighbor ['neighbor-address' ], neighbor ['config' ]['peer-as' ])
90
+ print ( 'Neighbor: %s AS: %s' % (neighbor ['neighbor-address' ], neighbor ['config' ]['peer-as' ]) )
88
91
89
92
def detail_neighbor (self , neighbor_address ):
90
93
"""Prints the full json of a neighbor in Openconfig format
@@ -95,7 +98,7 @@ def detail_neighbor(self, neighbor_address):
95
98
bgp_neighbors = bgp ['openconfig-bgp:bgp' ]['neighbors' ]['neighbor' ]
96
99
for neighbor in bgp_neighbors :
97
100
if neighbor_address == neighbor ['neighbor-address' ]:
98
- print json .dumps (neighbor , indent = 4 , separators = (',' , ': ' ))
101
+ print ( json .dumps (neighbor , indent = 4 , separators = (',' , ': ' ) ))
99
102
inner = json .dumps (neighbor )
100
103
template = '{"openconfig-bgp:bgp": {"neighbors": {"neighbor" : [%s]}}}' % inner
101
104
return json .dumps (json .loads (template , object_pairs_hook = OrderedDict ), indent = 4 , separators = (',' , ': ' ))
@@ -158,12 +161,12 @@ def neighbor_policy(self, neighbor_address, policy, direction):
158
161
# Add the removed neighbors to list.
159
162
updated_neighbors .append ((neighbor ['neighbor-address' ], ip_type , curr_policy ))
160
163
updated_neighbors = json .dumps (updated_neighbors )
161
- print updated_neighbors
164
+ print ( updated_neighbors )
162
165
bgp_config = json .dumps (bgp )
163
166
err = self .merge_config (bgp_config )
164
167
if not err :
165
- print err
166
- print '\n New Neighbor Detail:\n '
168
+ print ( err )
169
+ print ( '\n New Neighbor Detail:\n ' )
167
170
self .detail_neighbor (neighbor_address )
168
171
169
172
def multiple_policies (self , policies , neighbor ):
@@ -186,7 +189,7 @@ def multiple_policies(self, policies, neighbor):
186
189
json_policy = json .loads (json_policy , object_pairs_hook = OrderedDict )
187
190
conditions .append (json_policy )
188
191
multi_policy = json .dumps (shell )
189
- print self .merge_config (multi_policy )
192
+ print ( self .merge_config (multi_policy ) )
190
193
return policy_name
191
194
192
195
def main ():
@@ -197,19 +200,19 @@ def main():
197
200
route = RoutePolicy ('localhost' , 57777 , 'vagrant' , 'vagrant' )
198
201
# List Policies from Router
199
202
route .list_policies ()
200
- print '\n next function\n '
203
+ print ( '\n next function\n ' )
201
204
# Get the full json object of the policy, allows for manipulation
202
205
policy = route .detail_policy ('TEST' )
203
- print '\n next function\n '
206
+ print ( '\n next function\n ' )
204
207
# List BGP neighbors
205
208
route .list_neighbors ()
206
- print '\n next function\n '
209
+ print ( '\n next function\n ' )
207
210
# Get the full json object of the BGP neighbor, allows for manipulation
208
211
route .detail_neighbor ('11.1.1.2' )
209
- print '\n next function\n '
212
+ print ( '\n next function\n ' )
210
213
# Apply multiple policies to a single neighbor interface
211
214
route .neighbor_policy ('11.1.1.2' , ['TEST' , 'SEND-MED-IGP' ], 'export-policy' )
212
- print '\n next function\n '
215
+ print ( '\n next function\n ' )
213
216
# Store policy in json file to modify
214
217
policy_file = open ('policy.json' , 'w' )
215
218
policy_file .write (policy )
0 commit comments