backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / services / protocols / bgp / api / rtconf.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  Runtime configuration manager.
18 """
19 import logging
20
21 from ryu.services.protocols.bgp.api.base import register
22 from ryu.services.protocols.bgp.api.base import RegisterWithArgChecks
23 from ryu.services.protocols.bgp.api.base import FLOWSPEC_FAMILY
24 from ryu.services.protocols.bgp.api.base import FLOWSPEC_RULES
25 from ryu.services.protocols.bgp.api.base import FLOWSPEC_ACTIONS
26 from ryu.services.protocols.bgp.core_manager import CORE_MANAGER
27 from ryu.services.protocols.bgp.rtconf.base import ConfWithId
28 from ryu.services.protocols.bgp.rtconf.base import RuntimeConfigError
29 from ryu.services.protocols.bgp.rtconf import neighbors
30 from ryu.services.protocols.bgp.rtconf.neighbors import NeighborConf
31 from ryu.services.protocols.bgp.rtconf.vrfs import ROUTE_DISTINGUISHER
32 from ryu.services.protocols.bgp.rtconf.vrfs import VRF_RF
33 from ryu.services.protocols.bgp.rtconf.vrfs import VRF_RF_IPV4
34 from ryu.services.protocols.bgp.rtconf.vrfs import VrfConf
35 from ryu.services.protocols.bgp import constants as const
36
37 LOG = logging.getLogger('bgpspeaker.api.rtconf')
38
39
40 # =============================================================================
41 # Neighbor configuration related APIs
42 # =============================================================================
43
44
45 def _get_neighbor_conf(neigh_ip_address):
46     """Returns neighbor configuration for given neighbor ip address.
47
48     Raises exception if no neighbor with `neigh_ip_address` exists.
49     """
50     neigh_conf = \
51         CORE_MANAGER.neighbors_conf.get_neighbor_conf(neigh_ip_address)
52     if not neigh_conf:
53         raise RuntimeConfigError(desc='No Neighbor configuration with IP'
54                                  ' address %s' % neigh_ip_address)
55     assert isinstance(neigh_conf, NeighborConf)
56     return neigh_conf
57
58
59 @register(name='neighbor.create')
60 def create_neighbor(**kwargs):
61     neigh_conf = NeighborConf(**kwargs)
62     CORE_MANAGER.neighbors_conf.add_neighbor_conf(neigh_conf)
63     return True
64
65
66 @RegisterWithArgChecks(name='neighbor.update_enabled',
67                        req_args=[neighbors.IP_ADDRESS, neighbors.ENABLED])
68 def update_neighbor_enabled(neigh_ip_address, enabled):
69     neigh_conf = _get_neighbor_conf(neigh_ip_address)
70     neigh_conf.enabled = enabled
71     return True
72
73
74 @RegisterWithArgChecks(name='neighbor.update',
75                        req_args=[neighbors.IP_ADDRESS, neighbors.CHANGES])
76 def update_neighbor(neigh_ip_address, changes):
77     rets = []
78     for k, v in changes.items():
79         if k == neighbors.MULTI_EXIT_DISC:
80             rets.append(_update_med(neigh_ip_address, v))
81
82         if k == neighbors.ENABLED:
83             rets.append(update_neighbor_enabled(neigh_ip_address, v))
84
85         if k == neighbors.CONNECT_MODE:
86             rets.append(_update_connect_mode(neigh_ip_address, v))
87
88     return all(rets)
89
90
91 def _update_med(neigh_ip_address, value):
92     neigh_conf = _get_neighbor_conf(neigh_ip_address)
93     neigh_conf.multi_exit_disc = value
94     LOG.info('MED value for neigh: %s updated to %s', neigh_conf, value)
95     return True
96
97
98 def _update_connect_mode(neigh_ip_address, value):
99     neigh_conf = _get_neighbor_conf(neigh_ip_address)
100     neigh_conf.connect_mode = value
101     return True
102
103
104 @RegisterWithArgChecks(name='neighbor.delete',
105                        req_args=[neighbors.IP_ADDRESS])
106 def delete_neighbor(neigh_ip_address):
107     neigh_conf = _get_neighbor_conf(neigh_ip_address)
108     if neigh_conf:
109         neigh_conf.enabled = False
110         CORE_MANAGER.neighbors_conf.remove_neighbor_conf(neigh_ip_address)
111         return True
112     return False
113
114
115 @RegisterWithArgChecks(name='neighbor.get',
116                        req_args=[neighbors.IP_ADDRESS])
117 def get_neighbor_conf(neigh_ip_address):
118     """Returns a neighbor configuration for given ip address if exists."""
119     neigh_conf = _get_neighbor_conf(neigh_ip_address)
120     return neigh_conf.settings
121
122
123 @register(name='neighbors.get')
124 def get_neighbors_conf():
125     return CORE_MANAGER.neighbors_conf.settings
126
127
128 @RegisterWithArgChecks(name='neighbor.in_filter.get',
129                        req_args=[neighbors.IP_ADDRESS])
130 def get_neighbor_in_filter(neigh_ip_address):
131     """Returns a neighbor in_filter for given ip address if exists."""
132     core = CORE_MANAGER.get_core_service()
133     peer = core.peer_manager.get_by_addr(neigh_ip_address)
134     return peer.in_filters
135
136
137 @RegisterWithArgChecks(name='neighbor.in_filter.set',
138                        req_args=[neighbors.IP_ADDRESS, neighbors.IN_FILTER])
139 def set_neighbor_in_filter(neigh_ip_address, filters):
140     """Returns a neighbor in_filter for given ip address if exists."""
141     core = CORE_MANAGER.get_core_service()
142     peer = core.peer_manager.get_by_addr(neigh_ip_address)
143     peer.in_filters = filters
144     return True
145
146
147 @RegisterWithArgChecks(name='neighbor.out_filter.get',
148                        req_args=[neighbors.IP_ADDRESS])
149 def get_neighbor_out_filter(neigh_ip_address):
150     """Returns a neighbor out_filter for given ip address if exists."""
151     core = CORE_MANAGER.get_core_service()
152     ret = core.peer_manager.get_by_addr(neigh_ip_address).out_filters
153     return ret
154
155
156 @RegisterWithArgChecks(name='neighbor.out_filter.set',
157                        req_args=[neighbors.IP_ADDRESS, neighbors.OUT_FILTER])
158 def set_neighbor_out_filter(neigh_ip_address, filters):
159     """Sets the out_filter of a neighbor."""
160     core = CORE_MANAGER.get_core_service()
161     peer = core.peer_manager.get_by_addr(neigh_ip_address)
162     peer.out_filters = filters
163     return True
164
165
166 @RegisterWithArgChecks(name='neighbor.attribute_map.set',
167                        req_args=[neighbors.IP_ADDRESS,
168                                  neighbors.ATTRIBUTE_MAP],
169                        opt_args=[ROUTE_DISTINGUISHER, VRF_RF])
170 def set_neighbor_attribute_map(neigh_ip_address, at_maps,
171                                route_dist=None, route_family=VRF_RF_IPV4):
172     """set attribute_maps to the neighbor."""
173     core = CORE_MANAGER.get_core_service()
174     peer = core.peer_manager.get_by_addr(neigh_ip_address)
175
176     at_maps_key = const.ATTR_MAPS_LABEL_DEFAULT
177     at_maps_dict = {}
178
179     if route_dist is not None:
180         vrf_conf =\
181             CORE_MANAGER.vrfs_conf.get_vrf_conf(route_dist, route_family)
182         if vrf_conf:
183             at_maps_key = ':'.join([route_dist, route_family])
184         else:
185             raise RuntimeConfigError(desc='No VrfConf with rd %s' %
186                                           route_dist)
187
188     at_maps_dict[const.ATTR_MAPS_LABEL_KEY] = at_maps_key
189     at_maps_dict[const.ATTR_MAPS_VALUE] = at_maps
190     peer.attribute_maps = at_maps_dict
191
192     return True
193
194
195 @RegisterWithArgChecks(name='neighbor.attribute_map.get',
196                        req_args=[neighbors.IP_ADDRESS],
197                        opt_args=[ROUTE_DISTINGUISHER, VRF_RF])
198 def get_neighbor_attribute_map(neigh_ip_address, route_dist=None,
199                                route_family=VRF_RF_IPV4):
200     """Returns a neighbor attribute_map for given ip address if exists."""
201     core = CORE_MANAGER.get_core_service()
202     peer = core.peer_manager.get_by_addr(neigh_ip_address)
203     at_maps_key = const.ATTR_MAPS_LABEL_DEFAULT
204
205     if route_dist is not None:
206         at_maps_key = ':'.join([route_dist, route_family])
207     at_maps = peer.attribute_maps.get(at_maps_key)
208     if at_maps:
209         return at_maps.get(const.ATTR_MAPS_ORG_KEY)
210     else:
211         return []
212
213 # =============================================================================
214 # VRF configuration related APIs
215 # =============================================================================
216
217
218 @register(name='vrf.create')
219 def create_vrf(**kwargs):
220     vrf_conf = VrfConf(**kwargs)
221     CORE_MANAGER.vrfs_conf.add_vrf_conf(vrf_conf)
222     return True
223
224
225 @register(name='vrf.update')
226 def update_vrf(**kwargs):
227     route_dist = kwargs.get(ROUTE_DISTINGUISHER)
228     vrf_id = kwargs.get(ConfWithId.ID)
229     vrf_rf = kwargs.get(VRF_RF)
230     vrf_conf = CORE_MANAGER.vrfs_conf.get_vrf_conf(
231         route_dist, vrf_rf, vrf_id=vrf_id
232     )
233
234     # If we do not have a VrfConf with given id, we create one.
235     if not vrf_conf:
236         create_vrf(**kwargs)
237     else:
238         vrf_conf.update(**kwargs)
239     return True
240
241
242 @RegisterWithArgChecks(name='vrf.delete', req_args=[ROUTE_DISTINGUISHER])
243 def delete_vrf(route_dist):
244     vrf_conf = CORE_MANAGER.vrfs_conf.remove_vrf_conf(route_dist)
245     if vrf_conf:
246         return True
247
248     return False
249
250
251 @RegisterWithArgChecks(
252     name='vrf.get',
253     req_args=[ROUTE_DISTINGUISHER],
254     opt_args=[VRF_RF])
255 def get_vrf(route_dist, route_family=VRF_RF_IPV4):
256     vrf_conf = CORE_MANAGER.vrfs_conf.get_vrf_conf(
257         route_dist, vrf_rf=route_family
258     )
259     if not vrf_conf:
260         raise RuntimeConfigError(desc='No VrfConf with vpn id %s' %
261                                  route_dist)
262     return vrf_conf.settings
263
264
265 @register(name='vrfs.get')
266 def get_vrfs_conf():
267     vrfs_conf = CORE_MANAGER.vrfs_conf
268     return vrfs_conf.settings
269
270 # =============================================================================
271 # network configuration related APIs
272 # =============================================================================
273
274
275 @register(name='network.add')
276 def add_network(prefix, next_hop=None):
277     tm = CORE_MANAGER.get_core_service().table_manager
278     tm.update_global_table(prefix, next_hop)
279     return True
280
281
282 @register(name='network.del')
283 def del_network(prefix):
284     tm = CORE_MANAGER.get_core_service().table_manager
285     tm.update_global_table(prefix, is_withdraw=True)
286     return True
287
288 # =============================================================================
289 # BMP configuration related APIs
290 # =============================================================================
291
292
293 @register(name='bmp.start')
294 def bmp_start(host, port):
295     core = CORE_MANAGER.get_core_service()
296     return core.start_bmp(host, port)
297
298
299 @register(name='bmp.stop')
300 def bmp_stop(host, port):
301     core = CORE_MANAGER.get_core_service()
302     return core.stop_bmp(host, port)
303
304
305 # =============================================================================
306 # BGP Flow Specification Routes related APIs
307 # =============================================================================
308
309 @RegisterWithArgChecks(
310     name='flowspec.add',
311     req_args=[FLOWSPEC_FAMILY, FLOWSPEC_RULES],
312     opt_args=[FLOWSPEC_ACTIONS])
313 def add_flowspec(flowspec_family, rules, **kwargs):
314     tm = CORE_MANAGER.get_core_service().table_manager
315     tm.update_flowspec_global_table(flowspec_family, rules, **kwargs)
316     return True
317
318
319 @RegisterWithArgChecks(
320     name='flowspec.del',
321     req_args=[FLOWSPEC_FAMILY, FLOWSPEC_RULES])
322 def del_flowspec(flowspec_family, rules):
323     tm = CORE_MANAGER.get_core_service().table_manager
324     tm.update_flowspec_global_table(flowspec_family, rules, is_withdraw=True)
325     return True