Signed-off-by: Isaku Yamahata <[email protected]>
---
 bin/ryu-manager                                    |    1 +
 .../__init__.py => app/conf_switch_key.py}         |    6 +-
 ryu/app/rest_conf_switch.py                        |  170 ++++++++++++++++++++
 3 files changed, 174 insertions(+), 3 deletions(-)
 copy ryu/{ofproto/__init__.py => app/conf_switch_key.py} (73%)
 create mode 100644 ryu/app/rest_conf_switch.py

diff --git a/bin/ryu-manager b/bin/ryu-manager
index 8fa40bf..24f9f25 100755
--- a/bin/ryu-manager
+++ b/bin/ryu-manager
@@ -46,6 +46,7 @@ gflags.DEFINE_multistring('app_lists',
                            # 'ryu.app.discovery.Discovery',
                            # 'ryu.app.discovery.LinkEventWatcher',
                            'ryu.app.rest_switch.SwitchStatusController',
+                           'ryu.app.rest_conf_switch.ConfSwitchController',
                            'ryu.app.rest_tunnel.GRETunnelController',
                            # 'ryu.app.event_dumper.EventDumper',
                            # 'ryu.app.rest_discovery.DiscoveryController',
diff --git a/ryu/ofproto/__init__.py b/ryu/app/conf_switch_key.py
similarity index 73%
copy from ryu/ofproto/__init__.py
copy to ryu/app/conf_switch_key.py
index 5d88936..c29e560 100644
--- a/ryu/ofproto/__init__.py
+++ b/ryu/app/conf_switch_key.py
@@ -1,5 +1,5 @@
-# Copyright (C) 2011 Nippon Telegraph and Telephone Corporation.
-# Copyright (C) 2011 Isaku Yamahata <yamahata at valinux co jp>
+# Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
+# Copyright (C) 2012 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.
@@ -14,4 +14,4 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-from . import ofproto_v1_0 as ofproto
+OVSDB_ADDR = 'ovsdb_addr'       # value <method>:<port>[:<ip>]
diff --git a/ryu/app/rest_conf_switch.py b/ryu/app/rest_conf_switch.py
new file mode 100644
index 0000000..b5e9478
--- /dev/null
+++ b/ryu/app/rest_conf_switch.py
@@ -0,0 +1,170 @@
+# Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
+# Copyright (C) 2012 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 httplib
+import json
+import logging
+
+from ryu.app.wsapi import json_parse_message_body
+from ryu.app.wsapi import wsapi
+from ryu.app.wsapi import WSPathArbitraryString
+from ryu.app.wsapi import WSPathStaticString
+from ryu.app.wspath import DPID
+from ryu.app.wspath import WSPathSwitch
+from ryu.base import app_manager
+from ryu.controller import conf_switch
+from ryu.lib import dpid as lib_dpid
+
+
+LOG = logging.getLogger(__name__)
+
+
+# REST API for switch configuration
+#
+# get all the switches
+# GET /v1.0/conf/switches
+#
+# get all the configuration keys of a switch
+# GET /v1.0/conf/switches/<dpid>
+#
+# delete all the configuration of a switch
+# DELETE /v1.0/conf/switches/<dpid>
+#
+# set the <key> configuration of a switch
+# PUT /v1.0/conf/switches/<dpid>/<key>
+#
+# get the <key> configuration of a switch
+# GET /v1.0/conf/switches/<dpid>/<key>
+#
+# delete the <key> configuration of a switch
+# DELETE /v1.0/conf/switches/<dpid>/<key>
+#
+# where
+# <dpid>: datapath id in 16 hex
+
+
+class ConfSwitchController(app_manager.RyuApp):
+    _CONTEXTS = {
+        'conf_switch': app_manager.noarg_app_context_factory(
+            conf_switch.ConfSwitchSet),
+        }
+
+    _KEY = '<key>'
+
+    def __init__(self, *args, **kwargs):
+        super(ConfSwitchController, self).__init__(*args, **kwargs)
+        self.conf_switch = kwargs['conf_switch']
+
+        self.ws = wsapi()
+        self.api = self.ws.get_version('1.0')
+        self._register()
+
+    def list_switches(self, request, _data):
+        request.setHeader('Content-Type', 'application/json')
+        dpids = self.conf_switch.dpids()
+        return json.dumps([lib_dpid.dpid_to_str(dpid) for dpid in dpids])
+
+    def _do_switch(self, request, data, func, ret_func):
+        dpid = data[DPID]
+        try:
+            ret = func(dpid)
+        except KeyError:
+            request.setResponseCode(httplib.NOT_FOUND)
+            return 'no dpid is found %s' % dpid
+
+        return ret_func(ret)
+
+    def delete_switch(self, request, data):
+        def _delete_switch(dpid):
+            self.conf_switch.delete_dpid(dpid)
+            return None
+
+        def _ret(_ret):
+            return ''
+
+        return self._do_switch(request, data, _delete_switch, _ret)
+
+    def list_keys(self, request, data):
+        def _list_keys(dpid):
+            return self.conf_switch.keys(dpid)
+
+        def _ret(keys):
+            request.setHeader('Content-Type', 'application/json')
+            return json.dumps(keys)
+
+        return self._do_switch(request, data, _list_keys, _ret)
+
+    def _do_key(self, request, data, func, ret_func):
+        dpid = data[DPID]
+        key = data[self._KEY]
+        try:
+            ret = func(dpid, key)
+        except KeyError:
+            request.setResponseCode(httplib.NOT_FOUND)
+            return 'no dpid/key is found %s %s' % (dpid, key)
+
+        return ret_func(ret)
+
+    def set_key(self, request, data):
+        def _set_val(dpid, key):
+            val = json_parse_message_body(request)
+            self.conf_switch.set_key(dpid, key, val)
+            return None
+
+        def _ret(_ret):
+            return ''
+
+        return self._do_key(request, data, _set_val, _ret)
+
+    def get_key(self, request, data):
+        def _get_key(dpid, key):
+            return self.conf_switch.get(dpid, key)
+
+        def _ret(val):
+            request.setHeader('Content-Type', 'application/json')
+            return json.dumps(val)
+
+        return self._do_key(request, data, _get_key, _ret)
+
+    def delete_key(self, request, data):
+        def _delete_key(dpid, key):
+            self.conf_switch.delete(dpid, key)
+            return None
+
+        def _ret(_ret):
+            return ''
+
+        return self._do_key(request, data, _delete_key, _ret)
+
+    def _register(self):
+        path_conf_switches = (WSPathStaticString('conf'),
+                              WSPathStaticString('switches'))
+        self.api.register_request(self.list_switches, 'GET',
+                                  path_conf_switches, 'Get list of switches')
+
+        path_switch = path_conf_switches + (WSPathSwitch(DPID), )
+        self.api.register_request(self.delete_switch, 'DELETE',
+                                  path_switch, 'Delete a switch')
+        self.api.register_request(self.list_keys, 'GET',
+                                  path_switch, 'Get List of keys of a switch')
+
+        path_key = path_switch + (WSPathArbitraryString(self._KEY), )
+        self.api.register_request(self.set_key, 'PUT',
+                                  path_key, 'Set value of a key of a switch')
+        self.api.register_request(self.get_key, 'GET',
+                                  path_key, 'Get value of a key of a switch')
+        self.api.register_request(self.delete_key, 'DELETE', path_key,
+                                  'delete value of a key of a switch')
-- 
1.7.1.1


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to