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 Defines APIs related to Core/CoreManager.
19 from ryu.lib import hub
21 from ryu.services.protocols.bgp.api.base import register
22 from ryu.services.protocols.bgp.core_manager import CORE_MANAGER
23 from ryu.services.protocols.bgp.rtconf.base import RuntimeConfigError
24 from ryu.services.protocols.bgp.rtconf.common import CommonConf
27 NEIGHBOR_RESET_WAIT_TIME = 3
30 @register(name='core.start')
32 """Starts new context using provided configuration.
34 Raises RuntimeConfigError if a context is already active.
36 if CORE_MANAGER.started:
37 raise RuntimeConfigError('Current context has to be stopped to start '
41 waiter = kwargs.pop('waiter')
44 common_config = CommonConf(**kwargs)
45 hub.spawn(CORE_MANAGER.start, *[], **{'common_conf': common_config,
50 @register(name='core.stop')
52 """Stops current context is one is active.
54 Raises RuntimeConfigError if runtime is not active or initialized yet.
56 if not CORE_MANAGER.started:
57 raise RuntimeConfigError('No runtime is active. Call start to create '
63 @register(name='core.reset_neighbor')
64 def reset_neighbor(ip_address):
65 neighs_conf = CORE_MANAGER.neighbors_conf
66 neigh_conf = neighs_conf.get_neighbor_conf(ip_address)
67 # Check if we have neighbor with given IP.
69 raise RuntimeConfigError('No neighbor configuration found for given'
70 ' IP: %s' % ip_address)
71 # If neighbor is enabled, we disable it.
72 if neigh_conf.enabled:
73 # Disable neighbor to close existing session.
74 neigh_conf.enabled = False
75 # Enable neighbor after NEIGHBOR_RESET_WAIT_TIME
76 # this API works asynchronously
77 # it's recommended to check it really reset neighbor later
80 neigh_conf.enabled = True
81 hub.spawn_after(NEIGHBOR_RESET_WAIT_TIME, up)
83 raise RuntimeConfigError('Neighbor %s is not enabled, hence cannot'
84 ' reset.' % ip_address)
88 # =============================================================================
89 # Common configuration related APIs
90 # =============================================================================
92 @register(name='comm_conf.get')
93 def get_common_conf():
94 comm_conf = CORE_MANAGER.common_conf
95 return comm_conf.settings