9f634aaf85a621287fe837b7efc23349cd9c5c2b
[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)):
190             gateway_info = kwargs.get('external_gateway_info')
191
192             self.external_network_id = gateway_info.get('network_id')
193             self.external_fixed_ips = gateway_info.get('external_fixed_ips')
194         else:
195             self.external_fixed_ips = kwargs.get('external_fixed_ips', None)
196             self.external_network_id = kwargs.get('external_network_id', None)
197
198     def __eq__(self, other):
199         return (self.name == other.name and self.id == other.id and
200                 self.status == other.status and
201                 self.tenant_id == other.tenant_id and
202                 self.admin_state_up == other.admin_state_up and
203                 self.external_network_id == other.external_network_id and
204                 self.external_fixed_ips == other.external_fixed_ips and
205                 self.port_subnets == other.port_subnets)
206
207
208 class InterfaceRouter:
209     """
210     SNAPS domain object for interface routers. Should contain attributes that
211     are shared amongst cloud providers
212     """
213     def __init__(self, **kwargs):
214         """
215         Constructor
216         """
217         self.id = kwargs.get('id')
218         self.subnet_id = kwargs.get('subnet_id')
219         self.port_id = kwargs.get('port_id')
220
221     def __eq__(self, other):
222         return (self.id == other.id and self.subnet_id == other.subnet_id and
223                 self.port_id == other.port_id)
224
225
226 class SecurityGroup:
227     """
228     SNAPS domain object for SecurityGroups. Should contain attributes that
229     are shared amongst cloud providers
230     """
231     def __init__(self, **kwargs):
232         """
233         Constructor
234         :param name: the security group's name
235         :param id: the security group's id
236         :param description: the security group's description
237         :param project_id: the security group's project_id
238         :param rules: list of SecurityGroupRule objects associated to this
239         """
240         self.name = kwargs.get('name')
241         self.id = kwargs.get('id')
242         self.description = kwargs.get('description')
243         self.project_id = kwargs.get('project_id', kwargs.get('tenant_id'))
244
245         self.rules = list()
246         if kwargs.get('rules') and isinstance(kwargs.get('rules'), list):
247             for rule in kwargs.get('rules'):
248                 if isinstance(rule, SecurityGroupRule):
249                     self.rules.append(rule)
250                 else:
251                     self.rules.append(SecurityGroupRule(**rule))
252
253     def __eq__(self, other):
254         return (self.name == other.name and
255                 self.id == other.id and
256                 self.description == other.description and
257                 self.project_id == other.project_id and
258                 self.rules == other.rules)
259
260
261 class SecurityGroupRule:
262     """
263     SNAPS domain object for Security Group Rules. Should contain attributes
264     that are shared amongst cloud providers
265     """
266     def __init__(self, **kwargs):
267         """
268         Constructor
269         :param id: the security group rule's id
270         :param security_group_id: the ID of the associated security group
271         :param description: the security group rule's description
272         :param direction: the security group rule's direction
273         :param ethertype: the security group rule's ethertype
274         :param port_range_min: the security group rule's port_range_min
275         :param port_range_max: the security group rule's port_range_max
276         :param protocol: the security group rule's protocol
277         :param remote_group_id: the security group rule's remote_group_id
278         :param remote_ip_prefix: the security group rule's remote_ip_prefix
279         """
280         self.id = kwargs.get('id')
281         self.security_group_id = kwargs.get('security_group_id')
282         self.description = kwargs.get('description')
283         self.direction = kwargs.get('direction')
284         self.ethertype = kwargs.get('ethertype')
285         self.port_range_min = kwargs.get('port_range_min')
286         self.port_range_max = kwargs.get('port_range_max')
287         self.protocol = kwargs.get('protocol')
288         self.remote_group_id = kwargs.get('remote_group_id')
289         self.remote_ip_prefix = kwargs.get('remote_ip_prefix')
290
291     def __eq__(self, other):
292         return (self.id == other.id and
293                 self.security_group_id == other.security_group_id and
294                 self.description == other.description and
295                 self.direction == other.direction and
296                 self.ethertype == other.ethertype and
297                 self.port_range_min == other.port_range_min and
298                 self.port_range_max == other.port_range_max and
299                 self.protocol == other.protocol and
300                 self.remote_group_id == other.remote_group_id and
301                 self.remote_ip_prefix == other.remote_ip_prefix)