Oved Ourfali has uploaded a new change for review.

Change subject: core: handling nic ordering in import VM
......................................................................

core: handling nic ordering in import VM

This patch handles the NIC ordering when importing a VM as cloned.
The NIC ordering is determined by the MAC address (if it is set), or
otherwise on the name of the NIC.

Change-Id: Ibfee334136e7258a7d19006d8f7e7774997be484
Bug-Url: https://bugzilla.redhat.com/1040630
Signed-off-by: Oved Ourfali <oourf...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommand.java
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/VmInterfaceManager.java
2 files changed, 64 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/22/22422/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommand.java
index a3a7f17..44f5382 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ImportVmCommand.java
@@ -3,6 +3,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -10,6 +11,7 @@
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.commons.collections.ListUtils;
 import org.apache.commons.lang.StringUtils;
 import org.ovirt.engine.core.bll.job.ExecutionContext;
 import org.ovirt.engine.core.bll.job.ExecutionHandler;
@@ -1035,6 +1037,17 @@
         AuditLogDirector.log(logable, AuditLogType.VM_IMPORT_INFO);
     }
 
+    private List<String> allocateMacAddresses(int numberOfAddresses) {
+        List<String> macAddresses = new ArrayList<String>(numberOfAddresses);
+
+        for (int i = 0; i < numberOfAddresses; ++i) {
+            macAddresses.add(MacPoolManager.getInstance().allocateNewMac());
+        }
+        Collections.sort(macAddresses);
+
+        return macAddresses;
+    }
+
     protected void addVmInterfaces() {
         VmInterfaceManager vmInterfaceManager = new VmInterfaceManager();
 
@@ -1044,14 +1057,46 @@
                         getVdsGroup().getcompatibility_version(),
                         
AuditLogType.IMPORTEXPORT_IMPORT_VM_INVALID_INTERFACES);
 
-        for (VmNetworkInterface iface : getVm().getInterfaces()) {
+        List<VmNetworkInterface> nics = getVm().getInterfaces();
+
+        Collections.sort(nics, new Comparator<VmNetworkInterface>() {
+            @Override
+            public int compare(VmNetworkInterface nic1, VmNetworkInterface 
nic2) {
+                // If the NIC has a MAC address, we sort by it.
+                // Otherwise we sort by  the name of the NIC
+                if (StringUtils.isNotEmpty(nic1.getMacAddress()) && 
StringUtils.isNotEmpty(nic2.getMacAddress())) {
+                    return 
nic1.getMacAddress().compareTo(nic2.getMacAddress());
+                }
+                return nic1.getName().compareTo(nic2.getName());
+            }
+        });
+
+        // If we import it as a new entity, then we allocate all MAC addresses 
in advance
+        List<String> macAddresses = ListUtils.EMPTY_LIST;
+        if (getParameters().isImportAsNewEntity()) {
+            macAddresses = allocateMacAddresses(nics.size());
+        }
+
+        for (int i = 0; i < nics.size(); ++i) {
+            VmNetworkInterface iface = nics.get(i);
             initInterface(iface);
             vnicProfileHelper.updateNicWithVnicProfileForUser(iface, 
getCurrentUser());
-            vmInterfaceManager.add(iface,
-                    getCompensationContext(),
-                    getParameters().isImportAsNewEntity(),
-                    getVm().getOs(),
-                    getVdsGroup().getcompatibility_version());
+
+            // If importing as a new entity, we add the NIC using the 
allocated MAC address
+            if (getParameters().isImportAsNewEntity()) {
+                vmInterfaceManager.add(iface,
+                        getCompensationContext(),
+                        macAddresses.get(i),
+                        getVm().getOs(),
+                        getVdsGroup().getcompatibility_version());
+            // Otherwise, we just add the NIC
+            } else {
+                vmInterfaceManager.add(iface,
+                        getCompensationContext(),
+                        false,
+                        getVm().getOs(),
+                        getVdsGroup().getcompatibility_version());
+            }
             macsAdded.add(iface.getMacAddress());
         }
 
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/VmInterfaceManager.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/VmInterfaceManager.java
index 3b70df4..6bedd9a 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/VmInterfaceManager.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/VmInterfaceManager.java
@@ -70,6 +70,19 @@
         compensationContext.snapshotNewEntity(iface.getStatistics());
     }
 
+    public void add(final VmNic iface,
+                    CompensationContext compensationContext,
+                    String macAddress,
+                    int osId,
+                    Version clusterCompatibilityVersion) {
+
+        iface.setMacAddress(macAddress);
+        getVmNicDao().save(iface);
+        getVmNetworkStatisticsDao().save(iface.getStatistics());
+        compensationContext.snapshotNewEntity(iface);
+        compensationContext.snapshotNewEntity(iface.getStatistics());
+    }
+
     public OsRepository getOsRepository() {
         return SimpleDependecyInjector.getInstance().get(OsRepository.class);
     }


-- 
To view, visit http://gerrit.ovirt.org/22422
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibfee334136e7258a7d19006d8f7e7774997be484
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Oved Ourfali <oourf...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to