Moti Asayag has uploaded a new change for review. Change subject: engine: Introduce network attachments command ......................................................................
engine: Introduce network attachments command The Add/Update/Remove network attachments are added in order to support a solely network attachment action. Change-Id: I3c1b7a4e0b5ceacf6ea436c4ede84e414817dbdd Signed-off-by: Moti Asayag <[email protected]> --- A backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/AddNetworkAttachmentCommand.java A backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/RemoveNetworkAttachmentCommand.java A backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/UpdateNetworkAttachmentCommand.java A backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/NetworkAttachmentParameters.java M backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java 5 files changed, 322 insertions(+), 1 deletion(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/71/34971/1 diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/AddNetworkAttachmentCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/AddNetworkAttachmentCommand.java new file mode 100644 index 0000000..eca81f7 --- /dev/null +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/AddNetworkAttachmentCommand.java @@ -0,0 +1,118 @@ +package org.ovirt.engine.core.bll.network.host; + +import java.util.List; + +import org.ovirt.engine.core.bll.NonTransactiveCommandAttribute; +import org.ovirt.engine.core.bll.VdsCommand; +import org.ovirt.engine.core.bll.validator.HostInterfaceValidator; +import org.ovirt.engine.core.bll.validator.NetworkAttachmentValidator; +import org.ovirt.engine.core.bll.validator.NetworkAttachmentsValidator; +import org.ovirt.engine.core.common.action.HostSetupNetworksParameters; +import org.ovirt.engine.core.common.action.NetworkAttachmentParameters; +import org.ovirt.engine.core.common.action.VdcActionType; +import org.ovirt.engine.core.common.action.VdcReturnValueBase; +import org.ovirt.engine.core.common.businessentities.Entities; +import org.ovirt.engine.core.common.businessentities.VDS; +import org.ovirt.engine.core.common.businessentities.network.NetworkAttachment; +import org.ovirt.engine.core.common.businessentities.network.VdsNetworkInterface; +import org.ovirt.engine.core.common.errors.VdcBllMessages; +import org.ovirt.engine.core.compat.Guid; + +@NonTransactiveCommandAttribute +public class AddNetworkAttachmentCommand<T extends NetworkAttachmentParameters> extends VdsCommand<T> { + + private List<VdsNetworkInterface> hostNics; + + public AddNetworkAttachmentCommand(T parameters) { + super(parameters); + } + + @Override + protected boolean canDoAction() { + VDS host = getVds(); + + if (host == null) { + return failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_HOST_NOT_EXIST); + } + + if (!HostSetupNetworksCommand.SUPPORTED_HOST_STATUSES.contains(host.getStatus())) { + addCanDoActionMessage(VdcBllMessages.VAR__HOST_STATUS__UP_MAINTENANCE_OR_NON_OPERATIONAL); + return failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_VDS_STATUS_ILLEGAL); + } + + NetworkAttachmentCompleter completer = new NetworkAttachmentCompleter(getHostInterfaces()); + completer.completeNicName(getParameters().getNetworkAttachment()); + + return validateNetworkAttachment() && validateHostInterface() && validateCrossAttachments(); + } + + @Override + protected void executeCommand() { + HostSetupNetworksParameters params = new HostSetupNetworksParameters(getParameters().getVdsId()); + params.getNetworkAttachments().add(getParameters().getNetworkAttachment()); + VdcReturnValueBase returnValue = runInternalAction(VdcActionType.HostSetupNetworks, params); + Guid createdAttachmentId = resolveCreatedAttachmentId(); + getReturnValue().setActionReturnValue(createdAttachmentId); + propagateFailure(returnValue); + setSucceeded(returnValue.getSucceeded()); + } + + private Guid resolveCreatedAttachmentId() { + Guid createdAttachmentId = null; + List<NetworkAttachment> attachmentsOnNic = + getDbFacade().getNetworkAttachmentDao().getAllForNic(getParameters().getNetworkAttachment().getNicId()); + for (NetworkAttachment attachment : attachmentsOnNic) { + if (attachment.getNetworkId().equals(getParameters().getNetworkAttachment().getNetworkId())) { + createdAttachmentId = attachment.getId(); + } + } + return createdAttachmentId; + } + + private boolean validateCrossAttachments() { + List<NetworkAttachment> expectedAttachments = getDbFacade().getNetworkAttachmentDao().getAllForHost(getVdsId()); + expectedAttachments.add(getParameters().getNetworkAttachment()); + NetworkAttachmentsValidator crossAttachmentsValidator = + new NetworkAttachmentsValidator(expectedAttachments, + Entities.businessEntitiesById(getNetworkDAO().getAllForCluster(getVdsGroupId()))); + return validate(crossAttachmentsValidator.validateNetworkExclusiveOnNics()); + } + + private boolean validateNetworkAttachment() { + NetworkAttachmentValidator validator = + new NetworkAttachmentValidator(getParameters().getNetworkAttachment(), getVds()); + + return validate(validator.networkAttachmentIsSet()) + && validate(validator.networkExists()) + && validate(validator.notExternalNetwork()) + && validate(validator.networkAttachedToCluster()) + && validate(validator.networkNotAttachedToHost()) + && validate(validator.ipConfiguredForStaticBootProtocol()) + && validate(validator.bootProtocolSetForDisplayNetwork()) + && validate(validator.nicExists()) + && validate(validator.validateGateway()); + } + + private boolean validateHostInterface() { + VdsNetworkInterface nic = null; + if (getParameters().getNetworkAttachment().getNicId() != null) { + nic = getDbFacade().getInterfaceDao().get(getParameters().getNetworkAttachment().getNicId()); + } else if (getParameters().getNetworkAttachment().getNicName() != null) { + nic = Entities.entitiesByName(getHostInterfaces()).get(getParameters().getNetworkAttachment().getNicName()); + } + + HostInterfaceValidator hostInterfaceValidator = new HostInterfaceValidator(nic); + return validate(hostInterfaceValidator.interfaceExists()) + && validate(hostInterfaceValidator.interfaceInHost(getVdsId())) + && validate(hostInterfaceValidator.validBond(getHostInterfaces())) + && validate(hostInterfaceValidator.networkCanBeAttached()); + } + + private List<VdsNetworkInterface> getHostInterfaces() { + if (hostNics == null) { + hostNics = getDbFacade().getInterfaceDao().getAllInterfacesForVds(getVdsId()); + } + + return hostNics; + } +} diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/RemoveNetworkAttachmentCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/RemoveNetworkAttachmentCommand.java new file mode 100644 index 0000000..5bae7d4 --- /dev/null +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/RemoveNetworkAttachmentCommand.java @@ -0,0 +1,53 @@ +package org.ovirt.engine.core.bll.network.host; + +import org.ovirt.engine.core.bll.NonTransactiveCommandAttribute; +import org.ovirt.engine.core.bll.VdsCommand; +import org.ovirt.engine.core.bll.validator.ModifiedNetworkAttachmentValidator; +import org.ovirt.engine.core.common.action.HostSetupNetworksParameters; +import org.ovirt.engine.core.common.action.NetworkAttachmentParameters; +import org.ovirt.engine.core.common.action.VdcActionType; +import org.ovirt.engine.core.common.action.VdcReturnValueBase; +import org.ovirt.engine.core.common.businessentities.VDS; +import org.ovirt.engine.core.common.businessentities.network.NetworkAttachment; +import org.ovirt.engine.core.common.errors.VdcBllMessages; + +@NonTransactiveCommandAttribute +public class RemoveNetworkAttachmentCommand<T extends NetworkAttachmentParameters> extends VdsCommand<T> { + + public RemoveNetworkAttachmentCommand(T parameters) { + super(parameters); + } + + @Override + protected boolean canDoAction() { + VDS host = getVds(); + + if (host == null) { + return failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_HOST_NOT_EXIST); + } + + if (!HostSetupNetworksCommand.SUPPORTED_HOST_STATUSES.contains(host.getStatus())) { + addCanDoActionMessage(VdcBllMessages.VAR__HOST_STATUS__UP_MAINTENANCE_OR_NON_OPERATIONAL); + return failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_VDS_STATUS_ILLEGAL); + } + + ModifiedNetworkAttachmentValidator validator = + new ModifiedNetworkAttachmentValidator(getParameters().getNetworkAttachment(), host); + + return validate(validator.networkAttachmentIsSet()) + && validate(validator.networkAttachmentExists()) + && validate(validator.notRemovingManagementNetwork()) + && validate(validator.networkNotUsedByVms()); + } + + @Override + protected void executeCommand() { + HostSetupNetworksParameters params = new HostSetupNetworksParameters(getParameters().getVdsId()); + NetworkAttachment networkAttachmentForRemove = + getDbFacade().getNetworkAttachmentDao().get(getParameters().getNetworkAttachment().getId()); + params.getRemovedNetworkAttachments().add(networkAttachmentForRemove); + VdcReturnValueBase returnValue = runInternalAction(VdcActionType.HostSetupNetworks, params); + propagateFailure(returnValue); + setSucceeded(returnValue.getSucceeded()); + } +} diff --git a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/UpdateNetworkAttachmentCommand.java b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/UpdateNetworkAttachmentCommand.java new file mode 100644 index 0000000..de48c8f --- /dev/null +++ b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/host/UpdateNetworkAttachmentCommand.java @@ -0,0 +1,119 @@ +package org.ovirt.engine.core.bll.network.host; + +import java.util.ArrayList; +import java.util.List; + +import org.ovirt.engine.core.bll.NonTransactiveCommandAttribute; +import org.ovirt.engine.core.bll.VdsCommand; +import org.ovirt.engine.core.bll.validator.HostInterfaceValidator; +import org.ovirt.engine.core.bll.validator.ModifiedNetworkAttachmentValidator; +import org.ovirt.engine.core.bll.validator.NetworkAttachmentsValidator; +import org.ovirt.engine.core.common.action.HostSetupNetworksParameters; +import org.ovirt.engine.core.common.action.NetworkAttachmentParameters; +import org.ovirt.engine.core.common.action.VdcActionType; +import org.ovirt.engine.core.common.action.VdcReturnValueBase; +import org.ovirt.engine.core.common.businessentities.Entities; +import org.ovirt.engine.core.common.businessentities.VDS; +import org.ovirt.engine.core.common.businessentities.network.NetworkAttachment; +import org.ovirt.engine.core.common.businessentities.network.VdsNetworkInterface; +import org.ovirt.engine.core.common.errors.VdcBllMessages; + +@NonTransactiveCommandAttribute +public class UpdateNetworkAttachmentCommand<T extends NetworkAttachmentParameters> extends VdsCommand<T> { + + private List<VdsNetworkInterface> hostNics; + + public UpdateNetworkAttachmentCommand(T parameters) { + super(parameters); + } + + @Override + protected boolean canDoAction() { + VDS host = getVds(); + + if (host == null) { + return failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_HOST_NOT_EXIST); + } + + if (!HostSetupNetworksCommand.SUPPORTED_HOST_STATUSES.contains(host.getStatus())) { + addCanDoActionMessage(VdcBllMessages.VAR__HOST_STATUS__UP_MAINTENANCE_OR_NON_OPERATIONAL); + return failCanDoAction(VdcBllMessages.ACTION_TYPE_FAILED_VDS_STATUS_ILLEGAL); + } + + NetworkAttachmentCompleter completer = new NetworkAttachmentCompleter(getHostInterfaces()); + completer.completeNicName(getParameters().getNetworkAttachment()); + + return validateAttachment() && validateHostInterface() && validateCrossAttachments(); + } + + @Override + protected void executeCommand() { + HostSetupNetworksParameters params = new HostSetupNetworksParameters(getParameters().getVdsId()); + params.getNetworkAttachments().add(getParameters().getNetworkAttachment()); + VdcReturnValueBase returnValue = runInternalAction(VdcActionType.HostSetupNetworks, params); + propagateFailure(returnValue); + setSucceeded(returnValue.getSucceeded()); + } + + private boolean validateCrossAttachments() { + List<NetworkAttachment> expectedAttachments = getExpectedNetworkAttachments(); + NetworkAttachmentsValidator crossAttachmentsValidator = + new NetworkAttachmentsValidator(expectedAttachments, + Entities.businessEntitiesById(getNetworkDAO().getAllForCluster(getVdsGroupId()))); + return validate(crossAttachmentsValidator.validateNetworkExclusiveOnNics()); + } + + private boolean validateHostInterface() { + VdsNetworkInterface nic = null; + if (getParameters().getNetworkAttachment().getNicId() != null) { + nic = getDbFacade().getInterfaceDao().get(getParameters().getNetworkAttachment().getNicId()); + } else if (getParameters().getNetworkAttachment().getNicName() != null) { + nic = Entities.entitiesByName(getHostInterfaces()).get(getParameters().getNetworkAttachment().getNicName()); + } + + HostInterfaceValidator hostInterfaceValidator = new HostInterfaceValidator(nic); + return validate(hostInterfaceValidator.interfaceExists()) + && validate(hostInterfaceValidator.interfaceInHost(getVdsId())) + && validate(hostInterfaceValidator.validBond(getHostInterfaces())) + && validate(hostInterfaceValidator.networkCanBeAttached()); + } + + private boolean validateAttachment() { + ModifiedNetworkAttachmentValidator validator = + new ModifiedNetworkAttachmentValidator(getParameters().getNetworkAttachment(), getVds()); + + return validate(validator.networkAttachmentIsSet()) + && validate(validator.networkAttachmentExists()) + && validate(validator.networkExists()) + && validate(validator.notExternalNetwork()) + && validate(validator.notRemovingManagementNetwork()) + && validate(validator.networkNotUsedByVms()) + && validate(validator.networkAttachedToCluster()) + && validate(validator.ipConfiguredForStaticBootProtocol()) + && validate(validator.bootProtocolSetForDisplayNetwork()) + && validate(validator.nicExists()) + && validate(validator.validateGateway()); + } + + private List<NetworkAttachment> getExpectedNetworkAttachments() { + List<NetworkAttachment> expectedAttachments = new ArrayList<>(); + List<NetworkAttachment> existingAttachments = + getDbFacade().getNetworkAttachmentDao().getAllForHost(getVdsId()); + for (NetworkAttachment existingAttachment : existingAttachments) { + if (!existingAttachment.equals(getParameters().getNetworkAttachment())) { + expectedAttachments.add(existingAttachment); + } + } + + existingAttachments.add(getParameters().getNetworkAttachment()); + return expectedAttachments; + } + + private List<VdsNetworkInterface> getHostInterfaces() { + if (hostNics == null) { + hostNics = getDbFacade().getInterfaceDao().getAllInterfacesForVds(getVdsId()); + } + + return hostNics; + } +} diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/NetworkAttachmentParameters.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/NetworkAttachmentParameters.java new file mode 100644 index 0000000..6d1b8e8 --- /dev/null +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/NetworkAttachmentParameters.java @@ -0,0 +1,28 @@ +package org.ovirt.engine.core.common.action; + +import javax.validation.Valid; +import javax.validation.constraints.NotNull; + +import org.ovirt.engine.core.common.businessentities.network.NetworkAttachment; +import org.ovirt.engine.core.compat.Guid; + +public class NetworkAttachmentParameters extends VdsActionParameters { + + private static final long serialVersionUID = -5132029941161321131L; + + @Valid + @NotNull + private NetworkAttachment networkAttachment; + + public NetworkAttachmentParameters() { + } + + public NetworkAttachmentParameters(Guid hostId, NetworkAttachment networkAttachment) { + super(hostId); + this.networkAttachment = networkAttachment; + } + + public NetworkAttachment getNetworkAttachment() { + return networkAttachment; + } +} diff --git a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java index f49c55c..adf5099 100644 --- a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java +++ b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/action/VdcActionType.java @@ -391,7 +391,10 @@ RemoveMacPool(3102, ActionGroup.DELETE_MAC_POOL, false, QuotaDependency.NONE), // Network Attachments - HostSetupNetworks(3200, ActionGroup.CONFIGURE_HOST_NETWORK, QuotaDependency.NONE); + HostSetupNetworks(3200, ActionGroup.CONFIGURE_HOST_NETWORK, QuotaDependency.NONE), + AddNetworkAttachment(3201, ActionGroup.CONFIGURE_HOST_NETWORK, QuotaDependency.NONE), + UpdateNetworkAttachment(3202, ActionGroup.CONFIGURE_HOST_NETWORK, QuotaDependency.NONE), + RemoveNetworkAttachment(3203, ActionGroup.CONFIGURE_HOST_NETWORK, QuotaDependency.NONE); private int intValue; private ActionGroup actionGroup; -- To view, visit http://gerrit.ovirt.org/34971 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I3c1b7a4e0b5ceacf6ea436c4ede84e414817dbdd Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Moti Asayag <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
