Example #1
0
def add_network_segment(context,
                        network_id,
                        segment,
                        segment_index=0,
                        is_dynamic=False):
    with context.session.begin(subtransactions=True):
        record = segments_model.NetworkSegment(
            id=uuidutils.generate_uuid(),
            network_id=network_id,
            network_type=segment.get(NETWORK_TYPE),
            physical_network=segment.get(PHYSICAL_NETWORK),
            segmentation_id=segment.get(SEGMENTATION_ID),
            segment_index=segment_index,
            is_dynamic=is_dynamic)
        context.session.add(record)
        registry.notify(resources.SEGMENT,
                        events.PRECOMMIT_CREATE,
                        trigger=add_network_segment,
                        context=context,
                        segment=record)
        segment['id'] = record.id
    LOG.info(
        _LI("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
            })
Example #2
0
def create_dynamic_segment(network_id, segmentation_id, network_type,
                           physical_network):
    segment_id = str(uuid.uuid1())
    dynamic_segment = {
        'segmentation_id': segmentation_id,
        'id': segment_id,
        'network_type': network_type,
        'network_id': network_id,
        'physical_network': physical_network,
        'is_dynamic': True
    }
    session = db_api.get_writer_session()
    session.add(segment_models.NetworkSegment(**dynamic_segment))
    session.flush()
    return dynamic_segment
Example #3
0
    def _create_segment_db(self, context, segment_id, segment):
        with context.session.begin(subtransactions=True):
            network_id = segment['network_id']
            physical_network = segment[extension.PHYSICAL_NETWORK]
            if physical_network == constants.ATTR_NOT_SPECIFIED:
                physical_network = None
            network_type = segment[extension.NETWORK_TYPE]
            segmentation_id = segment[extension.SEGMENTATION_ID]
            if segmentation_id == constants.ATTR_NOT_SPECIFIED:
                segmentation_id = None
            name = segment['name']
            if name == constants.ATTR_NOT_SPECIFIED:
                name = None
            description = segment['description']
            if description == constants.ATTR_NOT_SPECIFIED:
                description = None
            args = {
                'id': segment_id,
                'network_id': network_id,
                'name': name,
                'description': description,
                db.PHYSICAL_NETWORK: physical_network,
                db.NETWORK_TYPE: network_type,
                db.SEGMENTATION_ID: segmentation_id
            }
            # Calculate the index of segment
            segment_index = 0
            segments = self.get_segments(context,
                                         filters={'network_id': [network_id]},
                                         fields=['segment_index'],
                                         sorts=[('segment_index', True)])
            if segments:
                # NOTE(xiaohhui): The new index is the last index + 1, this
                # may cause discontinuous segment_index. But segment_index
                # can functionally work as the order index for segments.
                segment_index = (segments[-1].get('segment_index') + 1)
            args['segment_index'] = segment_index

            new_segment = segment_model.NetworkSegment(**args)
            context.session.add(new_segment)
            # Do some preliminary operations before committing the segment to
            # db
            registry.notify(resources.SEGMENT,
                            events.PRECOMMIT_CREATE,
                            self,
                            context=context,
                            segment=new_segment)
            return new_segment
Example #4
0
def create_network(tenant_id,
                   net_id,
                   seg_id,
                   shared=False,
                   network_type='vlan',
                   physical_network='default'):
    session = db_api.get_writer_session()
    ndb = db_lib.NeutronNets()
    net_ctx = get_network_context(tenant_id,
                                  net_id,
                                  seg_id,
                                  shared=shared,
                                  network_type=network_type,
                                  physical_network=physical_network,
                                  session=session)
    ndb.create_network(net_ctx, {'network': net_ctx.current})
    for segment in net_ctx.network_segments:
        session.add(segment_models.NetworkSegment(**segment))
    session.flush()
    return net_ctx