backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / controller / ofp_api.py
1 # Copyright (C) 2017 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 OpenFlow related APIs of ryu.controller module.
18 """
19
20 import netaddr
21
22 from ryu.base import app_manager
23 from ryu.lib import hub
24 from ryu.lib import ip
25 from . import ofp_event
26
27
28 _TMP_ADDRESSES = {}
29
30
31 def register_switch_address(addr, interval=None):
32     """
33     Registers a new address to initiate connection to switch.
34
35     Registers a new IP address and port pair of switch to let
36     ryu.controller.controller.OpenFlowController to try to initiate
37     connection to switch.
38
39     :param addr: A tuple of (host, port) pair of switch.
40     :param interval: Interval in seconds to try to connect to switch
41     """
42     assert len(addr) == 2
43     assert ip.valid_ipv4(addr[0]) or ip.valid_ipv6(addr[0])
44     ofp_handler = app_manager.lookup_service_brick(ofp_event.NAME)
45     _TMP_ADDRESSES[addr] = interval
46
47     def _retry_loop():
48         # Delays registration if ofp_handler is not started yet
49         while True:
50             if ofp_handler.controller is not None:
51                 for a, i in _TMP_ADDRESSES.items():
52                     ofp_handler.controller.spawn_client_loop(a, i)
53                     hub.sleep(1)
54                 break
55             hub.sleep(1)
56
57     hub.spawn(_retry_loop)
58
59
60 def unregister_switch_address(addr):
61     """
62     Unregister the given switch address.
63
64     Unregisters the given switch address to let
65     ryu.controller.controller.OpenFlowController stop trying to initiate
66     connection to switch.
67
68     :param addr: A tuple of (host, port) pair of switch.
69     """
70     ofp_handler = app_manager.lookup_service_brick(ofp_event.NAME)
71     # Do nothing if ofp_handler is not started yet
72     if ofp_handler.controller is None:
73         return
74     ofp_handler.controller.stop_client_loop(addr)