networks.py 6.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Copyright 2011-2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
33

34
35
from functools import wraps
from django.db import transaction
36
from django.conf import settings
37
38
39
40

from snf_django.lib.api import faults
from synnefo.api import util
from synnefo import quotas
41
from synnefo.db.models import Network, Backend
42
43
from synnefo.db.utils import validate_mac
from synnefo.db.pools import EmptyPool
44
from synnefo.logic import backend as backend_mod
45
from synnefo.logic import utils
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

from logging import getLogger
log = getLogger(__name__)


def validate_network_action(network, action):
    if network.deleted:
        raise faults.BadRequest("Network has been deleted.")


def network_command(action):
    def decorator(func):
        @wraps(func)
        @transaction.commit_on_success()
        def wrapper(network, *args, **kwargs):
            validate_network_action(network, action)
            return func(network, *args, **kwargs)
        return wrapper
    return decorator


@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
71
72
73
74
    if flavor is None:
        raise faults.BadRequest("Missing request parameter 'type'")
    elif flavor not in Network.FLAVORS.keys():
        raise faults.BadRequest("Invalid network type '%s'" % flavor)

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
81
82
    utils.check_name_length(name, Network.NETWORK_NAME_LENGTH, "Network name "
                            "is too long")

83
    try:
84
        fmode, flink, fmac_prefix, ftags = util.values_from_flavor(flavor)
85
86
87
88
89
    except EmptyPool:
        log.error("Failed to allocate resources for network of type: %s",
                  flavor)
        msg = "Failed to allocate resources for network."
        raise faults.ServiceUnavailable(msg)
90
91
92
93
94
95

    mode = mode or fmode
    link = link or flink
    mac_prefix = mac_prefix or fmac_prefix
    tags = tags or ftags

96
97
    validate_mac(mac_prefix + "0:00:00:00")

98
99
100
101
102
103
    # Check that given link is unique!
    if (link is not None and flavor == "IP_LESS_ROUTED" and
       Network.objects.filter(deleted=False, mode=mode, link=link).exists()):
        msg = "Link '%s' is already used." % link
        raise faults.BadRequest(msg)

104
105
    network = Network.objects.create(
        name=name,
106
        userid=userid,
107
108
109
110
111
        flavor=flavor,
        mode=mode,
        link=link,
        mac_prefix=mac_prefix,
        tags=tags,
112
        public=public,
113
        external_router=public,
114
        floating_ip_pool=floating_ip_pool,
115
        action='CREATE',
116
117
        state='ACTIVE',
        drained=drained)
118

119
120
121
122
    if link is None:
        network.link = "%slink-%d" % (settings.BACKEND_PREFIX_ID, network.id)
        network.save()

123
124
125
    # Issue commission to Quotaholder and accept it since at the end of
    # this transaction the Network object will be created in the DB.
    # Note: the following call does a commit!
126
127
    if not public:
        quotas.issue_and_accept_commission(network)
128

129
130
131
    return network


132
133
134
135
136
137
138
139
140
141
def create_network_in_backends(network):
    job_ids = []
    for bend in Backend.objects.filter(offline=False):
        network.create_backend_network(bend)
        jobs = backend_mod.create_network(network=network, backend=bend,
                                          connect=True)
        job_ids.extend(jobs)
    return job_ids


142
143
144
145
146
147
148
149
150
@network_command("RENAME")
def rename(network, name):
    network.name = name
    network.save()
    return network


@network_command("DESTROY")
def delete(network):
151
    if network.nics.exists():
152
        raise faults.Conflict("Cannot delete network. There are ports still"
153
                              " configured on network network %s" % network.id)
154
    if network.ips.filter(deleted=False, floating_ip=True).exists():
155
        msg = "Cannot delete netowrk. Network has allocated floating IPs."
156
        raise faults.Conflict(msg)
157
158

    network.action = "DESTROY"
159
160
    # Mark network as drained to prevent automatic allocation of
    # public/floating IPs while the network is being deleted
161
162
    if network.public:
        network.drained = True
163
164
165
    network.save()

    # Delete network to all backends that exists
166
167
168
169
170
    for bnet in network.backend_networks.exclude(operstate="DELETED"):
        backend_mod.delete_network(network, bnet.backend)
    else:
        # If network does not exist in any backend, update the network state
        backend_mod.update_network_state(network)
171
    return network