backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / app / rest_topology.py
1 # Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
2 #
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
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import json
17
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
25
26 # REST API for switch configuration
27 #
28 # get all the switches
29 # GET /v1.0/topology/switches
30 #
31 # get the switch
32 # GET /v1.0/topology/switches/<dpid>
33 #
34 # get all the links
35 # GET /v1.0/topology/links
36 #
37 # get the links of a switch
38 # GET /v1.0/topology/links/<dpid>
39 #
40 # get all the hosts
41 # GET /v1.0/topology/hosts
42 #
43 # get the hosts of a switch
44 # GET /v1.0/topology/hosts/<dpid>
45 #
46 # where
47 # <dpid>: datapath id in 16 hex
48
49
50 class TopologyAPI(app_manager.RyuApp):
51     _CONTEXTS = {
52         'wsgi': WSGIApplication
53     }
54
55     def __init__(self, *args, **kwargs):
56         super(TopologyAPI, self).__init__(*args, **kwargs)
57
58         wsgi = kwargs['wsgi']
59         wsgi.register(TopologyController, {'topology_api_app': self})
60
61
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']
66
67     @route('topology', '/v1.0/topology/switches',
68            methods=['GET'])
69     def list_switches(self, req, **kwargs):
70         return self._switches(req, **kwargs)
71
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)
76
77     @route('topology', '/v1.0/topology/links',
78            methods=['GET'])
79     def list_links(self, req, **kwargs):
80         return self._links(req, **kwargs)
81
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)
86
87     @route('topology', '/v1.0/topology/hosts',
88            methods=['GET'])
89     def list_hosts(self, req, **kwargs):
90         return self._hosts(req, **kwargs)
91
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)
96
97     def _switches(self, req, **kwargs):
98         dpid = None
99         if 'dpid' in 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)
104
105     def _links(self, req, **kwargs):
106         dpid = None
107         if 'dpid' in 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)
112
113     def _hosts(self, req, **kwargs):
114         dpid = None
115         if 'dpid' in 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)