Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # hack on XML element creation and create a subclass to override HelloHandler's
  4. # build() method to format the XML in a way that the brocades actually like
  5.  
  6. from ncclient.xml_ import *
  7. from ncclient.transport.session import HelloHandler
  8. from ncclient.operations.rpc import RPC, RaiseMode
  9. from ncclient.operations import util
  10.  
  11. # register brocade namespace and create functions to create proper xml for
  12. # hello/capabilities exchange
  13.  
  14. BROCADE_1_0 = "http://brocade.com/ns/netconf/config/netiron-config/"
  15. register_namespace('brcd', BROCADE_1_0)
  16.  
  17. brocade_new_ele = lambda tag, ns, attrs={}, **extra: ET.Element(qualify(tag, ns), attrs, **extra)
  18.  
  19. brocade_sub_ele = lambda parent, tag, ns, attrs={}, **extra: ET.SubElement(parent, qualify(tag, ns), attrs, **extra)
  20.  
  21. # subclass RPC to override self._id to change uuid-generated message-id's;
  22. # Brocades seem to not be able to handle the really long id's
  23. class BrcdRPC(RPC):
  24. def __init__(self, session, async=False, timeout=30, raise_mode=RaiseMode.NONE):
  25. self._id = "1"
  26. return super(BrcdRPC, self).self._id
  27.  
  28. class BrcdHelloHandler(HelloHandler):
  29. def __init__(self):
  30. return super(BrcdHelloHandler, self).__init__()
  31.  
  32. @staticmethod
  33. def build(capabilities):
  34. hello = brocade_new_ele("hello", None, {'xmlns':"urn:ietf:params:xml:ns:netconf:base:1.0"})
  35. caps = brocade_sub_ele(hello, "capabilities", None)
  36. def fun(uri): brocade_sub_ele(caps, "capability", None).text = uri
  37. map(fun, capabilities)
  38. return to_xml(hello)
  39. #return super(BrcdHelloHandler, self).build() ???
  40.  
  41. # since there's no classes I'm assuming I can just override the function itself
  42. # in ncclient.operations.util?
  43. def build_filter(spec, capcheck=None):
  44. type = None
  45. if isinstance(spec, tuple):
  46. type, criteria = spec
  47. # brocades want the netconf prefix on subtree filter attribute
  48. rep = new_ele("filter", {'nc:type':type})
  49. if type == "xpath":
  50. rep.attrib["select"] = criteria
  51. elif type == "subtree":
  52. rep.append(to_ele(criteria))
  53. else:
  54. raise OperationError("Invalid filter type")
  55. else:
  56. rep = validated_element(spec, ("filter", qualify("filter")),
  57. attrs=("type",))
  58. # TODO set type var here, check if select attr present in case of xpath..
  59. if type == "xpath" and capcheck is not None:
  60. capcheck(":xpath")
  61. return rep
  62.  
  63. #!/usr/bin/env python
  64.  
  65. from ncclient import manager
  66. from brcd_ncclient import *
  67.  
  68. manager.logging.basicConfig(filename='ncclient.log', level=manager.logging.DEBUG)
  69.  
  70. # brocade server capabilities advertising as 1.1 compliant when they're really not
  71. # this will stop ncclient from attempting 1.1 chunked netconf message transactions
  72. manager.CAPABILITIES = ['urn:ietf:params:netconf:capability:writeable-running:1.0', 'urn:ietf:params:netconf:base:1.0']
  73.  
  74. # BROCADE_1_0 is the namespace defined for netiron configs in brcd_ncclient
  75. # this maps to the 'brcd' prefix used in xml elements, ie subtree filter criteria
  76. with manager.connect(host='hostname_or_ip', username='username', password='password') as m:
  77. # 'get' request with no filter - for brocades just shows 'show version' data
  78. c = m.get()
  79. print c
  80. # 'get-config' request with 'mpls-config' filter - if no filter is
  81. # supplied with 'get-config', brocade returns nothing
  82. netironcfg = brocade_new_ele('netiron-config', BROCADE_1_0)
  83. mplsconfig = brocade_sub_ele(netironcfg, 'mpls-config', BROCADE_1_0)
  84. filterstr = to_xml(netironcfg)
  85. c2 = m.get_config(source='running', filter=('subtree', filterstr))
  86. print c2
  87. # so far it only looks like the supported filters for 'get-config'
  88. # operations are: 'interface-config', 'vlan-config' and 'mpls-config'
  89.  
  90. <?xml version='1.0' encoding='UTF-8'?>
  91. <nc:hello xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
  92. <nc:capabilities>
  93. <nc:capability>urn:ietf:params:netconf:base:1.0</nc:capability>
  94. <nc:capability>urn:ietf:params:netconf:capability:writeable-running:1.0</nc:capability>
  95. </nc:capabilities>
  96. </nc:hello>
  97.  
  98. <?xml version="1.0" encoding="UTF-8"?>
  99. <hello xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  100. <capabilities>
  101. <capability>urn:ietf:params:netconf:base:1.0</capability>
  102. <capability>urn:ietf:params:netconf:capability:writeable-running:1.0</capability>
  103. </capabilities>
  104. </hello>
  105.  
  106. #!/usr/bin/env python
  107.  
  108. from ncclient import manager
  109. from ncclient.xml_ import *
  110.  
  111. brcd_new_ele = lambda tag, ns, attrs={}, **extra: ET.Element(qualify(tag, ns), attrs, **extra)
  112. brcd_sub_ele = lambda parent, tag, ns, attrs={}, **extra: ET.SubElement(parent, qualify(tag, ns), attrs, **extra)
  113.  
  114. BROCADE_1_0 = "http://brocade.com/ns/netconf/config/netiron-config/"
  115. register_namespace('brcd', BROCADE_1_0)
  116.  
  117. @staticmethod
  118. def brcd_build(capabilities):
  119. hello = brcd_new_ele("hello", None, {'xmlns':"urn:ietf:params:xml:ns:netconf:base:1.0"})
  120. caps = brcd_sub_ele(hello, "capabilities", None)
  121. def fun(uri): brcd_sub_ele(caps, "capability", None).text = uri
  122. map(fun, capabilities)
  123. return to_xml(hello)
  124.  
  125. def brcd_build_filter(spec, capcheck=None):
  126. type = None
  127. if isinstance(spec, tuple):
  128. type, criteria = spec
  129. # brocades want the netconf prefix on subtree filter attribute
  130. rep = new_ele("filter", {'nc:type':type})
  131. if type == "xpath":
  132. rep.attrib["select"] = criteria
  133. elif type == "subtree":
  134. rep.append(to_ele(criteria))
  135. else:
  136. raise OperationError("Invalid filter type")
  137. else:
  138. rep = validated_element(spec, ("filter", qualify("filter")),
  139. attrs=("type",))
  140. if type == "xpath" and capcheck is not None:
  141. capcheck(":xpath")
  142. return rep
  143.  
  144. manager.transport.session.HelloHandler.build = brcd_build
  145. manager.operations.util.build_filter = brcd_build_filter
  146.  
  147. #!/usr/bin/env python
  148.  
  149. from brcd_ncclient import *
  150.  
  151. manager.logging.basicConfig(filename='ncclient.log', level=manager.logging.DEBUG)
  152.  
  153. manager.CAPABILITIES = ['urn:ietf:params:netconf:capability:writeable-running:1.0', 'urn:ietf:params:netconf:base:1.0']
  154.  
  155. with manager.connect(host='host', username='user', password='password') as m:
  156. netironcfg = brcd_new_ele('netiron-config', BROCADE_1_0)
  157. mplsconfig = brcd_sub_ele(netironcfg, 'mpls-config', BROCADE_1_0)
  158. filterstr = to_xml(netironcfg)
  159. c2 = m.get_config(source='running', filter=('subtree', filterstr))
  160. print c2
  161.  
  162. class RPC(object):
  163. DEPENDS = []
  164. REPLY_CLS = RPCReply
  165. def __init__(self, session, async=False, timeout=30, raise_mode=RaiseMode.NONE):
  166. self._session = session
  167. try:
  168. for cap in self.DEPENDS:
  169. self._assert(cap)
  170. except AttributeError:
  171. pass
  172. self._async = async
  173. self._timeout = timeout
  174. self._raise_mode = raise_mode
  175. self._id = uuid1().urn # Keeps things simple instead of having a class attr with running ID that has to be locked
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement