Moti Asayag has uploaded a new change for review.

Change subject: engine: Introduce HostNetworkInterfacesPersister
......................................................................

engine: Introduce HostNetworkInterfacesPersister

The class will be used to persist the network interfaces
as reported by VDSM to the engine db:
* New nics will be inserted
* Existing nics will be reported
* Unreported nics will be removed

This class will later be used for persisting the network
attachments entities of each network interface.

Change-Id: Iec0701a302781a9fa147c65818764bddf8429828
Signed-off-by: Moti Asayag <masa...@redhat.com>
---
A 
backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/vdsbroker/HostNetworkInterfacesPersister.java
M 
backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/vdsbroker/HostNetworkTopologyPersisterImpl.java
2 files changed, 145 insertions(+), 54 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/70/34070/1

diff --git 
a/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/vdsbroker/HostNetworkInterfacesPersister.java
 
b/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/vdsbroker/HostNetworkInterfacesPersister.java
new file mode 100644
index 0000000..47e43ef
--- /dev/null
+++ 
b/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/vdsbroker/HostNetworkInterfacesPersister.java
@@ -0,0 +1,122 @@
+package org.ovirt.engine.core.vdsbroker.vdsbroker;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.ovirt.engine.core.common.businessentities.Entities;
+import 
org.ovirt.engine.core.common.businessentities.network.VdsNetworkInterface;
+import org.ovirt.engine.core.dal.dbbroker.DbFacade;
+import org.ovirt.engine.core.dao.network.InterfaceDao;
+
+public class HostNetworkInterfacesPersister {
+
+    private DbFacade dbFacade;
+
+    public HostNetworkInterfacesPersister(DbFacade dbFacade) {
+        this.dbFacade = dbFacade;
+    }
+
+    public void removeUnreportedInterfaces(Map<String, VdsNetworkInterface> 
reportedNicsByNames,
+            List<VdsNetworkInterface> dbNics) {
+        for (VdsNetworkInterface dbNic : dbNics) {
+            if (!reportedNicsByNames.containsKey(dbNic.getName())) {
+                getInterfaceDao().removeInterfaceFromVds(dbNic.getId());
+                getInterfaceDao().removeStatisticsForVds(dbNic.getId());
+            }
+        }
+    }
+
+    public List<VdsNetworkInterface> updateModifiedInterfaces(Map<String, 
VdsNetworkInterface> reportedNicsByNames,
+            List<VdsNetworkInterface> dbNics,
+            Map<String, VdsNetworkInterface> modifiedNicsByName) {
+        List<VdsNetworkInterface> nicsForUpdate = 
extractNicsForUpdate(reportedNicsByNames, dbNics, modifiedNicsByName);
+        if (!nicsForUpdate.isEmpty()) {
+            getInterfaceDao().massUpdateInterfacesForVds(nicsForUpdate);
+        }
+
+        return nicsForUpdate;
+    }
+
+    public List<VdsNetworkInterface> 
createNewInterfaces(List<VdsNetworkInterface> reportedNics,
+            List<VdsNetworkInterface> nicsForUpdate,
+            Map<String, VdsNetworkInterface> modifiedNicsByName) {
+        List<VdsNetworkInterface> nicsForCreate = 
extractNicsForCreate(reportedNics, nicsForUpdate, modifiedNicsByName);
+
+        for (VdsNetworkInterface vdsIface : nicsForCreate) {
+            getInterfaceDao().saveInterfaceForVds(vdsIface);
+            getInterfaceDao().saveStatisticsForVds(vdsIface.getStatistics());
+        }
+
+        return nicsForCreate;
+    }
+
+    private List<VdsNetworkInterface> 
extractNicsForCreate(List<VdsNetworkInterface> reportedNics,
+            List<VdsNetworkInterface> nicsForUpdate,
+            Map<String, VdsNetworkInterface> modifiedNicsByName) {
+        List<VdsNetworkInterface> nicsForCreate = new ArrayList<>();
+        Set<String> nicsNamesForUpdate = Entities.objectNames(nicsForUpdate);
+        for (VdsNetworkInterface reportedNic : reportedNics) {
+            if (!nicsNamesForUpdate.contains(reportedNic.getName())) {
+                nicsForCreate.add(reportedNic);
+            }
+        }
+
+        updateNicsWithUserConfiguration(nicsForCreate, modifiedNicsByName);
+        return nicsForCreate;
+    }
+
+    private void updateNicsWithUserConfiguration(List<VdsNetworkInterface> 
nicsForUpdate,
+            Map<String, VdsNetworkInterface> nicsByName) {
+        if (nicsByName == null) {
+            return;
+        }
+
+        for (VdsNetworkInterface nicForUpdate : nicsForUpdate) {
+            if (nicsByName.containsKey(nicForUpdate.getName())) {
+                VdsNetworkInterface nic = 
nicsByName.get(nicForUpdate.getName());
+                nicForUpdate.setLabels(nic.getLabels());
+                nicForUpdate.setQosOverridden(nic.isQosOverridden());
+                nicForUpdate.setCustomProperties(nic.getCustomProperties());
+            }
+        }
+    }
+
+    private List<VdsNetworkInterface> extractNicsForUpdate(Map<String, 
VdsNetworkInterface> reportedNicsByNames,
+            List<VdsNetworkInterface> dbNics,
+            Map<String, VdsNetworkInterface> modifiedNicsByName) {
+        List<VdsNetworkInterface> nicsForUpdate = new ArrayList<>();
+
+        for (VdsNetworkInterface dbNic : dbNics) {
+            if (reportedNicsByNames.containsKey(dbNic.getName())) {
+                VdsNetworkInterface reportedNic = 
reportedNicsByNames.get(dbNic.getName());
+                preservePreConfiguredNicAttributes(dbNic, reportedNic);
+                nicsForUpdate.add(reportedNic);
+            }
+        }
+
+        updateNicsWithUserConfiguration(nicsForUpdate, modifiedNicsByName);
+        return nicsForUpdate;
+    }
+
+    /**
+     * Preserves the pre configured attributes of a reported nic according to 
what was saved in the database so
+     * interfaces which were already existing in the database will not loose 
their attributes.
+     *
+     * @param dbNic
+     *            the network interface before the last report
+     * @param reportedNic
+     *            the reported network interface
+     */
+    private void preservePreConfiguredNicAttributes(VdsNetworkInterface dbNic, 
VdsNetworkInterface reportedNic) {
+        reportedNic.setId(dbNic.getId());
+        reportedNic.setLabels(dbNic.getLabels());
+        reportedNic.setQosOverridden(dbNic.isQosOverridden());
+        reportedNic.setCustomProperties(dbNic.getCustomProperties());
+    }
+
+    private InterfaceDao getInterfaceDao() {
+        return dbFacade.getInterfaceDao();
+    }
+}
diff --git 
a/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/vdsbroker/HostNetworkTopologyPersisterImpl.java
 
b/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/vdsbroker/HostNetworkTopologyPersisterImpl.java
index cb7676f..2d9b6d9 100644
--- 
a/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/vdsbroker/HostNetworkTopologyPersisterImpl.java
+++ 
b/backend/manager/modules/vdsbroker/src/main/java/org/ovirt/engine/core/vdsbroker/vdsbroker/HostNetworkTopologyPersisterImpl.java
@@ -22,7 +22,6 @@
 import org.ovirt.engine.core.dal.dbbroker.DbFacade;
 import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogDirector;
 import org.ovirt.engine.core.dal.dbbroker.auditloghandling.AuditLogableBase;
-import org.ovirt.engine.core.dao.network.InterfaceDao;
 import org.ovirt.engine.core.dao.network.NetworkQoSDao;
 import org.ovirt.engine.core.utils.NetworkUtils;
 import org.ovirt.engine.core.utils.linq.LinqUtils;
@@ -203,61 +202,31 @@
         }
     }
 
+    /**
+     * Persists the network topology as reported by vdsm, with respect to the 
following:
+     * <ul>
+     * <li>Pre-configured provided interfaces will maintain their settings 
(labels, properties and QoS)</li>
+     * <li>Network attachments will be created for nics without it and on 
which a known network is configured</li>
+     * <li>A nic which existed on db and wasn't reported will be removed with 
its network attachment</li>
+     * </ul>
+     *
+     * @param reportedNics
+     *            network interfaces reported by vdsm
+     * @param dbNics
+     *            network interfaces from the database prior to vdsm report
+     * @param modifiedNicsByName
+     *            network interfaces that should preserve their configuration
+     */
     private void persistTopology(List<VdsNetworkInterface> reportedNics,
-                                 List<VdsNetworkInterface> dbNics,
-                                 Map<String, VdsNetworkInterface> nicsByName) {
-        InterfaceDao interfaceDAO = DbFacade.getInstance().getInterfaceDao();
-        List<String> updatedIfaces = new ArrayList<>();
-        List<VdsNetworkInterface> dbIfacesToBatch = new ArrayList<>();
-        Map<String, VdsNetworkInterface> hostNicsByNames = 
Entities.entitiesByName(reportedNics);
+            List<VdsNetworkInterface> dbNics,
+            Map<String, VdsNetworkInterface> modifiedNicsByName) {
+        Map<String, VdsNetworkInterface> reportedNicsByNames = 
Entities.entitiesByName(reportedNics);
 
-        // First we check what interfaces need to update/delete
-        for (VdsNetworkInterface dbIface : dbNics) {
-            if (hostNicsByNames.containsKey(dbIface.getName())) {
-                VdsNetworkInterface vdsIface = 
hostNicsByNames.get(dbIface.getName());
-
-                // we preserve only the ID and the labels from the Database
-                // everything else is what we got from getVdsCapabilities
-                vdsIface.setId(dbIface.getId());
-                vdsIface.setLabels(dbIface.getLabels());
-                vdsIface.setQosOverridden(dbIface.isQosOverridden());
-                vdsIface.setCustomProperties(dbIface.getCustomProperties());
-                dbIfacesToBatch.add(vdsIface);
-                updatedIfaces.add(vdsIface.getName());
-            } else {
-                interfaceDAO.removeInterfaceFromVds(dbIface.getId());
-                interfaceDAO.removeStatisticsForVds(dbIface.getId());
-            }
-        }
-
-        if (nicsByName != null) {
-            updateInterfacesWithUserConfiguration(dbIfacesToBatch, nicsByName);
-            updateInterfacesWithUserConfiguration(reportedNics, nicsByName);
-        }
-
-        if (!dbIfacesToBatch.isEmpty()) {
-            interfaceDAO.massUpdateInterfacesForVds(dbIfacesToBatch);
-        }
-
-        // now all that left is add the interfaces that not exists in the 
Database
-        for (VdsNetworkInterface vdsIface : reportedNics) {
-            if (!updatedIfaces.contains(vdsIface.getName())) {
-                interfaceDAO.saveInterfaceForVds(vdsIface);
-                interfaceDAO.saveStatisticsForVds(vdsIface.getStatistics());
-            }
-        }
-    }
-
-    private void 
updateInterfacesWithUserConfiguration(List<VdsNetworkInterface> nicsForUpdate,
-                                                       Map<String, 
VdsNetworkInterface> nicsByName) {
-        for (VdsNetworkInterface nicForUpdate : nicsForUpdate) {
-            if (nicsByName.containsKey(nicForUpdate.getName())) {
-                VdsNetworkInterface nic = 
nicsByName.get(nicForUpdate.getName());
-                nicForUpdate.setLabels(nic.getLabels());
-                nicForUpdate.setQosOverridden(nic.isQosOverridden());
-                nicForUpdate.setCustomProperties(nic.getCustomProperties());
-            }
-        }
+        HostNetworkInterfacesPersister persister = new 
HostNetworkInterfacesPersister(DbFacade.getInstance());
+        persister.removeUnreportedInterfaces(reportedNicsByNames, dbNics);
+        List<VdsNetworkInterface> nicsForUpdate =
+                persister.updateModifiedInterfaces(reportedNicsByNames, 
dbNics, modifiedNicsByName);
+        persister.createNewInterfaces(reportedNics, nicsForUpdate, 
modifiedNicsByName);
     }
 
     private String getVmNetworksImplementedAsBridgeless(VDS host, 
List<Network> clusterNetworks) {


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

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

Reply via email to