from ncclient import manager
from ncclient.xml_ import *
import lxml.etree as ET
import re
from typing import Optional, Union
from uuid import UUID, uuid4, uuid5
import logging
create_media='''
2
C_BAND
1
2
192006250
192106250
port-1-in
'''
delete_media_channel='''
1
1
'''
delete_optical_band= '''
1
1
'''
edit_optical_channel= '''
channel-7
channel-7
300
3400000
1
7
7
ENABLED
'''
def extract_channel_xmlns (data_xml:str,is_opticalband:bool):
xml_bytes = data_xml.encode("utf-8")
root = ET.fromstring(xml_bytes)
namespace=None
channels=None
if (not is_opticalband) :
optical_channel_namespaces = {
'ns': 'urn:ietf:params:xml:ns:netconf:base:1.0',
'oc': 'http://openconfig.net/yang/platform',
}
channels= root.find('.//{*}optical-channel',optical_channel_namespaces)
if channels is not None :
optical_channel_namespace = channels.tag.replace("optical-channel", "")
namespace=optical_channel_namespace.replace("{", "").replace("}", "")
else :
optical_band_namespaces= {
'oc':'http://openconfig.net/yang/wavelength-router'
}
channels= root.find('.//{*}optical-bands',optical_band_namespaces)
if channels is not None:
optical_channel_namespace = channels.tag.replace("optical-bands", "")
namespace=optical_channel_namespace.replace("{", "").replace("}", "")
return namespace
def extract_roadm_ports (xml_data:str):
ports =[]
pattern2=r'\bMG_ON_PORT_TYPE'
pattern = r'\bMG_ON_OPTICAL_PORT_WAVEBAND\b'
xml_bytes = xml_data.encode("utf-8")
root = ET.fromstring(xml_bytes)
namespace = {'oc': 'http://openconfig.net/yang/platform'}
ports = []
components = root.findall('.//oc:component',namespace)
#print(f"component {components}")
for component in components:
properties = component.find(".//oc:properties",namespace)
if (properties is not None):
for property in properties :
value = property.find(".//oc:value",namespace)
name= property.find('.//oc:name',namespace)
if (re.search(pattern2,name.text)):
value = property.find(".//oc:value",namespace)
name_element= component.find(".//oc:name",namespace)
print('value',value.text)
ports.append((name_element.text,value.text))
# if (re.search(pattern2,value.text)):
# #print('value',value.text)
# name_element= component.find(".//oc:name",namespace)
# ports.append(name_element.text)
return ports
device = {
'host': '172.17.254.22', # IP address or hostname of the remote machine
'port': 2022, # SSH port (default: 22)
'username': 'admin', # SSH username
'password': 'admin', # SSH password
'device_params': {'name': 'default'},
'hostkey_verify':False,
"allow_agent":False
,"look_for_keys":False
}
if __name__ == '__main__':
with manager.connect(host=device['host']
,port=device['port']
,username=device['username']
,password=device['password']
,hostkey_verify=device['hostkey_verify']
,allow_agent=device['allow_agent']
,look_for_keys=device['look_for_keys']) as m :
#edit_result = m.edit_config (target="running",config=delete_media_channel )
result = m.get_config (source="running").data_xml
#ports = extract_roadm_ports(result)
# optical_band_namespaces="http://flex-scale-project.eu/yang/flex-scale-mg-on"
#namespaces={"oc":"http://openconfig.net/yang/wavelength-router"}
#obj=extract_media_channels(result,namespaces)
#obj1=extract_optical_bands(result,namespaces)
# road_info= extract_openroadm_info(result)
# circuits=extract_roadm_circuits_pack(result)
#print (f'edit result {edit_result}')
print(f"result {result}")
#print(f"media_cahnnels {obj}")
#print(f"optical_bands {obj1}")
#print(f"circuits {circuits}")
# with open("context.log","w") as f:
# print (result,file=f)