def add_network_segment(context, network_id, segment, segment_index=0, is_dynamic=False): with db_api.CONTEXT_WRITER.using(context): netseg_obj = network_obj.NetworkSegment( context, 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) netseg_obj.create() registry.publish(resources.SEGMENT, events.PRECOMMIT_CREATE, add_network_segment, payload=events.DBEventPayload( context, resource_id=netseg_obj.id, states=(netseg_obj, ))) segment['id'] = netseg_obj.id LOG.info( "Added segment %(id)s of type %(network_type)s for network " "%(network_id)s", { 'id': netseg_obj.id, 'network_type': netseg_obj.network_type, 'network_id': netseg_obj.network_id })
def test_v1_2_to_v1_1_drops_segment_id_in_binding_levels(self): port_new = self._create_test_port() segment = network.NetworkSegment( self.context, # TODO(ihrachys) we should be able to create a segment object # without explicitly specifying id, but it's currently not working id=uuidutils.generate_uuid(), network_id=port_new.network_id, network_type='vxlan') segment.create() # TODO(ihrachys) we should be able to create / update level objects via # Port object, but it's currently not working binding = ports.PortBindingLevel(self.context, port_id=port_new.id, host='host1', level=0, segment_id=segment.id) binding.create() port_new = ports.Port.get_object(self.context, id=port_new.id) port_v1_1 = port_new.obj_to_primitive(target_version='1.1') lvl = port_v1_1['versioned_object.data']['binding_levels'][0] self.assertNotIn('segment_id', lvl['versioned_object.data']) # check that we also downgraded level object version self.assertEqual('1.0', lvl['versioned_object.version']) # finally, prove that binding primitive is now identical to direct # downgrade of the binding object binding_v1_0 = binding.obj_to_primitive(target_version='1.0') self.assertEqual(binding_v1_0, lvl)
def setUp(self): super(TestCacheBackedPluginApi, self).setUp() self._api = rpc.CacheBackedPluginApi(lib_topics.PLUGIN) self._api._legacy_interface = mock.Mock() self._api.remote_resource_cache = mock.Mock() self._network_id = uuidutils.generate_uuid() self._segment_id = uuidutils.generate_uuid() self._segment = network.NetworkSegment( id=self._segment_id, network_id=self._network_id, network_type=constants.TYPE_FLAT) self._port_id = uuidutils.generate_uuid() self._network = network.Network(id=self._network_id, segments=[self._segment]) self._port = ports.Port( id=self._port_id, network_id=self._network_id, mac_address=netaddr.EUI('fa:16:3e:ec:c7:d9'), admin_state_up=True, security_group_ids=set([uuidutils.generate_uuid()]), fixed_ips=[], allowed_address_pairs=[], device_owner=constants.DEVICE_OWNER_COMPUTE_PREFIX, bindings=[ ports.PortBinding(port_id=self._port_id, host='host1', status=constants.ACTIVE, profile={}) ], binding_levels=[ ports.PortBindingLevel(port_id=self._port_id, host='host1', level=0, segment=self._segment) ])
def add_network_segment(context, network_id, segment, segment_index=0, is_dynamic=False): with db_api.context_manager.writer.using(context): netseg_obj = network_obj.NetworkSegment( context, 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) netseg_obj.create() registry.notify(resources.SEGMENT, events.PRECOMMIT_CREATE, trigger=add_network_segment, context=context, segment=netseg_obj) segment['id'] = netseg_obj.id LOG.info( "Added segment %(id)s of type %(network_type)s for network " "%(network_id)s", { 'id': netseg_obj.id, 'network_type': netseg_obj.network_type, 'network_id': netseg_obj.network_id })
def _create_segment_db(self, context, segment_id, segment): with db_api.CONTEXT_WRITER.using(context): 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 = network.NetworkSegment(context, **args) new_segment.create() # Do some preliminary operations before committing the segment to # db registry.publish(resources.SEGMENT, events.PRECOMMIT_CREATE, self, payload=events.DBEventPayload( context, resource_id=segment_id, states=(new_segment, ))) # The new segment might have been updated by the callbacks # subscribed to the PRECOMMIT_CREATE event. So update it in the DB new_segment.update() return new_segment
def _create_segment(self, segmentation_id=None, network_id=None, physical_network=None, network_type=None): attr = self.get_random_object_fields(net_obj.NetworkSegment) attr.update({ 'network_id': network_id or self._create_test_network_id(), 'network_type': network_type or constants.TYPE_VLAN, 'physical_network': physical_network or 'foo', 'segmentation_id': segmentation_id or random.randint( constants.MIN_VLAN_TAG, constants.MAX_VLAN_TAG)}) _segment = net_obj.NetworkSegment(self.context, **attr) _segment.create() return _segment
def _create_segment_db(self, context, segment_id, segment): with db_api.context_manager.writer.using(context): 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 = network.NetworkSegment(context, **args) new_segment.create() # 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
def test_validate_min_bw_rule_vs_physnet_non_physnet(self): scenarios = [ ({'physical_network': 'fake physnet'}, self.assertTrue), ({}, self.assertFalse), ] for segment_kwargs, test_method in scenarios: segment = network_object.NetworkSegment(**segment_kwargs) net = network_object.Network(mock.Mock(), segments=[segment]) rule = mock.Mock() rule.rule_type = qos_consts.RULE_TYPE_MINIMUM_BANDWIDTH port = mock.Mock() with mock.patch( 'neutron.objects.network.Network.get_object', return_value=net): test_method(self.driver.validate_rule_for_port( mock.Mock(), rule, port))
def _create_segment(self, context, network_id, physical_network, network_type, segmentation_id, segment_index, host, is_dynamic): with db_api.context_manager.writer.using(context): netseg_obj = network_obj.NetworkSegment( context, id=uuidutils.generate_uuid(), network_id=network_id, network_type=network_type, physical_network=physical_network, segmentation_id=segmentation_id, hosts=[host], segment_index=segment_index, is_dynamic=is_dynamic) netseg_obj.create() return self._make_segment_dict(netseg_obj)