backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / services / protocols / bgp / api / core.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  Defines APIs related to Core/CoreManager.
18 """
19 from ryu.lib import hub
20
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
25
26
27 NEIGHBOR_RESET_WAIT_TIME = 3
28
29
30 @register(name='core.start')
31 def start(**kwargs):
32     """Starts new context using provided configuration.
33
34     Raises RuntimeConfigError if a context is already active.
35     """
36     if CORE_MANAGER.started:
37         raise RuntimeConfigError('Current context has to be stopped to start '
38                                  'a new context.')
39
40     try:
41         waiter = kwargs.pop('waiter')
42     except KeyError:
43         waiter = hub.Event()
44     common_config = CommonConf(**kwargs)
45     hub.spawn(CORE_MANAGER.start, *[], **{'common_conf': common_config,
46                                           'waiter': waiter})
47     return True
48
49
50 @register(name='core.stop')
51 def stop(**kwargs):
52     """Stops current context is one is active.
53
54     Raises RuntimeConfigError if runtime is not active or initialized yet.
55     """
56     if not CORE_MANAGER.started:
57         raise RuntimeConfigError('No runtime is active. Call start to create '
58                                  'a runtime')
59     CORE_MANAGER.stop()
60     return True
61
62
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.
68     if not neigh_conf:
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
78
79         def up():
80             neigh_conf.enabled = True
81         hub.spawn_after(NEIGHBOR_RESET_WAIT_TIME, up)
82     else:
83         raise RuntimeConfigError('Neighbor %s is not enabled, hence cannot'
84                                  ' reset.' % ip_address)
85     return True
86
87
88 # =============================================================================
89 # Common configuration related APIs
90 # =============================================================================
91
92 @register(name='comm_conf.get')
93 def get_common_conf():
94     comm_conf = CORE_MANAGER.common_conf
95     return comm_conf.settings