1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 from neutronclient.common.exceptions import NotFound
19 from snaps.config.router import RouterConfig
20 from snaps.openstack.openstack_creator import OpenStackNetworkObject
21 from snaps.openstack.utils import neutron_utils
23 __author__ = 'spisarski'
25 logger = logging.getLogger('OpenStackNetwork')
28 class OpenStackRouter(OpenStackNetworkObject):
30 Class responsible for managing a router in OpenStack
33 def __init__(self, os_creds, router_settings):
35 Constructor - all parameters are required
36 :param os_creds: The credentials to connect with OpenStack
37 :param router_settings: The settings used to create a router object
38 (must be an instance of the RouterConfig
41 super(self.__class__, self).__init__(os_creds)
43 if not router_settings:
44 raise RouterCreationError('router_settings is required')
46 self.router_settings = router_settings
48 # Attributes instantiated on create()
50 self.__internal_subnets = list()
51 self.__internal_router_interface = None
53 # Dict where the port object is the key and any newly created router
54 # interfaces are the value
59 Loads the existing router.
60 :return: the Router domain object
62 super(self.__class__, self).initialize()
64 self.__router = neutron_utils.get_router(
65 self._neutron, router_settings=self.router_settings)
67 for internal_subnet_name in self.router_settings.internal_subnets:
68 internal_subnet = neutron_utils.get_subnet(
69 self._neutron, subnet_name=internal_subnet_name)
71 self.__internal_subnets.append(internal_subnet)
73 raise RouterCreationError(
74 'Subnet not found with name ' + internal_subnet_name)
76 for port_setting in self.router_settings.port_settings:
77 port = neutron_utils.get_port(
78 self._neutron, port_settings=port_setting)
80 self.__ports.append(port)
86 Responsible for creating the router.
87 :return: the Router domain object
92 self.__router = neutron_utils.create_router(
93 self._neutron, self._os_creds, self.router_settings)
95 for internal_subnet_name in self.router_settings.internal_subnets:
96 internal_subnet = neutron_utils.get_subnet(
97 self._neutron, subnet_name=internal_subnet_name)
99 self.__internal_subnets.append(internal_subnet)
101 logger.debug('Adding router to subnet...')
102 router_intf = neutron_utils.add_interface_router(
103 self._neutron, self.__router,
104 subnet=internal_subnet)
105 self.__internal_router_interface = router_intf
107 raise RouterCreationError(
108 'Subnet not found with name ' + internal_subnet_name)
110 for port_setting in self.router_settings.port_settings:
111 port = neutron_utils.get_port(
112 self._neutron, port_settings=port_setting)
114 'Retrieved port %s for router - %s', port_setting.name,
115 self.router_settings.name)
117 self.__ports.append(port)
120 port = neutron_utils.create_port(
121 self._neutron, self._os_creds, port_setting)
124 'Created port %s for router - %s',
126 self.router_settings.name)
127 self.__ports.append(port)
128 neutron_utils.add_interface_router(self._neutron,
132 raise RouterCreationError(
133 'Error creating port with name - '
136 self.__router = neutron_utils.get_router_by_id(
137 self._neutron, self.__router.id)
142 Removes and deletes all items created in reverse order.
144 for port in self.__ports:
146 'Removing router interface from router %s and port %s',
147 self.router_settings.name, port.name)
149 neutron_utils.remove_interface_router(self._neutron,
150 self.__router, port=port)
153 self.__ports = list()
155 for internal_subnet in self.__internal_subnets:
157 'Removing router interface from router %s and subnet %s',
158 self.router_settings.name, internal_subnet.name)
160 neutron_utils.remove_interface_router(self._neutron,
162 subnet=internal_subnet)
165 self.__internal_subnets = list()
168 logger.info('Removing router ' + self.router_settings.name)
170 neutron_utils.delete_router(self._neutron, self.__router)
175 def get_router(self):
177 Returns the OpenStack router object
182 def get_internal_router_interface(self):
184 Returns the OpenStack internal router interface object
187 return self.__internal_router_interface
190 class RouterCreationError(Exception):
192 Exception to be thrown when an router instance cannot be created
196 class RouterSettings(RouterConfig):
198 Class to hold the configuration settings required for creating OpenStack
203 def __init__(self, **kwargs):
204 from warnings import warn
205 warn('Use snaps.config.router.RouterConfig instead',
207 super(self.__class__, self).__init__(**kwargs)