backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / services / protocols / bgp / api / jsonrpc.py
1 # Copyright (C) 2014 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
17 from ryu.base import app_manager
18 from ryu.lib import hub
19 from ryu.app.wsgi import websocket, ControllerBase, WSGIApplication
20 from ryu.app.wsgi import rpc_public, WebSocketRPCServer
21 from ryu.services.protocols.bgp.api.base import call
22 from ryu.services.protocols.bgp.api.base import PREFIX
23 from ryu.services.protocols.bgp.rtconf.common import LOCAL_AS
24 from ryu.services.protocols.bgp.rtconf.common import ROUTER_ID
25 from ryu.services.protocols.bgp.rtconf import neighbors
26
27 bgp_instance_name = 'bgp_api_app'
28 url = '/bgp/ws'
29
30
31 class BgpWSJsonRpc(app_manager.RyuApp):
32     _CONTEXTS = {
33         'wsgi': WSGIApplication,
34     }
35
36     def __init__(self, *args, **kwargs):
37         super(BgpWSJsonRpc, self).__init__(*args, **kwargs)
38
39         wsgi = kwargs['wsgi']
40         wsgi.register(
41             BgpWSJsonRpcController,
42             data={bgp_instance_name: self},
43         )
44         self._ws_manager = wsgi.websocketmanager
45
46     @rpc_public('core.start')
47     def _core_start(self, as_number=64512, router_id='10.0.0.1'):
48         common_settings = {}
49         common_settings[LOCAL_AS] = as_number
50         common_settings[ROUTER_ID] = str(router_id)
51         waiter = hub.Event()
52         call('core.start', waiter=waiter, **common_settings)
53         waiter.wait()
54         return {}
55
56     @rpc_public('neighbor.create')
57     def _neighbor_create(self, ip_address='192.168.177.32',
58                          remote_as=64513):
59         bgp_neighbor = {}
60         bgp_neighbor[neighbors.IP_ADDRESS] = str(ip_address)
61         bgp_neighbor[neighbors.REMOTE_AS] = remote_as
62         call('neighbor.create', **bgp_neighbor)
63         return {}
64
65     @rpc_public('network.add')
66     def _prefix_add(self, prefix='10.20.0.0/24'):
67         networks = {}
68         networks[PREFIX] = str(prefix)
69         call('network.add', **networks)
70         return {}
71
72     @rpc_public('neighbors.get')
73     def _neighbors_get(self):
74         return call('neighbors.get')
75
76     @rpc_public('show.rib')
77     def _show_rib(self, family='ipv4'):
78         show = {}
79         show['params'] = ['rib', family]
80         return call('operator.show', **show)
81
82
83 class BgpWSJsonRpcController(ControllerBase):
84     def __init__(self, req, link, data, **config):
85         super(BgpWSJsonRpcController, self).__init__(
86             req, link, data, **config)
87         self.bgp_api_app = data[bgp_instance_name]
88
89     @websocket('bgp', url)
90     def _websocket_handler(self, ws):
91         rpc_server = WebSocketRPCServer(ws, self.bgp_api_app)
92         rpc_server.serve_forever()