json_loader.py 956 Bytes
Newer Older
import json

def getInterfaceFromJson():
    json_file = open('interfaces.json', 'r',encoding='utf-8')
    interface_data = json.load(json_file)
    return interface_data

def getInterfaceFromNodeNames(json_file,node_name_src,node_name_dst):

    interface_src=[]
    interface_dst=[]
    for device in json_file['devices']:
        print("dev: %s",device.keys())
        if device['name'] == node_name_src:
            interface_src=list(device['links'].keys())[list(device['links'].values()).index(node_name_dst)]
        if device['name'] == node_name_dst:
            interface_dst=list(device['links'].keys())[list(device['links'].values()).index(node_name_src)]

    return interface_src,interface_dst
    

if __name__ == "__main__":
    data=getInterfaceFromJson()
    print("data: %s",data['devices'])
    # for device in data['devices']:
    #     print(device['interfaces'].keys())

    print(getInterfaceFromNodeNames(data,"HL2-2-1","HL2-2-2"))