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