Moti Asayag has uploaded a new change for review.

Change subject: engine: Adjust host networks when cluster changes
......................................................................

engine: Adjust host networks when cluster changes

When moving a host between clusters, the labeled host's
networks should be updated by the assignment of those
labeled network to the cluster:
- Cluster-assigned labeled networks should be configured
  on the hosts if a nic is labeled with those labels
- Labeled networks which were assigned to the host nics
  and not assigned to the target cluster should be removed
  from the host.

Change-Id: I5fa6e324c2833366a5fb34db9d2d754426dce316
Signed-off-by: Moti Asayag <masa...@redhat.com>
---
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ChangeVDSClusterCommand.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/AuditLogType.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
M 
backend/manager/modules/dal/src/main/resources/bundles/AuditLogMessages.properties
A nohup.out
5 files changed, 162 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/91/23191/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ChangeVDSClusterCommand.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ChangeVDSClusterCommand.java
index 3e6c080..5d581f0 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ChangeVDSClusterCommand.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/ChangeVDSClusterCommand.java
@@ -2,24 +2,34 @@
 
 import java.util.ArrayList;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.ovirt.engine.core.bll.context.CommandContext;
+import org.ovirt.engine.core.bll.network.NetworkParametersBuilder;
+import org.ovirt.engine.core.bll.network.cluster.NetworkHelper;
 import org.ovirt.engine.core.bll.utils.ClusterUtils;
 import org.ovirt.engine.core.bll.utils.GlusterUtil;
 import org.ovirt.engine.core.bll.utils.PermissionSubject;
 import org.ovirt.engine.core.common.AuditLogType;
 import org.ovirt.engine.core.common.VdcObjectType;
 import org.ovirt.engine.core.common.action.ChangeVDSClusterParameters;
+import org.ovirt.engine.core.common.action.SetupNetworksParameters;
 import org.ovirt.engine.core.common.action.VdcActionType;
 import org.ovirt.engine.core.common.action.VdcReturnValueBase;
 import org.ovirt.engine.core.common.action.VdsActionParameters;
 import org.ovirt.engine.core.common.businessentities.ArchitectureType;
+import org.ovirt.engine.core.common.businessentities.Entities;
 import org.ovirt.engine.core.common.businessentities.StoragePool;
 import org.ovirt.engine.core.common.businessentities.StorageType;
 import org.ovirt.engine.core.common.businessentities.VDS;
 import org.ovirt.engine.core.common.businessentities.VDSGroup;
 import org.ovirt.engine.core.common.businessentities.VdsStatic;
+import org.ovirt.engine.core.common.businessentities.network.Network;
+import 
org.ovirt.engine.core.common.businessentities.network.VdsNetworkInterface;
+import org.ovirt.engine.core.common.errors.VdcBLLException;
+import org.ovirt.engine.core.common.errors.VdcBllErrors;
 import org.ovirt.engine.core.common.errors.VdcBllMessages;
 import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
 import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
@@ -27,9 +37,13 @@
 import 
org.ovirt.engine.core.common.vdscommands.gluster.RemoveGlusterServerVDSParameters;
 import org.ovirt.engine.core.compat.Guid;
 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.gluster.GlusterDBUtils;
+import org.ovirt.engine.core.utils.NetworkUtils;
 import org.ovirt.engine.core.utils.ObjectIdentityChecker;
 import org.ovirt.engine.core.utils.lock.EngineLock;
+import org.ovirt.engine.core.utils.threadpool.ThreadPoolUtil;
 import org.ovirt.engine.core.utils.transaction.TransactionMethod;
 import org.ovirt.engine.core.utils.transaction.TransactionSupport;
 
@@ -193,7 +207,33 @@
             getVdsSpmIdMapDAO().removeByVdsAndStoragePool(getVds().getId(), 
getSourceCluster().getStoragePoolId());
         }
 
+        if 
(NetworkHelper.setupNetworkSupported(getTargetCluster().getcompatibility_version()))
 {
+            configureNetworks();
+        }
+
         setSucceeded(true);
+    }
+
+    private void configureNetworks() {
+        ChangeClusterParametersBuilder builder = new 
ChangeClusterParametersBuilder();
+        final SetupNetworksParameters params;
+
+        try {
+            params = builder.buildParameters(getVdsId(), 
getSourceCluster().getId(), getTargetCluster().getId());
+        } catch (VdcBLLException e) {
+            AuditLogDirector.log(new AuditLogableBase(getVdsId()),
+                    
AuditLogType.CONFIGURE_NETWORK_BY_LABELS_WHEN_CHANGING_CLUSTER_FAILED);
+            return;
+        }
+
+        ThreadPoolUtil.execute(new Runnable() {
+
+            @Override
+            public void run() {
+                
getBackend().runInternalAction(VdcActionType.PersistentSetupNetworks, params);
+
+            }
+        });
     }
 
     @Override
@@ -299,4 +339,94 @@
         }
         return targetCluster;
     }
+
+    private class ChangeClusterParametersBuilder extends 
NetworkParametersBuilder {
+
+        public SetupNetworksParameters buildParameters(Guid hostId, Guid 
sourceClusterId, Guid targetClusterId) {
+            List<Network> targetClusterNetworks = 
getNetworkDAO().getAllForCluster(targetClusterId);
+            Map<String, Network> targetClusterNetworksByName = 
Entities.entitiesByName(targetClusterNetworks);
+
+            SetupNetworksParameters params = 
createSetupNetworksParameters(hostId);
+            Map<String, VdsNetworkInterface> nicsByNetwork =
+                    
Entities.hostInterfacesByNetworkName(params.getInterfaces());
+            Map<String, List<Network>> networksByLabel = 
getClusterNetworksByLabel(targetClusterNetworks);
+            List<VdsNetworkInterface> hostNics = new 
ArrayList<>(params.getInterfaces());
+
+            // Detect which networks should be added and which should be 
removed
+            for (VdsNetworkInterface nic : hostNics) {
+                adjustNetworksByLabel(targetClusterNetworksByName, params, 
nicsByNetwork, networksByLabel, nic);
+            }
+
+            return params;
+        }
+
+        /**
+         * Add or remove labeled networks from a nic by the assignment of 
networks to the target cluster:
+         * <ul>
+         * <li>Assigned labeled network will be defined on the nic if isn't 
already</li>
+         * <li>Unassigned labeled network will be removed from a nic if isn't 
already</li>
+         * </ul>
+         *
+         * @param targetClusterNetworksByName
+         *            a map of the destination cluster networks by their name
+         * @param params
+         *            the setup networks parameters to be adjusted according 
to the findings
+         * @param nicsByNetwork
+         *            a map of nics by the network attached to them
+         * @param networksByLabel
+         *            a map of labeled networks by their label
+         * @param nic
+         *            the current examined network interface
+         */
+        public void adjustNetworksByLabel(Map<String, Network> 
targetClusterNetworksByName,
+                SetupNetworksParameters params,
+                Map<String, VdsNetworkInterface> nicsByNetwork,
+                Map<String, List<Network>> networksByLabel,
+                VdsNetworkInterface nic) {
+            if (nic.getLabels() == null) {
+                return;
+            }
+
+            for (String label : nic.getLabels()) {
+                for (Network net : networksByLabel.get(label)) {
+                    if (targetClusterNetworksByName.containsKey(net.getName())
+                            && !nicsByNetwork.containsKey(net.getName())) {
+                        configureNetwork(nic, params.getInterfaces(), net);
+                    } else if 
(!targetClusterNetworksByName.containsKey(net.getName())
+                            && nicsByNetwork.containsKey(net.getName())) {
+                        removeNetworkFromParameters(params, nic, net);
+                    }
+                }
+            }
+        }
+
+        public void removeNetworkFromParameters(SetupNetworksParameters 
params, VdsNetworkInterface nic, Network net) {
+            if (NetworkUtils.isVlan(net)) {
+                VdsNetworkInterface vlan = 
getVlanDevice(params.getInterfaces(), nic, net);
+                if (vlan == null) {
+                    throw new 
VdcBLLException(VdcBllErrors.NETWORK_LABEL_CONFLICT);
+                } else {
+                    params.getInterfaces().remove(vlan);
+                }
+            }
+        }
+
+        /**
+         * Returns a map of labels to the cluster networks represented by that 
label.
+         */
+        private Map<String, List<Network>> 
getClusterNetworksByLabel(List<Network> clusterNetworks) {
+            Map<String, List<Network>> networksByLabel = new HashMap<>();
+            for (Network network : clusterNetworks) {
+                if (network.getLabel() != null) {
+                    if (!networksByLabel.containsKey(network.getLabel())) {
+                        networksByLabel.put(network.getLabel(), new 
ArrayList<Network>());
+                    }
+
+                    networksByLabel.get(network.getLabel()).add(network);
+                }
+            }
+
+            return networksByLabel;
+        }
+    }
 }
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/AuditLogType.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/AuditLogType.java
index ffd9d9c..aa0ab0f 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/AuditLogType.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/AuditLogType.java
@@ -617,6 +617,7 @@
     SUBNET_REMOVAL_FAILED(1141),
     SUBNET_ADDED(1142),
     SUBNET_ADDITION_FAILED(1143),
+    CONFIGURE_NETWORK_BY_LABELS_WHEN_CHANGING_CLUSTER_FAILED(1144),
 
     // Import/Export
     IMPORTEXPORT_STARTING_EXPORT_VM(1162),
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
index 6653848..d9d0a66 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dal/dbbroker/auditloghandling/AuditLogDirector.java
@@ -838,6 +838,7 @@
         severities.put(AuditLogType.SUBNET_REMOVAL_FAILED, 
AuditLogSeverity.ERROR);
         severities.put(AuditLogType.SUBNET_ADDED, AuditLogSeverity.NORMAL);
         severities.put(AuditLogType.SUBNET_ADDITION_FAILED, 
AuditLogSeverity.ERROR);
+        
severities.put(AuditLogType.CONFIGURE_NETWORK_BY_LABELS_WHEN_CHANGING_CLUSTER_FAILED,
 AuditLogSeverity.ERROR);
     }
 
     private static void initExtrnalEvents() {
diff --git 
a/backend/manager/modules/dal/src/main/resources/bundles/AuditLogMessages.properties
 
b/backend/manager/modules/dal/src/main/resources/bundles/AuditLogMessages.properties
index f5b5ef5..9baad45 100644
--- 
a/backend/manager/modules/dal/src/main/resources/bundles/AuditLogMessages.properties
+++ 
b/backend/manager/modules/dal/src/main/resources/bundles/AuditLogMessages.properties
@@ -452,6 +452,7 @@
 LABEL_NIC_FAILED=Failed to label network interface card ${NicName} with label 
${Label} on host ${VdsName}.
 UNLABEL_NIC=Label ${Label} was removed from network interface card ${NicName} 
on host ${VdsName}.
 UNLABEL_NIC_FAILED=Failed to remove label ${Label} from network interface card 
${NicName} on host ${VdsName}.
+CONFIGURE_NETWORK_BY_LABELS_WHEN_CHANGING_CLUSTER_FAILED=Failed to configure 
networks on host ${VdsName} while changing its cluster.
 PROVIDER_ADDED=Provider ${ProviderName} was added. (User: ${UserName})
 PROVIDER_ADDITION_FAILED=Failed to add provider ${ProviderName}. (User: 
${UserName})
 PROVIDER_UPDATED=Provider ${ProviderName} was updated. (User: ${UserName})
diff --git a/nohup.out b/nohup.out
new file mode 100644
index 0000000..546014b
--- /dev/null
+++ b/nohup.out
@@ -0,0 +1,29 @@
+[debug] execute contextualize
+[debug] execute contextualize
+Unable to delete file 
/home/masayag/work/workspace/ovirt-engine/.metadata/.plugins/org.eclipse.wst.internet.cache/1603273050.cache
 from cache.
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize
+[debug] execute contextualize


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

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