The server that handles request for OFConfig operation

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

diff --git a/ryu/lib/of_config/manager.py b/ryu/lib/of_config/manager.py
new file mode 100644
index 0000000..705e775
--- /dev/null
+++ b/ryu/lib/of_config/manager.py
@@ -0,0 +1,80 @@
+# 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
+
+from ryu.base import app_manager
+from ryu.controller import handler
+from ryu.lib.of_config import capable_switch as ofc_capable_switch
+from ryu.lib.of_config import events as ofc_events
+
+
+class OFConfigManager(app_manager.RyuApp):
+    def __init__(self, *args, **kwargs):
+        super(OFConfigManager, self).__init__(*args, **kwargs)
+        self.sessions = {}
+
+    def _send_value(self, ev, ret):
+        reply = ofc_events.EventOFConfigReply(ev.src, True, ret)
+        self.send_reply(reply)
+
+    def _send_exception(self, ev, exc):
+        reply = ofc_events.EventOFConfigReply(ev.src, False, exc)
+        self.send_reply(reply)
+
+    @handler.set_ev_cls(ofc_events.EventOFConfigRequestConnect)
+    def connect_handler(self, ev):
+        try:
+            capable_switch = ofc_capable_switch.CapableSwitch(
+                *ev.args, **ev.kwargs)
+            capable_switch.connect()
+        except ncclient.NCClientError as exc:
+            self._send_exception(ev, exc)
+            return
+
+        session_id = id(capable_switch)
+        self.sessions[session_id] = capable_switch
+        self._send_value(ev, session_id)
+
+    @handler.set_ev_cls(ofc_events.EventOFConfigRequestCloseSession)
+    def close_session_handler(self, ev):
+        session_id = ev.session_id
+        capable_switch = self.sessions.get(session_id)
+        if not capable_switch:
+            self._send_exception(
+                ev, ValueError('unknown session %s' % session_id))
+            return
+        ret = capable_switch.close_session()
+        del self.sessions[session_id]
+        self._send_value(ev, ret)
+
+    @handler.set_ev_cls(ofc_events.EventOFConfigRequest)
+    def request_handler(self, ev):
+        capable_switch = self.sessions.get(ev.session_id)
+        if not capable_switch:
+            self._send_exception(
+                ev, ValueError('unknown session %s' % ev.session_id))
+            return
+        method = getattr(capable_switch, ev.method)
+        if not method:
+            self._send_exception(ev, ValueError('unknown method %s' % method))
+            return
+        try:
+            ret = method(**ev.kwargs)
+        except ncclient.NCClientError as exc:
+            self._send_exception(ev, exc)
+            return
+        self._send_value(ev, ret)
-- 
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