backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / services / protocols / bgp / api / import_map.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  Import-map configuration.
18 """
19 import logging
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.core_managers.import_map_manager\
24     import ImportMapAlreadyExistsError
25 from ryu.services.protocols.bgp.rtconf.base import RuntimeConfigError
26
27 LOG = logging.getLogger('bgpspeaker.api.import_map')
28
29
30 @register(name='importmap.create')
31 def create_importmap(type, action, name, value, route_family=None):
32     if action != 'drop':
33         raise RuntimeConfigError(
34             'Unknown action. For now we only support "drop" action.'
35         )
36
37     if type not in ('prefix_match', 'rt_match'):
38         raise RuntimeConfigError(
39             'Unknown type. We support only "prefix_match" and "rt_match".'
40         )
41
42     if type == 'prefix_match':
43         return _create_prefix_match_importmap(name, value, route_family)
44     elif type == 'rt_match':
45         return _create_rt_match_importmap(name, value)
46
47
48 def _create_prefix_match_importmap(name, value, route_family):
49     core_service = CORE_MANAGER.get_core_service()
50     importmap_manager = core_service.importmap_manager
51     try:
52         if route_family == 'ipv4':
53             importmap_manager.create_vpnv4_nlri_import_map(name, value)
54         elif route_family == 'ipv6':
55             importmap_manager.create_vpnv6_nlri_import_map(name, value)
56         else:
57             raise RuntimeConfigError(
58                 'Unknown address family %s. it should be ipv4 or ipv6'
59                 % route_family
60             )
61     except ImportMapAlreadyExistsError:
62         raise RuntimeConfigError(
63             'Map with this name already exists'
64         )
65
66     return True
67
68
69 def _create_rt_match_importmap(name, value):
70     core_service = CORE_MANAGER.get_core_service()
71     importmap_manager = core_service.importmap_manager
72     try:
73         importmap_manager.create_rt_import_map(name, value)
74     except ImportMapAlreadyExistsError:
75         raise RuntimeConfigError(
76             'Map with this name already exists'
77         )
78
79     return True