1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 from ryu.app.wsgi import ControllerBase
19 from ryu.app.wsgi import Response
20 from ryu.app.wsgi import route
21 from ryu.app.wsgi import WSGIApplication
22 from ryu.base import app_manager
23 from ryu.lib import dpid as dpid_lib
24 from ryu.topology.api import get_switch, get_link, get_host
26 # REST API for switch configuration
28 # get all the switches
29 # GET /v1.0/topology/switches
32 # GET /v1.0/topology/switches/<dpid>
35 # GET /v1.0/topology/links
37 # get the links of a switch
38 # GET /v1.0/topology/links/<dpid>
41 # GET /v1.0/topology/hosts
43 # get the hosts of a switch
44 # GET /v1.0/topology/hosts/<dpid>
47 # <dpid>: datapath id in 16 hex
50 class TopologyAPI(app_manager.RyuApp):
52 'wsgi': WSGIApplication
55 def __init__(self, *args, **kwargs):
56 super(TopologyAPI, self).__init__(*args, **kwargs)
59 wsgi.register(TopologyController, {'topology_api_app': self})
62 class TopologyController(ControllerBase):
63 def __init__(self, req, link, data, **config):
64 super(TopologyController, self).__init__(req, link, data, **config)
65 self.topology_api_app = data['topology_api_app']
67 @route('topology', '/v1.0/topology/switches',
69 def list_switches(self, req, **kwargs):
70 return self._switches(req, **kwargs)
72 @route('topology', '/v1.0/topology/switches/{dpid}',
73 methods=['GET'], requirements={'dpid': dpid_lib.DPID_PATTERN})
74 def get_switch(self, req, **kwargs):
75 return self._switches(req, **kwargs)
77 @route('topology', '/v1.0/topology/links',
79 def list_links(self, req, **kwargs):
80 return self._links(req, **kwargs)
82 @route('topology', '/v1.0/topology/links/{dpid}',
83 methods=['GET'], requirements={'dpid': dpid_lib.DPID_PATTERN})
84 def get_links(self, req, **kwargs):
85 return self._links(req, **kwargs)
87 @route('topology', '/v1.0/topology/hosts',
89 def list_hosts(self, req, **kwargs):
90 return self._hosts(req, **kwargs)
92 @route('topology', '/v1.0/topology/hosts/{dpid}',
93 methods=['GET'], requirements={'dpid': dpid_lib.DPID_PATTERN})
94 def get_hosts(self, req, **kwargs):
95 return self._hosts(req, **kwargs)
97 def _switches(self, req, **kwargs):
100 dpid = dpid_lib.str_to_dpid(kwargs['dpid'])
101 switches = get_switch(self.topology_api_app, dpid)
102 body = json.dumps([switch.to_dict() for switch in switches])
103 return Response(content_type='application/json', body=body)
105 def _links(self, req, **kwargs):
108 dpid = dpid_lib.str_to_dpid(kwargs['dpid'])
109 links = get_link(self.topology_api_app, dpid)
110 body = json.dumps([link.to_dict() for link in links])
111 return Response(content_type='application/json', body=body)
113 def _hosts(self, req, **kwargs):
116 dpid = dpid_lib.str_to_dpid(kwargs['dpid'])
117 hosts = get_host(self.topology_api_app, dpid)
118 body = json.dumps([host.to_dict() for host in hosts])
119 return Response(content_type='application/json', body=body)