Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / networks.py @ 6e9255ab

History | View | Annotate | Download (5.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
from functools import wraps
34
from django.db import transaction
35

    
36
from snf_django.lib.api import faults
37
from synnefo.api import util
38
from synnefo import quotas
39
from synnefo.db.models import Network
40
from synnefo.db.utils import validate_mac
41
from synnefo.db.pools import EmptyPool
42
from synnefo.logic import backend
43

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

    
47

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

    
52

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

    
63

    
64
@transaction.commit_on_success
65
def create(user_id, name, flavor, subnet=None, gateway=None, subnet6=None,
66
           gateway6=None, public=False, dhcp=True, link=None, mac_prefix=None,
67
           mode=None, floating_ip_pool=False, tags=None):
68
    if flavor is None:
69
        raise faults.BadRequest("Missing request parameter 'type'")
70
    elif flavor not in Network.FLAVORS.keys():
71
        raise faults.BadRequest("Invalid network type '%s'" % flavor)
72

    
73
    if mac_prefix is not None and flavor == "MAC_FILTERED":
74
        raise faults.BadRequest("Can not override MAC_FILTERED mac-prefix")
75
    if link is not None and flavor == "PHYSICAL_VLAN":
76
        raise faults.BadRequest("Can not override PHYSICAL_VLAN link")
77

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

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

    
92
    mode = mode or fmode
93
    link = link or flink
94
    mac_prefix = mac_prefix or fmac_prefix
95
    tags = tags or ftags
96

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

    
99
    network = Network.objects.create(
100
        name=name,
101
        userid=user_id,
102
        subnet=subnet,
103
        subnet6=subnet6,
104
        gateway=gateway,
105
        gateway6=gateway6,
106
        dhcp=dhcp,
107
        flavor=flavor,
108
        mode=mode,
109
        link=link,
110
        mac_prefix=mac_prefix,
111
        tags=tags,
112
        public=public,
113
        floating_ip_pool=floating_ip_pool,
114
        action='CREATE',
115
        state='ACTIVE')
116

    
117
    # Issue commission to Quotaholder and accept it since at the end of
118
    # this transaction the Network object will be created in the DB.
119
    # Note: the following call does a commit!
120
    if not public:
121
        quotas.issue_and_accept_commission(network)
122
    return network
123

    
124

    
125
@network_command("RENAME")
126
def rename(network, name):
127
    network.name = name
128
    network.save()
129
    return network
130

    
131

    
132
@network_command("DESTROY")
133
def delete(network):
134
    if network.machines.exists():
135
        raise faults.NetworkInUse("Can not delete network. Servers connected"
136
                                  " to this network exists.")
137
    if network.floating_ips.filter(deleted=False).exists():
138
        msg = "Can not delete netowrk. Network has allocated floating IPs."
139
        raise faults.NetworkInUse(msg)
140

    
141
    network.action = "DESTROY"
142
    network.save()
143

    
144
    # Delete network to all backends that exists
145
    backend_networks = network.backend_networks.exclude(operstate="DELETED")
146
    for bnet in backend_networks:
147
        backend.delete_network(network, bnet.backend)
148
    # If network does not exist in any backend, update the network state
149
    if not backend_networks:
150
        backend.update_network_state(network)
151
    return network