root / snf-cyclades-app / synnefo / logic / networks.py @ 35ea7f67
History | View | Annotate | Download (6.1 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 |
|
34 |
from functools import wraps |
35 |
from django.db import transaction |
36 |
from django.conf import settings |
37 |
|
38 |
from snf_django.lib.api import faults |
39 |
from synnefo.api import util |
40 |
from synnefo import quotas |
41 |
from synnefo.db.models import Network, Backend |
42 |
from synnefo.db.utils import validate_mac |
43 |
from synnefo.db.pools import EmptyPool |
44 |
from synnefo.logic import backend as backend_mod |
45 |
from synnefo.logic import utils |
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, link=None, mac_prefix=None, mode=None, |
69 |
floating_ip_pool=False, tags=None, public=False, drained=False): |
70 |
if flavor is None: |
71 |
raise faults.BadRequest("Missing request parameter 'type'") |
72 |
elif flavor not in Network.FLAVORS.keys(): |
73 |
raise faults.BadRequest("Invalid network type '%s'" % flavor) |
74 |
|
75 |
if mac_prefix is not None and flavor == "MAC_FILTERED": |
76 |
raise faults.BadRequest("Cannot override MAC_FILTERED mac-prefix") |
77 |
if link is not None and flavor == "PHYSICAL_VLAN": |
78 |
raise faults.BadRequest("Cannot override PHYSICAL_VLAN link") |
79 |
|
80 |
utils.check_name_length(name, Network.NETWORK_NAME_LENGTH, "Network name "
|
81 |
"is too long")
|
82 |
|
83 |
try:
|
84 |
fmode, flink, fmac_prefix, ftags = util.values_from_flavor(flavor) |
85 |
except EmptyPool:
|
86 |
log.error("Failed to allocate resources for network of type: %s",
|
87 |
flavor) |
88 |
msg = "Failed to allocate resources for network."
|
89 |
raise faults.ServiceUnavailable(msg)
|
90 |
|
91 |
mode = mode or fmode
|
92 |
link = link or flink
|
93 |
mac_prefix = mac_prefix or fmac_prefix
|
94 |
tags = tags or ftags
|
95 |
|
96 |
validate_mac(mac_prefix + "0:00:00:00")
|
97 |
|
98 |
# Check that given link is unique!
|
99 |
if (link is not None and flavor == "IP_LESS_ROUTED" and |
100 |
Network.objects.filter(deleted=False, mode=mode, link=link).exists()):
|
101 |
msg = "Link '%s' is already used." % link
|
102 |
raise faults.BadRequest(msg)
|
103 |
|
104 |
network = Network.objects.create( |
105 |
name=name, |
106 |
userid=userid, |
107 |
flavor=flavor, |
108 |
mode=mode, |
109 |
link=link, |
110 |
mac_prefix=mac_prefix, |
111 |
tags=tags, |
112 |
public=public, |
113 |
external_router=public, |
114 |
floating_ip_pool=floating_ip_pool, |
115 |
action='CREATE',
|
116 |
state='ACTIVE',
|
117 |
drained=drained) |
118 |
|
119 |
if link is None: |
120 |
network.link = "%slink-%d" % (settings.BACKEND_PREFIX_ID, network.id)
|
121 |
network.save() |
122 |
|
123 |
# Issue commission to Quotaholder and accept it since at the end of
|
124 |
# this transaction the Network object will be created in the DB.
|
125 |
# Note: the following call does a commit!
|
126 |
if not public: |
127 |
quotas.issue_and_accept_commission(network) |
128 |
|
129 |
return network
|
130 |
|
131 |
|
132 |
def create_network_in_backends(network): |
133 |
job_ids = [] |
134 |
for bend in Backend.objects.filter(offline=False): |
135 |
network.create_backend_network(bend) |
136 |
jobs = backend_mod.create_network(network=network, backend=bend, |
137 |
connect=True)
|
138 |
job_ids.extend(jobs) |
139 |
return job_ids
|
140 |
|
141 |
|
142 |
@network_command("RENAME") |
143 |
def rename(network, name): |
144 |
utils.check_name_length(name, Network.NETWORK_NAME_LENGTH, "Network name "
|
145 |
"is too long")
|
146 |
network.name = name |
147 |
network.save() |
148 |
return network
|
149 |
|
150 |
|
151 |
@network_command("DESTROY") |
152 |
def delete(network): |
153 |
if network.nics.exists():
|
154 |
raise faults.Conflict("Cannot delete network. There are ports still" |
155 |
" configured on network network %s" % network.id)
|
156 |
if network.ips.filter(deleted=False, floating_ip=True).exists(): |
157 |
msg = "Cannot delete netowrk. Network has allocated floating IPs."
|
158 |
raise faults.Conflict(msg)
|
159 |
|
160 |
network.action = "DESTROY"
|
161 |
# Mark network as drained to prevent automatic allocation of
|
162 |
# public/floating IPs while the network is being deleted
|
163 |
if network.public:
|
164 |
network.drained = True
|
165 |
network.save() |
166 |
|
167 |
# Delete network to all backends that exists
|
168 |
for bnet in network.backend_networks.exclude(operstate="DELETED"): |
169 |
backend_mod.delete_network(network, bnet.backend) |
170 |
else:
|
171 |
# If network does not exist in any backend, update the network state
|
172 |
backend_mod.update_network_state(network) |
173 |
return network
|