Newer
Older
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
Andrea Sgambelluri
committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<wavelength-router xmlns="http://openconfig.net/yang/wavelength-router">
<media-channels>
<channel operation="create">
<index>2</index>
<config>
<name>C_BAND</name>
<optical-band-parent xmlns="http://flex-scale-project.eu/yang/flex-scale-mg-on">1</optical-band-parent>
<index>2</index>
<lower-frequency>192006250</lower-frequency>
<upper-frequency>192106250</upper-frequency>
</config>
<source>
<config>
<port-name>port-1-in</port-name>
</config>
</source>
</channel>
</media-channels>
</wavelength-router>
</config>
'''
delete_media_channel='''
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<wavelength-router xmlns="http://openconfig.net/yang/wavelength-router">
<media-channels>
<channel operation="delete">
<index>1</index>
<config>
<index>1</index>
</config>
</channel>
</media-channels>
delete_optical_band= '''
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<wavelength-router xmlns="http://openconfig.net/yang/wavelength-router">
<optical-bands xmlns="http://flex-scale-project.eu/yang/flex-scale-mg-on">
<optical-band operation="delete">
<index>1</index>
<config>
<index>1</index>
</config>
</optical-band>
</optical-bands>
</wavelength-router>
</config>
'''
edit_optical_channel= '''
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
<components xmlns="http://openconfig.net/yang/platform">
<component>
<name>channel-7</name>
<config>
<name>channel-7</name>
</config>
<optical-channel xmlns="http://openconfig.net/yang/terminal-device">
<config>
<target-output-power>300</target-output-power>
<frequency>3400000</frequency>
<operational-mode>1</operational-mode>
</config>
</optical-channel>
</component>
</components>
<terminal-device xmlns="http://openconfig.net/yang/terminal-device">
<logical-channels>
<channel>
<index>7</index>
<config>
<index>7</index>
<admin-state>ENABLED</admin-state>
</config>
</channel>
</logical-channels>
</terminal-device>
</config>
'''
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("}", "")
Andrea Sgambelluri
committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
Andrea Sgambelluri
committed
'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
}
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 :
Andrea Sgambelluri
committed
#edit_result = m.edit_config (target="running",config=delete_media_channel )
result = m.get_config (source="running").data_xml
Andrea Sgambelluri
committed
#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)