Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / networks.py @ 94b7399e

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
        floating_ip_pool=floating_ip_pool,
118
        action='CREATE',
119
        state='ACTIVE')
120

    
121
    if subnet:
122
        s = Subnet.objects.create(network=network,
123
                                  ipversion=4,
124
                                  cidr=subnet,
125
                                  gateway=gateway,
126
                                  dhcp=dhcp)
127
        s.ip_pools.create(size=0)
128

    
129
    if subnet6:
130
        Subnet.objects.create(network=network,
131
                              ipversion=6,
132
                              cidr=subnet6,
133
                              gateway=gateway6,
134
                              dhcp=dhcp)
135

    
136
    # Issue commission to Quotaholder and accept it since at the end of
137
    # this transaction the Network object will be created in the DB.
138
    # Note: the following call does a commit!
139
    if not public:
140
        quotas.issue_and_accept_commission(network)
141

    
142
    if not lazy_create:
143
        if floating_ip_pool:
144
            backends = Backend.objects.filter(offline=False)
145
        elif backends is None:
146
            backends = []
147

    
148
        for bend in backends:
149
            network.create_backend_network(bend)
150
            backend_mod.create_network(network=network, backend=bend,
151
                                       connect=True)
152
    return network
153

    
154

    
155
@network_command("RENAME")
156
def rename(network, name):
157
    network.name = name
158
    network.save()
159
    return network
160

    
161

    
162
@network_command("DESTROY")
163
def delete(network):
164
    if network.machines.exists():
165
        raise faults.Conflict("Can not delete network. Servers connected"
166
                              " to this network exists.")
167
    if network.ips.filter(deleted=False, floating_ip=True).exists():
168
        msg = "Can not delete netowrk. Network has allocated floating IPs."
169
        raise faults.Conflict(msg)
170

    
171
    network.action = "DESTROY"
172
    network.save()
173

    
174
    # Delete network to all backends that exists
175
    for bnet in network.backend_networks.exclude(operstate="DELETED"):
176
        backend_mod.delete_network(network, bnet.backend)
177
    else:
178
        # If network does not exist in any backend, update the network state
179
        backend_mod.update_network_state(network)
180
    return network
181

    
182

    
183
def validate_network_params(subnet=None, gateway=None, subnet6=None,
184
                            gateway6=None):
185
    if subnet:
186
        try:
187
            # Use strict option to not all subnets with host bits set
188
            network = ipaddr.IPv4Network(subnet, strict=True)
189
        except ValueError:
190
            raise faults.BadRequest("Invalid network IPv4 subnet")
191

    
192
        # Check that network size is allowed!
193
        prefixlen = network.prefixlen
194
        if prefixlen > 29 or prefixlen <= settings.MAX_CIDR_BLOCK:
195
            raise faults.OverLimit(
196
                message="Unsupported network size",
197
                details="Netmask must be in range: (%s, 29]" %
198
                settings.MAX_CIDR_BLOCK)
199
        if gateway:  # Check that gateway belongs to network
200
            try:
201
                gateway = ipaddr.IPv4Address(gateway)
202
            except ValueError:
203
                raise faults.BadRequest("Invalid network IPv4 gateway")
204
            if not gateway in network:
205
                raise faults.BadRequest("Invalid network IPv4 gateway")
206

    
207
    if subnet6:
208
        try:
209
            # Use strict option to not all subnets with host bits set
210
            network6 = ipaddr.IPv6Network(subnet6, strict=True)
211
        except ValueError:
212
            raise faults.BadRequest("Invalid network IPv6 subnet")
213
        # Check that network6 is an /64 subnet, because this is imposed by
214
        # 'mac2eui64' utiity.
215
        if network6.prefixlen != 64:
216
            msg = ("Unsupported IPv6 subnet size. Network netmask must be"
217
                   " /64")
218
            raise faults.BadRequest(msg)
219
        if gateway6:
220
            try:
221
                gateway6 = ipaddr.IPv6Address(gateway6)
222
            except ValueError:
223
                raise faults.BadRequest("Invalid network IPv6 gateway")
224
            if not gateway6 in network6:
225
                raise faults.BadRequest("Invalid network IPv6 gateway")