Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.6 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, gateway=None, subnet6=None,
66
           gateway6=None, public=False, dhcp=True):
67
    if flavor is None:
68
        raise faults.BadRequest("Missing request parameter 'type'")
69
    elif flavor not in Network.FLAVORS.keys():
70
        raise faults.BadRequest("Invalid network type '%s'" % flavor)
71

    
72
    # Check that network parameters are valid
73
    util.validate_network_params(subnet, gateway, subnet6, gateway6)
74

    
75
    try:
76
        mode, link, mac_prefix, tags = util.values_from_flavor(flavor)
77
    except EmptyPool:
78
        log.error("Failed to allocate resources for network of type: %s",
79
                  flavor)
80
        msg = "Failed to allocate resources for network."
81
        raise faults.ServiceUnavailable(msg)
82
    validate_mac(mac_prefix + "0:00:00:00")
83

    
84
    network = Network.objects.create(
85
        name=name,
86
        userid=user_id,
87
        subnet=subnet,
88
        subnet6=subnet6,
89
        gateway=gateway,
90
        gateway6=gateway6,
91
        dhcp=dhcp,
92
        flavor=flavor,
93
        mode=mode,
94
        link=link,
95
        mac_prefix=mac_prefix,
96
        tags=tags,
97
        action='CREATE',
98
        state='ACTIVE')
99

    
100
    # Issue commission to Quotaholder and accept it since at the end of
101
    # this transaction the Network object will be created in the DB.
102
    # Note: the following call does a commit!
103
    quotas.issue_and_accept_commission(network)
104
    return network
105

    
106

    
107
@network_command("RENAME")
108
def rename(network, name):
109
    network.name = name
110
    network.save()
111
    return network
112

    
113

    
114
@network_command("DESTROY")
115
def delete(network):
116
    if network.machines.exists():
117
        raise faults.NetworkInUse("Can not delete network. Servers connected"
118
                                  " to this network exists.")
119
    if network.floating_ips.filter(deleted=False).exists():
120
        msg = "Can not delete netowrk. Network has allocated floating IPs."
121
        raise faults.NetworkInUse(msg)
122

    
123
    network.action = "DESTROY"
124
    network.save()
125

    
126
    # Delete network to all backends that exists
127
    backend_networks = network.backend_networks.exclude(operstate="DELETED")
128
    for bnet in backend_networks:
129
        backend.delete_network(network, bnet.backend)
130
    # If network does not exist in any backend, update the network state
131
    if not backend_networks:
132
        backend.update_network_state(network)
133
    return network