def cdp_neighbors_json(device_name): response = odl_http_get(url_neighbours, {'node-id': device_name}, 'application/json', expected_status_code=(200, 404)) j = json_loads(response.text) container = j["cdp"]["nodes"]["node"][0] print(json_dumps(container, indent=2)) if "neighbors" in container: neighbors = container["neighbors"] print(len(neighbors)) print(json_dumps(neighbors, indent=2))
def cdp_neighbors_json(device_name): response = odl_http_get(url_neighbours.format(**{'node-id': quote_plus(device_name), }), 'application/json', expected_status_code=(200, 404) ) j = json_loads(response.text) container = j["cdp"]["nodes"]["node"][0] print(json_dumps(container, indent=2)) if "neighbors" in container: neighbors = container["neighbors"] print(len(neighbors)) print(json_dumps(neighbors, indent=2))
def findneighbor(device_name): ''' Description : This function retrieve the cdp information for the specific device through restconf API. It then parse the data and store the information in global var like nodes and edges. ''' #get the result from cdp url for device_name response = odl_http_get( url_neighbours.format(**{ 'node-id': quote_plus(device_name), }), 'application/json', expected_status_code=(200, 404)) if response.status_code == 400: print(_BUG_CHECK % 'This interface is not provided!') return None j = json_loads(response.text) #add the node into the nodes, we store the device_id nodes.append(dict(nodename=devname2devid(device_name))) container = j["cdp"]["nodes"]["node"][0] #print(json_dumps(container, indent=2)) if "neighbors" in container: neighbors = container["neighbors"] if 'summaries' in neighbors: summaries = neighbors['summaries']['summary'] #now we get the list of summarirs, each summary is a {} dict type for summary in summaries: #here we skip the mgt interface if 'MgmtEth' in summary['interface-name']: continue #add the data of an edge dict_item = {'localnode':devname2devid(device_name),\ 'remotenode':summary['device-id'],\ 'localifname':summary['interface-name'],\ 'remoteifname':summary['cdp-neighbor'][0]['port-id'] } #At first we have to set the interface's bandwidth to maxmium, #in our design, we use available_bandwidth to implement the #route calculate and bandwidth reserve. if initial_flag == True: dict_item['maxbandwidth'] = _MAXMIUM_BANDWIDTH dict_item['available_bandwidth'] = _MAXMIUM_BANDWIDTH # now we have to set the local network address and networkmask netresult = interface_configuration( devid2devname(dict_item['localnode']), dict_item['localifname']) # !!-.-!! here i can only accees the element by index even if the debugger tell me the data in it is address=10.0xxx, but # it is not a dict. so can anyone help? if netresult is not None: dict_item['localaddr'] = netresult[3] dict_item['localmask'] = netresult[4] netresult = None else: print( _BUG_CHECK % 'The interface of the local node is wrong, maybe the device is not mounted' ) # now it is the remote address and networkmask netresult = interface_configuration( devid2devname(dict_item['remotenode']), dict_item['remoteifname']) if netresult is not None: dict_item['remoteaddr'] = netresult[3] dict_item['remotemask'] = netresult[4] else: print( _BUG_CHECK % 'The interface of the remote node is wrong, maybe the device is not mounted' ) edges.append(dict_item) else: print(_BUG_CHECK % 'The node has no neighbors or maybe the device is not mounted') return None
def findneighbor(device_name): ''' Description : This function retrieve the cdp information for the specific device through restconf API. It then parse the data and store the information in global var like nodes and edges. ''' #get the result from cdp url for device_name response = odl_http_get(url_neighbours.format(**{'node-id': quote_plus(device_name), }), 'application/json', expected_status_code=(200, 404) ) if response.status_code == 400: print(_BUG_CHECK %'This interface is not provided!') return None j = json_loads(response.text) #add the node into the nodes, we store the device_id nodes.append(dict(nodename=devname2devid(device_name))) container = j["cdp"]["nodes"]["node"][0] #print(json_dumps(container, indent=2)) if "neighbors" in container: neighbors = container["neighbors"] if 'summaries' in neighbors: summaries = neighbors['summaries']['summary'] #now we get the list of summarirs, each summary is a {} dict type for summary in summaries: #here we skip the mgt interface if 'MgmtEth' in summary['interface-name']: continue #add the data of an edge dict_item = {'localnode':devname2devid(device_name),\ 'remotenode':summary['device-id'],\ 'localifname':summary['interface-name'],\ 'remoteifname':summary['cdp-neighbor'][0]['port-id'] } #At first we have to set the interface's bandwidth to maxmium, #in our design, we use available_bandwidth to implement the #route calculate and bandwidth reserve. if initial_flag == True : dict_item['maxbandwidth'] = _MAXMIUM_BANDWIDTH dict_item['available_bandwidth'] = _MAXMIUM_BANDWIDTH # now we have to set the local network address and networkmask netresult = interface_configuration(devid2devname(dict_item['localnode']), dict_item['localifname']) # !!-.-!! here i can only accees the element by index even if the debugger tell me the data in it is address=10.0xxx, but # it is not a dict. so can anyone help? if netresult is not None : dict_item['localaddr'] = netresult[3] dict_item['localmask'] = netresult[4] netresult = None else : print(_BUG_CHECK %'The interface of the local node is wrong, maybe the device is not mounted') # now it is the remote address and networkmask netresult = interface_configuration(devid2devname(dict_item['remotenode']), dict_item['remoteifname']) if netresult is not None : dict_item['remoteaddr'] = netresult[3] dict_item['remotemask'] = netresult[4] else : print(_BUG_CHECK %'The interface of the remote node is wrong, maybe the device is not mounted') edges.append(dict_item) else : print(_BUG_CHECK %'The node has no neighbors or maybe the device is not mounted') return None