Added ext_net_name into template substitution variable.
[snaps.git] / snaps / openstack / create_network.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
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:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
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 import logging
16
17 from neutronclient.common.exceptions import NotFound
18
19 from snaps.openstack.openstack_creator import OpenStackNetworkObject
20 from snaps.openstack.utils import keystone_utils, neutron_utils
21
22 __author__ = 'spisarski'
23
24 logger = logging.getLogger('OpenStackNetwork')
25
26
27 class OpenStackNetwork(OpenStackNetworkObject):
28     """
29     Class responsible for managing a network in OpenStack
30     """
31
32     def __init__(self, os_creds, network_settings):
33         """
34         Constructor - all parameters are required
35         :param os_creds: The credentials to connect with OpenStack
36         :param network_settings: The settings used to create a network
37         """
38         super(self.__class__, self).__init__(os_creds)
39
40         self.network_settings = network_settings
41
42         # Attributes instantiated on create()
43         self.__network = None
44         self.__subnets = list()
45
46     def initialize(self):
47         """
48         Loads the existing OpenStack network/subnet
49         :return: The Network domain object or None
50         """
51         super(self.__class__, self).initialize()
52
53         self.__network = neutron_utils.get_network(
54             self._neutron, network_settings=self.network_settings,
55             project_id=self.network_settings.get_project_id(self._os_creds))
56
57         if self.__network:
58             for subnet_setting in self.network_settings.subnet_settings:
59                 sub_inst = neutron_utils.get_subnet(
60                     self._neutron, subnet_settings=subnet_setting)
61                 if sub_inst:
62                     self.__subnets.append(sub_inst)
63                     logger.debug(
64                         "Subnet '%s' created successfully" % sub_inst.id)
65
66         return self.__network
67
68     def create(self):
69         """
70         Responsible for creating not only the network but then a private
71         subnet, router, and an interface to the router.
72         :return: the Network domain object
73         """
74         self.initialize()
75
76         if not self.__network:
77             self.__network = neutron_utils.create_network(
78                 self._neutron, self._os_creds, self.network_settings)
79             logger.debug(
80                 "Network '%s' created successfully" % self.__network.id)
81
82         for subnet_setting in self.network_settings.subnet_settings:
83             sub_inst = neutron_utils.get_subnet(
84                 self._neutron, subnet_settings=subnet_setting)
85             if not sub_inst:
86                 sub_inst = neutron_utils.create_subnet(
87                     self._neutron, subnet_setting, self._os_creds,
88                     self.__network)
89             if sub_inst:
90                 self.__subnets.append(sub_inst)
91                 logger.debug(
92                     "Subnet '%s' created successfully" % sub_inst.id)
93
94         return self.__network
95
96     def clean(self):
97         """
98         Removes and deletes all items created in reverse order.
99         """
100         for subnet in self.__subnets:
101             try:
102                 logger.info(
103                     'Deleting subnet with name ' + subnet.name)
104                 neutron_utils.delete_subnet(self._neutron, subnet)
105             except NotFound as e:
106                 logger.warning(
107                     'Error deleting subnet with message - ' + str(e))
108                 pass
109         self.__subnets = list()
110
111         if self.__network:
112             try:
113                 neutron_utils.delete_network(self._neutron, self.__network)
114             except NotFound:
115                 pass
116
117             self.__network = None
118
119     def get_network(self):
120         """
121         Returns the created OpenStack network object
122         :return: the OpenStack network object
123         """
124         return self.__network
125
126     def get_subnets(self):
127         """
128         Returns the OpenStack subnet objects
129         :return:
130         """
131         return self.__subnets
132
133
134 class NetworkSettings:
135     """
136     Class representing a network configuration
137     """
138
139     def __init__(self, **kwargs):
140         """
141         Constructor - all parameters are optional
142         :param name: The network name.
143         :param admin_state_up: The administrative status of the network.
144                                True = up / False = down (default True)
145         :param shared: Boolean value indicating whether this network is shared
146                        across all projects/tenants. By default, only
147                        administrative users can change this value.
148         :param project_name: Admin-only. The name of the project that will own
149                              the network. This project can be different from
150                              the project that makes the create network request.
151                              However, only administrative users can specify a
152                              project ID other than their own. You cannot change
153                              this value through authorization policies.
154         :param external: when true, will setup an external network
155                          (default False).
156         :param network_type: the type of network (i.e. vlan|flat).
157         :param physical_network: the name of the physical network
158                                  (this is required when network_type is 'flat')
159         :param segmentation_id: the id of the segmentation
160                                  (this is required when network_type is 'vlan')
161         :param subnets or subnet_settings: List of SubnetSettings objects.
162         :return:
163         """
164
165         self.project_id = None
166
167         self.name = kwargs.get('name')
168         if kwargs.get('admin_state_up') is not None:
169             self.admin_state_up = bool(kwargs['admin_state_up'])
170         else:
171             self.admin_state_up = True
172
173         if kwargs.get('shared') is not None:
174             self.shared = bool(kwargs['shared'])
175         else:
176             self.shared = None
177
178         self.project_name = kwargs.get('project_name')
179
180         if kwargs.get('external') is not None:
181             self.external = bool(kwargs.get('external'))
182         else:
183             self.external = False
184
185         self.network_type = kwargs.get('network_type')
186         self.physical_network = kwargs.get('physical_network')
187         self.segmentation_id = kwargs.get('segmentation_id')
188
189         self.subnet_settings = list()
190         subnet_settings = kwargs.get('subnets')
191         if not subnet_settings:
192             subnet_settings = kwargs.get('subnet_settings')
193         if subnet_settings:
194             for subnet_config in subnet_settings:
195                 if isinstance(subnet_config, SubnetSettings):
196                     self.subnet_settings.append(subnet_config)
197                 else:
198                     self.subnet_settings.append(
199                         SubnetSettings(**subnet_config['subnet']))
200
201         if not self.name or len(self.name) < 1:
202             raise NetworkSettingsError('Name required for networks')
203
204     def get_project_id(self, os_creds):
205         """
206         Returns the project ID for a given project_name or None
207         :param os_creds: the credentials required for keystone client retrieval
208         :return: the ID or None
209         """
210         if self.project_id:
211             return self.project_id
212         else:
213             if self.project_name:
214                 keystone = keystone_utils.keystone_client(os_creds)
215                 project = keystone_utils.get_project(
216                     keystone=keystone, project_name=self.project_name)
217                 if project:
218                     return project.id
219
220         return None
221
222     def dict_for_neutron(self, os_creds):
223         """
224         Returns a dictionary object representing this object.
225         This is meant to be converted into JSON designed for use by the Neutron
226         API
227         TODO - expand automated testing to exercise all parameters
228
229         :param os_creds: the OpenStack credentials
230         :return: the dictionary object
231         """
232         out = dict()
233
234         if self.name:
235             out['name'] = self.name
236         if self.admin_state_up is not None:
237             out['admin_state_up'] = self.admin_state_up
238         if self.shared:
239             out['shared'] = self.shared
240         if self.project_name:
241             project_id = self.get_project_id(os_creds)
242             if project_id:
243                 out['tenant_id'] = project_id
244             else:
245                 raise NetworkSettingsError(
246                     'Could not find project ID for project named - ' +
247                     self.project_name)
248         if self.network_type:
249             out['provider:network_type'] = self.network_type
250         if self.physical_network:
251             out['provider:physical_network'] = self.physical_network
252         if self.segmentation_id:
253             out['provider:segmentation_id'] = self.segmentation_id
254         if self.external:
255             out['router:external'] = self.external
256         return {'network': out}
257
258
259 class NetworkSettingsError(Exception):
260     """
261     Exception to be thrown when networks settings attributes are incorrect
262     """
263
264
265 class SubnetSettings:
266     """
267     Class representing a subnet configuration
268     """
269
270     def __init__(self, **kwargs):
271         """
272         Constructor - all parameters are optional except cidr (subnet mask)
273         :param cidr: The CIDR. REQUIRED if config parameter is None
274         :param ip_version: The IP version, which is 4 or 6.
275         :param name: The subnet name.
276         :param project_name: The name of the project who owns the network.
277                              Only administrative users can specify a project ID
278                              other than their own. You cannot change this value
279                              through authorization policies.
280         :param start: The start address for the allocation pools.
281         :param end: The end address for the allocation pools.
282         :param gateway_ip: The gateway IP address.
283         :param enable_dhcp: Set to true if DHCP is enabled and false if DHCP is
284                             disabled.
285         :param dns_nameservers: A list of DNS name servers for the subnet.
286                                 Specify each name server as an IP address
287                                 and separate multiple entries with a space.
288                                 For example [8.8.8.7 8.8.8.8].
289         :param host_routes: A list of host route dictionaries for the subnet.
290                             For example:
291                                 "host_routes":[
292                                     {
293                                         "destination":"0.0.0.0/0",
294                                         "nexthop":"123.456.78.9"
295                                     },
296                                     {
297                                         "destination":"192.168.0.0/24",
298                                         "nexthop":"192.168.0.1"
299                                     }
300                                 ]
301         :param destination: The destination for static route
302         :param nexthop: The next hop for the destination.
303         :param ipv6_ra_mode: A valid value is dhcpv6-stateful,
304                              dhcpv6-stateless, or slaac.
305         :param ipv6_address_mode: A valid value is dhcpv6-stateful,
306                                   dhcpv6-stateless, or slaac.
307         :raise: SubnetSettingsError when config does not have or cidr values
308                 are None
309         """
310         self.cidr = kwargs.get('cidr')
311         if kwargs.get('ip_version'):
312             self.ip_version = kwargs['ip_version']
313         else:
314             self.ip_version = 4
315
316         # Optional attributes that can be set after instantiation
317         self.name = kwargs.get('name')
318         self.project_name = kwargs.get('project_name')
319         self.start = kwargs.get('start')
320         self.end = kwargs.get('end')
321         self.gateway_ip = kwargs.get('gateway_ip')
322         self.enable_dhcp = kwargs.get('enable_dhcp')
323
324         if kwargs.get('dns_nameservers'):
325             self.dns_nameservers = kwargs.get('dns_nameservers')
326         else:
327             self.dns_nameservers = ['8.8.8.8']
328
329         self.host_routes = kwargs.get('host_routes')
330         self.destination = kwargs.get('destination')
331         self.nexthop = kwargs.get('nexthop')
332         self.ipv6_ra_mode = kwargs.get('ipv6_ra_mode')
333         self.ipv6_address_mode = kwargs.get('ipv6_address_mode')
334
335         if not self.name or not self.cidr:
336             raise SubnetSettingsError('Name and cidr required for subnets')
337
338     def dict_for_neutron(self, os_creds, network=None):
339         """
340         Returns a dictionary object representing this object.
341         This is meant to be converted into JSON designed for use by the Neutron
342         API
343         :param os_creds: the OpenStack credentials
344         :param network: The network object on which the subnet will be created
345                         (optional)
346         :return: the dictionary object
347         """
348         out = {
349             'cidr': self.cidr,
350             'ip_version': self.ip_version,
351         }
352
353         if network:
354             out['network_id'] = network.id
355         if self.name:
356             out['name'] = self.name
357         if self.project_name:
358             keystone = keystone_utils.keystone_client(os_creds)
359             project = keystone_utils.get_project(
360                 keystone=keystone, project_name=self.project_name)
361             project_id = None
362             if project:
363                 project_id = project.id
364             if project_id:
365                 out['tenant_id'] = project_id
366             else:
367                 raise SubnetSettingsError(
368                     'Could not find project ID for project named - ' +
369                     self.project_name)
370         if self.start and self.end:
371             out['allocation_pools'] = [{'start': self.start, 'end': self.end}]
372         if self.gateway_ip:
373             out['gateway_ip'] = self.gateway_ip
374         if self.enable_dhcp is not None:
375             out['enable_dhcp'] = self.enable_dhcp
376         if self.dns_nameservers and len(self.dns_nameservers) > 0:
377             out['dns_nameservers'] = self.dns_nameservers
378         if self.host_routes and len(self.host_routes) > 0:
379             out['host_routes'] = self.host_routes
380         if self.destination:
381             out['destination'] = self.destination
382         if self.nexthop:
383             out['nexthop'] = self.nexthop
384         if self.ipv6_ra_mode:
385             out['ipv6_ra_mode'] = self.ipv6_ra_mode
386         if self.ipv6_address_mode:
387             out['ipv6_address_mode'] = self.ipv6_address_mode
388         return out
389
390
391 class SubnetSettingsError(Exception):
392     """
393     Exception to be thrown when subnet settings attributes are incorrect
394     """
395
396
397 class PortSettings:
398     """
399     Class representing a port configuration
400     """
401
402     def __init__(self, **kwargs):
403         """
404         Constructor
405         :param name: A symbolic name for the port (optional).
406         :param network_name: The name of the network on which to create the
407                              port (required).
408         :param admin_state_up: A boolean value denoting the administrative
409                                status of the port. True = up / False = down
410         :param project_name: The name of the project who owns the network.
411                              Only administrative users can specify a project ID
412                              other than their own. You cannot change this value
413                              through authorization policies.
414         :param mac_address: The MAC address. If you specify an address that is
415                             not valid, a Bad Request (400) status code is
416                             returned. If you do not specify a MAC address,
417                             OpenStack Networking tries to allocate one. If a
418                             failure occurs, a Service Unavailable (503) status
419                             code is returned.
420         :param ip_addrs: A list of dict objects where each contains two keys
421                          'subnet_name' and 'ip' values which will get mapped to
422                          self.fixed_ips. These values will be directly
423                          translated into the fixed_ips dict
424         :param fixed_ips: A dict where the key is the subnet IDs and value is
425                           the IP address to assign to the port
426         :param security_groups: One or more security group IDs.
427         :param allowed_address_pairs: A dictionary containing a set of zero or
428                                       more allowed address pairs. An address
429                                       pair contains an IP address and MAC
430                                       address.
431         :param opt_value: The extra DHCP option value.
432         :param opt_name: The extra DHCP option name.
433         :param device_owner: The ID of the entity that uses this port.
434                              For example, a DHCP agent.
435         :param device_id: The ID of the device that uses this port.
436                           For example, a virtual server.
437         :return:
438         """
439         if 'port' in kwargs:
440             kwargs = kwargs['port']
441
442         self.network = None
443
444         self.name = kwargs.get('name')
445         self.network_name = kwargs.get('network_name')
446
447         if kwargs.get('admin_state_up') is not None:
448             self.admin_state_up = bool(kwargs['admin_state_up'])
449         else:
450             self.admin_state_up = True
451
452         self.project_name = kwargs.get('project_name')
453         self.mac_address = kwargs.get('mac_address')
454         self.ip_addrs = kwargs.get('ip_addrs')
455         self.fixed_ips = kwargs.get('fixed_ips')
456         self.security_groups = kwargs.get('security_groups')
457         self.allowed_address_pairs = kwargs.get('allowed_address_pairs')
458         self.opt_value = kwargs.get('opt_value')
459         self.opt_name = kwargs.get('opt_name')
460         self.device_owner = kwargs.get('device_owner')
461         self.device_id = kwargs.get('device_id')
462
463         if not self.network_name:
464             raise PortSettingsError(
465                 'The attribute network_name is required')
466
467     def __set_fixed_ips(self, neutron):
468         """
469         Sets the self.fixed_ips value
470         :param neutron: the Neutron client
471         :return: None
472         """
473         if not self.fixed_ips and self.ip_addrs:
474             self.fixed_ips = list()
475
476             for ip_addr_dict in self.ip_addrs:
477                 subnet = neutron_utils.get_subnet(
478                     neutron, subnet_name=ip_addr_dict['subnet_name'])
479                 if subnet and 'ip' in ip_addr_dict:
480                     self.fixed_ips.append({'ip_address': ip_addr_dict['ip'],
481                                            'subnet_id': subnet.id})
482                 else:
483                     raise PortSettingsError(
484                         'Invalid port configuration, subnet does not exist '
485                         'with name - ' + ip_addr_dict['subnet_name'])
486
487     def dict_for_neutron(self, neutron, os_creds):
488         """
489         Returns a dictionary object representing this object.
490         This is meant to be converted into JSON designed for use by the Neutron
491         API
492
493         TODO - expand automated testing to exercise all parameters
494         :param neutron: the Neutron client
495         :param os_creds: the OpenStack credentials
496         :return: the dictionary object
497         """
498         self.__set_fixed_ips(neutron)
499
500         out = dict()
501
502         project_id = None
503         if self.project_name:
504             keystone = keystone_utils.keystone_client(os_creds)
505             project = keystone_utils.get_project(
506                 keystone=keystone, project_name=self.project_name)
507             if project:
508                 project_id = project.id
509
510         if not self.network:
511             self.network = neutron_utils.get_network(
512                 neutron, network_name=self.network_name, project_id=project_id)
513         if not self.network:
514             raise PortSettingsError(
515                 'Cannot locate network with name - ' + self.network_name)
516
517         out['network_id'] = self.network.id
518
519         if self.admin_state_up is not None:
520             out['admin_state_up'] = self.admin_state_up
521         if self.name:
522             out['name'] = self.name
523         if self.project_name:
524             if project_id:
525                 out['tenant_id'] = project_id
526             else:
527                 raise PortSettingsError(
528                     'Could not find project ID for project named - ' +
529                     self.project_name)
530         if self.mac_address:
531             out['mac_address'] = self.mac_address
532         if self.fixed_ips and len(self.fixed_ips) > 0:
533             out['fixed_ips'] = self.fixed_ips
534         if self.security_groups:
535             out['security_groups'] = self.security_groups
536         if self.allowed_address_pairs and len(self.allowed_address_pairs) > 0:
537             out['allowed_address_pairs'] = self.allowed_address_pairs
538         if self.opt_value:
539             out['opt_value'] = self.opt_value
540         if self.opt_name:
541             out['opt_name'] = self.opt_name
542         if self.device_owner:
543             out['device_owner'] = self.device_owner
544         if self.device_id:
545             out['device_id'] = self.device_id
546         return {'port': out}
547
548     def __eq__(self, other):
549         return (self.name == other.name and
550                 self.network_name == other.network_name and
551                 self.admin_state_up == other.admin_state_up and
552                 self.project_name == other.project_name and
553                 self.mac_address == other.mac_address and
554                 self.ip_addrs == other.ip_addrs and
555                 self.fixed_ips == other.fixed_ips and
556                 self.security_groups == other.security_groups and
557                 self.allowed_address_pairs == other.allowed_address_pairs and
558                 self.opt_value == other.opt_value and
559                 self.opt_name == other.opt_name and
560                 self.device_owner == other.device_owner and
561                 self.device_id == other.device_id)
562
563
564 class PortSettingsError(Exception):
565     """
566     Exception to be thrown when port settings attributes are incorrect
567     """