def main():
    print("""
#Description: Testcase for draft-ietf-netmod-rfc7277bis-00.txt ietf-ip module.
#Procedure:
#1 - <edit-config> configuration as in rfc7277bis Appendix A.
#2 - <get-data> the /interfaces container and verify data is same as in rfc7277bis Appendix B.
""")

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--server",
        help=
        "server name e.g. 127.0.0.1 or server.com (127.0.0.1 if not specified)"
    )
    parser.add_argument("--user",
                        help="username e.g. admin ($USER if not specified)")
    parser.add_argument("--port", help="port e.g. 830 (830 if not specified)")
    parser.add_argument(
        "--password",
        help="password e.g. mypass123 (passwordless if not specified)")

    args = parser.parse_args()

    if (args.server == None or args.server == ""):
        server = "127.0.0.1"
    else:
        server = args.server

    if (args.port == None or args.port == ""):
        port = 830
    else:
        port = int(args.port)

    if (args.user == None or args.user == ""):
        user = os.getenv('USER')
    else:
        user = args.user

    if (args.password == None or args.password == ""):
        password = None
    else:
        password = args.password

    conn_raw = litenc.litenc()
    ret = conn_raw.connect(server=server,
                           port=port,
                           user=user,
                           password=password)
    if ret != 0:
        print "[FAILED] Connecting to server=%(server)s:" % {'server': server}
        return (-1)
    print "[OK] Connecting to server=%(server)s:" % {'server': server}
    conn = litenc_lxml.litenc_lxml(conn_raw, strip_namespaces=True)
    ret = conn_raw.send("""
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>
 </capabilities>
</hello>
""")
    if ret != 0:
        print("[FAILED] Sending <hello>")
        return (-1)
    (ret, reply_xml) = conn_raw.receive()
    if ret != 0:
        print("[FAILED] Receiving <hello>")
        return (-1)

    print "[OK] Receiving <hello> =%(reply_xml)s:" % {'reply_xml': reply_xml}

    print("Connected ...")

    get_yang_library_rpc = """
<get>
  <filter type="subtree">
    <modules-state xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-library"/>
  </filter>
</get>
"""

    print("<get> - /ietf-yang-library:modules-state ...")
    result = conn.rpc(get_yang_library_rpc, strip_ns=False)
    print lxml.etree.tostring(result,
                              pretty_print=True,
                              inclusive_ns_prefixes=True)
    namespaces = {"nc": "urn:ietf:params:xml:ns:netconf:base:1.0"}
    data = result.xpath('./nc:data', namespaces=namespaces)
    assert (len(data) == 1)
    #Copied from draft-ietf-netmod-rfc7277bis-00 Appendix A.
    edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
       <interfaces
           xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"
           xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">
         <interface>
           <name>eth0</name>
           <type>ianaift:ethernetCsmacd</type>
           <ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip">
             <address>
               <ip>192.0.2.1</ip>
               <prefix-length>24</prefix-length>
             </address>
           </ipv4>
           <ipv6 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip">
             <mtu>1280</mtu>
             <address>
               <ip>2001:db8::10</ip>
               <prefix-length>32</prefix-length>
             </address>
             <dup-addr-detect-transmits>0</dup-addr-detect-transmits>
           </ipv6>
         </interface>
       </interfaces>
    </config>
</edit-config>
"""

    print("<edit-config> - load Appendix B. example config to 'candidate' ...")
    result = conn.rpc(edit_config_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('./ok')
    assert (len(ok) == 1)

    commit_rpc = '<commit/>'

    print("<commit> - commit example config ...")
    result = conn.rpc(commit_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('./ok')
    assert (len(ok) == 1)

    get_example_data_rpc = """
<get-data xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-datastores">
  <datastore xmlns:ds="urn:ietf:params:xml:ns:yang:ietf-datastores">ds:operational</datastore>
  <subtree-filter>
    <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"/>
  </subtree-filter>
  <with-origin/>
</get-data>
"""

    print("<get-data> - Appendix B. data ...")
    result = conn.rpc(get_example_data_rpc, strip_ns=False)
    print lxml.etree.tostring(result,
                              pretty_print=True,
                              inclusive_ns_prefixes=True)
    namespaces = {
        "ncds": "urn:ietf:params:xml:ns:yang:ietf-netconf-datastores"
    }
    data = result.xpath('./ncds:data', namespaces=namespaces)
    assert (len(data) == 1)
    #Copy from draft-netconf-nmda-netconf-01
    #removed  <forwarding>false</forwarding> if this is the default value then enabled should also be present
    #removed  <mtu>1500</mtu> since it is not in the configuration
    #add or:origin="or:learned" to the ipv4 neighbor
    #removed   <forwarding>false</forwarding> from ipv6 since it is not in the configuration

    expected = """
<data xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-datastores" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
       <interfaces
           xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"
           xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type"
           xmlns:or="urn:ietf:params:xml:ns:yang:ietf-origin">
         <interface or:origin="or:intended">
           <name>eth0</name>
           <type>ianaift:ethernetCsmacd</type>
           <!-- other parameters from ietf-interfaces omitted -->
           <ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip">
             <address>
               <ip>192.0.2.1</ip>
               <prefix-length>24</prefix-length>
               <origin>static</origin>
             </address>
             <neighbor or:origin="or:learned">
               <ip>192.0.2.2</ip>
               <link-layer-address>
                 00:01:02:03:04:05
               </link-layer-address>
             </neighbor>
           </ipv4>
           <ipv6 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip">
             <mtu>1280</mtu>
             <address>
               <ip>2001:db8::10</ip>
               <prefix-length>32</prefix-length>
               <origin>static</origin>
               <status>preferred</status>
             </address>
             <address or:origin="or:learned">
               <ip>2001:db8::1:100</ip>
               <prefix-length>32</prefix-length>
               <origin>dhcp</origin>
               <status>preferred</status>
             </address>
             <dup-addr-detect-transmits>0</dup-addr-detect-transmits>
             <neighbor or:origin="or:learned">
               <ip>2001:db8::1</ip>
               <link-layer-address>
                 00:01:02:03:04:05
               </link-layer-address>
               <origin>dynamic</origin>
               <is-router/>
               <state>reachable</state>
             </neighbor>
             <neighbor or:origin="or:learned">
               <ip>2001:db8::4</ip>
               <origin>dynamic</origin>
               <state>incomplete</state>
             </neighbor>
           </ipv6>
         </interface>
       </interfaces>
</data>
"""

    expected = lxml.etree.fromstring(expected)

    #strip comments
    comments = expected.xpath('//comment()')
    for c in comments:
        p = c.getparent()
        p.remove(c)

#strip namespaces
    data1 = expected.xpath('.', namespaces=namespaces)
    data_expected = strip_namespaces(data1[0])
    data_received = strip_namespaces(data[0])

    #sort schema lists by key in alphabetical key order - hardcoded /interfaces/interface[name]
    for node in data_expected.findall("./interfaces"):
        node[:] = sorted(node, key=lambda child: get_interface_name(child))
    for node in data_received.findall("./interfaces"):
        node[:] = sorted(node, key=lambda child: get_interface_name(child))

    #sort attributes
    a = StringIO()
    b = StringIO()
    data_expected.write_c14n(a)
    data_received.write_c14n(b)

    print("Expected:")
    print(a.getvalue())
    print("Received:")
    print(b.getvalue())
    if yang_data_equal(lxml.etree.fromstring(a.getvalue()),
                       lxml.etree.fromstring(b.getvalue())):
        print "Bingo!"
        return 0
    else:
        print "Error: YANG data not equal!"
        return 1
Beispiel #2
0
def main():
    print("""
#Description: Demonstrate that duplicated list entries in edit-config are detected.
#Procedure:
#Description: Using sub-interfaces configure vlan bridge.
#Procedure:
#1 - Create interface "xe0", "ge0", "ge1" of type=ethernetCsmacd.
#2 - Create sub-interface "xe0-green" - s-vlan-id=1000, "ge1-green" - c-vlan-id=10 of type=ethSubInterface.
#3 - Create VLAN bridge with "ge0", "ge1-green" and "xe0-green".
#4 - Commit.
""")

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--server",
        help=
        "server name e.g. 127.0.0.1 or server.com (127.0.0.1 if not specified)"
    )
    parser.add_argument("--user",
                        help="username e.g. admin ($USER if not specified)")
    parser.add_argument("--port", help="port e.g. 830 (830 if not specified)")
    parser.add_argument(
        "--password",
        help="password e.g. mypass123 (passwordless if not specified)")

    args = parser.parse_args()

    if (args.server == None or args.server == ""):
        server = "127.0.0.1"
    else:
        server = args.server

    if (args.port == None or args.port == ""):
        port = 830
    else:
        port = int(args.port)

    if (args.user == None or args.user == ""):
        user = os.getenv('USER')
    else:
        user = args.user

    if (args.password == None or args.password == ""):
        password = None
    else:
        password = args.password

    conn_raw = litenc.litenc()
    ret = conn_raw.connect(server=server,
                           port=port,
                           user=user,
                           password=password)
    if ret != 0:
        print "[FAILED] Connecting to server=%(server)s:" % {'server': server}
        return (-1)
    print "[OK] Connecting to server=%(server)s:" % {'server': server}
    conn = litenc_lxml.litenc_lxml(conn_raw)
    ret = conn_raw.send("""
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>
 </capabilities>
</hello>
""")
    if ret != 0:
        print("[FAILED] Sending <hello>")
        return (-1)
    (ret, reply_xml) = conn_raw.receive()
    if ret != 0:
        print("[FAILED] Receiving <hello>")
        return (-1)

    print "[OK] Receiving <hello> =%(reply_xml)s:" % {'reply_xml': reply_xml}

    print("Connected ...")

    commit_rpc = """
<commit/>
"""
    print("commit ...")
    result = conn.rpc(commit_rpc)

    edit_config_rpc = """
<edit-config>
   <target>
     <candidate/>
   </target>
   <default-operation>merge</default-operation>
   <test-option>set</test-option>
<config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
    <interface>
      <name>ge0</name>
      <type
        xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
    </interface>
    <interface>
      <name>ge1</name>
      <type
        xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
    </interface>
    <interface>
      <name>ge1-green</name>
      <type
        xmlns:if-cmn="urn:ietf:params:xml:ns:yang:ietf-interfaces-common">if-cmn:ethSubInterface</type>
      <encapsulation xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces-common">
        <flexible xmlns="urn:ietf:params:xml:ns:yang:ietf-flexible-encapsulation">
          <match>
            <dot1q-vlan-tagged>
              <outer-tag>
                <dot1q-tag>
                  <tag-type
                    xmlns:dot1q-types="urn:ieee:std:802.1Q:yang:ieee802-dot1q-types">dot1q-types:c-vlan</tag-type>
                  <vlan-id>10</vlan-id>
                </dot1q-tag>
              </outer-tag>
            </dot1q-vlan-tagged>
          </match>
        </flexible>
      </encapsulation>
      <forwarding-mode xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces-common" xmlns:if-cmn="urn:ietf:params:xml:ns:yang:ietf-interfaces-common">if-cmn:layer-2-forwarding</forwarding-mode>
      <parent-interface xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces-common">ge0</parent-interface>
    </interface>
    <interface>
      <name>xe0</name>
      <type
        xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
    </interface>
    <interface>
      <name>xe0-green</name>
      <type
        xmlns:if-cmn="urn:ietf:params:xml:ns:yang:ietf-interfaces-common">if-cmn:ethSubInterface</type>
      <encapsulation xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces-common">
        <flexible xmlns="urn:ietf:params:xml:ns:yang:ietf-flexible-encapsulation">
          <match>
            <dot1q-vlan-tagged>
              <outer-tag>
                <dot1q-tag>
                  <tag-type
                    xmlns:dot1q-types="urn:ieee:std:802.1Q:yang:ieee802-dot1q-types">dot1q-types:s-vlan</tag-type>
                  <vlan-id>1000</vlan-id>
                </dot1q-tag>
              </outer-tag>
              <second-tag>
                <dot1q-tag>
                  <tag-type
                    xmlns:dot1q-types="urn:ieee:std:802.1Q:yang:ieee802-dot1q-types">dot1q-types:c-vlan</tag-type>
                  <vlan-id>10</vlan-id>
                </dot1q-tag>
              </second-tag>
            </dot1q-vlan-tagged>
          </match>
        </flexible>
      </encapsulation>
      <forwarding-mode xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces-common" xmlns:if-cmn="urn:ietf:params:xml:ns:yang:ietf-interfaces-common">if-cmn:layer-2-forwarding</forwarding-mode>
      <parent-interface xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces-common">xe0</parent-interface>
    </interface>
  </interfaces>
  <nacm xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-acm"/>
  <vlans xmlns="http://example.com/ns/vlans">
    <vlan>
      <name>green</name>
      <interface>ge0</interface>
      <interface>ge1-green</interface>
      <interface>xe0-green</interface>
    </vlan>
  </vlans>
</config>
 </edit-config>
"""
    print("edit-config - create vlan ...")
    result = conn.rpc(edit_config_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('ok')
    print result
    print ok
    print lxml.etree.tostring(result)
    assert (len(ok) == 1)

    commit_rpc = """
<commit/>
"""
    print("commit ...")
    result = conn.rpc(commit_rpc)
    print lxml.etree.tostring(result)
    assert (len(ok) == 1)

    edit_config_rpc = """
<edit-config>
   <target>
     <candidate/>
   </target>
   <default-operation>merge</default-operation>
   <test-option>set</test-option>
 <config>
  <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="delete">
  </interfaces>
  <vlans xmlns="http://example.com/ns/vlans" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="delete">
  </vlans>
 </config>
 </edit-config>
"""
    print("edit-config - clean-up ...")
    result = conn.rpc(edit_config_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('ok')
    print result
    print ok
    print lxml.etree.tostring(result)
    assert (len(ok) == 1)

    commit_rpc = """
<commit/>
"""
    print("commit ...")
    result = conn.rpc(commit_rpc)
    print lxml.etree.tostring(result)
    assert (len(ok) == 1)
Beispiel #3
0
def main():
	print("""
#Description: Demonstrate that derived-from-or-self() works.
#Procedure:
#1 - Create interfaces foo type=fast-ethernet and bar type=gigabit-ethernet.
#2 - Validate fast-ethernet-specific leaf can be commited on foo.
#3 - Validate fast-ethernet-specific leaf can not be commited on bar.
""")

	parser = argparse.ArgumentParser()
	parser.add_argument("--server", help="server name e.g. 127.0.0.1 or server.com (127.0.0.1 if not specified)")
	parser.add_argument("--user", help="username e.g. admin ($USER if not specified)")
	parser.add_argument("--port", help="port e.g. 830 (830 if not specified)")
	parser.add_argument("--password", help="password e.g. mypass123 (passwordless if not specified)")

	args = parser.parse_args()

	if(args.server==None or args.server==""):
		server="127.0.0.1"
	else:
		server=args.server

	if(args.port==None or args.port==""):
		port=830
	else:
		port=int(args.port)

	if(args.user==None or args.user==""):
		user=os.getenv('USER')
	else:
		user=args.user

	if(args.password==None or args.password==""):
		password=None
	else:
		password=args.password


	conn_raw = litenc.litenc()
	ret = conn_raw.connect(server=server, port=port, user=user, password=password)
	if ret != 0:
		print "[FAILED] Connecting to server=%(server)s:" % {'server':server}
		return(-1)
	print "[OK] Connecting to server=%(server)s:" % {'server':server}
	conn=litenc_lxml.litenc_lxml(conn_raw, strip_namespaces=True)
	ret = conn_raw.send("""
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>
 </capabilities>
</hello>
""")
	if ret != 0:
		print("[FAILED] Sending <hello>")
		return(-1)
	(ret, reply_xml)=conn_raw.receive()
	if ret != 0:
		print("[FAILED] Receiving <hello>")
		return(-1)

	print "[OK] Receiving <hello> =%(reply_xml)s:" % {'reply_xml':reply_xml}


	print("Connected ...")

	edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
      <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="delete"/>
    </config>
  </edit-config>
"""
	print("edit-config - delete /interfaces ...")
	result = conn.rpc(edit_config_rpc)

	commit_rpc = """
<commit/>
"""
	print("commit ...")
	result = conn.rpc(commit_rpc)

	edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
      <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
        <interface xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="create">
          <name>foo</name>
          <type
            xmlns:txfd="http://yuma123.org/ns/test-xpath-derived-from-or-self">txfd:fast-ethernet</type>
        </interface>
        <interface xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="create">
          <name>bar</name>
          <type
            xmlns:txfd="http://yuma123.org/ns/test-xpath-derived-from-or-self">txfd:other</type>
        </interface>
      </interfaces>
    </config>
  </edit-config>
"""
	print("edit-config - create  'foo' ...")
	result = conn.rpc(edit_config_rpc)
	print lxml.etree.tostring(result)
	ok = result.xpath('ok')
	print result
	print ok
	print lxml.etree.tostring(result)
	assert(len(ok)==1)

	commit_rpc = """
<commit/>
"""
	result = conn.rpc(commit_rpc)
	print lxml.etree.tostring(result)
	ok = result.xpath('ok')
	print result
	print ok
	print lxml.etree.tostring(result)
	assert(len(ok)==1)

	edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
      <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
        <interface>
          <name>foo</name>
          <fast-ethernet-specific xmlns="http://yuma123.org/ns/test-xpath-derived-from-or-self" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="create">01:23:45:67:89:AB</fast-ethernet-specific>
        </interface>
      </interfaces>
    </config>
  </edit-config>
"""
	print("edit-config - create  'foo' ethernet-mac ...")
	result = conn.rpc(edit_config_rpc)
	print lxml.etree.tostring(result)
	ok = result.xpath('ok')
	print result
	print ok
	print lxml.etree.tostring(result)
	assert(len(ok)==1)

	commit_rpc = """
<commit/>
"""
	result = conn.rpc(commit_rpc)
	print lxml.etree.tostring(result)
	ok = result.xpath('ok')
	print result
	print ok
	print lxml.etree.tostring(result)
	assert(len(ok)==1)

	edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
      <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
        <interface>
          <name>bar</name>
          <fast-ethernet-specific xmlns="http://yuma123.org/ns/test-xpath-derived-from-or-self" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="create">01:23:45:67:89:AB</fast-ethernet-specific>
        </interface>
      </interfaces>
    </config>
  </edit-config>
"""
	print("edit-config - create  'bar' ethernet-mac ...")
	result = conn.rpc(edit_config_rpc)
	print lxml.etree.tostring(result)
	ok = result.xpath('ok')
	print result
	print ok
	print lxml.etree.tostring(result)
	#assert(len(ok)==0)
	commit_rpc = """
<commit/>
"""
	result = conn.rpc(commit_rpc)
	print lxml.etree.tostring(result)
	ok = result.xpath('ok')
	print result
	print ok
	print lxml.etree.tostring(result)
	assert(len(ok)==1)

	get_config_rpc = """
<get-config>
 <source>
  <running/>
 </source>
</get-config>
"""
	print("get-config ...")
	result = conn.rpc(get_config_rpc)

	fast_ethernet_specific_foo = result.xpath('data/interfaces/interface[name="foo"]/fast-ethernet-specific')
	fast_ethernet_specific_bar = result.xpath('data/interfaces/interface[name="bar"]/fast-ethernet-specific')
	assert(len(fast_ethernet_specific_foo)==1)
	assert(len(fast_ethernet_specific_bar)==0)
def main():
    print("""
#Description: Test instance-identifier built-in type.
#Procedure:
#1 - Create  /top/list[idx="4"] and /top/id-list[id="/top/list[idx=\"4\"]"].
#2 - Try to create /top/id-list[id="/top/list[idx=\"5\"]"].
""")

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--server",
        help=
        "server name e.g. 127.0.0.1 or server.com (127.0.0.1 if not specified)"
    )
    parser.add_argument("--user",
                        help="username e.g. admin ($USER if not specified)")
    parser.add_argument("--port", help="port e.g. 830 (830 if not specified)")
    parser.add_argument(
        "--password",
        help="password e.g. mypass123 (passwordless if not specified)")

    args = parser.parse_args()

    if (args.server == None or args.server == ""):
        server = "127.0.0.1"
    else:
        server = args.server

    if (args.port == None or args.port == ""):
        port = 830
    else:
        port = int(args.port)

    if (args.user == None or args.user == ""):
        user = os.getenv('USER')
    else:
        user = args.user

    if (args.password == None or args.password == ""):
        password = None
    else:
        password = args.password

    conn_raw = litenc.litenc()
    ret = conn_raw.connect(server=server,
                           port=port,
                           user=user,
                           password=password)
    if ret != 0:
        print "[FAILED] Connecting to server=%(server)s:" % {'server': server}
        return (-1)
    print "[OK] Connecting to server=%(server)s:" % {'server': server}
    conn = litenc_lxml.litenc_lxml(conn_raw, strip_namespaces=True)
    ret = conn_raw.send("""
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>
 </capabilities>
</hello>
""")
    if ret != 0:
        print("[FAILED] Sending <hello>")
        return (-1)
    (ret, reply_xml) = conn_raw.receive()
    if ret != 0:
        print("[FAILED] Receiving <hello>")
        return (-1)

    print "[OK] Receiving <hello> =%(reply_xml)s:" % {'reply_xml': reply_xml}

    print("Connected ...")

    edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
      <top xmlns="http://yuma123.org/ns/test-instance-identifier" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="delete">
      </top>
    </config>
  </edit-config>
"""
    print("edit-config ...")
    result = conn.rpc(edit_config_rpc)

    commit_rpc = """
<commit/>
"""
    print("commit ...")
    result = conn.rpc(commit_rpc)
    ok = result.xpath('//ok')
    assert (len(ok) == 1)

    print(
        '''#1 - Create  /top/list[idx="4"] and /top/id-list[id="/top/list[idx=\"4\"]"].'''
    )
    edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
      <top xmlns="http://yuma123.org/ns/test-instance-identifier">
          <list xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="create">
              <idx>4</idx>
          </list>
          <id-list xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="create">
              <id xmlns:tii="http://yuma123.org/ns/test-instance-identifier">/tii:top/tii:list[tii:idx="4"]</id>
          </id-list>
      </top>
    </config>
  </edit-config>
"""
    #          <id-list xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="create">
    #              <id>/top/list[idx=4]</id>
    #          </id-list>

    print("edit-config ...")
    result = conn.rpc(edit_config_rpc)
    print result
    ok = result.xpath('//ok')
    assert (len(ok) == 1)

    commit_rpc = """
<commit/>
"""
    print("commit ...")
    result = conn.rpc(commit_rpc)
    ok = result.xpath('//ok')
    assert (len(ok) == 1)

    print('''#2 - Try to create /top/id-list[id="/top/list[idx=\"5\"]"].''')

    edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
      <top xmlns="http://yuma123.org/ns/test-instance-identifier">
          <id-list xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="create">
              <id xmlns:tii="http://yuma123.org/ns/test-instance-identifier">/tii:top/tii:list[tii:idx="5"]</id>
          </id-list>
      </top>
    </config>
  </edit-config>
"""

    print("edit-config ...")
    result = conn.rpc(edit_config_rpc)
    ok = result.xpath('//ok')
    assert (len(ok) == 1)

    commit_rpc = """
<commit/>
"""
    print("commit ...")
    result = conn.rpc(commit_rpc)
    print result
    ok = result.xpath('//ok')
    assert (len(ok) == 0)

    discard_changes_rpc = """
<discard-changes/>
"""
    print("discard-changes ...")
    result = conn.rpc(discard_changes_rpc)
    ok = result.xpath('//ok')
    assert (len(ok) == 1)

    edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
      <top xmlns="http://yuma123.org/ns/test-instance-identifier" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="delete">
      </top>
    </config>
  </edit-config>
"""
    print("edit-config ...")
    result = conn.rpc(edit_config_rpc)
    result = conn.rpc(commit_rpc)
    ok = result.xpath('//ok')

    commit_rpc = """
<commit/>
"""
    print("commit ...")
    result = conn.rpc(commit_rpc)
    ok = result.xpath('//ok')
    assert (len(ok) == 1)
def main():
    print("#Description: Attempt to create a leaf with a conditional enum.")

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--server",
        help=
        "server name e.g. 127.0.0.1 or server.com (127.0.0.1 if not specified)"
    )
    parser.add_argument("--user",
                        help="username e.g. admin ($USER if not specified)")
    parser.add_argument("--port", help="port e.g. 830 (830 if not specified)")
    parser.add_argument(
        "--password",
        help="password e.g. mypass123 (passwordless if not specified)")
    parser.add_argument("--expectfail",
                        help="Feature is disabled and RPC is expected to fail",
                        action='store_true')

    args = parser.parse_args()

    if (args.server == None or args.server == ""):
        server = "127.0.0.1"
    else:
        server = args.server

    if (args.port == None or args.port == ""):
        port = 830
    else:
        port = int(args.port)

    if (args.user == None or args.user == ""):
        user = os.getenv('USER')
    else:
        user = args.user

    if (args.password == None or args.password == ""):
        password = None
    else:
        password = args.password

    conn_raw = litenc.litenc()
    ret = conn_raw.connect(server=server,
                           port=port,
                           user=user,
                           password=password)
    if ret != 0:
        print("[FAILED] Connecting to server=%s:" % {'server': server})
        return (-1)
    print("[OK] Connecting to server=%(server)s:" % {'server': server})
    conn = litenc_lxml.litenc_lxml(conn_raw, strip_namespaces=True)
    ret = conn_raw.send("""
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>
 </capabilities>
</hello>
""")

    if ret != 0:
        print("[FAILED] Sending <hello>")
        return (-1)

    # receive <hello>
    result = conn.receive()

    exp = None
    if args.expectfail:
        exp = ['//bad-value']

    rv, reply = send_rpc(conn, rpc_discard_changes)
    if rv > 0:
        return rv

    rv, reply = send_rpc(conn, rpc_create, exp)
    if rv > 0:
        return rv

    return 0
Beispiel #6
0
def main():
    print("""
#Description: Send invalid configuration.  Check later to see if
# netconfd crashed.
""")

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--server",
        help=
        "server name e.g. 127.0.0.1 or server.com (127.0.0.1 if not specified)"
    )
    parser.add_argument("--user",
                        help="username e.g. admin ($USER if not specified)")
    parser.add_argument("--port", help="port e.g. 830 (830 if not specified)")
    parser.add_argument(
        "--password",
        help="password e.g. mypass123 (passwordless if not specified)")

    args = parser.parse_args()

    if (args.server == None or args.server == ""):
        server = "127.0.0.1"
    else:
        server = args.server

    if (args.port == None or args.port == ""):
        port = 830
    else:
        port = int(args.port)

    if (args.user == None or args.user == ""):
        user = os.getenv('USER')
    else:
        user = args.user

    if (args.password == None or args.password == ""):
        password = None
    else:
        password = args.password

    conn_raw = litenc.litenc()
    ret = conn_raw.connect(server=server,
                           port=port,
                           user=user,
                           password=password)
    if ret != 0:
        print "[FAILED] Connecting to server=%(server)s:" % {'server': server}
        return (-1)
    print "[OK] Connecting to server=%(server)s:" % {'server': server}
    conn = litenc_lxml.litenc_lxml(conn_raw)
    ret = conn_raw.send("""
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>
 </capabilities>
</hello>
""")
    if ret != 0:
        print("[FAILED] Sending <hello>")
        return (-1)

    # receive <hello>
    result = conn.receive()

    print("# send invalid value for union element of leaf-list")

    get_rpc = """
<edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <target>
    <candidate/>
  </target>
  <default-operation>merge</default-operation>
  <test-option>set</test-option>
  <config>
    <test
      xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"
      nc:operation="create"
      xmlns="http://yuma123.org/ns/test-leaflist-union-test">
      <address>this should not work</address>
    </test>
  </config>
</edit-config>
"""
    print("edit-config ...")
    result = conn.rpc(get_rpc)
    result = litenc_lxml.strip_namespaces(result)

    print lxml.etree.tostring(result)

    return (0)
def main():
    print("""
#Description: Testcase for draft-ietf-netmod-rfc8022bis-04 ietf-routing module.
#Procedure:
#1 - <edit-config> configuration as in rfc8022bis Appendix D.
#2 - <get-data> the /interfaces and /routing containers and verify data is same as in rfc8022bis Appendix E.
""")

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--server",
        help=
        "server name e.g. 127.0.0.1 or server.com (127.0.0.1 if not specified)"
    )
    parser.add_argument("--user",
                        help="username e.g. admin ($USER if not specified)")
    parser.add_argument("--port", help="port e.g. 830 (830 if not specified)")
    parser.add_argument(
        "--password",
        help="password e.g. mypass123 (passwordless if not specified)")

    args = parser.parse_args()

    if (args.server == None or args.server == ""):
        server = "127.0.0.1"
    else:
        server = args.server

    if (args.port == None or args.port == ""):
        port = 830
    else:
        port = int(args.port)

    if (args.user == None or args.user == ""):
        user = os.getenv('USER')
    else:
        user = args.user

    if (args.password == None or args.password == ""):
        password = None
    else:
        password = args.password

    conn_raw = litenc.litenc()
    ret = conn_raw.connect(server=server,
                           port=port,
                           user=user,
                           password=password)
    if ret != 0:
        print "[FAILED] Connecting to server=%(server)s:" % {'server': server}
        return (-1)
    print "[OK] Connecting to server=%(server)s:" % {'server': server}
    conn = litenc_lxml.litenc_lxml(conn_raw, strip_namespaces=True)
    ret = conn_raw.send("""
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>
 </capabilities>
</hello>
""")
    if ret != 0:
        print("[FAILED] Sending <hello>")
        return (-1)
    (ret, reply_xml) = conn_raw.receive()
    if ret != 0:
        print("[FAILED] Receiving <hello>")
        return (-1)

    print "[OK] Receiving <hello> =%(reply_xml)s:" % {'reply_xml': reply_xml}

    print("Connected ...")

    get_yang_library_rpc = """
<get>
  <filter type="subtree">
    <modules-state xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-library"/>
  </filter>
</get>
"""

    print("<get> - /ietf-yang-library:modules-state ...")
    result = conn.rpc(get_yang_library_rpc, strip_ns=False)
    print lxml.etree.tostring(result,
                              pretty_print=True,
                              inclusive_ns_prefixes=True)
    namespaces = {"nc": "urn:ietf:params:xml:ns:netconf:base:1.0"}
    data = result.xpath('./nc:data', namespaces=namespaces)
    assert (len(data) == 1)
    #Copied from draft-ietf-netmod-rfc8022bis-00 Appendix E.
    json_config = """
   {
     "ietf-interfaces:interfaces": {
       "interface": [
         {
           "name": "eth0",
           "type": "iana-if-type:ethernetCsmacd",
           "description": "Uplink to ISP.",
           "phys-address": "00:0C:42:E5:B1:E9",
           "oper-status": "up",
           "statistics": {
             "discontinuity-time": "2015-10-24T17:11:27+02:00"
           },
           "ietf-ip:ipv4": {
             "forwarding": true,
             "mtu": 1500,
             "address": [
               {
                 "ip": "192.0.2.1",
                 "prefix-length": 24
               }
             ],
           },
           "ietf-ip:ipv6": {
             "forwarding": true,
             "mtu": 1500,
             "address": [
               {
                 "ip": "2001:0db8:0:1::1",
                 "prefix-length": 64
               }
             ],
             "autoconf": {
               "create-global-addresses": false
             }
             "ietf-ipv6-unicast-routing:
                ipv6-router-advertisements": {
               "send-advertisements": false
             }
           }
         },
         {
           "name": "eth1",
           "type": "iana-if-type:ethernetCsmacd",
           "description": "Interface to the internal network.",
           "phys-address": "00:0C:42:E5:B1:EA",
           "oper-status": "up",
           "statistics": {
             "discontinuity-time": "2015-10-24T17:11:29+02:00"
           },
           "ietf-ip:ipv4": {
             "forwarding": true,
             "mtu": 1500,
             "address": [
               {
                 "ip": "198.51.100.1",
                 "prefix-length": 24
               }
             ],
           },
           "ietf-ip:ipv6": {
             "forwarding": true,
             "mtu": 1500,
             "address": [
               {
                 "ip": "2001:0db8:0:2::1",
                 "prefix-length": 64
               }
             ],
             "autoconf": {
               "create-global-addresses": false
             },
             "ietf-ipv6-unicast-routing:
                ipv6-router-advertisements": {
               "send-advertisements": true,
               "prefix-list": {
                 "prefix": [
                   {
                     "prefix-spec": "2001:db8:0:2::/64"
                   }
                 ]
               }
             }
           }
         }
       ]
     },

     "ietf-routing:routing": {
       "router-id": "192.0.2.1",
       "control-plane-protocols": {
         "control-plane-protocol": [
           {
             "type": "ietf-routing:static",
             "name": "st0",
             "description":
               "Static routing is used for the internal network.",
             "static-routes": {
               "ietf-ipv4-unicast-routing:ipv4": {
                 "route": [
                   {
                     "destination-prefix": "0.0.0.0/0",
                     "next-hop": {
                       "next-hop-address": "192.0.2.2"
                     }
                   }
                 ]
               },
               "ietf-ipv6-unicast-routing:ipv6": {
                 "route": [
                   {
                     "destination-prefix": "::/0",
                     "next-hop": {
                       "next-hop-address": "2001:db8:0:1::2"
                     }
                   }
                 ]
               }
             }
           }
         ]
       }
       "ribs": {
         "rib": [
           {
             "name": "ipv4-master",
             "address-family":
               "ietf-ipv4-unicast-routing:ipv4-unicast",
             "default-rib": true,
             "routes": {
               "route": [
                 {
                   "ietf-ipv4-unicast-routing:destination-prefix":
                     "192.0.2.1/24",
                   "next-hop": {
                     "outgoing-interface": "eth0"
                   },
                   "route-preference": 0,
                   "source-protocol": "ietf-routing:direct",
                   "last-updated": "2015-10-24T17:11:27+02:00"
                 },
                 {
                   "ietf-ipv4-unicast-routing:destination-prefix":
                     "198.51.100.0/24",
                   "next-hop": {
                     "outgoing-interface": "eth1"
                   },
                   "source-protocol": "ietf-routing:direct",
                   "route-preference": 0,
                   "last-updated": "2015-10-24T17:11:27+02:00"
                 },
                 {
                   "ietf-ipv4-unicast-routing:destination-prefix":
                     "0.0.0.0/0",
                   "source-protocol": "ietf-routing:static",
                   "route-preference": 5,
                   "next-hop": {
                     "ietf-ipv4-unicast-routing:next-hop-address":
                       "192.0.2.2"
                   },
                   "last-updated": "2015-10-24T18:02:45+02:00"
                 }
               ]
             }
           },
           {
             "name": "ipv6-master",
             "address-family":
               "ietf-ipv6-unicast-routing:ipv6-unicast",
             "default-rib": true,
             "routes": {
               "route": [
                 {
                   "ietf-ipv6-unicast-routing:destination-prefix":
                     "2001:db8:0:1::/64",
                   "next-hop": {
                     "outgoing-interface": "eth0"
                   },
                   "source-protocol": "ietf-routing:direct",
                   "route-preference": 0,
                   "last-updated": "2015-10-24T17:11:27+02:00"
                 },
                 {
                   "ietf-ipv6-unicast-routing:destination-prefix":
                     "2001:db8:0:2::/64",
                   "next-hop": {
                     "outgoing-interface": "eth1"
                   },
                   "source-protocol": "ietf-routing:direct",
                   "route-preference": 0,
                   "last-updated": "2015-10-24T17:11:27+02:00"
                 },
                 {
                   "ietf-ipv6-unicast-routing:destination-prefix":
                     "::/0",
                   "next-hop": {
                     "ietf-ipv6-unicast-routing:next-hop-address":
                       "2001:db8:0:1::2"
                   },
                   "source-protocol": "ietf-routing:static",
                   "route-preference": 5,
                   "last-updated": "2015-10-24T18:02:45+02:00"
                 }
               ]
             }
           }
         ]
       }
     },
   }
"""
    edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
    %(xml_config)
    </config>
</edit-config>
""" % {
        'xml_config': json2xml(json_config)
    }

    print("<edit-config> - load Appendix D. example config to 'candidate' ...")
    result = conn.rpc(edit_config_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('./ok')
    assert (len(ok) == 1)

    commit_rpc = '<commit/>'

    print("<commit> - commit example config ...")
    result = conn.rpc(commit_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('./ok')
    assert (len(ok) == 1)

    get_example_data_rpc = """
<get-data xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-datastores">
  <datastore xmlns:ds="urn:ietf:params:xml:ns:yang:ietf-datastores">ds:operational</datastore>
  <subtree-filter>
    <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"/>
    <routing xmlns="urn:ietf:params:xml:ns:yang:ietf-routing"/>
  </subtree-filter>
  <with-origin/>
</get-data>
"""

    print("<get-data> - Appendix E. data ...")
    result = conn.rpc(get_example_data_rpc, strip_ns=False)
    print lxml.etree.tostring(result,
                              pretty_print=True,
                              inclusive_ns_prefixes=True)
    namespaces = {
        "ncds": "urn:ietf:params:xml:ns:yang:ietf-netconf-datastores"
    }
    data = result.xpath('./ncds:data', namespaces=namespaces)
    assert (len(data) == 1)

    expected = """
<data xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-datastores" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
      <routing
        xmlns="urn:ietf:params:xml:ns:yang:ietf-routing"
        xmlns:or="urn:ietf:params:xml:ns:yang:ietf-origin">

        <router-id or:origin="or:intended">192.0.2.1</router-id>
        <control-plane-protocols or:origin="or:intended">
          <control-plane-protocl>
            <type>ietf-routing:static</type>
            <name></name>
            <static-routes>
              <ietf-ipv4-unicast-routing:ipv4>
                <route>
                  <destination-prefix>0.0.0.0/0</destination-prefix>
                  <next-hop>
                    <next-hop-address>192.0.2.2</next-hop-address>
                  </next-hop>
                </route>
              </ietf-ipv4-unicast-routing:ipv4>
              <ietf-ipv6-unicast-routing:ipv6>
                <route>
                  <destination-prefix>::/0</destination-prefix>
                  <next-hop>
                    <next-hop-address>2001:db8:0:1::2</next-hop-address>
                  </next-hop>
                </route>
              </ietf-ipv6-unicast-routing:ipv6>
            </static-routes>
          </control-plane-protocl>
        </control-plane-protocols>

        <ribs>
          <rib or:origin="or:intended">
            <name>ipv4-master</name>
            <address-family>
              ietf-ipv4-unicast-routing:ipv4-unicast
            </address-family>
            <default-rib>true</default-rib>
            <routes>
              <route>
                <ietf-ipv4-unicast-routing:destination-prefix>
                  192.0.2.1/24
                </ietf-ipv4-unicast-routing:destination-prefix>
                <next-hop>
                  <outgoing-interface>eth0</outgoing-interface>

                </next-hop>
                <route-preference>0</route-preference>
                <source-protocol>ietf-routing:direct</source-protocol>
                <last-updated>2015-10-24T17:11:27+02:00</last-updated>
              </route>
              <route>
                <ietf-ipv4-unicast-routing:destination-prefix>
                  98.51.100.0/24
                </ietf-ipv4-unicast-routing:destination-prefix>
                <next-hop>
                  <outgoing-interface>eth1</outgoing-interface>
                </next-hop>
                <route-preference>0</route-preference>
                <source-protocol>ietf-routing:direct</source-protocol>
                <last-updated>2015-10-24T17:11:27+02:00</last-updated>
              </route>
              <route>
                <ietf-ipv4-unicast-routing:destination-prefix>0.0.0.0/0
                </ietf-ipv4-unicast-routing:destination-prefix>
                <next-hop>
                  <ietf-ipv4-unicast-routing:next-hop-address>192.0.2.2
                  </ietf-ipv4-unicast-routing:next-hop-address>
                </next-hop>
                <route-preference>5</route-preference>
                <source-protocol>ietf-routing:static</source-protocol>
                <last-updated>2015-10-24T18:02:45+02:00</last-updated>
              </route>
            </routes>
          </rib>
          <rib or:origin="or:intended">
            <name>ipv6-master</name>
            <address-family>
              ietf-ipv6-unicast-routing:ipv6-unicast
            </address-family>
            <default-rib>true</default-rib>
            <routes>
              <route>
                <ietf-ipv6-unicast-routing:destination-prefix>
                  2001:db8:0:1::/64
                </ietf-ipv6-unicast-routing:destination-prefix>
                <next-hop>
                  <outgoing-interface>eth0</outgoing-interface>
                </next-hop>
                <route-preference>0</route-preference>
                <source-protocol>ietf-routing:direct</source-protocol>
                <last-updated>2015-10-24T17:11:27+02:00</last-updated>
              </route>
              <route>
                <ietf-ipv6-unicast-routing:destination-prefix>
                  2001:db8:0:2::/64
                </ietf-ipv6-unicast-routing:destination-prefix>
                <next-hop>
                  <outgoing-interface>eth1</outgoing-interface>
                </next-hop>
                <route-preference>0</route-preference>
                <source-protocol>ietf-routing:direct</source-protocol>
                <last-updated>2015-10-24T17:11:27+02:00</last-updated>
              </route>
              <route>
                <ietf-ipv6-unicast-routing:destination-prefix>::/0
                </ietf-ipv6-unicast-routing:destination-prefix>
                <next-hop>
                  <ietf-ipv6-unicast-routing:next-hop-address>
                    2001:db8:0:1::2
                  </ietf-ipv6-unicast-routing:next-hop-address>
                </next-hop>
                <route-preference>5</route-preference>
                <source-protocol>ietf-routing:static</source-protocol>
                <last-updated>2015-10-24T18:02:45+02:00</last-updated>
              </route>
            </routes>
          </rib>
        </ribs>
      </routing>
</data>
"""

    expected = lxml.etree.fromstring(expected)

    #strip comments
    comments = expected.xpath('//comment()')
    for c in comments:
        p = c.getparent()
        p.remove(c)

#strip namespaces
    data1 = expected.xpath('.', namespaces=namespaces)
    data_expected = strip_namespaces(data1[0])
    data_received = strip_namespaces(data[0])

    #sort schema lists by key in alphabetical key order - hardcoded /interfaces/interface[name]
    for node in data_expected.findall("./interfaces"):
        node[:] = sorted(node, key=lambda child: get_interface_name(child))
    for node in data_received.findall("./interfaces"):
        node[:] = sorted(node, key=lambda child: get_interface_name(child))

    #sort attributes
    a = StringIO()
    b = StringIO()
    data_expected.write_c14n(a)
    data_received.write_c14n(b)

    print("Expected:")
    print(a.getvalue())
    print("Received:")
    print(b.getvalue())
    if yang_data_equal(lxml.etree.fromstring(a.getvalue()),
                       lxml.etree.fromstring(b.getvalue())):
        print "Bingo!"
        return 0
    else:
        print "Error: YANG data not equal!"
        return 1
def main():
    print("""
#Description: Demonstrate that edit-config works.
#Procedure:
#1 - Create interface "foo" and commit. Verify commit succeeds.
#2 - Create interface "bar" and commit. Verify commit fails.
""")

    port = 830
    server = "127.0.0.1"

    conn_raw = litenc.litenc()
    ret = conn_raw.connect(server=server, port=port, user=os.getenv('USER'))
    if ret != 0:
        print "[FAILED] Connecting to server=%(server)s:" % {'server': server}
        return (-1)
    print "[OK] Connecting to server=%(server)s:" % {'server': server}
    conn = litenc_lxml.litenc_lxml(conn_raw, strip_namespaces=True)
    ret = conn_raw.send("""
<hello>
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>\
 </capabilities>\
</hello>
""")
    if ret != 0:
        print("[FAILED] Sending <hello>")
        return (-1)
    (ret, reply_xml) = conn_raw.receive()
    if ret != 0:
        print("[FAILED] Receiving <hello>")
        return (-1)

    print "[OK] Receiving <hello> =%(reply_xml)s:" % {'reply_xml': reply_xml}

    print("Connected ...")

    edit_rpc = """
<edit-config xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <target>
  <candidate/>
 </target>
 <default-operation>merge</default-operation>
 <test-option>set</test-option>
 <config>
  <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
   <interface xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="create">
     <name>%(interface)s</name>
     <type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
   </interface>
  </interfaces>
 </config>
</edit-config>
"""
    print("edit-config - create 'foo' ...")
    result = conn.rpc(edit_rpc % {'interface': "foo"})
    ok = result.xpath('ok')
    print result
    print ok
    print lxml.etree.tostring(result)
    assert (len(ok) == 1)

    commit_rpc = """
<commit/>
"""
    print("commit ...")
    result = conn.rpc(commit_rpc)

    print("edit-config - create 'bar' ...")
    result = conn.rpc(edit_rpc % {'interface': "bar"})

    print("commit ...")
    result = conn.rpc(commit_rpc)
    ok = result.xpath('//ok')
    assert (len(ok) != 1)
Beispiel #9
0
def main():
    print("""
#Description: Testcase for draft-ietf-netmod-rfc7223bis-00.txt ietf-interfaces module.
#Procedure:
#1 - <edit-config> configuration as in rfc7223bis Appendix D.
#2 - <get-data> the /interfaces container and verify data is same as in rfc7223bis Appendix E.
""")

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--server",
        help=
        "server name e.g. 127.0.0.1 or server.com (127.0.0.1 if not specified)"
    )
    parser.add_argument("--user",
                        help="username e.g. admin ($USER if not specified)")
    parser.add_argument("--port", help="port e.g. 830 (830 if not specified)")
    parser.add_argument(
        "--password",
        help="password e.g. mypass123 (passwordless if not specified)")

    args = parser.parse_args()

    if (args.server == None or args.server == ""):
        server = "127.0.0.1"
    else:
        server = args.server

    if (args.port == None or args.port == ""):
        port = 830
    else:
        port = int(args.port)

    if (args.user == None or args.user == ""):
        user = os.getenv('USER')
    else:
        user = args.user

    if (args.password == None or args.password == ""):
        password = None
    else:
        password = args.password

    conn_raw = litenc.litenc()
    ret = conn_raw.connect(server=server,
                           port=port,
                           user=user,
                           password=password)
    if ret != 0:
        print "[FAILED] Connecting to server=%(server)s:" % {'server': server}
        return (-1)
    print "[OK] Connecting to server=%(server)s:" % {'server': server}
    conn = litenc_lxml.litenc_lxml(conn_raw)
    ret = conn_raw.send("""
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>
 </capabilities>
</hello>
""")
    if ret != 0:
        print("[FAILED] Sending <hello>")
        return (-1)
    (ret, reply_xml) = conn_raw.receive()
    if ret != 0:
        print("[FAILED] Receiving <hello>")
        return (-1)

    print "[OK] Receiving <hello> =%(reply_xml)s:" % {'reply_xml': reply_xml}

    print("Connected ...")

    get_yang_library_rpc = """
<get>
  <filter type="subtree">
    <modules-state xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-library"/>
  </filter>
</get>
"""

    print("<get> - /ietf-yang-library:modules-state ...")
    result = conn.rpc(get_yang_library_rpc, strip_ns=False)
    print lxml.etree.tostring(result,
                              pretty_print=True,
                              inclusive_ns_prefixes=True)
    namespaces = {"nc": "urn:ietf:params:xml:ns:netconf:base:1.0"}
    data = result.xpath('./nc:data', namespaces=namespaces)
    assert (len(data) == 1)
    #Copied from draft-ietf-netmod-rfc7223bis-00 Appendix D.
    edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
      <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"
           xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type"
           xmlns:vlan="http://example.com/vlan"
           nc:operation="replace">
         <interface>
           <name>eth0</name>
           <type>ianaift:ethernetCsmacd</type>
           <enabled>false</enabled>
         </interface>

         <interface>
           <name>eth1</name>
           <type>ianaift:ethernetCsmacd</type>
           <enabled>true</enabled>
           <vlan:vlan-tagging>true</vlan:vlan-tagging>
         </interface>

         <interface>
           <name>eth1.10</name>
           <type>ianaift:l2vlan</type>
           <enabled>true</enabled>
           <vlan:base-interface>eth1</vlan:base-interface>
           <vlan:vlan-id>10</vlan:vlan-id>
         </interface>

         <interface>
           <name>lo1</name>
           <type>ianaift:softwareLoopback</type>
           <enabled>true</enabled>
         </interface>
       </interfaces>
    </config>
</edit-config>
"""

    print("<edit-config> - load Appendix D. example config to 'candidate' ...")
    result = conn.rpc(edit_config_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('./ok')
    assert (len(ok) == 1)

    commit_rpc = '<commit/>'

    print("<commit> - commit example config ...")
    result = conn.rpc(commit_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('./ok')
    assert (len(ok) == 1)

    get_example_data_rpc = """
<get-data xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-datastores">
  <datastore xmlns:ds="urn:ietf:params:xml:ns:yang:ietf-datastores">ds:operational</datastore>
  <subtree-filter>
    <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"/>
  </subtree-filter>
  <with-origin/>
</get-data>
"""

    print("<get-data> - Appendix E. data ...")
    result = conn.rpc(get_example_data_rpc, strip_ns=False)
    print lxml.etree.tostring(result,
                              pretty_print=True,
                              inclusive_ns_prefixes=True)
    namespaces = {
        "ncds": "urn:ietf:params:xml:ns:yang:ietf-netconf-datastores"
    }
    data = result.xpath('./ncds:data', namespaces=namespaces)
    assert (len(data) == 1)
    #Copy from draft-netconf-nmda-netconf-01
    expected = """
<data xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-datastores" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
  <interfaces
      xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"
      xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type"
      xmlns:vlan="http://example.com/vlan"
      xmlns:or="urn:ietf:params:xml:ns:yang:ietf-origin">
    <interface or:origin="or:intended">
      <name>eth0</name>
      <type>ianaift:ethernetCsmacd</type>
      <enabled>false</enabled>
      <admin-status>down</admin-status>
      <oper-status>down</oper-status>
      <if-index>2</if-index>
      <phys-address>00:01:02:03:04:05</phys-address>
      <statistics>
        <discontinuity-time>
          2013-04-01T03:00:00+00:00
        </discontinuity-time>
        <!-- counters now shown here -->
      </statistics>
    </interface>

    <interface or:origin="or:intended">
      <name>eth1</name>
      <type>ianaift:ethernetCsmacd</type>
      <enabled>true</enabled>
      <admin-status>up</admin-status>
      <oper-status>up</oper-status>
      <if-index>7</if-index>
      <phys-address>00:01:02:03:04:06</phys-address>
      <higher-layer-if>eth1.10</higher-layer-if>
      <statistics>
        <discontinuity-time>
          2013-04-01T03:00:00+00:00
        </discontinuity-time>
       <!-- counters now shown here -->
     </statistics>
      <vlan:vlan-tagging>true</vlan:vlan-tagging>
    </interface>
    <interface or:origin="or:intended">
      <name>eth1.10</name>
      <type>ianaift:l2vlan</type>
      <enabled>true</enabled>
      <admin-status>up</admin-status>
      <oper-status>up</oper-status>
      <if-index>9</if-index>
      <lower-layer-if>eth1</lower-layer-if>
      <statistics>
       <discontinuity-time>
         2013-04-01T03:00:00+00:00
        </discontinuity-time>
        <!-- counters now shown here -->
      </statistics>
      <vlan:base-interface>eth1</vlan:base-interface>
     <vlan:vlan-id>10</vlan:vlan-id>
    </interface>
    <!-- This interface is not configured -->
    <interface or:origin="or:system">
      <name>eth2</name>
      <type>ianaift:ethernetCsmacd</type>
      <admin-status>down</admin-status>
      <oper-status>down</oper-status>
      <if-index>8</if-index>
      <phys-address>00:01:02:03:04:07</phys-address>
      <statistics>
        <discontinuity-time>
          2013-04-01T03:00:00+00:00
        </discontinuity-time>
        <!-- counters now shown here -->
      </statistics>
     </interface>
     <interface or:origin="or:intended">
      <name>lo1</name>
      <type>ianaift:softwareLoopback</type>
      <enabled>true</enabled>
      <admin-status>up</admin-status>
      <oper-status>up</oper-status>
      <if-index>1</if-index>
      <statistics>
        <discontinuity-time>
          2013-04-01T03:00:00+00:00
        </discontinuity-time>
        <!-- counters now shown here -->
      </statistics>
    </interface>
  </interfaces>
</data>
"""

    expected = lxml.etree.fromstring(expected)

    #strip comments
    comments = expected.xpath('//comment()')
    for c in comments:
        p = c.getparent()
        p.remove(c)

#strip namespaces
    data1 = expected.xpath('.', namespaces=namespaces)
    data_expected = strip_namespaces(data1[0])
    data_received = strip_namespaces(data[0])

    #sort schema lists by key in alphabetical key order - hardcoded /interfaces/interface[name]
    for node in data_expected.findall("./interfaces"):
        node[:] = sorted(node, key=lambda child: get_interface_name(child))
    for node in data_received.findall("./interfaces"):
        node[:] = sorted(node, key=lambda child: get_interface_name(child))

    #sort attributes
    a = StringIO()
    b = StringIO()
    data_expected.write_c14n(a)
    data_received.write_c14n(b)

    print("Expected:")
    print(a.getvalue())
    print("Received:")
    print(b.getvalue())
    if yang_data_equal(lxml.etree.fromstring(a.getvalue()),
                       lxml.etree.fromstring(b.getvalue())):
        print "Bingo!"
        return 0
    else:
        print "Error: YANG data not equal!"
        return 1
Beispiel #10
0
def main():
    print("""
#Description: Demonstrate that current() works.
#Procedure:
#1 - Create interfaces foo enabled=true and bar enabled=false.
#2 - Validate foo-in with outgoing-interface=foo can be created.
#3 - Validate bar-in with outgoing-interface=bar can not be created.
""")

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--server",
        help=
        "server name e.g. 127.0.0.1 or server.com (127.0.0.1 if not specified)"
    )
    parser.add_argument("--user",
                        help="username e.g. admin ($USER if not specified)")
    parser.add_argument("--port", help="port e.g. 830 (830 if not specified)")
    parser.add_argument(
        "--password",
        help="password e.g. mypass123 (passwordless if not specified)")

    args = parser.parse_args()

    if (args.server == None or args.server == ""):
        server = "127.0.0.1"
    else:
        server = args.server

    if (args.port == None or args.port == ""):
        port = 830
    else:
        port = int(args.port)

    if (args.user == None or args.user == ""):
        user = os.getenv('USER')
    else:
        user = args.user

    if (args.password == None or args.password == ""):
        password = None
    else:
        password = args.password

    conn_raw = litenc.litenc()
    ret = conn_raw.connect(server=server,
                           port=port,
                           user=user,
                           password=password)
    if ret != 0:
        print "[FAILED] Connecting to server=%(server)s:" % {'server': server}
        return (-1)
    print "[OK] Connecting to server=%(server)s:" % {'server': server}
    conn = litenc_lxml.litenc_lxml(conn_raw)
    ret = conn_raw.send("""
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>
 </capabilities>
</hello>
""")
    if ret != 0:
        print("[FAILED] Sending <hello>")
        return (-1)
    (ret, reply_xml) = conn_raw.receive()
    if ret != 0:
        print("[FAILED] Receiving <hello>")
        return (-1)

    print "[OK] Receiving <hello> =%(reply_xml)s:" % {'reply_xml': reply_xml}

    print("Connected ...")

    edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
      <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="delete"/>
    </config>
  </edit-config>
"""
    print("edit-config - delete /interfaces ...")
    result = conn.rpc(edit_config_rpc)

    commit_rpc = """
<commit/>
"""
    print("commit ...")
    result = conn.rpc(commit_rpc)

    edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
      <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
        <interface xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="create">
          <name>foo</name>
          <type
            xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
          <enabled>true</enabled>
        </interface>
        <interface xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="create">
          <name>bar</name>
          <type
            xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
          <enabled>false</enabled>
        </interface>
      </interfaces>
    </config>
  </edit-config>
"""
    print("edit-config - create  'foo and bar' ...")
    result = conn.rpc(edit_config_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('ok')
    print result
    print ok
    print lxml.etree.tostring(result)
    assert (len(ok) == 1)

    commit_rpc = """
<commit/>
"""
    print("commit ...")
    result = conn.rpc(commit_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('//ok')
    assert (len(ok) == 1)

    edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
        <mgmt-interface xmlns="http://yuma123.org/ns/test-xpath-deref"><name>foo</name><type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type></mgmt-interface>
    </config>
  </edit-config>
"""
    print("edit-config - create mgmt-interface=foo ...")
    result = conn.rpc(edit_config_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('//ok')
    assert (len(ok) == 1)

    print("commit ...")
    result = conn.rpc(commit_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('//ok')
    assert (len(ok) == 1)
    result = conn.rpc(commit_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('//ok')
    assert (len(ok) == 1)

    edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
        <mgmt-interface xmlns="http://yuma123.org/ns/test-xpath-deref"><name>foo</name><type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type></mgmt-interface>
    </config>
  </edit-config>
"""
    print("edit-config - delete mgmt-interface ...")
    result = conn.rpc(edit_config_rpc)

    print("commit ...")
    result = conn.rpc(commit_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('//ok')
    assert (len(ok) == 1)

    edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
        <mgmt-interface xmlns="http://yuma123.org/ns/test-xpath-deref"><name>bar</name><type>ianaift:ethernetCsmacd</type></mgmt-interface>
    </config>
  </edit-config>
"""
    print("edit-config - create mgmt-interface=bar ...")
    result = conn.rpc(edit_config_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('//ok')
    assert (len(ok) == 1)

    print("commit ...")
    result = conn.rpc(commit_rpc)
    print lxml.etree.tostring(result)
    ok = result.xpath('//ok')
    assert (len(ok) != 1)
Beispiel #11
0
def main():
	print("""
#Description: Verify netconf-* session-start, etc. notifications are implemented.
#Procedure:
#1 - Open session #1 and <create-subscription>
#2 - Open additional session #2 and verify <netconf-session-start> is sent with correct paramenters on #1.
#3 - TODO
""")

	parser = argparse.ArgumentParser()
	parser.add_argument("--server", help="server name e.g. 127.0.0.1 or server.com (127.0.0.1 if not specified)")
	parser.add_argument("--user", help="username e.g. admin ($USER if not specified)")
	parser.add_argument("--port", help="port e.g. 830 (830 if not specified)")
	parser.add_argument("--password", help="password e.g. mypass123 (passwordless if not specified)")
	parser.add_argument("--with-filter-subtree", action='store_true', help="when present create-subscription has a filter to receive only netconf-session-end")

	args = parser.parse_args()

	if(args.server==None or args.server==""):
		server="127.0.0.1"
	else:
		server=args.server

	if(args.port==None or args.port==""):
		port=830
	else:
		port=int(args.port)

	if(args.user==None or args.user==""):
		user=os.getenv('USER')
	else:
		user=args.user

	if(args.password==None or args.password==""):
		password=None
	else:
		password=args.password

	conn = litenc.litenc()
	ret = conn.connect(server=server, port=port, user=user, password=password)
	if ret != 0:
		print "[FAILED] Connecting to server=%(server)s:" % {'server':server}
		return(-1)
	print "[OK] Connecting to server=%(server)s:" % {'server':server}
	conn_lxml=litenc_lxml.litenc_lxml(conn,strip_namespaces=True)
	ret = conn.send("""
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>
 </capabilities>
</hello>
""")

#  <capability>urn:ietf:params:netconf:capability:notification:1.0</capability>
#  <capability>urn:ietf:params:netconf:capability:interleave:1.0</capability>
#  <capability>urn:ietf:params:xml:ns:yang:ietf-netconf-notifications?module=ietf-netconf-notifications&amp;revision=2012-02-06</capability>

	if ret != 0:
		print("[FAILED] Sending <hello>")
		return(-1)
	(ret, reply_xml)=conn.receive()
	if ret != 0:
		print("[FAILED] Receiving <hello>")
		return(-1)

	print "[OK] Receiving <hello> =%(reply_xml)s:" % {'reply_xml':reply_xml}

	filter=""
	if(args.with_filter_subtree):
		filter="""
<filter xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0" xmlns:netconf="urn:ietf:params:xml:ns:netconf:base:1.0" netconf:type="subtree">
 <netconf-session-end xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-notifications"/>
</filter>"""

	ret = conn.send("""
<rpc xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" message-id="1">
 <create-subscription xmlns="urn:ietf:params:xml:ns:netconf:notification:1.0">
 %(filter)s
</create-subscription>
</rpc>
"""%{'filter':filter})
	if ret != 0:
		print("[FAILED] Sending <create-subscription>")
		return(-1)

	(ret, reply_xml)=conn.receive()
	if ret != 0:
		print("[FAILED] Receiving <create-subscription> reply")
		return(-1)

	print "[OK] Receiving <create-subscription> reply =%(reply_xml)s:" % {'reply_xml':reply_xml}

	conn2 = litenc.litenc()
	ret = conn2.connect(server=server, port=port, user=user, password=password)
	if ret != 0:
		print "[FAILED] Connecting to server=%(server)s:" % {'server':server}
		return(-1)
	print "[OK] Connecting to server=%(server)s:" % {'server':server}

	ret = conn2.send("""
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>
 </capabilities>
</hello>
""")
	if ret != 0:
		print("[FAILED] Sending <hello>")
		return(-1)
	(ret, reply_xml)=conn2.receive()
	if ret != 0:
		print("[FAILED] Receiving <hello>")
		return(-1)

	print "[OK] Receiving <hello> =%(reply_xml)s:" % {'reply_xml':reply_xml}

	conn2_lxml=litenc_lxml.litenc_lxml(conn2,strip_namespaces=True)
	ret = conn2_lxml.rpc("""
<close-session/>
""")
	if(not args.with_filter_subtree):
		notification_xml=conn_lxml.receive()
		if notification_xml == None:
			print("[FAILED] Receiving <netconf-session-start> notification")
			return(-1)

		print lxml.etree.tostring(notification_xml)

	notification_xml=conn_lxml.receive()
	if notification_xml == None:
		print("[FAILED] Receiving <netconf-session-end> notification")
		return(-1)

	print lxml.etree.tostring(notification_xml)

	notification_xml=litenc_lxml.strip_namespaces(notification_xml)
	match=notification_xml.xpath("/notification/netconf-session-end")
	assert(len(match)==1)

	print "[OK] Receiving <netconf-session-end>"

	return(0)
def main():
	print("""
#Description: Testcase for RFC8022 ietf-routing module.
#Procedure:
#1 - <edit-config> configuration as in RFC8022 Appendix D.
#2 - <get> the /interfaces /interfaces-state /routing /routing-state and verify data is same as in RFC8022 Appendix D.
""")

	parser = argparse.ArgumentParser()
	parser.add_argument("--server", help="server name e.g. 127.0.0.1 or server.com (127.0.0.1 if not specified)")
	parser.add_argument("--user", help="username e.g. admin ($USER if not specified)")
	parser.add_argument("--port", help="port e.g. 830 (830 if not specified)")
	parser.add_argument("--password", help="password e.g. mypass123 (passwordless if not specified)")

	args = parser.parse_args()

	if(args.server==None or args.server==""):
		server="127.0.0.1"
	else:
		server=args.server

	if(args.port==None or args.port==""):
		port=830
	else:
		port=int(args.port)

	if(args.user==None or args.user==""):
		user=os.getenv('USER')
	else:
		user=args.user

	if(args.password==None or args.password==""):
		password=None
	else:
		password=args.password


	conn_raw = litenc.litenc()
	ret = conn_raw.connect(server=server, port=port, user=user, password=password)
	if ret != 0:
		print "[FAILED] Connecting to server=%(server)s:" % {'server':server}
		return(-1)
	print "[OK] Connecting to server=%(server)s:" % {'server':server}
	conn=litenc_lxml.litenc_lxml(conn_raw,strip_namespaces=True)
	ret = conn_raw.send("""
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>
 </capabilities>
</hello>
""")
	if ret != 0:
		print("[FAILED] Sending <hello>")
		return(-1)
	(ret, reply_xml)=conn_raw.receive()
	if ret != 0:
		print("[FAILED] Receiving <hello>")
		return(-1)

	print "[OK] Receiving <hello> =%(reply_xml)s:" % {'reply_xml':reply_xml}

	print("Connected ...")

	get_yang_library_rpc = """
<get>
  <filter type="subtree">
    <modules-state xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-library"/>
  </filter>
</get>
"""

	print("<get> - /ietf-yang-library:modules-state ...")
	result = conn.rpc(get_yang_library_rpc, strip_ns=False)
	print lxml.etree.tostring(result, pretty_print=True, inclusive_ns_prefixes=True)
        namespaces = {"nc":"urn:ietf:params:xml:ns:netconf:base:1.0"}
	data = result.xpath('./nc:data', namespaces=namespaces)
	assert(len(data)==1)

	edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
      <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="replace">
        <interface>
          <name>eth0</name>
          <description>Uplink to ISP.</description>
          <type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
          <ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip">
            <address>
              <ip>192.0.2.1</ip>
              <prefix-length>24</prefix-length>
            </address>
          </ipv4>
        </interface>
      </interfaces>
    </config>
</edit-config>
"""

	print("<edit-config> - load example config to 'candidate' ...")
	result = conn.rpc(edit_config_rpc)
	print lxml.etree.tostring(result)
	ok = result.xpath('./ok')
	assert(len(ok)==1)

	commit_rpc = '<commit/>'

	print("<commit> - commit example config ...")
	result = conn.rpc(commit_rpc)
	print lxml.etree.tostring(result)
	ok = result.xpath('./ok')
	assert(len(ok)==1)



	get_example_data_rpc = """
<get>
  <filter type="subtree">
    <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"/>
    <interfaces-state xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces"/>
    <routing xmlns="urn:ietf:params:xml:ns:yang:ietf-routing"/>
    <routing-state xmlns="urn:ietf:params:xml:ns:yang:ietf-routing"/>
  </filter>
</get>
"""

	print("<get> - example data ...")
	result = conn.rpc(get_example_data_rpc, strip_ns=False)
	print lxml.etree.tostring(result, pretty_print=True, inclusive_ns_prefixes=True)
        namespaces = {"nc":"urn:ietf:params:xml:ns:netconf:base:1.0"}
	data = result.xpath('./nc:data', namespaces=namespaces)
	assert(len(data)==1)

	expected="""
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
    <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
      <interface>
        <name>eth0</name>
        <description>Uplink to ISP.</description>
        <type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
        <ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip">
          <address>
            <ip>192.0.2.1</ip>
            <prefix-length>24</prefix-length>
          </address>
        </ipv4>
      </interface>
    </interfaces>
    <interfaces-state xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
      <interface>
        <name>eth0</name>
        <description>Uplink to ISP.</description>
        <type xmlns:ianaift="urn:ietf:params:xml:ns:yang:iana-if-type">ianaift:ethernetCsmacd</type>
        <phys-address>00:0C:42:E5:B1:E9</phys-address>
        <oper-status>up</oper-status>
	<statistics>
          <discontinuity-time>2015-10-24T17:11:27+02:00</discontinuity-time>
        </statistics>
        <ipv4 xmlns="urn:ietf:params:xml:ns:yang:ietf-ip">
          <address>
            <ip>192.0.2.1</ip>
            <prefix-length>24</prefix-length>
          </address>
        </ipv4>
      </interface>
    </interfaces-state>
</data>
"""

	data_expected=etree.parse(StringIO(lxml.etree.tostring(lxml.etree.fromstring(expected), pretty_print=True)))
	data_received=etree.parse(StringIO(lxml.etree.tostring(data[0], pretty_print=True)))

	a = StringIO();
	b = StringIO();
	data_expected.write_c14n(a)
	data_received.write_c14n(b)

	if yang_data_equal(lxml.etree.fromstring(a.getvalue()), lxml.etree.fromstring(b.getvalue())):
		print "Bingo!"
		return 0
	else:
		print "Expected (A) different from received (B):"
		print "A:"
		print a.getvalue()
		print "B:"
		print b.getvalue()
		return 1
def main():
    print("""
#Description: Read and edit anyxml.
#Procedure:
#1 - Read /c/a and validate
#2 - Replace /c/a read back and validate
""")

    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--server",
        help=
        "server name e.g. 127.0.0.1 or server.com (127.0.0.1 if not specified)"
    )
    parser.add_argument("--user",
                        help="username e.g. admin ($USER if not specified)")
    parser.add_argument("--port", help="port e.g. 830 (830 if not specified)")
    parser.add_argument(
        "--password",
        help="password e.g. mypass123 (passwordless if not specified)")

    args = parser.parse_args()

    if (args.server == None or args.server == ""):
        server = "127.0.0.1"
    else:
        server = args.server

    if (args.port == None or args.port == ""):
        port = 830
    else:
        port = int(args.port)

    if (args.user == None or args.user == ""):
        user = os.getenv('USER')
    else:
        user = args.user

    if (args.password == None or args.password == ""):
        password = None
    else:
        password = args.password

    conn_raw = litenc.litenc()
    ret = conn_raw.connect(server=server,
                           port=port,
                           user=user,
                           password=password)
    if ret != 0:
        print "[FAILED] Connecting to server=%(server)s:" % {'server': server}
        return (-1)
    print "[OK] Connecting to server=%(server)s:" % {'server': server}
    conn = litenc_lxml.litenc_lxml(conn_raw, strip_namespaces=True)
    ret = conn_raw.send("""
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>
 </capabilities>
</hello>
""")
    if ret != 0:
        print("[FAILED] Sending <hello>")
        return (-1)
    (ret, reply_xml) = conn_raw.receive()
    if ret != 0:
        print("[FAILED] Receiving <hello>")
        return (-1)

    print "[OK] Receiving <hello> =%(reply_xml)s:" % {'reply_xml': reply_xml}
    conn = litenc_lxml.litenc_lxml(conn_raw)

    print("Connected ...")

    namespaces = {
        "nc": "urn:ietf:params:xml:ns:netconf:base:1.0",
        "test-anyxml":
        "http://yuma123.org/ns/test/netconfd/anyxml/test-anyxml",
        "one": "urn:1",
        "two": "urn:2"
    }

    edit_config_rpc = """
<edit-config>
    <target>
      <candidate/>
    </target>
    <default-operation>merge</default-operation>
    <test-option>set</test-option>
    <config>
      <c xmlns="http://yuma123.org/ns/test/netconfd/anyxml/test-anyxml" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0" nc:operation="replace">
        <a>
          <one foo="blah" xmlns="http://yuma123.org/ns/test/netconfd/anyxml/test-anyxml"><two bar="blaer" xmlns="http://yuma123.org/ns/test/netconfd/anyxml/test-anyxml">2</two><!--<tree/>--></one>
        </a>
      </c>
    </config>
</edit-config>
"""
    commit_rpc = """
<commit/>
"""

    get_rpc = """
<get>
  <filter type="subtree">
    <c xmlns="http://yuma123.org/ns/test/netconfd/anyxml/test-anyxml">
      <a/>
    </c>
  </filter>
</get>
"""

    print("<edit-config> - /c/a ...")
    result = conn.rpc(edit_config_rpc)
    print lxml.etree.tostring(result,
                              pretty_print=True,
                              inclusive_ns_prefixes=True)
    print("<commit> ...")
    result = conn.rpc(commit_rpc)
    print lxml.etree.tostring(result,
                              pretty_print=True,
                              inclusive_ns_prefixes=True)
    print("<get> - /c/a ...")
    result = conn.rpc(get_rpc)

    print lxml.etree.tostring(result,
                              pretty_print=True,
                              inclusive_ns_prefixes=True)
    one = result.xpath("./nc:data/test-anyxml:c/test-anyxml:a/test-anyxml:one",
                       namespaces=namespaces)
    assert (len(one) == 1)
Beispiel #14
0
def main():
	print("""
#Description:
## Verify get works.
#Procedure:
## do <get>
""")

	parser = argparse.ArgumentParser()
	parser.add_argument("--server", help="server name e.g. 127.0.0.1 or server.com (127.0.0.1 if not specified)")
	parser.add_argument("--user", help="username e.g. admin ($USER if not specified)")
	parser.add_argument("--port", help="port e.g. 830 (830 if not specified)")
	parser.add_argument("--password", help="password e.g. mypass123 (passwordless if not specified)")

	args = parser.parse_args()

	if(args.server==None or args.server==""):
		server="127.0.0.1"
	else:
		server=args.server

	if(args.port==None or args.port==""):
		port=830
	else:
		port=int(args.port)

	if(args.user==None or args.user==""):
		user=os.getenv('USER')
	else:
		user=args.user

	if(args.password==None or args.password==""):
		password=None
	else:
		password=args.password


	conn_raw = litenc.litenc()
	ret = conn_raw.connect(server=server, port=port, user=user, password=password)
	if ret != 0:
		print "[FAILED] Connecting to server=%(server)s:" % {'server':server}
		return(-1)
	print "[OK] Connecting to server=%(server)s:" % {'server':server}
	conn=litenc_lxml.litenc_lxml(conn_raw)
	ret = conn_raw.send("""
<hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
 <capabilities>
  <capability>urn:ietf:params:netconf:base:1.0</capability>
 </capabilities>
</hello>
""")
	if ret != 0:
		print("[FAILED] Sending <hello>")
		return(-1)

	# receive <hello>
	result=conn.receive()


	get_rpc = """
  <get xmlns="urn:ietf:params:xml:ns:netconf:base:1.0"></get>
"""
	result = conn.rpc(get_rpc)
	print lxml.etree.tostring(result)
	rpc_error = result.xpath('rpc-error')
	assert(len(rpc_error)==0)
	print "[OK] Getting"

	get_rpc_w_filter = """
  <get xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
    <filter type="subtree">
      <sys:system-state xmlns:yuma123-sys="http://yuma123.org/ns/yuma123-system"
                        xmlns:sys="urn:ietf:params:xml:ns:yang:ietf-system">
        <yuma123-sys:yuma>
          <yuma123-sys:sysNetconfServerCLI/>
        </yuma123-sys:yuma>
      </sys:system-state>
    </filter>
  </get>
"""
	result = conn.rpc(get_rpc_w_filter)
	print lxml.etree.tostring(result)
	rpc_error = result.xpath('rpc-error')
	assert(len(rpc_error)==0)
	print "[OK] Getting with subtree filter"