9cc1dd1fa14f650da94ecd5b704fc955a35057aa
[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         """
26         self.name = kwargs.get('name')
27         self.id = kwargs.get('id')
28         self.admin_state_up = kwargs.get('admin_state_up')
29         self.shared = kwargs.get('shared')
30         self.external = kwargs.get('router:external', kwargs.get('external'))
31         self.type = kwargs.get('provider:network_type', kwargs.get('type'))
32
33     def __eq__(self, other):
34         return (self.name == other.name and self.id == other.id and
35                 self.admin_state_up == other.admin_state_up and
36                 self.shared == other.shared and
37                 self.external == other.external and self.type == other.type)
38
39
40 class Subnet:
41     """
42     SNAPS domain object for interface routers. Should contain attributes that
43     are shared amongst cloud providers
44     """
45     def __init__(self, **kwargs):
46         """
47         Constructor
48         """
49         self.name = kwargs.get('name')
50         self.id = kwargs.get('id')
51         self.cidr = kwargs.get('cidr')
52         self.ip_version = kwargs.get('ip_version')
53         self.gateway_ip = kwargs.get('gateway_ip')
54         self.enable_dhcp = kwargs.get('enable_dhcp')
55         self.dns_nameservers = kwargs.get('dns_nameservers')
56         self.host_routes = kwargs.get('host_routes')
57         self.ipv6_ra_mode = kwargs.get('ipv6_ra_mode')
58         self.ipv6_address_mode = kwargs.get('ipv6_address_mode')
59
60         self.start = None
61         self.end = None
62         if ('allocation_pools' in kwargs and
63                 len(kwargs['allocation_pools']) > 0):
64             # Will need to ultimately support a list of pools
65             pools = kwargs['allocation_pools'][0]
66             if 'start' in pools:
67                 self.start = pools['start']
68             if 'end' in pools:
69                 self.end = pools['end']
70
71     def __eq__(self, other):
72         return (self.name == other.name and
73                 self.id == other.id and
74                 self.cidr == other.cidr and
75                 self.ip_version == other.ip_version and
76                 self.gateway_ip == other.gateway_ip and
77                 self.enable_dhcp == other.enable_dhcp and
78                 self.dns_nameservers == other.dns_nameservers and
79                 self.host_routes == other.host_routes and
80                 self.ipv6_ra_mode == other.ipv6_ra_mode and
81                 self.ipv6_address_mode == other.ipv6_address_mode and
82                 self.start == other.start and self.end == other.end)
83
84
85 class Port:
86     """
87     SNAPS domain object for ports. Should contain attributes that
88     are shared amongst cloud providers
89     """
90     def __init__(self, **kwargs):
91         """
92         Constructor
93         :param name: the security group's name
94         :param id: the security group's id
95         :param description: description
96         :param ips|fixed_ips: a list of IP addresses
97         :param mac_address: the port's MAC addresses
98         :param allowed_address_pairs: the port's allowed_address_pairs value
99         :param admin_state_up: T|F whether or not the port is up
100         :param device_id: device's ID
101         :param device_owner: device's owner
102         :param network_id: associated network ID
103         :param port_security_enabled: T|F whether or not the port security is
104                                       enabled
105         :param security_groups: the security group IDs associated with port
106         :param project_id: the associated project/tenant ID
107         """
108         self.name = kwargs.get('name')
109         self.id = kwargs.get('id')
110         self.description = kwargs.get('description')
111         self.ips = kwargs.get('ips', kwargs.get('fixed_ips'))
112         self.mac_address = kwargs.get('mac_address')
113         self.allowed_address_pairs = kwargs.get('allowed_address_pairs')
114         self.admin_state_up = kwargs.get('admin_state_up')
115         self.device_id = kwargs.get('device_id')
116         self.device_owner = kwargs.get('device_owner')
117         self.network_id = kwargs.get('network_id')
118         self.port_security_enabled = kwargs.get('port_security_enabled')
119         self.security_groups = kwargs.get('security_groups')
120         self.project_id = kwargs.get('tenant_id', kwargs.get('project_id'))
121
122     def __eq__(self, other):
123         return (self.name == other.name and self.id == other.id and
124                 self.ips == other.ips, self.mac_address == other.mac_address)
125
126
127 class Router:
128     """
129     SNAPS domain object for routers. Should contain attributes that are shared
130     amongst cloud providers
131     """
132     def __init__(self, **kwargs):
133         """
134         Constructor
135         :param name: the router's name
136         :param id: the router's id
137         """
138         self.name = kwargs.get('name')
139         self.id = kwargs.get('id')
140         self.status = kwargs.get('status')
141         self.tenant_id = kwargs.get('tenant_id')
142         self.admin_state_up = kwargs.get('admin_state_up')
143         self.external_gateway_info = kwargs.get('external_gateway_info')
144
145     def __eq__(self, other):
146         return (self.name == other.name and self.id == other.id and
147                 self.status == other.status and
148                 self.tenant_id == other.tenant_id and
149                 self.admin_state_up == other.admin_state_up and
150                 self.external_gateway_info == other.external_gateway_info)
151
152
153 class InterfaceRouter:
154     """
155     SNAPS domain object for interface routers. Should contain attributes that
156     are shared amongst cloud providers
157     """
158     def __init__(self, **kwargs):
159         """
160         Constructor
161         """
162         self.id = kwargs.get('id')
163         self.subnet_id = kwargs.get('subnet_id')
164         self.port_id = kwargs.get('port_id')
165
166     def __eq__(self, other):
167         return (self.id == other.id and self.subnet_id == other.subnet_id and
168                 self.port_id == other.port_id)
169
170
171 class SecurityGroup:
172     """
173     SNAPS domain object for SecurityGroups. Should contain attributes that
174     are shared amongst cloud providers
175     """
176     def __init__(self, **kwargs):
177         """
178         Constructor
179         :param name: the security group's name
180         :param id: the security group's id
181         """
182         self.name = kwargs.get('name')
183         self.id = kwargs.get('id')
184         self.description = kwargs.get('description')
185         self.project_id = kwargs.get('project_id', kwargs.get('tenant_id'))
186
187     def __eq__(self, other):
188         return (self.name == other.name and self.id == other.id and
189                 self.project_id == other.project_id)
190
191
192 class SecurityGroupRule:
193     """
194     SNAPS domain object for Security Group Rules. Should contain attributes
195     that are shared amongst cloud providers
196     """
197     def __init__(self, **kwargs):
198         """
199         Constructor
200         :param id: the security group rule's id
201         :param sec_grp_id: the ID of the associated security group
202         :param description: the security group rule's description
203         :param direction: the security group rule's direction
204         :param ethertype: the security group rule's ethertype
205         :param port_range_min: the security group rule's port_range_min
206         :param port_range_max: the security group rule's port_range_max
207         :param protocol: the security group rule's protocol
208         :param remote_group_id: the security group rule's remote_group_id
209         :param remote_ip_prefix: the security group rule's remote_ip_prefix
210         """
211         self.id = kwargs.get('id')
212         self.security_group_id = kwargs.get('security_group_id')
213         self.description = kwargs.get('description')
214         self.direction = kwargs.get('direction')
215         self.ethertype = kwargs.get('ethertype')
216         self.port_range_min = kwargs.get('port_range_min')
217         self.port_range_max = kwargs.get('port_range_max')
218         self.protocol = kwargs.get('protocol')
219         self.remote_group_id = kwargs.get('remote_group_id')
220         self.remote_ip_prefix = kwargs.get('remote_ip_prefix')
221
222     def __eq__(self, other):
223         return (self.id == other.id and
224                 self.security_group_id == other.security_group_id and
225                 self.description == other.description and
226                 self.direction == other.direction and
227                 self.ethertype == other.ethertype and
228                 self.port_range_min == other.port_range_min and
229                 self.port_range_max == other.port_range_max and
230                 self.protocol == other.protocol and
231                 self.remote_group_id == other.remote_group_id and
232                 self.remote_ip_prefix == other.remote_ip_prefix)