aabec21b47d3d9ea339e0cb3caed8e676a9682bc
[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
43     def __eq__(self, other):
44         return (self.name == other.name and self.id == other.id and
45                 self.project_id == other.project_id and
46                 self.admin_state_up == other.admin_state_up and
47                 self.shared == other.shared and
48                 self.external == other.external and
49                 self.type == other.type and
50                 self.subnets == other.subnets)
51
52
53 class Subnet:
54     """
55     SNAPS domain object for interface routers. Should contain attributes that
56     are shared amongst cloud providers
57     """
58     def __init__(self, **kwargs):
59         """
60         Constructor
61         :param name: the network's name
62         :param id: the subnet's ID
63         :param project_id: the associated project ID
64         :param network_id: the network's ID
65         :param cidr: the CIDR
66         :param ip_version: the IP version
67         :param gateway_ip: the IP of the gateway
68         :param enable_dhcp: T/F if DHCP is enabled
69         :param dns_nameservers: list of DNS server IPs
70         :param host_routes: routes as returned in a dict by Neutron
71         :param ipv6_ra_mode: IPv6 RA Mode
72         :param ipv6_address_mode: IPv6 Address Mode
73         :param start: start IP address pool
74         :param end: end IP address pool
75         """
76         self.name = kwargs.get('name')
77         self.id = kwargs.get('id')
78         self.project_id = kwargs.get('project_id')
79         self.network_id = kwargs.get('network_id')
80         self.cidr = kwargs.get('cidr')
81         self.ip_version = kwargs.get('ip_version')
82         self.gateway_ip = kwargs.get('gateway_ip')
83         self.enable_dhcp = kwargs.get('enable_dhcp')
84         self.dns_nameservers = kwargs.get('dns_nameservers')
85         self.host_routes = kwargs.get('host_routes')
86         self.ipv6_ra_mode = kwargs.get('ipv6_ra_mode')
87         self.ipv6_address_mode = kwargs.get('ipv6_address_mode')
88
89         self.start = None
90         self.end = None
91         if ('allocation_pools' in kwargs and
92                 len(kwargs['allocation_pools']) > 0):
93             # Will need to ultimately support a list of pools
94             pools = kwargs['allocation_pools'][0]
95             if 'start' in pools:
96                 self.start = pools['start']
97             if 'end' in pools:
98                 self.end = pools['end']
99
100     def __eq__(self, other):
101         return (self.name == other.name and
102                 self.id == other.id and
103                 self.project_id == other.project_id and
104                 self.network_id == other.network_id and
105                 self.cidr == other.cidr and
106                 self.ip_version == other.ip_version and
107                 self.gateway_ip == other.gateway_ip and
108                 self.enable_dhcp == other.enable_dhcp and
109                 self.dns_nameservers == other.dns_nameservers and
110                 self.host_routes == other.host_routes and
111                 self.ipv6_ra_mode == other.ipv6_ra_mode and
112                 self.ipv6_address_mode == other.ipv6_address_mode and
113                 self.start == other.start and self.end == other.end)
114
115
116 class Port:
117     """
118     SNAPS domain object for ports. Should contain attributes that
119     are shared amongst cloud providers
120     """
121     def __init__(self, **kwargs):
122         """
123         Constructor
124         :param name: the security group's name
125         :param id: the security group's id
126         :param description: description
127         :param ips|fixed_ips: a list of IP addresses
128         :param mac_address: the port's MAC addresses
129         :param allowed_address_pairs: the port's allowed_address_pairs value
130         :param admin_state_up: T|F whether or not the port is up
131         :param device_id: device's ID
132         :param device_owner: device's owner
133         :param network_id: associated network ID
134         :param port_security_enabled: T|F whether or not the port security is
135                                       enabled
136         :param security_groups: the security group IDs associated with port
137         :param project_id: the associated project/tenant ID
138         """
139         self.name = kwargs.get('name')
140         self.id = kwargs.get('id')
141         self.description = kwargs.get('description')
142         self.ips = kwargs.get('ips', kwargs.get('fixed_ips'))
143         self.mac_address = kwargs.get('mac_address')
144         self.allowed_address_pairs = kwargs.get('allowed_address_pairs')
145         self.admin_state_up = kwargs.get('admin_state_up')
146         self.device_id = kwargs.get('device_id')
147         self.device_owner = kwargs.get('device_owner')
148         self.network_id = kwargs.get('network_id')
149         self.port_security_enabled = kwargs.get('port_security_enabled')
150         self.security_groups = kwargs.get('security_groups')
151         self.project_id = kwargs.get('tenant_id', kwargs.get('project_id'))
152
153     def __eq__(self, other):
154         return (self.name == other.name and self.id == other.id and
155                 self.ips == other.ips, self.mac_address == other.mac_address)
156
157
158 class Router:
159     """
160     SNAPS domain object for routers. Should contain attributes that are shared
161     amongst cloud providers
162     """
163     def __init__(self, **kwargs):
164         """
165         Constructor
166         :param name: the router's name
167         :param id: the router's id
168         :param status: the router's status
169         :param tenant_id: the router's project/tenant ID
170         :param admin_state_up: Router is up when True
171         :param external_gateway_info: dict() for populating external_network_id
172                                       and external_fixed_ips
173                    external_network_id: ID of the external network to route
174                                         in dict under key 'external_fixed_ips'
175                    external_fixed_ips: List IP addresses associated with the
176                                        external_network_id found in dict under
177                                        key 'network_id'
178         :param port_subnets: list of tuples where #1 is the Port domain object
179                              and #2 is a list of associated Subnet domain
180                              objects
181         """
182         self.name = kwargs.get('name')
183         self.id = kwargs.get('id')
184         self.status = kwargs.get('status')
185         self.tenant_id = kwargs.get('tenant_id')
186         self.admin_state_up = kwargs.get('admin_state_up')
187         self.port_subnets = kwargs.get('port_subnets')
188
189         if (kwargs.get('external_gateway_info') and
190                 isinstance(kwargs.get('external_gateway_info'), dict) and
191                 kwargs.get('external_gateway_info').get('external_fixed_ips')):
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)