def sync_network_states(network_vlan_ranges):
    """Synchronize network_states table with current configured VLAN ranges."""

    session = db.get_session()
    with session.begin():
        # get existing allocations for all physical networks
        allocations = dict()
        states = (session.query(l2network_models_v2.NetworkState).all())
        for state in states:
            if state.physical_network not in allocations:
                allocations[state.physical_network] = set()
            allocations[state.physical_network].add(state)

        # process vlan ranges for each configured physical network
        for physical_network, vlan_ranges in network_vlan_ranges.iteritems():
            # determine current configured allocatable vlans for this
            # physical network
            vlan_ids = set()
            for vlan_range in vlan_ranges:
                vlan_ids |= set(xrange(vlan_range[0], vlan_range[1] + 1))

            # remove from table unallocated vlans not currently allocatable
            if physical_network in allocations:
                for state in allocations[physical_network]:
                    try:
                        # see if vlan is allocatable
                        vlan_ids.remove(state.vlan_id)
                    except KeyError:
                        # it's not allocatable, so check if its allocated
                        if not state.allocated:
                            # it's not, so remove it from table
                            LOG.debug(
                                _("Removing vlan %(vlan_id)s on "
                                  "physical network %(physical_network)s"
                                  " from pool"), {
                                      'vlan_id': state.vlan_id,
                                      'physical_network': physical_network
                                  })
                            session.delete(state)
                del allocations[physical_network]

            # add missing allocatable vlans to table
            for vlan_id in sorted(vlan_ids):
                state = l2network_models_v2.NetworkState(
                    physical_network, vlan_id)
                session.add(state)

        # remove from table unallocated vlans for any unconfigured physical
        # networks
        for states in allocations.itervalues():
            for state in states:
                if not state.allocated:
                    LOG.debug(
                        _("Removing vlan %(vlan_id)s on physical "
                          "network %(physical_network)s"
                          " from pool"), {
                              'vlan_id': state.vlan_id,
                              'physical_network': physical_network
                          })
                    session.delete(state)
Example #2
0
def reserve_specific_network(session, physical_network, vlan_id):
    with session.begin(subtransactions=True):
        try:
            state = (session.query(l2network_models_v2.NetworkState).
                     filter_by(physical_network=physical_network,
                               vlan_id=vlan_id).
                     with_lockmode('update').
                     one())
            if state.allocated:
                if vlan_id == constants.FLAT_VLAN_ID:
                    raise q_exc.FlatNetworkInUse(
                        physical_network=physical_network)
                else:
                    raise q_exc.VlanIdInUse(vlan_id=vlan_id,
                                            physical_network=physical_network)
            LOG.debug(_("Reserving specific vlan %(vlan_id)s on physical "
                        "network %(physical_network)s from pool"),
                      {'vlan_id': vlan_id,
                       'physical_network': physical_network})
            state.allocated = True
        except exc.NoResultFound:
            LOG.debug(_("Reserving specific vlan %(vlan_id)s on physical "
                        "network %(physical_network)s outside pool"),
                      {'vlan_id': vlan_id,
                       'physical_network': physical_network})
            state = l2network_models_v2.NetworkState(physical_network, vlan_id)
            state.allocated = True
            session.add(state)
Example #3
0
def reserve_specific_network(session, physical_network, vlan_id):
    with session.begin(subtransactions=True):
        try:
            state = (session.query(l2network_models_v2.NetworkState).filter_by(
                physical_network=physical_network, vlan_id=vlan_id).one())
            if state.allocated:
                raise q_exc.VlanIdInUse(vlan_id=vlan_id,
                                        physical_network=physical_network)
            LOG.debug("reserving specific vlan %s on physical network %s "
                      "from pool" % (vlan_id, physical_network))
            state.allocated = True
        except exc.NoResultFound:
            LOG.debug("reserving specific vlan %s on physical network %s "
                      "outside pool" % (vlan_id, physical_network))
            state = l2network_models_v2.NetworkState(physical_network, vlan_id)
            state.allocated = True
            session.add(state)
Example #4
0
def sync_network_states(network_vlan_ranges):
    """Synchronize network_states table with current configured VLAN ranges."""

    # process vlan ranges for each physical network separately
    for physical_network, vlan_ranges in network_vlan_ranges.iteritems():

        # determine current configured allocatable vlans for this
        # physical network
        vlan_ids = set()
        for vlan_range in vlan_ranges:
            vlan_ids |= set(xrange(vlan_range[0], vlan_range[1] + 1))

        session = db.get_session()
        with session.begin():
            # remove from table unallocated vlans not currently allocatable
            try:
                states = (session.query(
                    l2network_models_v2.NetworkState).filter_by(
                        physical_network=physical_network).all())
                for state in states:
                    try:
                        # see if vlan is allocatable
                        vlan_ids.remove(state.vlan_id)
                    except KeyError:
                        # it's not allocatable, so check if its allocated
                        if not state.allocated:
                            # it's not, so remove it from table
                            LOG.debug("removing vlan %s on physical network "
                                      "%s from pool" %
                                      (state.vlan_id, physical_network))
                            session.delete(state)
            except exc.NoResultFound:
                pass

            # add missing allocatable vlans to table
            for vlan_id in sorted(vlan_ids):
                state = l2network_models_v2.NetworkState(
                    physical_network, vlan_id)
                session.add(state)