Beispiel #1
0
 def test_NETCONFVisitor(self):
     Component = testComponent()
     serializer = NETCONFVisitor()
     output = Component.parse(serializer)
     xml_string = serializer.print(output)
     teststring = '<test><leaf_l1_var1>var1</leaf_l1_var1><leaf_l1_var2 /><leafList_l1>l_var1</leafList_l1><leafList_l1>l_var2</leafList_l1><leafList_l1>l_var3</leafList_l1><leaf_withKey_l1 attr_key="value">var4</leaf_withKey_l1><level2><leaf_l2_var1>l2_var1</leaf_l2_var1><leaf_l2_var2 /><leafList_l2>var4</leafList_l2><leafList_l2>var4</leafList_l2><leafList_l2>var4</leafList_l2><leaf_withKey_l2 testkey="value">var4</leaf_withKey_l2></level2><level3><level3><leaf_l3_var1>l3_var1</leaf_l3_var1><leaf_l3_var2 /><leafList_l3>l_var1</leafList_l3><leafList_l3>l_var2</leafList_l3><leafList_l3>l_var3</leafList_l3><leaf_withKey_l3_var3 l3_key="key">var3</leaf_withKey_l3_var3></level3></level3></test>'
     self.assertEqual(xml_string, teststring)
def create_prefix_list_payload(name, prefixes):
    helper = ConfigurePrefixList(name=name, replace=True)

    for prefix in prefixes:
        if (prefix['exact'] is True):
            helper.addPrefix(action='permit', network=prefix['prefix'])
        else:
            args = {'action': 'permit', 'network': prefix['prefix']}
            if ('less-equal' in prefix):
                args['le'] = prefix['less-equal']
            if ('greater-equal' in prefix):
                args['ge'] = prefix['greater-equal']
            helper.addPrefix(**args)

    config = configComponent()
    native = nativeComponent()
    ip = ipComponent()
    prefix_list = helper.getPrefixList()

    config.add(native)
    native.add(ip)
    ip.add(prefix_list)
    serializer = NETCONFVisitor()
    output = config.parse(serializer)
    xml_string = serializer.print(output)
    return xml_string
def restore_device(device, backup_path):
    rpc = rpcComponent()
    roolback = rollbackComponent(target_url=backup_path)
    rpc.add(roolback)
    serializer = NETCONFVisitor()
    output = rpc.parse(serializer)
    rollbackPayload = serializer.print(output)

    reply = device.nc.request(rollbackPayload, timeout=40)
    rollbackData = xmltodict.parse(reply)
    if 'result' not in rollbackData['rpc-reply']:
        raise Exception(reply)
Beispiel #4
0
 def testStaticRoute(self):
     self.maxDiff = None
     data = {
         'prefix': '172.30.0.0/24',
         'name': 'Test',
         'nexthop': '192.168.0.4'
     }
     expected = "<config><network-instances xmlns=\"http://openconfig.net/yang/network-instance\"><network-instance><name>default</name><protocols><protocol><identifier xmlns:oc-pol-types=\"http://openconfig.net/yang/policy-types\">oc-pol-types:STATIC</identifier><name>DEFAULT</name><config><identifier xmlns:oc-pol-types=\"http://openconfig.net/yang/policy-types\">oc-pol-types:STATIC</identifier><name>DEFAULT</name></config><static-routes><static><prefix>{prefix}</prefix><config><prefix>{prefix}</prefix></config><next-hops><next-hop><index>{name}</index><config><index>{name}</index><next-hop>{nexthop}</next-hop></config></next-hop></next-hops></static></static-routes></protocol></protocols></network-instance></network-instances></config>"
     static = CreateStaticRoute(**data)
     serializer = NETCONFVisitor()
     output = static.parse(serializer)
     xml_string = serializer.print(output)
     self.assertEqual(xml_string, expected.format(**data))
def create_backup(device):
    rpc = rpcComponent()
    ckP = checkpointComponent()
    rpc.add(ckP)
    serializer = NETCONFVisitor()
    output = rpc.parse(serializer)
    backup_payload = serializer.print(output)

    checkpointReply = device.nc.request(backup_payload, timeout=40)
    checkpointStatus = xmltodict.parse(checkpointReply)
    if (checkpointStatus['rpc-reply']['result']['#text'] !=
            'Checkpoint successful'):
        raise Exception(
            f"Failed to backup on {device.name}\nRPC response: {checkpointReply}"
        )
def get_configured_prefixes(as_set, device):
    filter_component = filterComponent()
    native_component = nativeComponent()
    ip_component = ipComponent()
    helper = ConfigurePrefixList(name=as_set)
    prefix_list_component = helper.getPrefixList()

    filter_component.add(native_component)
    native_component.add(ip_component)
    ip_component.add(prefix_list_component)
    serializer = NETCONFVisitor()
    output = filter_component.parse(serializer)
    filter_payload = serializer.print(output)

    configured_prefixes = device.nc.get_config(source='running',
                                               filter=filter_payload)
    configObject = xmltodict.parse(configured_prefixes.data_xml)
    conf_prefixes = configObject['data']['native']['ip']['prefix-list'][
        'prefixes']['seq']
    prefixes = []
    if (isinstance(conf_prefixes, list)):
        for prefix in conf_prefixes:
            prefix_object = {'prefix': prefix['ip']}
            if (('le' in prefix) or ('ge' in prefix)):
                prefix_object['exact'] = False
                if ('le' in prefix):
                    prefix_object['less-equal'] = int(prefix['le'])
                if ('ge' in prefix):
                    prefix_object['greater-equal'] = int(prefix['ge'])
            else:
                prefix_object['exact'] = True
            prefixes.append(prefix_object)
    else:
        prefix = conf_prefixes
        prefix_object = {'prefix': prefix['ip']}
        if (('le' in prefix) or ('ge' in prefix)):
            prefix_object['exact'] = True
            if ('le' in prefix):
                prefix_object['less-equal'] = int(prefix['le'])
            if ('ge' in prefix):
                prefix_object['greater-equal'] = int(prefix['ge'])
        else:
            prefix_object['exact'] = False
        prefixes.append(prefix_object)

    return prefixes
 def test_CorrectIPInterfaceCreation(self):
     self.maxDiff = None
     data = {
         'name': 'GigabitEthernet0/2',
         'if_type': 'ethernet',
         'if_description': "Test Interface",
         'if_mtu': 1500,
         'enabled': True,
         'sub_index': 0,
         'sub_desc': None,
         'ip_version': 4,
         'address': '10.0.0.1',
         'prefix_length': 30
     }
     iface = createIPInterface(**data)
     serializer = NETCONFVisitor()
     output = iface.parse(serializer)
     xml_string = serializer.print(output)
     data['if_type'] = self.IETF_INTERFACE_TYPES[data['if_type']]
     if (data['enabled']):
         data['enabled'] = 'true'
     teststring = "<interface><name>{name}</name><config><description>{if_description}</description><mtu>{if_mtu}</mtu><enabled>{enabled}</enabled><type xmlns:ianaift=\"urn:ietf:params:xml:ns:yang:iana-if-type\">{if_type}</type></config><subinterfaces><subinterface><index>{sub_index}</index><oc-ip:ipv4><oc-ip:addresses><oc-ip:address><oc-ip:ip>{address}</oc-ip:ip><oc-ip:config><oc-ip:ip>{address}</oc-ip:ip><oc-ip:prefix-length>{prefix_length}</oc-ip:prefix-length></oc-ip:config></oc-ip:address></oc-ip:addresses></oc-ip:ipv4></subinterface></subinterfaces></interface>"
     self.assertEqual(xml_string, teststring.format(**data))
Beispiel #8
0
 def test_NETCONFException(self):
     Component = testComponent(level4=True)
     serializer = NETCONFVisitor()
     self.assertRaises(VisitorError, Component.parse, serializer)