Signed-off-by: Isaku Yamahata <[email protected]>
---
bin/ryu-manager | 1 +
ryu/app/rest_discovery.py | 105 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 106 insertions(+), 0 deletions(-)
create mode 100644 ryu/app/rest_discovery.py
diff --git a/bin/ryu-manager b/bin/ryu-manager
index 615d6c0..8c62f14 100755
--- a/bin/ryu-manager
+++ b/bin/ryu-manager
@@ -40,6 +40,7 @@ gflags.DEFINE_multistring('app_lists',
# 'ryu.app.discovery.Discovery',
# 'ryu.app.discovery.LinkEventWatcher',
# 'ryu.app.event_dumper.EventDumper',
+ # 'ryu.app.rest_discovery.DiscoveryController',
],
'application module name to run')
diff --git a/ryu/app/rest_discovery.py b/ryu/app/rest_discovery.py
new file mode 100644
index 0000000..8819f98
--- /dev/null
+++ b/ryu/app/rest_discovery.py
@@ -0,0 +1,105 @@
+# 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
+import time
+
+from ryu.app.wsapi import wsapi
+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 dpset
+from ryu.controller import link_set
+from ryu.lib import dpid as lib_dpid
+
+
+LOG = logging.getLogger(__name__)
+
+
+# REST API for discovery status
+#
+# get all the links
+# GET /v1.0/topology/links
+#
+# get the links connected <dpid>
+# GET /v1.0/topology/switch/<dpid>/links
+#
+# where
+# <dpid>: datapath id in 16 hex
+
+
+class DiscoveryController(app_manager.RyuApp):
+ _CONTEXTS = {
+ 'dpset': app_manager.noarg_app_context_factory(dpset.DPSet),
+ 'link_set': app_manager.noarg_app_context_factory(link_set.LinkSet),
+ }
+
+ def __init__(self, *args, **kwargs):
+ super(DiscoveryController, self).__init__(*args, **kwargs)
+ self.dpset = kwargs['dpset']
+ self.link_set = kwargs['link_set']
+
+ self.ws = wsapi()
+ self.api = self.ws.get_version('1.0')
+ self._register()
+
+ @staticmethod
+ def _format_link(link, timestamp, now):
+ return {
+ 'timestamp': now - timestamp,
+ 'dp1': lib_dpid.dpid_to_str(link.src.dpid),
+ 'port1': link.src.port_no,
+ 'dp2': lib_dpid.dpid_to_str(link.dst.dpid),
+ 'port2': link.dst.port_no,
+ }
+
+ def _format_response(self, iteritems):
+ now = time.time()
+ response = {
+ 'identifier': 'name',
+ 'items': [self._format_link(link, ts, now)
+ for link, ts in iteritems],
+ }
+ return json.dumps(response)
+
+ def get_links(self, request, _data):
+ request.setHeader('Content-Type', 'application/json')
+ return self._format_response(self.link_set.get_iteritems())
+
+ def get_switch_links(self, request, data):
+ request.setHeader('Content-Type', 'application/json')
+ dpid = data[DPID]
+ if self.dpset.get(dpid) is None:
+ request.setResponseCode(httplib.NOT_FOUND)
+ return 'dpid %s is not founf' % dpid
+
+ return self._format_response(self.link_set.get_iteritems(dpid))
+
+ def _register(self):
+ path_topology = (WSPathStaticString('topology'), )
+
+ path_links = path_topology + (WSPathStaticString('links'), )
+ self.api.register_request(self.get_links, 'GET', path_links,
+ 'Get list of links.')
+
+ path_switch_links = path_topology + (WSPathStaticString('switch'),
+ WSPathSwitch(DPID),
+ WSPathStaticString('links'))
+ self.api.register_request(self.get_switch_links, 'GET',
+ path_switch_links, 'Get list of links.')
--
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