def test_ensure_network_profiles_created(self): # Ensure that both network profiles are created profile = n1kv_db.get_network_profile_by_type(p_const.TYPE_VLAN) self.assertEqual(p_const.TYPE_VLAN, profile.segment_type) profile = n1kv_db.get_network_profile_by_type(p_const.TYPE_VXLAN) self.assertEqual(p_const.TYPE_VXLAN, profile.segment_type) # Ensure no additional profiles are created (get by type returns one()) mech = mech_cisco_n1kv.N1KVMechanismDriver() mech._ensure_network_profiles_created_on_vsm() profile = n1kv_db.get_network_profile_by_type(p_const.TYPE_VLAN) self.assertEqual(p_const.TYPE_VLAN, profile.segment_type) profile = n1kv_db.get_network_profile_by_type(p_const.TYPE_VXLAN) self.assertEqual(p_const.TYPE_VXLAN, profile.segment_type)
def test_create_network_with_default_n1kv_network_profile_id(self): """Test network create without passing network profile id.""" with self.network() as network: np = n1kv_db.get_network_profile_by_type(p_const.TYPE_VLAN) net_np = n1kv_db.get_network_binding(network['network']['id']) self.assertEqual(network['network']['id'], net_np['network_id']) self.assertEqual(net_np['profile_id'], np['id'])
def create_network_postcommit(self, context): """Send network parameters to the VSM.""" network = context.current segment = context.network_segments[0] network_type = segment['network_type'] # NoOp for unsupported network types if not self._is_segment_valid_for_n1kv(segment['segmentation_id'], network_type): return session = context._plugin_context.session netp = n1kv_db.get_network_profile_by_type(network_type, session) try: self.n1kvclient.create_network_segment(network, netp) except (n1kv_exc.VSMError, n1kv_exc.VSMConnectionFailed) as e: with excutils.save_and_reraise_exception(reraise=False): LOG.info(e.message) raise ml2_exc.MechanismDriverError() LOG.info( _LI("Create network(postcommit) succeeded for network: " "%(network_id)s of type: %(network_type)s with segment " "id: %(segment_id)s"), { "network_id": network['id'], "network_type": network_type, "segment_id": segment['segmentation_id'] })
def test_remove_network_profile(self): _db_profile = n1kv_db.add_network_profile(TEST_NETWORK_PROFILE['name'], p_const.TYPE_VLAN) db_profile = n1kv_db.get_network_profile_by_type(p_const.TYPE_VLAN) self.assertIsNotNone(db_profile) self.assertEqual(_db_profile.id, db_profile.id) n1kv_db.remove_network_profile(_db_profile.id) self.assertRaises(c_exc.NetworkProfileNotFound, n1kv_db.get_network_profile_by_type, p_const.TYPE_VLAN)
def test_get_network_profiles_by_type(self): test_profiles = [{'name': 'test_profile1', 'segment_type': p_const.TYPE_VLAN}, {'name': 'test_profile2', 'segment_type': p_const.TYPE_VXLAN}] for p in test_profiles: n1kv_db.add_network_profile(p['name'], p['segment_type']) profile = n1kv_db.get_network_profile_by_type(p_const.TYPE_VLAN) self.assertEqual(test_profiles[0]['name'], profile['name']) self.assertEqual(test_profiles[0]['segment_type'], profile['segment_type'])
def test_remove_network_profile(self): _db_profile = n1kv_db.add_network_profile(TEST_NETWORK_PROFILE['name'], p_const.TYPE_VLAN) db_profile = n1kv_db.get_network_profile_by_type( p_const.TYPE_VLAN) self.assertIsNotNone(db_profile) self.assertEqual(_db_profile.id, db_profile.id) n1kv_db.remove_network_profile(_db_profile.id) self.assertRaises(c_exc.NetworkProfileNotFound, n1kv_db.get_network_profile_by_type, p_const.TYPE_VLAN)
def test_get_network_profiles_by_type(self): test_profiles = [{ 'name': 'test_profile1', 'segment_type': p_const.TYPE_VLAN }, { 'name': 'test_profile2', 'segment_type': p_const.TYPE_VXLAN }] for p in test_profiles: n1kv_db.add_network_profile(p['name'], p['segment_type']) profile = n1kv_db.get_network_profile_by_type(p_const.TYPE_VLAN) self.assertEqual(test_profiles[0]['name'], profile['name']) self.assertEqual(test_profiles[0]['segment_type'], profile['segment_type'])
def _ensure_network_profiles_created_on_vsm(self, db_session=None): # Try to create logical networks and network profiles on the VSM if # they don't exist already. for netp_type in [p_const.TYPE_VLAN, p_const.TYPE_VXLAN]: try: netp = n1kv_db.get_network_profile_by_type(netp_type) except n1kv_exc.NetworkProfileNotFound: # Create a network profile in Neutron DB netp = n1kv_db.add_network_profile(self.netp_name[netp_type], netp_type, db_session) try: # Create a network profile of type VLAN on the VSM self.n1kvclient.create_network_segment_pool(netp) # Catch any exception here and cleanup if so except (n1kv_exc.VSMConnectionFailed, n1kv_exc.VSMError): with excutils.save_and_reraise_exception(): n1kv_db.remove_network_profile(netp.id, db_session)
def create_network_postcommit(self, context): """Send network parameters to the VSM.""" network = context.current segment = context.network_segments[0] network_type = segment['network_type'] # NoOp for unsupported network types if not self._is_segment_valid_for_n1kv(segment['segmentation_id'], network_type): return session = context._plugin_context.session netp = n1kv_db.get_network_profile_by_type(network_type, session) try: self.n1kvclient.create_network_segment(network, netp) except(n1kv_exc.VSMError, n1kv_exc.VSMConnectionFailed): raise ml2_exc.MechanismDriverError() LOG.info(_LI("Create network(postcommit) succeeded for network: " "%(network_id)s of type: %(network_type)s with segment " "id: %(segment_id)s"), {"network_id": network['id'], "network_type": network_type, "segment_id": segment['segmentation_id']})
def create_network_precommit(self, context): """Update network binding information.""" network = context.current segment = context.network_segments[0] network_type = segment['network_type'] session = context._plugin_context.session # NoOp for unsupported network types if not self._is_segment_valid_for_n1kv(segment['segmentation_id'], network_type): return # Retrieve the network profile for network binding try: netp = n1kv_db.get_network_profile_by_type(network_type, session) except n1kv_exc.NetworkProfileNotFound: raise ml2_exc.MechanismDriverError() kwargs = {"network_id": network['id'], "network_type": network_type, "db_session": session, "segment_id": segment['segmentation_id'], "netp_id": netp['id']} n1kv_db.add_network_binding(**kwargs)
def create_network_precommit(self, context): """Update network binding information.""" network = context.current segment = context.network_segments[0] network_type = segment['network_type'] session = context._plugin_context.session # NoOp for unsupported network types if not self._is_segment_valid_for_n1kv(segment['segmentation_id'], network_type): return # Retrieve the network profile for network binding try: netp = n1kv_db.get_network_profile_by_type(network_type, session) except n1kv_exc.NetworkProfileNotFound: raise ml2_exc.MechanismDriverError() kwargs = { "network_id": network['id'], "network_type": network_type, "db_session": session, "segment_id": segment['segmentation_id'], "netp_id": netp['id'] } n1kv_db.add_network_binding(**kwargs)