Added MTU network config and updated flavor_metadata test support
[snaps.git] / snaps / domain / 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
16
17 class Network:
18     """
19     SNAPS domain object for interface routers. Should contain attributes that
20     are shared amongst cloud providers
21     """
22     def __init__(self, **kwargs):
23         """
24         Constructor
25         :param name: the network's name
26         :param id: the network's ID
27         :param project_id: the associated project ID
28         :param admin_state_up: T/F - network is up when True
29         :param shared: T/F - network can be shared amongst other project's
30         :param external: T/F - network is deemed to be external
31         :param type: vlan, vxlan, flat, etc.
32         :param subnets: list of Subnet objects
33         """
34         self.name = kwargs.get('name')
35         self.id = kwargs.get('id')
36         self.project_id = kwargs.get('project_id')
37         self.admin_state_up = kwargs.get('admin_state_up')
38         self.shared = kwargs.get('shared')
39         self.external = kwargs.get('router:external', kwargs.get('external'))
40         self.type = kwargs.get('provider:network_type', kwargs.get('type'))
41         self.subnets = kwargs.get('subnets', list())
42         self.mtu = kwargs.get('mtu')
43
44     def __eq__(self, other):
45         return (self.name == other.name and self.id == other.id and
46                 self.project_id == other.project_id and
47                 self.admin_state_up == other.admin_state_up and
48                 self.shared == other.shared and
49                 self.external == other.external and
50                 self.subnets == other.subnets and
51                 self.mtu == other.mtu)
52
53
54 class Subnet:
55     """
56     SNAPS domain object for interface routers. Should contain attributes that
57     are shared amongst cloud providers
58     """
59     def __init__(self, **kwargs):
60         """
61         Constructor
62         :param name: the network's name
63         :param id: the subnet's ID
64         :param project_id: the associated project ID
65         :param network_id: the network's ID
66         :param cidr: the CIDR
67         :param ip_version: the IP version
68         :param gateway_ip: the IP of the gateway
69         :param enable_dhcp: T/F if DHCP is enabled
70         :param dns_nameservers: list of DNS server IPs
71         :param host_routes: routes as returned in a dict by Neutron
72         :param ipv6_ra_mode: IPv6 RA Mode
73         :param ipv6_address_mode: IPv6 Address Mode
74         :param start: start IP address pool
75         :param end: end IP address pool
76         """
77         self.name = kwargs.get('name')
78         self.id = kwargs.get('id')
79         self.project_id = kwargs.get('project_id')
80         self.network_id = kwargs.get('network_id')
81         self.cidr = kwargs.get('cidr')
82         self.ip_version = kwargs.get('ip_version')
83         self.gateway_ip = kwargs.get('gateway_ip')
84         self.enable_dhcp = kwargs.get('enable_dhcp')
85         self.dns_nameservers = kwargs.get('dns_nameservers')
86         self.host_routes = kwargs.get('host_routes')
87         self.ipv6_ra_mode = kwargs.get('ipv6_ra_mode')
88         self.ipv6_address_mode = kwargs.get('ipv6_address_mode')
89
90         self.start = None
91         self.end = None
92         if ('allocation_pools' in kwargs and
93                 len(kwargs['allocation_pools']) > 0):
94             # Will need to ultimately support a list of pools
95             pools = kwargs['allocation_pools'][0]
96             if 'start' in pools:
97                 self.start = pools['start']
98             if 'end' in pools:
99                 self.end = pools['end']
100
101     def __eq__(self, other):
102         return (self.name == other.name and
103                 self.id == other.id and
104                 self.project_id == other.project_id and
105                 self.network_id == other.network_id and
106                 self.cidr == other.cidr and
107                 self.ip_version == other.ip_version and
108                 self.gateway_ip == other.gateway_ip and
109                 self.enable_dhcp == other.enable_dhcp and
110                 self.dns_nameservers == other.dns_nameservers and
111                 self.host_routes == other.host_routes and
112                 self.ipv6_ra_mode == other.ipv6_ra_mode and
113                 self.ipv6_address_mode == other.ipv6_address_mode and
114                 self.start == other.start and self.end == other.end)
115
116
117 class Port:
118     """
119     SNAPS domain object for ports. Should contain attributes that
120     are shared amongst cloud providers
121     """
122     def __init__(self, **kwargs):
123         """
124         Constructor
125         :param name: the security group's name
126         :param id: the security group's id
127         :param description: description
128         :param ips|fixed_ips: a list of IP addresses
129         :param mac_address: the port's MAC addresses
130         :param allowed_address_pairs: the port's allowed_address_pairs value
131         :param admin_state_up: T|F whether or not the port is up
132         :param device_id: device's ID
133         :param device_owner: device's owner
134         :param network_id: associated network ID
135         :param port_security_enabled: T|F whether or not the port security is
136                                       enabled
137         :param security_groups: the security group IDs associated with port
138         :param project_id: the associated project/tenant ID
139         """
140         self.name = kwargs.get('name')
141         self.id = kwargs.get('id')
142         self.description = kwargs.get('description')
143         self.ips = kwargs.get('ips', kwargs.get('fixed_ips'))
144         self.mac_address = kwargs.get('mac_address')
145         self.allowed_address_pairs = kwargs.get('allowed_address_pairs')
146         self.admin_state_up = kwargs.get('admin_state_up')
147         self.device_id = kwargs.get('device_id')
148         self.device_owner = kwargs.get('device_owner')
149         self.network_id = kwargs.get('network_id')
150         self.port_security_enabled = kwargs.get('port_security_enabled')
151         self.security_groups = kwargs.get('security_groups')
152         self.project_id = kwargs.get('tenant_id', kwargs.get('project_id'))
153
154     def __eq__(self, other):
155         return (self.name == other.name and self.id == other.id and
156                 self.ips == other.ips, self.mac_address == other.mac_address)
157
158
159 class Router:
160     """
161     SNAPS domain object for routers. Should contain attributes that are shared
162     amongst cloud providers
163     """
164     def __init__(self, **kwargs):
165         """
166         Constructor
167         :param name: the router's name
168         :param id: the router's id
169         :param status: the router's status
170         :param tenant_id: the router's project/tenant ID
171         :param admin_state_up: Router is up when True
172         :param external_gateway_info: dict() for populating external_network_id
173                                       and external_fixed_ips
174                    external_network_id: ID of the external network to route
175                                         in dict under key 'external_fixed_ips'
176                    external_fixed_ips: List IP addresses associated with the
177                                        external_network_id found in dict under
178                                        key 'network_id'
179         :param port_subnets: list of tuples where #1 is the Port domain object
180                              and #2 is a list of associated Subnet domain
181                              objects
182         """
183         self.name = kwargs.get('name')
184         self.id = kwargs.get('id')
185         self.status = kwargs.get('status')
186         self.tenant_id = kwargs.get('tenant_id')
187         self.admin_state_up = kwargs.get('admin_state_up')
188         self.port_subnets = kwargs.get('port_subnets')
189
190         if (kwargs.get('external_gateway_info') and
191                 isinstance(kwargs.get('external_gateway_info'), dict)):
192             gateway_info = kwargs.get('external_gateway_info')
193
194             self.external_network_id = gateway_info.get('network_id')
195             self.external_fixed_ips = gateway_info.get('external_fixed_ips')
196         else:
197             self.external_fixed_ips = kwargs.get('external_fixed_ips', None)
198             self.external_network_id = kwargs.get('external_network_id', None)
199
200     def __eq__(self, other):
201         return (self.name == other.name and self.id == other.id and
202                 self.status == other.status and
203                 self.tenant_id == other.tenant_id and
204                 self.admin_state_up == other.admin_state_up and
205                 self.external_network_id == other.external_network_id and
206                 self.external_fixed_ips == other.external_fixed_ips and
207                 self.port_subnets == other.port_subnets)
208
209
210 class InterfaceRouter:
211     """
212     SNAPS domain object for interface routers. Should contain attributes that
213     are shared amongst cloud providers
214     """
215     def __init__(self, **kwargs):
216         """
217         Constructor
218         """
219         self.id = kwargs.get('id')
220         self.subnet_id = kwargs.get('subnet_id')
221         self.port_id = kwargs.get('port_id')
222
223     def __eq__(self, other):
224         return (self.id == other.id and self.subnet_id == other.subnet_id and
225                 self.port_id == other.port_id)
226
227
228 class SecurityGroup:
229     """
230     SNAPS domain object for SecurityGroups. Should contain attributes that
231     are shared amongst cloud providers
232     """
233     def __init__(self, **kwargs):
234         """
235         Constructor
236         :param name: the security group's name
237         :param id: the security group's id
238         :param description: the security group's description
239         :param project_id: the security group's project_id
240         :param rules: list of SecurityGroupRule objects associated to this
241         """
242         self.name = kwargs.get('name')
243         self.id = kwargs.get('id')
244         self.description = kwargs.get('description')
245         self.project_id = kwargs.get('project_id', kwargs.get('tenant_id'))
246
247         self.rules = list()
248         if kwargs.get('rules') and isinstance(kwargs.get('rules'), list):
249             for rule in kwargs.get('rules'):
250                 if isinstance(rule, SecurityGroupRule):
251                     self.rules.append(rule)
252                 else:
253                     self.rules.append(SecurityGroupRule(**rule))
254
255     def __eq__(self, other):
256         return (self.name == other.name and
257                 self.id == other.id and
258                 self.description == other.description and
259                 self.project_id == other.project_id and
260                 self.rules == other.rules)
261
262
263 class SecurityGroupRule:
264     """
265     SNAPS domain object for Security Group Rules. Should contain attributes
266     that are shared amongst cloud providers
267     """
268     def __init__(self, **kwargs):
269         """
270         Constructor
271         :param id: the security group rule's id
272         :param security_group_id: the ID of the associated security group
273         :param description: the security group rule's description
274         :param direction: the security group rule's direction
275         :param ethertype: the security group rule's ethertype
276         :param port_range_min: the security group rule's port_range_min
277         :param port_range_max: the security group rule's port_range_max
278         :param protocol: the security group rule's protocol
279         :param remote_group_id: the security group rule's remote_group_id
280         :param remote_ip_prefix: the security group rule's remote_ip_prefix
281         """
282         self.id = kwargs.get('id')
283         self.security_group_id = kwargs.get('security_group_id')
284         self.description = kwargs.get('description')
285         self.direction = kwargs.get('direction')
286         self.ethertype = kwargs.get('ethertype')
287         self.port_range_min = kwargs.get('port_range_min')
288         self.port_range_max = kwargs.get('port_range_max')
289         self.protocol = kwargs.get('protocol')
290         self.remote_group_id = kwargs.get('remote_group_id')
291         self.remote_ip_prefix = kwargs.get('remote_ip_prefix')
292
293     def __eq__(self, other):
294         return (self.id == other.id and
295                 self.security_group_id == other.security_group_id and
296                 self.description == other.description and
297                 self.direction == other.direction and
298                 self.ethertype == other.ethertype and
299                 self.port_range_min == other.port_range_min and
300                 self.port_range_max == other.port_range_max and
301                 self.protocol == other.protocol and
302                 self.remote_group_id == other.remote_group_id and
303                 self.remote_ip_prefix == other.remote_ip_prefix)