Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / networks.py @ 1fb3293d

History | View | Annotate | Download (6.2 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

    
37
from snf_django.lib.api import faults
38
from synnefo.api import util
39
from synnefo import quotas
40
from synnefo.db.models import Network, Backend
41
from synnefo.db.utils import validate_mac
42
from synnefo.db.pools import EmptyPool
43
from synnefo.logic import backend as backend_mod, subnets
44

    
45
from logging import getLogger
46
log = getLogger(__name__)
47

    
48

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

    
53

    
54
def network_command(action):
55
    def decorator(func):
56
        @wraps(func)
57
        @transaction.commit_on_success()
58
        def wrapper(network, *args, **kwargs):
59
            validate_network_action(network, action)
60
            return func(network, *args, **kwargs)
61
        return wrapper
62
    return decorator
63

    
64

    
65
@transaction.commit_on_success
66
def create(userid, name, flavor, subnet=None, gateway=None, subnet6=None,
67
           gateway6=None, public=False, dhcp=True, link=None, mac_prefix=None,
68
           mode=None, floating_ip_pool=False, tags=None, backends=None,
69
           lazy_create=True):
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("Can not override MAC_FILTERED mac-prefix")
77
    if link is not None and flavor == "PHYSICAL_VLAN":
78
        raise faults.BadRequest("Can not override PHYSICAL_VLAN link")
79

    
80
    if subnet is None and floating_ip_pool:
81
        raise faults.BadRequest("IPv6 only networks can not be floating"
82
                                " pools.")
83
    # Check that network parameters are valid
84
    subnets.validate_subnet_params(subnet, gateway, subnet6, gateway6)
85

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

    
94
    mode = mode or fmode
95
    link = link or flink
96
    mac_prefix = mac_prefix or fmac_prefix
97
    tags = tags or ftags
98

    
99
    if (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
    validate_mac(mac_prefix + "0:00:00:00")
105

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

    
120
    if subnet:
121
        subnets._create_subnet(network.id, cidr=subnet, name="", ipversion=4,
122
                               gateway=gateway, dhcp=dhcp, user_id=userid)
123
    if subnet6:
124
        subnets._create_subnet(network.id, cidr=subnet6, name="", ipversion=6,
125
                               gateway=gateway6, dhcp=dhcp, user_id=userid)
126

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

    
133
    if not lazy_create:
134
        if floating_ip_pool:
135
            backends = Backend.objects.filter(offline=False)
136
        elif backends is None:
137
            backends = []
138

    
139
        for bend in backends:
140
            network.create_backend_network(bend)
141
            backend_mod.create_network(network=network, backend=bend,
142
                                       connect=True)
143
    return network
144

    
145

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

    
152

    
153
@network_command("DESTROY")
154
def delete(network):
155
    if network.machines.exists():
156
        raise faults.Conflict("Can not delete network. Servers connected"
157
                              " to this network exists.")
158
    if network.ips.filter(deleted=False, floating_ip=True).exists():
159
        msg = "Can not delete netowrk. Network has allocated floating IPs."
160
        raise faults.Conflict(msg)
161

    
162
    network.action = "DESTROY"
163
    network.save()
164

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