Commit fab0847a authored by Pablo Armingol's avatar Pablo Armingol
Browse files

Openconfig driver changes

1) Multivendor functionality
2) Working with dynamic templates
parent db17e18b
Loading
Loading
Loading
Loading
+126 −0
Original line number Original line Diff line number Diff line
from openconfig_acl import openconfig_acl
from pyangbind.lib.serialise import pybindIETFXMLEncoder

def acl_set_mgmt(parameters):
    Acl_name = parameters['name']
    Acl_type = parameters['type']
    ID       = parameters['sequence_id']
    DEL      = parameters['DEL']

    # Create an instance of the YANG model
    acl_instance = openconfig_acl()

    # Access the entry container
    acl_set = acl_instance.acl.acl_sets.acl_set.add(name = Acl_name, type=Acl_type)
    acl_entrie = acl_set.acl_entries.acl_entry.add(ID)

    if DEL: 
        # Dump the entire instance as RFC 7950 XML
        acl_set = pybindIETFXMLEncoder.serialise(acl_instance)
        #Delete replace
        acl_set = acl_set.replace('<acl-entry>','<acl-entry xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="delete">')
        #Generic replaces
        acl_set = acl_set.replace('<openconfig-acl xmlns="http://openconfig.net/yang/acl">',"")
        acl_set = acl_set.replace('<acl>','<acl xmlns="http://openconfig.net/yang/acl">')
        acl_set = acl_set.replace('</openconfig-acl>','')
        
    else:
        acl_set.config.name = Acl_name
        acl_set.config.type = Acl_type
        acl_entrie.config.sequence_id = ID
        
        # Configuration per type
        if "L2" in Acl_type:
            for variable, valor in parameters.items():
                if   "source_mac"          in variable and len(valor) != 0: acl_entrie.l2.config.source_mac = valor
                elif "destination_mac"     in variable and len(valor) != 0: acl_entrie.l2.config.destination_mac = valor
                
        elif "IPV4" in Acl_type:
            for variable, valor in parameters.items():
                if   "source_address"       in variable and len(valor) != 0: acl_entrie.ipv4.config.source_address = valor
                elif "destination_address"  in variable and len(valor) != 0: acl_entrie.ipv4.config.destination_address = valor
                elif "protocol"             in variable and len(valor) != 0: acl_entrie.ipv4.config.protocol = valor
                elif "hop_limit"            in variable and len(valor) != 0: acl_entrie.ipv4.config.hop_limit = valor
                elif "dscp"                 in variable and len(valor) != 0: acl_entrie.ipv4.config.dscp = valor
                  
            for variable, valor in parameters.items():
                if   "source_port"          in variable and len(valor) != 0: acl_entrie.transport.config.source_port = valor
                elif "destination_port"     in variable and len(valor) != 0: acl_entrie.transport.config.destination_port = valor
                elif "tcp_flags"            in variable and len(valor) != 0: acl_entrie.transport.config.tcp_flags = valor
                
        elif "IPV6" in Acl_type:
            for variable, valor in parameters.items():
                if   "source_address"       in variable and len(valor) != 0: acl_entrie.ipv6.config.source_address = valor
                elif "destination_address"  in variable and len(valor) != 0: acl_entrie.ipv6.config.destination_address = valor
                elif "protocol"             in variable and len(valor) != 0: acl_entrie.ipv6.config.protocol = valor
                elif "hop_limit"            in variable and len(valor) != 0: acl_entrie.ipv6.config.hop_limit = valor
                elif "dscp"                 in variable and len(valor) != 0: acl_entrie.ipv6.config.dscp = valor
        
        for variable, valor in parameters.items():
            if   "forwarding_action"        in variable and len(valor) != 0: acl_entrie.actions.config.forwarding_action = valor
            elif "log_action"               in variable and len(valor) != 0: acl_entrie.actions.config.log_action = valor
            
        # Dump the entire instance as RFC 7950 XML
        acl_set = pybindIETFXMLEncoder.serialise(acl_instance)
        acl_set = acl_set.replace('<openconfig-acl xmlns="http://openconfig.net/yang/acl">',"")
        acl_set = acl_set.replace('<acl>','<acl xmlns="http://openconfig.net/yang/acl">')
        acl_set = acl_set.replace('</openconfig-acl>','')

    return(acl_set)

def acl_interface(parameters):
    ID           = parameters['id']
    DEL          = parameters['DEL']
    verify       = str(parameters)                                    #Verify transforms the received parameters into a string format for later making verifications and modifications

    # Create an instance of the YANG model
    acl_instance = openconfig_acl()

    # Access the entry container
    interface = acl_instance.acl.interfaces.interface.add(id = ID)

    if DEL: 
        acl_set = pybindIETFXMLEncoder.serialise(acl_instance)                                      
        acl_set = acl_set.replace('<interface>','<interface xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="delete">')     

        # Dump the entire instance as RFC 7950 XML
        acl_set = acl_set.replace('<openconfig-acl xmlns="http://openconfig.net/yang/acl">'," ")     
        acl_set = acl_set.replace('<acl>','<acl xmlns="http://openconfig.net/yang/acl">')            
        acl_set = acl_set.replace('</openconfig-acl>','')                                           
    else:
        #Config - Interface
        interface.config.id = ID
        #If the Interface parameter is defined [OPTIONAL-PARAMETER]
        if verify.find('interface')>0:
            Interface = parameters['interface']        
            if  len(Interface) != 0: interface.interface_ref.config.interface = Interface        #If interface parameter has a value
        
        #If the Subinterface parameter is defined [OPTIONAL-PARAMETER]
        if verify.find('subinterface')>0:
            Subinterface = parameters['subinterface']        
            if  len(Interface) != 0: interface.interface_ref.config.subinterface = Subinterface   #If subinterface parameter has a value
        
         # Configuration per type
        if verify.find('set_name_ingress')>0:                           #If set_name_ingress is defined
            Ingress_name = parameters['set_name_ingress']        
            if  len(Ingress_name) != 0: 
                Ingress_type         = parameters['type_ingress']
                ingress= interface.ingress_acl_sets.ingress_acl_set.add(set_name = Ingress_name, type = Ingress_type)
                ingress.config.set_name = Ingress_name
                ingress.config.type     = Ingress_type
        
        if verify.find('set_name_egress')>0:                            #If set_name_egress is defined
            Egress_name = parameters['set_name_egress']        
            if  len(Egress_name) != 0: 
                Egress_name         = parameters['set_name_egress']
                Egress_type         = parameters['type_egress']
                egress= interface.egress_acl_sets.egress_acl_set.add(set_name = Egress_name, type = Egress_type)
                egress.config.set_name = Egress_name
                egress.config.type     = Egress_type
  
        # Dump the entire instance as RFC 7950 XML
        acl_set = pybindIETFXMLEncoder.serialise(acl_instance)                                      
        acl_set = acl_set.replace('<openconfig-acl xmlns="http://openconfig.net/yang/acl">',"")     
        acl_set = acl_set.replace('<acl>','<acl xmlns="http://openconfig.net/yang/acl">')           
        acl_set = acl_set.replace('</openconfig-acl>','')                                           
    return(acl_set)
+9903 −0

File added.

Preview size limit exceeded, changes collapsed.

+47 −1
Original line number Original line Diff line number Diff line
@@ -12,8 +12,13 @@
# See the License for the specific language governing permissions and
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.


import json
import lxml.etree as ET
import lxml.etree as ET
from typing import Collection, Dict
from typing import Collection, Dict, Any
from  ACL.ACL_multivendor  import      acl_set_mgmt, acl_interface        
from VPN.Network_instance_multivendor import create_network_instance, associate_virtual_circuit, associate_RP_to_NI, add_protocol_NI, create_table_conns, associate_If_to_NI
from VPN.Interfaces_multivendor       import create_If_SubIf  
from VPN.Routing_policy               import create_rp_def, create_rp_statement


def add_value_from_tag(target : Dict, field_name: str, field_value : ET.Element, cast=None) -> None:
def add_value_from_tag(target : Dict, field_name: str, field_value : ET.Element, cast=None) -> None:
    if field_value is None or field_value.text is None: return
    if field_value is None or field_value.text is None: return
@@ -24,3 +29,44 @@ def add_value_from_tag(target : Dict, field_name: str, field_value : ET.Element,
def add_value_from_collection(target : Dict, field_name: str, field_value : Collection) -> None:
def add_value_from_collection(target : Dict, field_name: str, field_value : Collection) -> None:
    if field_value is None or len(field_value) == 0: return
    if field_value is None or len(field_value) == 0: return
    target[field_name] = field_value
    target[field_name] = field_value

def generate_template(resource_key: str, resource_value: str, delete: bool) -> str:
    data: Dict[str, Any] = json.loads(resource_value)
    data['DEL'] = delete

    list_resource_key = resource_key.split("/")

    if "network_instance" in list_resource_key[1]:
        if "connection_point" in list_resource_key:
            result_template = associate_virtual_circuit(data)
        elif "inter_instance_policies" in list_resource_key:
            result_template = associate_RP_to_NI(data)
        elif "protocolos" in list_resource_key:
            result_template = add_protocol_NI(data)
        elif "table_connections" in list_resource_key:
            result_template = create_table_conns(data)
        elif "interface" in list_resource_key:
            result_template = associate_If_to_NI(data)
        else:
            result_template = create_network_instance(data)

    elif "interface" in list_resource_key[1]:
        if "subinterface" in list_resource_key:
            result_template = create_If_SubIf(data)

    elif "acl" in list_resource_key[1]:
        if "acl_set" in list_resource_key:
            result_template = acl_set_mgmt(data)
        else:
            result_template = acl_interface(data)

    elif "routing_policy" in list_resource_key[1]:
        if "bgp_defined_set" in list_resource_key:
            result_template = create_rp_def(data)
        else:
            result_template = create_rp_statement(data)
    
    else:
        result_template = ""

    return result_template
 No newline at end of file
+100 −0
Original line number Original line Diff line number Diff line
from openconfig_interfaces import openconfig_interfaces
from pyangbind.lib.serialise import pybindIETFXMLEncoder

def set_vlan(OptionalParams):                                   #[L2/L3] Sets a VLANID and a VENDOR that will be requested for executing the following methods
    verify = str(OptionalParams)                                    #Verify transforms the received parameters into a string format for later making verifications and modifications
    
    #If the Vendor parameter is defined [OPTIONAL-PARAMETER]
    if verify.find('vendor')>0:
        Vendor = OptionalParams['vendor']  

    #If the VlanID parameter is defined [OPTIONAL-PARAMETER]
    if verify.find('vlan_id')>0:
        VlanID = OptionalParams['vlan_id'] 
        if VlanID == 0 and "ADVA" in Vendor:
            vlan = '</description>\n \t    <untagged-allowed xmlns="http://www.advaoptical.com/cim/adva-dnos-oc-interfaces">true</untagged-allowed></config>'
        elif VlanID != 0:
            vlan = '</description>\n          </config>\n\t  <vlan xmlns="http://openconfig.net/yang/vlan"> \n\t    <match> \n\t      <single-tagged> \n \t\t<config>\n \t\t  <vlan-id>'+str(VlanID)+'</vlan-id> \n \t\t</config> \n \t      </single-tagged> \n \t    </match> \n \t  </vlan>'
        else:
            vlan = '</description>\n          </config>'
    else:
        vlan = '</description>\n          </config>'   
    return vlan

def set_ip(OptionalParams):                                     #[L3]    Sets a IPAddress that will be requested for executing the following L3VPN methods
    verify = str(OptionalParams)                                    #Verify transforms the received parameters into a string format for later making verifications and modifications
    
    #If the Address_ip parameter is defined [OPTIONAL-PARAMETER]
    if verify.find('address_ip')>0:
        IP     = OptionalParams['address_ip']  
        Prefix = OptionalParams['address_prefix']
        address = '  <ipv4 xmlns="http://openconfig.net/yang/interfaces/ip"> \n\t    <addresses> \n\t      <address> \n \t\t<ip>'+IP+'</ip> \n \t\t<config>\n \t\t  <ip>'+IP+'</ip> \n \t\t  <prefix-length>'+str(Prefix)+'</prefix-length> \n \t\t</config> \n \t      </address> \n \t    </addresses> \n \t  </ipv4>  \n \t</subinterface>'
    else:
        address ='</subinterface>'
    return address

def create_If_SubIf(parameters):                                #[L2/L3] Creates a Interface with a Subinterface as described in /interface[{:s}]/subinterface[{:d}]
    Interface_name     = parameters['name']
    DEL                = parameters['DEL']                      #If the parameters DEL is set to "TRUE" that will mean that is for making a DELETE, ELSE is for creating
    verify = str(parameters)                                    #Verify transforms the received parameters into a string format for later making verifications and modifications

    #Create an instance of the YANG model
    InterfaceInstance = openconfig_interfaces()

    if DEL==True:                                               #DELETE OPERATION
        # Access the entry container
        InterfaceInstance_set = InterfaceInstance.interfaces.interface.add(name = Interface_name)
        
        # Dump the entire instance as RFC 7950 XML
        InterfaceInstance_set = pybindIETFXMLEncoder.serialise(InterfaceInstance)

        #Replace for setting the "Delete" Operation
        InterfaceInstance_set = InterfaceInstance_set.replace('<interface>','<interface xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="delete">')

        #Generic Replaces
        InterfaceInstance_set = InterfaceInstance_set.replace('<openconfig-interfaces xmlns="http://openconfig.net/yang/interfaces">',"")
        InterfaceInstance_set = InterfaceInstance_set.replace('<interfaces>','<interfaces xmlns="http://openconfig.net/yang/interfaces">')
        InterfaceInstance_set = InterfaceInstance_set.replace('</openconfig-interfaces>','')

    else:                                                       #MERGE OPERATION
        Interface_type     = parameters['type']
        SubInterface_Index = parameters["index"]

        #Access the entry container
        InterfaceInstance_set = InterfaceInstance.interfaces.interface.add(name = Interface_name)
        InterfaceInstance_set.config.name = Interface_name
        InterfaceInstance_set.config.enabled = True

        #SubIntefaces-Config
        SubInterfaceInstance = InterfaceInstance_set.subinterfaces.subinterface.add(index = SubInterface_Index)
        SubInterfaceInstance.config.index = SubInterface_Index

        #If the description parameter is defined [OPTIONAL-PARAMETER]
        if verify.find('description')>0:
            Description = parameters['description']   
            if  len(Description) != 0: SubInterfaceInstance.config.description = Description   #If description parameter has a value

        #If the MTU parameter is defined [OPTIONAL-PARAMETER]
        if verify.find('mtu')>0:
            MTU = parameters['mtu']    
            if  MTU != 0: InterfaceInstance_set.config.mtu = MTU                       #If MTU parameter has a value

        #Dump the entire instance as RFC 750 XML
        InterfaceInstance_set = pybindIETFXMLEncoder.serialise(InterfaceInstance)

        #Replaces for adding the Interface Type
        InterfaceInstance_set = InterfaceInstance_set.replace('</config>\n      <subinterfaces>','  <type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:'+Interface_type+'</type>\n      </config>\n      <subinterfaces>')
        vlan = set_vlan(parameters)
        InterfaceInstance_set = InterfaceInstance_set.replace('</description>\n          </config>',vlan)
        
        if "l3ipvlan" in Interface_type: 
            ip = set_ip(parameters)
            InterfaceInstance_set = InterfaceInstance_set.replace('</subinterface>',ip)

        #Generic Replaces
        InterfaceInstance_set = InterfaceInstance_set.replace('<openconfig-interfaces xmlns="http://openconfig.net/yang/interfaces">',"")
        InterfaceInstance_set = InterfaceInstance_set.replace('<interfaces>','<interfaces xmlns="http://openconfig.net/yang/interfaces">')
        InterfaceInstance_set = InterfaceInstance_set.replace('</openconfig-interfaces>','')

    return (InterfaceInstance_set) 
+279 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading