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