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.
15 from snaps.config.network import PortConfig
16 from snaps.openstack.utils import neutron_utils, keystone_utils
19 class RouterConfig(object):
21 Class representing a router configuration
24 def __init__(self, **kwargs):
26 Constructor - all parameters are optional
27 :param name: The router name.
28 :param project_name: The name of the project who owns the network. Only
29 administrative users can specify a project ID
30 other than their own. You cannot change this value
31 through authorization policies.
32 :param external_gateway: Name of the external network to which to route
33 :param admin_state_up: The administrative status of the router.
34 True = up / False = down (default True)
35 :param internal_subnets: List of subnet names to which to connect this
36 router for Floating IP purposes
37 :param port_settings: List of PortConfig objects
40 self.name = kwargs.get('name')
41 self.project_name = kwargs.get('project_name')
42 self.external_gateway = kwargs.get('external_gateway')
44 self.admin_state_up = kwargs.get('admin_state_up', True)
45 self.enable_snat = kwargs.get('enable_snat')
46 if kwargs.get('internal_subnets'):
47 self.internal_subnets = kwargs['internal_subnets']
49 self.internal_subnets = list()
51 self.port_settings = list()
52 if kwargs.get('interfaces', kwargs.get('port_settings')):
53 interfaces = kwargs.get('interfaces', kwargs.get('port_settings'))
54 for interface in interfaces:
55 if isinstance(interface, PortConfig):
56 self.port_settings.append(interface)
58 self.port_settings.append(
59 PortConfig(**interface['port']))
62 raise RouterConfigError('Name is required')
64 def dict_for_neutron(self, neutron, os_creds):
66 Returns a dictionary object representing this object.
67 This is meant to be converted into JSON designed for use by the Neutron
70 TODO - expand automated testing to exercise all parameters
71 :param neutron: The neutron client to retrieve external network
72 information if necessary
73 :param os_creds: The OpenStack credentials for retrieving the keystone
74 client for looking up the project ID when the
75 self.project_name is not None
76 :return: the dictionary object
81 session = keystone_utils.keystone_session(os_creds)
82 keystone = keystone_utils.keystone_client(os_creds, session)
85 out['name'] = self.name
87 project = keystone_utils.get_project(
88 keystone=keystone, project_name=self.project_name)
90 out['tenant_id'] = project.id
92 raise RouterConfigError(
93 'Could not find project ID for project named - ' +
95 if self.admin_state_up is not None:
96 out['admin_state_up'] = self.admin_state_up
97 if self.external_gateway:
98 ext_net = neutron_utils.get_network(
99 neutron, keystone, network_name=self.external_gateway)
101 ext_gw['network_id'] = ext_net.id
102 out['external_gateway_info'] = ext_gw
104 raise RouterConfigError(
105 'Could not find the external network named - ' +
106 self.external_gateway)
108 keystone_utils.close_session(session)
110 return {'router': out}
113 class RouterConfigError(Exception):
115 Exception to be thrown when router settings attributes are incorrect