Esempio n. 1
0
    def _allocate_vlan_segment(self, network, host_id, level, host_config):

        segment_type = host_config.get('segment_type', 'vlan')
        segment_physnet = host_config.get('physical_network', None)

        session = db_api.get_session()
        segmentation_id = self._get_provider_attribute(
            network, "provider:segmentation_id")
        network_id = network["id"]
        segment = session.query(ml2_models.NetworkSegment).filter_by(
            segmentation_id=segmentation_id,
            physical_network=segment_physnet,
            network_type=segment_type,
            network_id=network_id,
            level=level).first()

        if not segment:
            with session.begin(subtransactions=True):
                segment = ml2_models.NetworkSegment(
                    id=uuidutils.generate_uuid(),
                    network_id=network_id,
                    network_type=segment_type,
                    physical_network=segment_physnet,
                    segmentation_id=segmentation_id,
                    segment_index=level,
                    is_dynamic=False)
                session.add(segment)

        return AllocationsModel(host=host_id,
                                level=level,
                                segment_type=segment_type,
                                segmentation_id=segmentation_id,
                                segment_id=segment.id,
                                network_id=network_id)
Esempio n. 2
0
def add_network_segment(session, network_id, segment):
    with session.begin(subtransactions=True):
        record = models.NetworkSegment(
            id=uuidutils.generate_uuid(),
            network_id=network_id,
            network_type=segment.get(api.NETWORK_TYPE),
            physical_network=segment.get(api.PHYSICAL_NETWORK),
            segmentation_id=segment.get(api.SEGMENTATION_ID)
        )
        session.add(record)
    LOG.info(_("Added segment %(id)s of type %(network_type)s for network"
               " %(network_id)s"),
             {'id': record.id,
              'network_type': record.network_type,
              'network_id': record.network_id})
Esempio n. 3
0
    def _allocate_vxlan_segment(self, network, host_id, level, host_config):
        LOG.info(_LI("Allocating segment for network type VXLAN"))
        segment_type = host_config.get('segment_type', 'vlan')
        segment_physnet = host_config.get('physical_network', None)

        session = db_api.get_session()
        network_id = network['id']

        with session.begin(subtransactions=True):

            LOG.debug(
                "Searching for available allocation for host id %(host_id)s "
                "segment_type %(segment_type)s network_id %(network_id)s segment_physnet %(segment_physnet)s",
                {
                    "host_id": host_id,
                    "segment_type": segment_type,
                    "segment_physnet": segment_physnet,
                    "network_id": network_id
                })

            alloc = session.query(AllocationsModel).filter_by(
                host=host_id,
                level=level,
                segment_type=segment_type,
                network_id=network_id).first()

            if alloc:
                # check if segment is existing, if not the allocation should be deleted and a new one allocated
                segment = (session.query(ml2_models.NetworkSegment).filter_by(
                    id=alloc.segment_id).first())
                if (segment):
                    return alloc
                else:
                    # TODO : handle case when allocation is on a segment no longer configured for use
                    # Or we can leave until 'natural' cleanup happens
                    alloc.update({"network_id": None, "segment_id": None})

            select = (session.query(AllocationsModel).filter_by(
                host=host_id,
                level=level,
                segment_type=segment_type,
                network_id=None))

            # Selected segment can be allocated before update by someone else,
            allocs = select.limit(100).all()

            if not allocs:
                LOG.error("No Allocation available")
                # No resource available
                return

            alloc = random.choice(allocs)

            segment = ml2_models.NetworkSegment(
                id=uuidutils.generate_uuid(),
                network_id=network_id,
                network_type=alloc.segment_type,
                physical_network=segment_physnet,
                segmentation_id=alloc.segmentation_id,
                segment_index=level,
                is_dynamic=False)
            session.add(segment)

            raw_segment = dict((k, alloc[k]) for k in self.primary_keys)
            LOG.debug(
                "%(type)s segment allocated from pool "
                "with %(segment)s ", {
                    "type": alloc.segment_type,
                    "segment": alloc.segmentation_id
                })

            count = (session.query(AllocationsModel).filter_by(
                network_id=None, **raw_segment).update({
                    "network_id": network_id,
                    'segment_id': segment.id
                }))

            if count:
                LOG.debug(
                    "%(type)s segment allocated from pool "
                    "success with %(segment)s ", {
                        "type": alloc.segment_type,
                        "segment": alloc.segment_id
                    })
                return alloc

            # Segment allocated since select
            LOG.debug(
                "Allocate %(type)s segment from pool "
                "failed with segment %(segment)s", {
                    "type": alloc.segment_type,
                    "segment": alloc.segment_id,
                    "level": alloc.level
                })

            session.commit()