diff --git a/nova/objects/migrate_data.py b/nova/objects/migrate_data.py index fa4427292b67..c1929c5479b0 100644 --- a/nova/objects/migrate_data.py +++ b/nova/objects/migrate_data.py @@ -12,10 +12,13 @@ # License for the specific language governing permissions and limitations # under the License. +import copy + from oslo_log import log from oslo_serialization import jsonutils from oslo_utils import versionutils +from nova import exception from nova import objects from nova.objects import base as obj_base from nova.objects import fields @@ -71,6 +74,25 @@ class VIFMigrateData(obj_base.NovaObject): def profile(self, profile_dict): self.profile_json = jsonutils.dumps(profile_dict) + def get_dest_vif(self): + """Get a destination VIF representation of this object. + + This method takes the source_vif and updates it to include the + destination host port binding information using the other fields + on this object. + + :return: nova.network.model.VIF object + """ + if 'source_vif' not in self: + raise exception.ObjectActionError( + action='get_dest_vif', reason='source_vif is not set') + vif = copy.deepcopy(self.source_vif) + vif['type'] = self.vif_type + vif['vnic_type'] = self.vnic_type + vif['profile'] = self.profile + vif['details'] = self.vif_details + return vif + @obj_base.NovaObjectRegistry.register_if(False) class LiveMigrateData(obj_base.NovaObject): diff --git a/nova/tests/unit/objects/test_migrate_data.py b/nova/tests/unit/objects/test_migrate_data.py index f7eb88dc56fc..d31c513d024d 100644 --- a/nova/tests/unit/objects/test_migrate_data.py +++ b/nova/tests/unit/objects/test_migrate_data.py @@ -15,9 +15,11 @@ from oslo_serialization import jsonutils from oslo_versionedobjects import base as ovo_base +from nova import exception from nova.network import model as network_model from nova import objects from nova.objects import migrate_data +from nova import test from nova.tests.unit.objects import test_objects from nova.tests import uuidsentinel as uuids @@ -550,3 +552,31 @@ class TestPowerVMLiveMigrateData(test_objects._LocalTest, class TestRemotePowerVMLiveMigrateData(test_objects._RemoteTest, _TestPowerVMLiveMigrateData): pass + + +class TestVIFMigrateData(test.NoDBTestCase): + + def test_get_dest_vif_source_vif_not_set(self): + migrate_vif = objects.VIFMigrateData( + port_id=uuids.port_id, vnic_type=network_model.VNIC_TYPE_NORMAL, + vif_type=network_model.VIF_TYPE_OVS, vif_details={}, + profile={}, host='fake-dest-host') + self.assertRaises( + exception.ObjectActionError, migrate_vif.get_dest_vif) + + def test_get_dest_vif(self): + source_vif = network_model.VIF( + id=uuids.port_id, type=network_model.VIF_TYPE_OVS, details={}, + vnic_type=network_model.VNIC_TYPE_DIRECT, profile={'foo': 'bar'}, + ovs_interfaceid=uuids.ovs_interfaceid) + migrate_vif = objects.VIFMigrateData( + port_id=uuids.port_id, vnic_type=network_model.VNIC_TYPE_NORMAL, + vif_type=network_model.VIF_TYPE_BRIDGE, vif_details={'bar': 'baz'}, + profile={}, host='fake-dest-host', source_vif=source_vif) + dest_vif = migrate_vif.get_dest_vif() + self.assertEqual(migrate_vif.port_id, dest_vif['id']) + self.assertEqual(migrate_vif.vnic_type, dest_vif['vnic_type']) + self.assertEqual(migrate_vif.vif_type, dest_vif['type']) + self.assertEqual(migrate_vif.vif_details, dest_vif['details']) + self.assertEqual(migrate_vif.profile, dest_vif['profile']) + self.assertEqual(uuids.ovs_interfaceid, dest_vif['ovs_interfaceid'])