Oved Ourfali has uploaded a new change for review.

Change subject: core: handle nic ordering when adding a VM
......................................................................

core: handle nic ordering when adding a VM

This patch handles the NIC ordering when creating a VM, as follows:
1. When creating a VM from template
 a. If the template has PCI addresses, then sort the NICs according
to that, allocating MAC addresses with the same ordering
 b. If the template doesn't contain PCI addresses, allocate MAC
addresses with an ordering identical to the naming of the NICs
2. When creating a VM from snapshot
 a. Follow login in 1a.
 b. If the snapshot doesn't contain PCI addresses, but it does contain
the MAC addresses, allocate new MAC addresses with the same ordering as
in the original snapshot
 c. If no PCI and no MAC addresses are available, order by the NIC name

Change-Id: I46b4ffc4ecaab824b90bf8b8ecdfe4ebad3074bf
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/AddVmCommand.java
1 file changed, 59 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/11/22411/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmCommand.java
index 9822243..a3f70c4 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/AddVmCommand.java
@@ -3,6 +3,7 @@
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.Comparator;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
@@ -42,6 +43,7 @@
 import org.ovirt.engine.core.common.businessentities.VDSGroup;
 import org.ovirt.engine.core.common.businessentities.VM;
 import org.ovirt.engine.core.common.businessentities.VMStatus;
+import org.ovirt.engine.core.common.businessentities.VmDevice;
 import org.ovirt.engine.core.common.businessentities.VmDeviceGeneralType;
 import org.ovirt.engine.core.common.businessentities.VmDeviceId;
 import org.ovirt.engine.core.common.businessentities.VmDynamic;
@@ -208,6 +210,15 @@
             _vmInterfaces = vmNetworkInterfaces == null ? new 
ArrayList<VmNic>() : vmNetworkInterfaces;
         }
         return _vmInterfaces;
+    }
+
+    protected Map<Guid, VmDevice> getVmInterfaceDevices() {
+        List<VmDevice> vmInterfaceDevicesList = 
getVmDeviceDao().getVmDeviceByVmIdAndType(vmInterfacesSourceId, 
VmDeviceGeneralType.INTERFACE);
+        Map<Guid, VmDevice> vmInterfaceDevices = new HashMap();
+        for (VmDevice device : vmInterfaceDevicesList) {
+            vmInterfaceDevices.put(device.getDeviceId(), device);
+        }
+        return vmInterfaceDevices;
     }
 
     protected List<? extends Disk> _vmDisks;
@@ -699,13 +710,59 @@
         return returnValue;
     }
 
+    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 List<VmNic> getSortedNics() {
+        List<VmNic> nics = getVmInterfaces();
+        final Map<Guid, VmDevice> vmInterfaceDevices = getVmInterfaceDevices();
+
+        Collections.sort(nics, new Comparator<VmNic>() {
+            @Override
+            public int compare(VmNic nic1, VmNic nic2) {
+                // If both devices have a PCI address then we compare by it
+                // Otherwise if both devices have a MAC address then we 
compare by it
+                // Otherwise we compare by name
+                VmDevice nic1Device = vmInterfaceDevices.get(nic1.getId());
+                VmDevice nic2Device = vmInterfaceDevices.get(nic2.getId());
+
+                if (nic1Device != null && nic2Device != null) {
+                    if (StringUtils.isNotEmpty(nic1Device.getAddress()) && 
StringUtils.isNotEmpty(nic2Device.getAddress())) {
+                        return 
nic1Device.getAddress().compareTo(nic2Device.getAddress());
+                    }
+                }
+                if (StringUtils.isNotEmpty(nic1.getMacAddress()) && 
StringUtils.isNotEmpty(nic2.getMacAddress())) {
+                    return 
nic1.getMacAddress().compareTo(nic2.getMacAddress());
+                }
+                return nic1.getName().compareTo(nic2.getName());
+            }
+        });
+
+        return nics;
+    }
+
     protected void addVmNetwork() {
+        List<VmNic> nics = getSortedNics();
+
+        final Map<Guid, VmDevice> vmInterfaceDevices = getVmInterfaceDevices();
+
+        List<String> macAddresses = allocateMacAddresses(nics.size());
+
         // Add interfaces from template
-        for (VmNic iface : getVmInterfaces()) {
+        for (int i = 0; i < nics.size(); ++i) {
+            VmNic iface = nics.get(i);
             Guid id = Guid.newGuid();
             srcVmNicIdToTargetVmNicIdMapping.put(iface.getId(), id);
             iface.setId(id);
-            iface.setMacAddress(MacPoolManager.getInstance().allocateNewMac());
+            iface.setMacAddress(macAddresses.get(i));
             
iface.setSpeed(VmInterfaceType.forValue(iface.getType()).getSpeed());
             iface.setVmTemplateId(null);
             iface.setVmId(getParameters().getVmStaticData().getId());


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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I46b4ffc4ecaab824b90bf8b8ecdfe4ebad3074bf
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