1 # Copyright (C) 2014 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.
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
27 bgp_instance_name = 'bgp_api_app'
31 class BgpWSJsonRpc(app_manager.RyuApp):
33 'wsgi': WSGIApplication,
36 def __init__(self, *args, **kwargs):
37 super(BgpWSJsonRpc, self).__init__(*args, **kwargs)
41 BgpWSJsonRpcController,
42 data={bgp_instance_name: self},
44 self._ws_manager = wsgi.websocketmanager
46 @rpc_public('core.start')
47 def _core_start(self, as_number=64512, router_id='10.0.0.1'):
49 common_settings[LOCAL_AS] = as_number
50 common_settings[ROUTER_ID] = str(router_id)
52 call('core.start', waiter=waiter, **common_settings)
56 @rpc_public('neighbor.create')
57 def _neighbor_create(self, ip_address='192.168.177.32',
60 bgp_neighbor[neighbors.IP_ADDRESS] = str(ip_address)
61 bgp_neighbor[neighbors.REMOTE_AS] = remote_as
62 call('neighbor.create', **bgp_neighbor)
65 @rpc_public('network.add')
66 def _prefix_add(self, prefix='10.20.0.0/24'):
68 networks[PREFIX] = str(prefix)
69 call('network.add', **networks)
72 @rpc_public('neighbors.get')
73 def _neighbors_get(self):
74 return call('neighbors.get')
76 @rpc_public('show.rib')
77 def _show_rib(self, family='ipv4'):
79 show['params'] = ['rib', family]
80 return call('operator.show', **show)
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]
89 @websocket('bgp', url)
90 def _websocket_handler(self, ws):
91 rpc_server = WebSocketRPCServer(ws, self.bgp_api_app)
92 rpc_server.serve_forever()