3d50eb500a606029f748b35c7a2e7f154bd928d5
[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 import enum
18 from neutronclient.common.exceptions import NetworkNotFoundClient, Unauthorized
19
20 from snaps.config.network import NetworkConfig, SubnetConfig, PortConfig
21 from snaps.openstack.openstack_creator import OpenStackNetworkObject
22 from snaps.openstack.utils import neutron_utils, keystone_utils
23
24 __author__ = 'spisarski'
25
26 logger = logging.getLogger('OpenStackNetwork')
27
28
29 class OpenStackNetwork(OpenStackNetworkObject):
30     """
31     Class responsible for managing a network in OpenStack
32     """
33
34     def __init__(self, os_creds, network_settings):
35         """
36         Constructor - all parameters are required
37         :param os_creds: The credentials to connect with OpenStack
38         :param network_settings: The settings used to create a network
39         """
40         super(self.__class__, self).__init__(os_creds)
41
42         self.network_settings = network_settings
43
44         # Attributes instantiated on create()
45         self.__network = None
46
47     def initialize(self):
48         """
49         Loads the existing OpenStack network/subnet
50         :return: The Network domain object or None
51         """
52         super(self.__class__, self).initialize()
53
54         try:
55             keystone = keystone_utils.keystone_client(self._os_creds)
56             self.__network = neutron_utils.get_network(
57                 self._neutron, keystone,
58                 network_settings=self.network_settings,
59                 project_name=self._os_creds.project_name)
60         except Unauthorized as e:
61             logger.warn('Unable to lookup network with name %s - %s',
62                         self.network_settings.name, e)
63
64         return self.__network
65
66     def create(self):
67         """
68         Responsible for creating not only the network but then a private
69         subnet, router, and an interface to the router.
70         :return: the Network domain object
71         """
72         self.initialize()
73
74         if not self.__network:
75             self.__network = neutron_utils.create_network(
76                 self._neutron, self._os_creds, self.network_settings)
77             logger.debug(
78                 'Network [%s] created successfully' % self.__network.id)
79
80         return self.__network
81
82     def clean(self):
83         """
84         Removes and deletes all items created in reverse order.
85         """
86         if self.__network:
87             try:
88                 neutron_utils.delete_network(self._neutron, self.__network)
89             except NetworkNotFoundClient:
90                 pass
91             self.__network = None
92
93     def get_network(self):
94         """
95         Returns the created OpenStack network object
96         :return: the OpenStack network object
97         """
98         return self.__network
99
100
101 class NetworkSettings(NetworkConfig):
102     """
103     Class to hold the configuration settings required for creating OpenStack
104     Network objects
105     deprecated
106     """
107
108     def __init__(self, **kwargs):
109         from warnings import warn
110         warn('Use snaps.config.network.NetworkConfig instead',
111              DeprecationWarning)
112         super(self.__class__, self).__init__(**kwargs)
113
114
115 class IPv6Mode(enum.Enum):
116     """
117     A rule's direction
118     deprecated - use snaps.config.network.IPv6Mode
119     """
120     slaac = 'slaac'
121     stateful = 'dhcpv6-stateful'
122     stateless = 'dhcpv6-stateless'
123
124
125 class SubnetSettings(SubnetConfig):
126     """
127     Class to hold the configuration settings required for creating OpenStack
128     Subnet objects
129     deprecated
130     """
131
132     def __init__(self, **kwargs):
133         from warnings import warn
134         warn('Use snaps.config.network.SubnetConfig instead',
135              DeprecationWarning)
136         super(self.__class__, self).__init__(**kwargs)
137
138
139 class PortSettings(PortConfig):
140     """
141     Class to hold the configuration settings required for creating OpenStack
142     Subnet objects
143     deprecated
144     """
145
146     def __init__(self, **kwargs):
147         from warnings import warn
148         warn('Use snaps.config.network.PortConfig instead',
149              DeprecationWarning)
150         super(self.__class__, self).__init__(**kwargs)