Revision af6a3bc5

b/snf-cyclades-app/synnefo/api/management/commands/network-create.py
36 36
from django.core.management.base import BaseCommand, CommandError
37 37

  
38 38
from synnefo.db.models import Network, Backend
39
from synnefo.api.util import network_link_from_type, validate_network_size
39
from synnefo.api.util import net_resources, validate_network_size
40 40
from synnefo.logic.backend import create_network
41 41
from synnefo import settings
42 42

  
......
105 105

  
106 106
        name = options['name']
107 107
        subnet = options['subnet']
108
        typ = options['type']
108
        net_type = options['type']
109 109
        backend_id = options['backend_id']
110 110
        public = options['public']
111 111

  
......
115 115
            raise CommandError("Subnet is required")
116 116
        if public and not backend_id:
117 117
            raise CommandError("backend-id is required")
118
        if public and not typ=='PUBLIC_ROUTED':
118
        if public and not net_type == 'PUBLIC_ROUTED':
119 119
            raise CommandError("Invalid type for public network")
120 120
        if backend_id and not public:
121 121
            raise CommandError("Private networks must be created to"
......
130 130
            except Backend.DoesNotExist:
131 131
                raise CommandError("Backend not found in DB")
132 132

  
133
        link = network_link_from_type(typ)
133
        link, mac_prefix = net_resources(net_type)
134 134

  
135 135
        subnet, gateway, subnet6, gateway6 = validate_network_info(options)
136 136

  
......
143 143
                subnet=subnet,
144 144
                gateway=gateway,
145 145
                dhcp=options['dhcp'],
146
                type=options['type'],
146
                type=net_type,
147 147
                public=public,
148 148
                link=link,
149
                mac_prefix=mac_prefix,
149 150
                gateway6=gateway6,
150 151
                subnet6=subnet6,
151 152
                state='PENDING')
b/snf-cyclades-app/synnefo/api/networks.py
165 165
        subnet6 = d.get('cidr6', None)
166 166
        gateway = d.get('gateway', None)
167 167
        gateway6 = d.get('gateway6', None)
168
        typ = d.get('type', 'PRIVATE_MAC_FILTERED')
168
        net_type = d.get('type', 'PRIVATE_MAC_FILTERED')
169 169
        dhcp = d.get('dhcp', True)
170 170
    except (KeyError, ValueError):
171 171
        raise BadRequest('Malformed request.')
172 172

  
173
    if typ == 'PUBLIC_ROUTED':
173
    if net_type == 'PUBLIC_ROUTED':
174 174
        raise Unauthorized('Can not create a public network.')
175 175

  
176 176
    user_networks = len(Network.objects.filter(userid=request.user_uniq,
......
183 183
        raise OverLimit("Unsupported network size.")
184 184

  
185 185
    try:
186
        link = util.network_link_from_type(typ)
186
        link, mac_prefix = util.net_resources(net_type)
187 187
        if not link:
188 188
            raise Exception("Can not create network. No connectivity link.")
189 189

  
......
195 195
                gateway=gateway,
196 196
                gateway6=gateway6,
197 197
                dhcp=dhcp,
198
                type=typ,
198
                type=net_type,
199 199
                link=link,
200
                mac_prefix=mac_prefix,
200 201
                action='CREATE',
201 202
                state='PENDING')
202 203
    except EmptyPool:
b/snf-cyclades-app/synnefo/api/util.py
59 59
                                BadMediaType)
60 60
from synnefo.db.models import (Flavor, VirtualMachine, VirtualMachineMetadata,
61 61
                               Network, BackendNetwork, NetworkInterface,
62
                               BridgePoolTable)
62
                               BridgePoolTable, MacPrefixPoolTable)
63 63

  
64 64
from synnefo.lib.astakos import get_user
65 65
from synnefo.plankton.backend import ImageBackend
......
395 395
    return "-".join(["nic", unicode(nic.machine.id), unicode(nic.index)])
396 396

  
397 397

  
398
def network_link_from_type(network_type):
399
    if network_type == 'PRIVATE_MAC_FILTERED':
398
def net_resources(net_type):
399
    mac_prefix = settings.MAC_POOL_BASE
400
    if net_type == 'PRIVATE_MAC_FILTERED':
400 401
        link = settings.PRIVATE_MAC_FILTERED_BRIDGE
401
    elif network_type == 'PRIVATE_PHYSICAL_VLAN':
402
        mac_pool = MacPrefixPoolTable.get_pool()
403
        mac_prefix = mac_pool.get()
404
        mac_pool.save()
405
    elif net_type == 'PRIVATE_PHYSICAL_VLAN':
402 406
        pool = BridgePoolTable.get_pool()
403 407
        link = pool.get()
404 408
        pool.save()
405
    elif network_type == 'CUSTOM_ROUTED':
409
    elif net_type == 'CUSTOM_ROUTED':
406 410
        link = settings.CUSTOM_ROUTED_ROUTING_TABLE
407
    elif network_type == 'CUSTOM_BRIDGED':
411
    elif net_type == 'CUSTOM_BRIDGED':
408 412
        link = settings.CUSTOM_BRIDGED_BRIDGE
409
    elif network_type == 'PUBLIC_ROUTED':
413
    elif net_type == 'PUBLIC_ROUTED':
410 414
        link = settings.PUBLIC_ROUTED_ROUTING_TABLE
411 415
    else:
412
        raise BadRequest('Unknown network network_type')
416
        raise BadRequest('Unknown network type')
413 417

  
414
    return link
418
    return link, mac_prefix
b/snf-cyclades-app/synnefo/db/models.py
548 548

  
549 549
        self.save()
550 550

  
551
    def __init__(self, *args, **kwargs):
552
        super(Network, self).__init__(*args, **kwargs)
553
        if not self.mac_prefix:
554
            # Allocate a MAC prefix for just created Network instances
555
            mac_pool = MacPrefixPoolTable.get_pool()
556
            mac_prefix = mac_pool.get()
557
            mac_pool.save()
558
            self.mac_prefix = mac_prefix
559

  
560 551
    def create_backend_network(self, backend=None):
561 552
        """Create corresponding BackendNetwork entries."""
562 553

  
b/snf-cyclades-app/synnefo/logic/management/commands/reconcile-pools.py
31 31

  
32 32
from synnefo.db.models import (Network, BackendNetwork,
33 33
                               BridgePoolTable, MacPrefixPoolTable)
34
from synnefo.settings import MAC_POOL_BASE
34 35

  
35 36

  
36 37
class Command(BaseCommand):
......
89 90
        for i in xrange(0, macp_pool.size()):
90 91
            if not macp_pool.is_available(i, index=True) and \
91 92
               not macp_pool.is_reserved(i, index=True):
92
                macs.append(macp_pool.index_to_value(i))
93
                value = macp_pool.index_to_value(i)
94
                if value != MAC_POOL_BASE:
95
                    macs.append(macp_pool.index_to_value(i))
93 96

  
94 97
        write("Used MAC prefixes from Pool: %d\n" % len(macs))
95 98

  
96
        network_mac_prefixes = Network.objects.filter(deleted=False)\
97
                                         .values_list('mac_prefix', flat=True)
99
        network_mac_prefixes = \
100
            Network.objects.filter(deleted=False)\
101
                            .exclude(mac_prefix=MAC_POOL_BASE) \
102
                            .values_list('mac_prefix', flat=True)
98 103
        write("Used MAC prefixes from Networks: %d\n" %
99 104
                          len(network_mac_prefixes))
100 105

  
......
121 126

  
122 127
        mac_prefixes = BackendNetwork.objects.filter(deleted=False)\
123 128
                                      .values_list('mac_prefix', flat=True)
124

  
129
        mac_prefixes = filter(lambda x: not x.startswith(MAC_POOL_BASE),
130
                              mac_prefixes)
125 131
        set_mac_prefixes = set(mac_prefixes)
126 132
        if len(mac_prefixes) > len(set_mac_prefixes):
127 133
            write("Found duplicated mac_prefixes:\n")

Also available in: Unified diff