Skip to content
Snippets Groups Projects
test.py 6.92 KiB
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


Mohammad Ismaeel's avatar
Mohammad Ismaeel committed
create_media='''
<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>  
Mohammad Ismaeel's avatar
Mohammad Ismaeel committed
  </wavelength-router>
</config>
'''
Mohammad Ismaeel's avatar
Mohammad Ismaeel committed
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>
'''


Mohammad Ismaeel's avatar
Mohammad Ismaeel committed
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
Mohammad Ismaeel's avatar
Mohammad Ismaeel committed
    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("}", "")
Mohammad Ismaeel's avatar
Mohammad Ismaeel committed
   
    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                
    'host': '172.17.254.22',        # IP address or hostname of the remote machine
    'port': 2022,                # SSH port (default: 22)
Mohammad Ismaeel's avatar
Mohammad Ismaeel committed
    '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__':
Mohammad Ismaeel's avatar
Mohammad Ismaeel committed
    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
        # 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)
Mohammad Ismaeel's avatar
Mohammad Ismaeel committed
        # 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}")
Mohammad Ismaeel's avatar
Mohammad Ismaeel committed

Mohammad Ismaeel's avatar
Mohammad Ismaeel committed
        #print(f"circuits {circuits}")
        # with open("context.log","w") as f:
        #      print (result,file=f)