Statistics
| Branch: | Tag: | Revision:

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

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
    quotas.issue_and_accept_commission(network)
121
    return network
122

    
123

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

    
130

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

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

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