Skip to content

Commit 1232d17

Browse files
author
kkumara3
committed
Updated Examples to support python3
1 parent 3b3f701 commit 1232d17

File tree

6 files changed

+45
-41
lines changed

6 files changed

+45
-41
lines changed

examples/cli.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""
1+
"""
22
GRPC Client with command line options
33
44
Usage:
@@ -26,14 +26,13 @@
2626
import sys
2727
sys.path.insert(0, '../')
2828
from lib.cisco_grpc_client import CiscoGRPCClient
29-
3029
from docopt import docopt
3130

3231
def main():
3332

3433
__version__ = 'GRPC_Client 1.0'
3534
arguments = docopt(__doc__, version=__version__)
36-
35+
3736
IP = arguments['<router_IP>']
3837
TCP_PORT = int(arguments['<port>'])
3938
user = arguments['<user>']
@@ -51,13 +50,13 @@ def main():
5150
else:
5251
path = 'Error'
5352
print(
54-
'get-oper argument must include --file option and json file to filter yang operational namespace'
53+
'get-oper argument must include --file option and json file to filter yang operational namespace'
5554
)
5655
try:
5756
err, result = client.getoper(path)
5857
if err:
59-
print err
60-
print result
58+
print(err)
59+
print(result)
6160
except AbortionError:
6261
print(
6362
'Unable to connect to local box, check your gRPC destination.'
@@ -69,14 +68,14 @@ def main():
6968
file = arguments['--file']
7069
path = open(file).read()
7170
# path = open('json/' + file).read()
72-
else:
73-
path = ""
71+
else:
72+
path = ""
7473

7574
try:
7675
err, result = client.getconfig(path)
7776
if err:
78-
print err
79-
print result
77+
print(err)
78+
print(result)
8079
except AbortionError:
8180
print(
8281
'Unable to connect to local box, check your gRPC destination.'
@@ -95,7 +94,7 @@ def main():
9594
try:
9695
err = client.mergeconfig(path)
9796
if err:
98-
print err
97+
print(err)
9998
#print result
10099
except AbortionError:
101100
print(
@@ -115,7 +114,7 @@ def main():
115114
try:
116115
err = client.replaceconfig(path)
117116
if err:
118-
print err
117+
print(err)
119118
except AbortionError:
120119
print(
121120
'Unable to connect to local box, check your gRPC destination.'

examples/grpc_cfg.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def get(self):
2020
try:
2121
err, result = self.client.getconfig(path)
2222
if err:
23-
print err
24-
print json.dumps(json.loads(result))
23+
print(err)
24+
print(json.dumps(json.loads(result)))
2525
except AbortionError:
2626
print(
2727
'Unable to connect to local box, check your gRPC destination.'
@@ -33,7 +33,7 @@ def replace(self):
3333
response = self.client.replaceconfig(path)
3434
if response.errors:
3535
err = json.loads(response.errors)
36-
print err
36+
print(err)
3737
except AbortionError:
3838
print(
3939
'Unable to connect to local box, check your gRPC destination.'
@@ -45,7 +45,7 @@ def merge(self):
4545
response = self.client.mergeconfig(path)
4646
if response.errors:
4747
err = json.loads(response.errors)
48-
print err
48+
print(err)
4949
except AbortionError:
5050
print(
5151
'Unable to connect to local box, check your gRPC destination.'
@@ -57,7 +57,7 @@ def delete(self):
5757
response = self.client.deleteconfig(path)
5858
if response.errors:
5959
err = json.loads(response.errors)
60-
print err
60+
print(err)
6161
except AbortionError:
6262
print(
6363
'Unable to connect to local box, check your gRPC destination.'

examples/grpc_example.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def main():
2222
try:
2323
err, result = client.getconfig(path)
2424
if err:
25-
print err
26-
print json.dumps(json.loads(result))
25+
print(err)
26+
print(json.dumps(json.loads(result)))
2727
except AbortionError:
2828
print(
2929
'Unable to connect to local box, check your gRPC destination.'

examples/route_policy.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
4. It needs to use templating to create new policies and apply to upload them in the end.
77
"""
88
import json
9-
from collections import OrderedDict
9+
try:
10+
from collections import OrderedDict
11+
except ImportError:
12+
from ordereddict import OrderedDict
1013
import sys
1114
sys.path.insert(0, '../')
1215
from lib.cisco_grpc_client import CiscoGRPCClient
@@ -36,7 +39,7 @@ def get_policies(self):
3639
path = '{"openconfig-routing-policy:routing-policy": [null]}'
3740
err, result = self.client.getconfig(path)
3841
if err:
39-
print err
42+
print(err)
4043
policies = json.loads(result, object_pairs_hook=OrderedDict)
4144
return policies
4245

@@ -45,9 +48,9 @@ def list_policies(self):
4548
"""
4649
bgp_policies = self.get_policies()
4750
policy_definitions = bgp_policies['openconfig-routing-policy:routing-policy']['policy-definitions']['policy-definition']
48-
print '\nPolicy Names:\n'
51+
print('\nPolicy Names:\n')
4952
for policy in policy_definitions:
50-
print 'Name: %s' % policy['name']
53+
print('Name: %s' % policy['name'])
5154

5255
def detail_policy(self, policy_name):
5356
"""Prints the full json of a policy in Openconfig and returns it
@@ -60,7 +63,7 @@ def detail_policy(self, policy_name):
6063
policy_definitions = bgp_policies['openconfig-routing-policy:routing-policy']['policy-definitions']['policy-definition']
6164
for policy in policy_definitions:
6265
if policy_name == policy['name']:
63-
print json.dumps(policy, indent=4, separators=(',', ': '))
66+
print(json.dumps(policy, indent=4, separators=(',', ': ')))
6467
inner = json.dumps(policy)
6568
template = '{"openconfig-routing-policy:routing-policy": {"policy-definitions": {"policy-definition": [%s]}}}' % inner
6669
return json.dumps(json.loads(template, object_pairs_hook=OrderedDict), indent=4, separators=(',', ': '))
@@ -73,7 +76,7 @@ def get_neighbors(self):
7376
path = '{"openconfig-bgp:bgp": [null]}'
7477
err, result = self.client.getconfig(path)
7578
if err:
76-
print err
79+
print(err)
7780
bgp = json.loads(result, object_pairs_hook=OrderedDict)
7881
return bgp
7982

@@ -82,9 +85,9 @@ def list_neighbors(self):
8285
"""
8386
bgp = self.get_neighbors()
8487
bgp_neighbors = bgp['openconfig-bgp:bgp']['neighbors']['neighbor']
85-
print "\nNeighbor's\n"
88+
print("\nNeighbor's\n")
8689
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']))
8891

8992
def detail_neighbor(self, neighbor_address):
9093
"""Prints the full json of a neighbor in Openconfig format
@@ -95,7 +98,7 @@ def detail_neighbor(self, neighbor_address):
9598
bgp_neighbors = bgp['openconfig-bgp:bgp']['neighbors']['neighbor']
9699
for neighbor in bgp_neighbors:
97100
if neighbor_address == neighbor['neighbor-address']:
98-
print json.dumps(neighbor, indent=4, separators=(',', ': '))
101+
print(json.dumps(neighbor, indent=4, separators=(',', ': ')))
99102
inner = json.dumps(neighbor)
100103
template = '{"openconfig-bgp:bgp": {"neighbors": {"neighbor" : [%s]}}}' % inner
101104
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):
158161
# Add the removed neighbors to list.
159162
updated_neighbors.append((neighbor['neighbor-address'], ip_type, curr_policy))
160163
updated_neighbors = json.dumps(updated_neighbors)
161-
print updated_neighbors
164+
print(updated_neighbors)
162165
bgp_config = json.dumps(bgp)
163166
err = self.merge_config(bgp_config)
164167
if not err:
165-
print err
166-
print '\nNew Neighbor Detail:\n'
168+
print(err)
169+
print('\nNew Neighbor Detail:\n')
167170
self.detail_neighbor(neighbor_address)
168171

169172
def multiple_policies(self, policies, neighbor):
@@ -186,7 +189,7 @@ def multiple_policies(self, policies, neighbor):
186189
json_policy = json.loads(json_policy, object_pairs_hook=OrderedDict)
187190
conditions.append(json_policy)
188191
multi_policy = json.dumps(shell)
189-
print self.merge_config(multi_policy)
192+
print(self.merge_config(multi_policy))
190193
return policy_name
191194

192195
def main():
@@ -197,19 +200,19 @@ def main():
197200
route = RoutePolicy('localhost', 57777, 'vagrant', 'vagrant')
198201
# List Policies from Router
199202
route.list_policies()
200-
print '\nnext function\n'
203+
print('\nnext function\n')
201204
# Get the full json object of the policy, allows for manipulation
202205
policy = route.detail_policy('TEST')
203-
print '\nnext function\n'
206+
print('\nnext function\n')
204207
# List BGP neighbors
205208
route.list_neighbors()
206-
print '\nnext function\n'
209+
print('\nnext function\n')
207210
# Get the full json object of the BGP neighbor, allows for manipulation
208211
route.detail_neighbor('11.1.1.2')
209-
print '\nnext function\n'
212+
print('\nnext function\n')
210213
# Apply multiple policies to a single neighbor interface
211214
route.neighbor_policy('11.1.1.2', ['TEST', 'SEND-MED-IGP'], 'export-policy')
212-
print '\nnext function\n'
215+
print('\nnext function\n')
213216
# Store policy in json file to modify
214217
policy_file = open('policy.json', 'w')
215218
policy_file.write(policy)

examples/telemetry_collector.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def print_connectivity(connectivity):
2222
# unmarshal is an optional argument, default is unmarshal = True
2323
# If unmarshal is false, out is in gpb k/v
2424
recv_count += 1
25-
print json.dumps(segment, indent=4, separators=(',', ': '))
26-
print 'End Telemetry Segment'
27-
print str(recv_count) + ' Segments Received'
25+
print(json.dumps(segment, indent=4, separators=(',', ': ')))
26+
print('End Telemetry Segment')
27+
print(str(recv_count) + ' Segments Received')
2828

lib/cisco_grpc_client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!bin/env
12
# Copyright 2016 Cisco Systems All rights reserved.
23
#
34
# Redistribution and use in source and binary forms, with or without
@@ -19,6 +20,7 @@
1920
# License for the specific language governing permissions and limitations under
2021
# the License.
2122

23+
""" Libraries to connect XR gRPC server """
2224

2325
import grpc
2426
from . import ems_grpc_pb2
@@ -58,7 +60,7 @@ def __init__(self, host, port, timeout, user, password, creds=None, options=None
5860
self._port = port
5961
self._channel = implementations.insecure_channel(self._host, self._port)
6062
self._stub = ems_grpc_pb2.beta_create_gRPCConfigOper_stub(self._channel)
61-
self._timeout = int(timeout)
63+
self._timeout = float(timeout)
6264
self._metadata = [('username', user), ('password', password)]
6365

6466
def __repr__(self):

0 commit comments

Comments
 (0)