Python Example to collect EPNM Data via RESTConf
Use Case
With the advent of standard's based APIs it is now possible to consider EPNM an open book that enables users to integrate it with any higher layer OSS systems or even with Cisco's own Network Planning tools such as WAE.
Here we show an example of how to use Python to grab EPNM's data in the JSON format and feed it to an external system.
JSON - GET & POST Collection Code
Copydef rest_get_json(baseURL, uri, user, password):
proxies = {
"http": None,
"https": None,
}
appformat = 'application/json'
headers = {'content-type': appformat, 'accept': appformat}
restURI = baseURL + uri
try:
r = requests.get(restURI, headers=headers, proxies=proxies, auth=(user, password), verify=False)
# print "HTTP response code is: " + str(r.status_code)
if r.status_code == 200:
return json.dumps(r.json(), indent=2)
else:
raise errors.InputError(restURI, "HTTP status code: " + str(r.status_code))
except errors.InputError as err:
print "Exception raised: " + str(type(err))
print err.expression
print err.message
return
def rest_post_json(baseURL, uri, thejson, user, password):
proxies = {
"http": None,
"https": None,
}
appformat = 'application/json'
headers = {'content-type': appformat, 'accept': appformat}
restURI = baseURL + uri
try:
r = requests.post(restURI, data=thejson, headers=headers, proxies=proxies, auth=(user, password),
verify=False)
# print "HTTP response code is: " + str(r.status_code)
if r.status_code == 200:
return json.dumps(r.json(), indent=2)
else:
raise errors.InputError(restURI, "HTTP status code: " + str(r.status_code))
except errors.InputError as err:
print "Exception raised: " + str(type(err))
print err.expression
print err.message
return
Main Code Block
This is a snippet from a possible bigger code that collects EPNM data and stores/transfers it in a particular format.
```python
import collectioncode.utils
import time
import re
import json
import logging
import sys
epnmipaddr = args.epnm_ipaddr
baseURL = "https://" + epnmipaddr + "/restconf"
epnmuser = args.epnm_user
epnmpassword = args.epnm_pass
collectioncode.collect.runcollector(baseURL, epnmuser, epnmpassword, args.seednode_id)
def runcollector(baseURL, epnmuser, epnmpassword, seednode_id):
logging.info("Collecting L1 nodes...")
collectL1Nodes_json(baseURL, epnmuser, epnmpassword)
def collectL1Nodes_json(baseURL, epnmuser, epnmpassword):
incomplete = True
startindex = 0
jsonmerged = {}
while incomplete:
uri = "/data/v1/cisco-resource-physical:node?product-series=Cisco Network Convergence System 2000 Series&.startIndex=" + str(startindex)
jsonresponse = collectioncode.utils.rest_get_json(baseURL, uri, epnmuser, epnmpassword)
jsonaddition = json.loads(jsonresponse)
firstindex = jsonaddition['com.response-message']['com.header']['com.firstIndex']
lastindex = jsonaddition['com.response-message']['com.header']['com.lastIndex']
if (lastindex - firstindex) == 99 and lastindex != -1:
startindex += 100
else:
incomplete = False
merge(jsonmerged,jsonaddition)
Copywith open("jsongets/l1-nodes.json", 'wb') as f:
f.write(json.dumps(jsonmerged, f, sort_keys=True, indent=4, separators=(',', ': ')))
f.close()
with open("jsongets/l1-nodes.json", 'rb') as f:
jsonresponse = f.read()
f.close()
thejson = json.loads(jsonresponse)
l1nodes = {}
i = 1
with open("jsonfiles/l1Nodes.json", 'wb') as f:
for node in thejson['com.response-message']['com.data']['nd.node']:
if node['nd.product-series'] == "Cisco Network Convergence System 2000 Series":
nodeName = node['nd.name']
logging.info("Processing node " + nodeName)
try:
latitude = node['nd.latitude']
longitude = node['nd.longitude']
except KeyError:
logging.error("Could not get longitude or latitidude for node " + nodeName + ". Setting to 0.0 and 0.0")
latitude = "0.0"
longitude = "0.0"
l1nodes['Node' + str(i)] = dict([('Name', nodeName), ('Latitude', latitude), ('Longitude', longitude)])
i += 1
f.write(json.dumps(l1nodes, f, sort_keys=True, indent=4, separators=(',', ': ')))
f.close()
```