This class implements direct API to ofconfig.

Signed-off-by: Isaku Yamahata <[email protected]>
---
 ryu/lib/of_config/capable_switch.py |  106 +++++++++++++++++++++++++++++++++++
 1 file changed, 106 insertions(+)
 create mode 100644 ryu/lib/of_config/capable_switch.py

diff --git a/ryu/lib/of_config/capable_switch.py 
b/ryu/lib/of_config/capable_switch.py
new file mode 100644
index 0000000..7d3823d
--- /dev/null
+++ b/ryu/lib/of_config/capable_switch.py
@@ -0,0 +1,106 @@
+# Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
+# Copyright (C) 2013 Isaku Yamahata <yamahata at private email ne jp>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import ncclient.manager
+import ncclient.xml_
+
+from ryu.lib import of_config
+from ryu.lib.of_config import constants as ofc_consts
+
+
+def get_ns_tag(tag):
+    if tag[0] == '{':
+        return tuple(tag[1:].split('}', 1))
+    return (None, tag)
+
+
+class CapableSwitch(object):
+    def __init__(self, connect_method='connect_ssh', *args, **kwargs):
+        super(CapableSwitch, self).__init__()
+        self._connect_method = connect_method
+        self._connect_args = args
+        self._connect_kwargs = kwargs
+        self.netconf = None
+        self.version = None
+        self.namespace = None
+
+    def connect(self):
+        if self.netconf:
+            return
+        connect = getattr(ncclient.manager, self._connect_method)
+        self.netconf = connect(*self._connect_args, **self._connect_kwargs)
+
+    def close_session(self):
+        if self.netconf:
+            self.netconf.close_session()
+            self.netconf = None
+
+    def __enter__(self):
+        self.connect()
+        return self
+
+    def __exit__(self):
+        self.close_session()
+
+    def client_capabilities(self):
+        return self.netconf.client_capabilities
+
+    def server_capabilities(self):
+        return self.netconf.server_capabilities
+
+    def _find_capable_switch(self, tree):
+        capable_switch = None
+        for element in tree.getchildren():
+            ns, tag = get_ns_tag(element.tag)
+            if tag != ofc_consts.CAPABLE_SWITCH:
+                continue
+
+            # assumes that <get> returns only single capable switch
+            assert capable_switch is None
+
+            capable_switch = element
+            if self.version:
+                versions = [(version, ns_) for version, ns_ in
+                            of_config.OFCONFIG_YANG_NAMESPACES.items()
+                            if ns == ns_]
+                if versions:
+                    assert len(versions) == 1
+                    version = versions[0]
+                    self.version, self.namespace = version
+
+        if not capable_switch:
+            # TODO:
+            pass
+
+        return capable_switch
+
+    def _find_capable_switch_xml(self, tree):
+        return ncclient.xml_.to_xml(self._find_capable_switch(tree))
+
+    def get(self, filter=None):
+        reply = self.netconf.get(filter)
+        return self._find_capable_switch_xml(reply.data_ele)
+
+    def get_config(self, source, filter=None):
+        reply = self.netconf.get_config(source, filter)
+        return self._find_capable_switch_xml(reply.data_ele)
+
+    def edit_config(self, target, config, default_operation=None,
+                    test_option=None, error_option=None):
+        self.netconf.edit_config(target, config,
+                                 default_operation, test_option, error_option)
+
+    # TODO: more netconf operations
-- 
1.7.10.4


------------------------------------------------------------------------------
Symantec Endpoint Protection 12 positioned as A LEADER in The Forrester  
Wave(TM): Endpoint Security, Q1 2013 and "remains a good choice" in the  
endpoint security space. For insight on selecting the right partner to 
tackle endpoint security challenges, access the full report. 
http://p.sf.net/sfu/symantec-dev2dev
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to