def del_eline(id, node): """ inputs: id (str) : E-Line index ( format ID.NI_ID.NI_ID | e.g : 2.1.2 ) node (str) : Device ID DISCLAIMER: This method won't actually delete the service, but will only set it as "Disable" To actually delete the service, so far you must use the NMS """ cwd = os.getcwd() if cwd.split('/')[-1] != 'snmp': os.chdir("Scripts/python/snmp/") f = open("json/huawei/elines.json", ) elines = json.load(f) f.close() """ f=open("json/inventory.json",) hosts=json.load(f) f.close() """ devices = get_request(URL_GET_DEVICES)[1]['device'] input_args = dict() uni2uni = elines[node]['uni2uni'] uni2nni = elines[node]['uni2nni'] nni2nni = elines[node]['nni2nni'] ip = get_ne_with_id(node, devices)['system-ipv4'] if id + '.1.1' in uni2nni.keys(): #id,board,subboard,port,vlan_num,vlan_str,name,qinqid,nms_id values = uni2nni[id + '.1.1'] board, subboard, port = values['port'].split('/') vlans = values['vlan_list'] vlan_num = len(vlans) vlan_str = bin2hex(get_vlan_str(vlans)) values.pop('vlan_list', None) values.pop('status', None) values.update( make_dict(['board', 'subboard', 'port', 'vlan_num', 'vlan_str'], [board, subboard, port, vlan_num, vlan_str])) quicksnmp.add_row(ip, delete_eline_row_oids(id + '.1.1', **values), credentials) print('Uni2Nni ' + id + ' deleted') elif id + '.1.2' in uni2uni.keys(): nms_index = uni2uni[id + '.1.2']['nms_index'] quicksnmp.add_row(ip, delete_uni2uni_row_oids(id + '.1.2', nms_index), credentials) update() time.sleep(1)
def delete_vlan(host_id, vlans_dict): """ inputs : host_id (str) : Device ID where create VLAN vlans_dict (dict) : Dict of VLANs to delete """ #Check workspace and load data files cwd = os.getcwd() if cwd.split('/')[-1] != 'snmp': os.chdir("Scripts/python/snmp/") """ #Load Hosts List f=open("json/inventory.json",) hosts=json.load(f) f.close() """ device = get_request(URL_GET_DEVICES + '/device/{device_id}'.format(device_id=host_id))[1] #host_info = hosts[host_id] _unexist, _deleted = dict(), dict() host_vlans_list = get_vlans_by_host(device['system-ipv4']) host_vlan_config = get_vlans_conf_by_host(device['system-ipv4']) for vlan in [*vlans_dict]: if vlan in [*host_vlans_list]: ports_assign = [ config for config in host_vlan_config if str(config['vlan']) == vlan ] if ports_assign != []: for port in ports_assign: remove_vlan_to_port(host_id, str(port['port_id']), {vlan: vlans_dict[vlan]}) delete_request = quicksnmp.add_row(device['system-ipv4'], delete_nec_vlan(vlan), credentials) _deleted[vlan] = vlans_dict[vlan] else: _unexist[vlan] = vlans_dict[vlan] return _deleted, _unexist
def assign_vlan_to_port(host_id, host_port_id, vlans_dict): """ inputs : host_id (str) : Device ID host_port_id (str) : Device port ID where add VLAN vlans_dict (dict) : Dict of VLANs to create """ #Check workspace and load data files cwd = os.getcwd() if cwd.split('/')[-1] != 'snmp': os.chdir("Scripts/python/snmp/") """ #Load Hosts List f=open("json/inventory.json",) hosts=json.load(f) f.close() """ device = get_request(URL_GET_DEVICES + '/device/{device_id}'.format(device_id=host_id))[1] #host_info = hosts[host_id] _exist, _assign = dict(), dict() host_vlans_list = get_vlans_by_host(device['system-ipv4']) host_vlan_config = get_vlans_conf_by_host(device['system-ipv4']) #All config in trunk mode host_port_config = [ str(id['vlan']) for id in host_vlan_config if str(id['port_id']) == host_port_id ] for vlan in [*vlans_dict]: if vlan in [*host_vlans_list] and (vlan not in host_port_config): _assign[vlan] = vlans_dict[vlan] #Assign VLAN to Port assign_request = quicksnmp.add_row( device['system-ipv4'], assign_nec_vlan(vlan, host_port_id), credentials) else: _exist[vlan] = vlans_dict[vlan] return _assign, _exist
def create_vlan(host_id, vlans_dict): """ inputs : host_id (str) : Device ID where create VLAN vlans_dict (dict) : Dict of VLANs to create """ #Check workspace and load data files cwd = os.getcwd() if cwd.split('/')[-1] != 'snmp': os.chdir("Scripts/python/snmp/") """ #Load Hosts List f=open("json/inventory.json",) hosts=json.load(f) f.close() """ device = get_request(URL_GET_DEVICES + '/device/{device_id}'.format(device_id=host_id))[1] #host_info = hosts[host_id] _exist, _created = dict(), dict() host_vlans_list = get_vlans_by_host(device['system-ipv4']) for vlan in [*vlans_dict]: if vlan in [*host_vlans_list]: _exist[vlan] = vlans_dict[vlan] else: _created[vlan] = vlans_dict[vlan] #Create VLAN add_request = quicksnmp.add_row( device['system-ipv4'], create_nec_vlan(vlan, vlans_dict[vlan]), credentials) return _created, _exist
def add_eline(node,interface_a,interface_b,name,vlans_a,vlans_b): """ inputs : node (str) : Device ID interface_a (str) : First interface name. Must respect the format <Board>/255/<Port> interface_b (str) : Second interface name. Must respect the format <Board>/255/<Port> name (str) : Service name vlans_a (list(int)) : List of allowed C-VLAN for interface_a vlans_b (list(int)) : List of allowed C-VLAN for interface_b qinqid (int) : Bridged QinQ ID """ #Check workspace and load data files cwd = os.getcwd() if cwd.split('/')[-1]!='snmp' : os.chdir("Scripts/python/snmp/") """ f=open("json/inventory.json",) hosts=json.load(f) f.close() """ device = get_request(URL_GET_DEVICES+'/device/{device_id}'.format(device_id = node))[1] board_a,subboard_a,port_a = interface_a.split('/') board_b,subboard_b,port_b = interface_b.split('/') """ f=open("json/huawei/qinqlinks.json",) qinqlinks_hua=json.load(f) f.close() f=open("json/huawei/elines.json",) elines=json.load(f) f.close() """ URL_GET_DEVICE_ELINES = URL_GET_DEVICES +'/device/{device_id}?depth=all'.format(device_id=node) req_get_elines = get_request(URL_GET_DEVICE_ELINES) elines = dict() try: qinqlinks_hua = [link['qinq-link-id'] for link in req_get_qinq_links[1]['qinq-link']] elines['uni2uni'] = { link['link-id']:link for link in req_get_elines[1]['uni-2-unis']['uni-2-uni'] } elines['uni2nni'] = { link['link-id']:link for link in req_get_elines[1]['uni-2-nnis']['uni-2-nni'] } elines['nni2nni'] = { link['link-id']:link for link in req_get_elines[1]['nni-2-nnis']['nni-2-nni'] } except: print('Error somewhere') elines_entries = dict() try: uni2nni=elines['uni2nni'] nni2nni=elines['nni2nni'] elines=elines['uni2uni'] #Aggregating every E-Lines in a single list elines_entries.update(uni2nni) elines_entries.update(nni2nni) elines_entries.update(uni2uni) except: uni2uni = dict() uni2nni = dict() nni2nni = dict() input_args = dict() ip = device['system-ipv4'] id=0 nms_id=0 #Getting the next available E-Line Index for i in range(65535): x=str(i+1)+'.1.2' if str(i+1)+'.1.2' not in elines.keys() and str(i+1)+'.1.1' not in uni2nni.keys(): if id==0: id= str(i+1)+'.1.2' if nms_id !=0: break else: #Checking conflicts with other E-Line services if str(i+1)+'.1.1' in uni2nni.keys(): x=str(i+1)+'.1.1' if (common_member(vlans_a,uni2nni[x]['vlan-list']) and uni2nni[x]['port']==interface_a) or (common_member(vlans_b,uni2nni[x]['vlan-list']) and uni2nni[x]['port']==interface_b) : print('A Vlan is already set in this port') get_elines.del_eline(str(i+1),node) elif str(i+1)+'.1.2' in elines.keys(): if (common_member(vlans_a,elines[str(i+1)+'.1.2']['vlan-list-a']) and elines[str(i+1)+'.1.2']['port-a']==interface_a) or (common_member(vlans_a,elines[str(i+1)+'.1.2']['vlan-list-b']) and elines[str(i+1)+'.1.2']['port-b']==interface_a) or (common_member(vlans_b,elines[str(i+1)+'.1.2']['vlan-list-a']) and elines[str(i+1)+'.1.2']['port-a']==interface_b) or (common_member(vlans_b,elines[str(i+1)+'.1.2']['vlan-list-b']) and elines[str(i+1)+'.1.2']['port-b']==interface_b) : print('A Vlan is already set in this port') get_elines.del_eline(str(i+1),node) #Getting the next available NMS service Index if len([info for info in elines_entries.values() if info['nms-index']==str(i+1)])==0: nms_id=str(i+1) if id !=0: break #Preparing request values vlan_str_a=str() vlan_str_b=str() for i in range(4096): if str(i+1) in vlans_a: vlan_str_a=vlan_str_a+'1' else: vlan_str_a=vlan_str_a+'0' if str(i+1) in vlans_b: vlan_str_b=vlan_str_b+'1' else: vlan_str_b=vlan_str_b+'0' print('vlan_a : ', vlans_a) print('vlan_b : ', vlans_b) print("--------Before vlan-----") print("vlan_str_a: ", vlan_str_a) print("vlan_str_b: ", vlan_str_b) vlan_num_a=len(vlans_a) vlan_num_b=len(vlans_b) vlan_str_a=bin2hex(vlan_str_a) vlan_str_b=bin2hex(vlan_str_b) print("--------After vlan-----") print("vlan_str_a: ", vlan_str_a) print("vlan_str_b: ", vlan_str_b) var_names=['name','board_a','subboard_a','port_a','board_b','subboard_b','port_b','vlan_num_a','vlan_num_b','vlan_str_a','vlan_str_b','nms_id'] var_values=[name,board_a,subboard_a,port_a,board_b,subboard_b,port_b,vlan_num_a,vlan_num_b,vlan_str_a,vlan_str_b,nms_id] input_args = make_dict(var_names,var_values) #Sending SNMP request quicksnmp.add_row(ip,get_uni2uni_row_oids(id,**input_args),credentials) get_elines.update()
def add_eline(node,name,qinqid_a,qinqid_b): """ inputs : node (str) : Device ID name (str) : Service name qinqid_a (int) : First QinQ ID qinqid_b (int) : Second QinQ ID """ #Check workspace cwd = os.getcwd() if cwd.split('/')[-1]!='snmp' : os.chdir("Scripts/python/snmp/") """ #Load some files f=open("json/inventory.json",) hosts=json.load(f) f.close() """ device = get_request(URL_GET_DEVICES+'/device/{device_id}'.format(device_id = node))[1] """ f=open("json/huawei/qinqlinks.json",) qinqlinks_hua=json.load(f) f.close() """ URL_GET_DEVICE_QINQ_LINK = URL_GET_DEVICES +'/device/{device_id}/qinq-links'.format(device_id=node) URL_GET_DEVICE_ELINES = URL_GET_DEVICES +'/device/{device_id}?depth=all'.format(device_id=node) req_get_elines = get_request(URL_GET_DEVICE_ELINES) req_get_qinq_links = get_request(URL_GET_DEVICE_QINQ_LINK) elines = dict() try: qinqlinks_hua = [link['qinq-link-id'] for link in req_get_qinq_links[1]['qinq-link']] elines['uni2uni'] = { link['link-id']:link for link in req_get_elines[1]['uni-2-unis']['uni-2-uni'] } elines['uni2nni'] = { link['link-id']:link for link in req_get_elines[1]['uni-2-nnis']['uni-2-nni'] } elines['nni2nni'] = { link['link-id']:link for link in req_get_elines[1]['nni-2-nnis']['nni-2-nni'] } except: qinqlinks_hua = list() """ f=open("json/huawei/elines.json",) elines=json.load(f) f.close() """ elines_entries = dict() try: uni2uni=elines['uni2uni'] uni2nni=elines['uni2nni'] nni2nni=elines['nni2nni'] #Aggregating every E-Lines in a single list elines_entries.update(uni2nni) elines_entries.update(nni2nni) elines_entries.update(uni2uni) except: uni2uni = dict() uni2nni = dict() nni2nni = dict() input_args = dict() ip = device['system-ipv4'] id=0 nms_id=0 #Getting the next available E-Line Index for i in range(65535): x=str(i+1)+'.1.2' if x not in uni2uni.keys() and x not in nni2nni.keys() and str(i+1)+'.1.1' not in uni2nni.keys(): if id==0: id= x if nms_id !=0: break #Checking conflicts with other E-Line services elif x in nni2nni.keys() : if (nni2nni[x]['qinq-link-id-a']==qinqid_a or nni2nni[x]['qinq-link-id-a']==qinqid_b) and (nni2nni[x]['qinq-link-id-b']==qinqid_a or nni2nni[x]['qinq-link-id-b']==qinqid_b): raise ValueError('This Nni2Nni bridge already exist') if len([info for info in elines_entries.values() if info['nms-index']==str(i+1)])==0: nms_id=str(i+1) if id !=0: break #Checking input QinQ Links availability if str(qinqid_a) not in qinqlinks_hua or str(qinqid_b) not in qinqlinks_hua: raise ValueError("One QinQ Link doesn't exist") #Creating and sending the SNMP request input_args = make_dict(['nms_id','qinqid_a','qinqid_b','name'],[nms_id,qinqid_a,qinqid_b,name]) quicksnmp.add_row(ip,get_nni2nni_row_oids(id,**input_args),credentials) get_elines.update()