root / snf-cyclades-app / synnefo / logic / networks.py @ 316787ab
History | View | Annotate | Download (8.3 kB)
1 |
# Copyright 2011-2013 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
import ipaddr |
34 |
|
35 |
from functools import wraps |
36 |
from django.db import transaction |
37 |
|
38 |
from django.conf import settings |
39 |
from snf_django.lib.api import faults |
40 |
from synnefo.api import util |
41 |
from synnefo import quotas |
42 |
from synnefo.db.models import Network, Backend, Subnet |
43 |
from synnefo.db.utils import validate_mac |
44 |
from synnefo.db.pools import EmptyPool |
45 |
from synnefo.logic import backend as backend_mod |
46 |
|
47 |
from logging import getLogger |
48 |
log = getLogger(__name__) |
49 |
|
50 |
|
51 |
def validate_network_action(network, action): |
52 |
if network.deleted:
|
53 |
raise faults.BadRequest("Network has been deleted.") |
54 |
|
55 |
|
56 |
def network_command(action): |
57 |
def decorator(func): |
58 |
@wraps(func)
|
59 |
@transaction.commit_on_success()
|
60 |
def wrapper(network, *args, **kwargs): |
61 |
validate_network_action(network, action) |
62 |
return func(network, *args, **kwargs)
|
63 |
return wrapper
|
64 |
return decorator
|
65 |
|
66 |
|
67 |
@transaction.commit_on_success
|
68 |
def create(userid, name, flavor, subnet=None, gateway=None, subnet6=None, |
69 |
gateway6=None, public=False, dhcp=True, link=None, mac_prefix=None, |
70 |
mode=None, floating_ip_pool=False, tags=None, backends=None, |
71 |
lazy_create=True):
|
72 |
if flavor is None: |
73 |
raise faults.BadRequest("Missing request parameter 'type'") |
74 |
elif flavor not in Network.FLAVORS.keys(): |
75 |
raise faults.BadRequest("Invalid network type '%s'" % flavor) |
76 |
|
77 |
if mac_prefix is not None and flavor == "MAC_FILTERED": |
78 |
raise faults.BadRequest("Can not override MAC_FILTERED mac-prefix") |
79 |
if link is not None and flavor == "PHYSICAL_VLAN": |
80 |
raise faults.BadRequest("Can not override PHYSICAL_VLAN link") |
81 |
|
82 |
if subnet is None and floating_ip_pool: |
83 |
raise faults.BadRequest("IPv6 only networks can not be floating" |
84 |
" pools.")
|
85 |
# Check that network parameters are valid
|
86 |
validate_network_params(subnet, gateway, subnet6, gateway6) |
87 |
|
88 |
try:
|
89 |
fmode, flink, fmac_prefix, ftags = util.values_from_flavor(flavor) |
90 |
except EmptyPool:
|
91 |
log.error("Failed to allocate resources for network of type: %s",
|
92 |
flavor) |
93 |
msg = "Failed to allocate resources for network."
|
94 |
raise faults.ServiceUnavailable(msg)
|
95 |
|
96 |
mode = mode or fmode
|
97 |
link = link or flink
|
98 |
mac_prefix = mac_prefix or fmac_prefix
|
99 |
tags = tags or ftags
|
100 |
|
101 |
if (flavor == "IP_LESS_ROUTED" and |
102 |
Network.objects.filter(deleted=False, mode=mode, link=link).exists()):
|
103 |
msg = "Link '%s' is already used." % link
|
104 |
raise faults.BadRequest(msg)
|
105 |
|
106 |
validate_mac(mac_prefix + "0:00:00:00")
|
107 |
|
108 |
network = Network.objects.create( |
109 |
name=name, |
110 |
userid=userid, |
111 |
flavor=flavor, |
112 |
mode=mode, |
113 |
link=link, |
114 |
mac_prefix=mac_prefix, |
115 |
tags=tags, |
116 |
public=public, |
117 |
external_router=public, |
118 |
floating_ip_pool=floating_ip_pool, |
119 |
action='CREATE',
|
120 |
state='ACTIVE')
|
121 |
|
122 |
if subnet:
|
123 |
s = Subnet.objects.create(network=network, |
124 |
ipversion=4,
|
125 |
cidr=subnet, |
126 |
gateway=gateway, |
127 |
dhcp=dhcp) |
128 |
s.ip_pools.create(size=0)
|
129 |
|
130 |
if subnet6:
|
131 |
Subnet.objects.create(network=network, |
132 |
ipversion=6,
|
133 |
cidr=subnet6, |
134 |
gateway=gateway6, |
135 |
dhcp=dhcp) |
136 |
|
137 |
# Issue commission to Quotaholder and accept it since at the end of
|
138 |
# this transaction the Network object will be created in the DB.
|
139 |
# Note: the following call does a commit!
|
140 |
if not public: |
141 |
quotas.issue_and_accept_commission(network) |
142 |
|
143 |
if not lazy_create: |
144 |
if floating_ip_pool:
|
145 |
backends = Backend.objects.filter(offline=False)
|
146 |
elif backends is None: |
147 |
backends = [] |
148 |
|
149 |
for bend in backends: |
150 |
network.create_backend_network(bend) |
151 |
backend_mod.create_network(network=network, backend=bend, |
152 |
connect=True)
|
153 |
return network
|
154 |
|
155 |
|
156 |
@network_command("RENAME") |
157 |
def rename(network, name): |
158 |
network.name = name |
159 |
network.save() |
160 |
return network
|
161 |
|
162 |
|
163 |
@network_command("DESTROY") |
164 |
def delete(network): |
165 |
if network.machines.exists():
|
166 |
raise faults.Conflict("Can not delete network. Servers connected" |
167 |
" to this network exists.")
|
168 |
if network.ips.filter(deleted=False, floating_ip=True).exists(): |
169 |
msg = "Can not delete netowrk. Network has allocated floating IPs."
|
170 |
raise faults.Conflict(msg)
|
171 |
|
172 |
network.action = "DESTROY"
|
173 |
network.save() |
174 |
|
175 |
# Delete network to all backends that exists
|
176 |
for bnet in network.backend_networks.exclude(operstate="DELETED"): |
177 |
backend_mod.delete_network(network, bnet.backend) |
178 |
else:
|
179 |
# If network does not exist in any backend, update the network state
|
180 |
backend_mod.update_network_state(network) |
181 |
return network
|
182 |
|
183 |
|
184 |
def validate_network_params(subnet=None, gateway=None, subnet6=None, |
185 |
gateway6=None):
|
186 |
if subnet:
|
187 |
try:
|
188 |
# Use strict option to not all subnets with host bits set
|
189 |
network = ipaddr.IPv4Network(subnet, strict=True)
|
190 |
except ValueError: |
191 |
raise faults.BadRequest("Invalid network IPv4 subnet") |
192 |
|
193 |
# Check that network size is allowed!
|
194 |
prefixlen = network.prefixlen |
195 |
if prefixlen > 29 or prefixlen <= settings.MAX_CIDR_BLOCK: |
196 |
raise faults.OverLimit(
|
197 |
message="Unsupported network size",
|
198 |
details="Netmask must be in range: (%s, 29]" %
|
199 |
settings.MAX_CIDR_BLOCK) |
200 |
if gateway: # Check that gateway belongs to network |
201 |
try:
|
202 |
gateway = ipaddr.IPv4Address(gateway) |
203 |
except ValueError: |
204 |
raise faults.BadRequest("Invalid network IPv4 gateway") |
205 |
if not gateway in network: |
206 |
raise faults.BadRequest("Invalid network IPv4 gateway") |
207 |
|
208 |
if subnet6:
|
209 |
try:
|
210 |
# Use strict option to not all subnets with host bits set
|
211 |
network6 = ipaddr.IPv6Network(subnet6, strict=True)
|
212 |
except ValueError: |
213 |
raise faults.BadRequest("Invalid network IPv6 subnet") |
214 |
# Check that network6 is an /64 subnet, because this is imposed by
|
215 |
# 'mac2eui64' utiity.
|
216 |
if network6.prefixlen != 64: |
217 |
msg = ("Unsupported IPv6 subnet size. Network netmask must be"
|
218 |
" /64")
|
219 |
raise faults.BadRequest(msg)
|
220 |
if gateway6:
|
221 |
try:
|
222 |
gateway6 = ipaddr.IPv6Address(gateway6) |
223 |
except ValueError: |
224 |
raise faults.BadRequest("Invalid network IPv6 gateway") |
225 |
if not gateway6 in network6: |
226 |
raise faults.BadRequest("Invalid network IPv6 gateway") |