From 8548b9a6401c1397f3a9cdf6bca98ff56971247d Mon Sep 17 00:00:00 2001 From: Michal Kabaj Date: Mon, 17 Sep 2018 15:10:43 +0200 Subject: DeploymentInfo tests and construction improvement - Improved construction by adding DeploymentInfoBuilder - DeploymentInfo immutable - added detailed unit tests Change-Id: I3dabb91d5190ef932e77f0c76e18fde8ca73c64d Issue-ID: SO-1055 Signed-off-by: Michal Kabaj --- .../org/onap/so/cloudify/beans/DeploymentInfo.java | 220 +++++++-------------- .../so/cloudify/beans/DeploymentInfoBuilder.java | 118 +++++++++++ .../onap/so/cloudify/utils/MsoCloudifyUtils.java | 60 ++++-- 3 files changed, 226 insertions(+), 172 deletions(-) create mode 100644 adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/beans/DeploymentInfoBuilder.java (limited to 'adapters/mso-adapter-utils/src/main') diff --git a/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/beans/DeploymentInfo.java b/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/beans/DeploymentInfo.java index c6e29d05d7..d2b3334c8c 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/beans/DeploymentInfo.java +++ b/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/beans/DeploymentInfo.java @@ -4,12 +4,14 @@ * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * ================================================================================ + * Copyright (C) 2018 Nokia. + * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,166 +22,78 @@ package org.onap.so.cloudify.beans; -import java.util.HashMap; import java.util.Map; -import org.onap.so.cloudify.v3.model.Deployment; -import org.onap.so.cloudify.v3.model.DeploymentOutputs; -import org.onap.so.cloudify.v3.model.Execution; - /* * This Java bean class relays Heat stack status information to ActiveVOS processes. - * + * * This bean is returned by all Heat-specific adapter operations (create, query, delete) */ -public class DeploymentInfo { - // Set defaults for everything - private String id = ""; - private DeploymentStatus status = DeploymentStatus.NOTFOUND; - private Map outputs = new HashMap(); - private Map inputs = new HashMap(); - private String lastAction; - private String actionStatus; - private String errorMessage; - - public DeploymentInfo () { - } - - public DeploymentInfo (String id, Map outputs) { - this.id = id; - if (outputs != null) this.outputs = outputs; - } - - public DeploymentInfo (String id) { - this.id = id; - } - - public DeploymentInfo (String id, DeploymentStatus status) { - this.id = id; - this.status = status; - } - - public DeploymentInfo (Deployment deployment) { - this(deployment, null, null); - } - - /** - * Construct a DeploymentInfo object from a deployment and the latest Execution action - * @param deployment - * @param execution - */ - public DeploymentInfo (Deployment deployment, DeploymentOutputs outputs, Execution execution) - { - if (deployment == null) { - this.id = null; - return; - } - - this.id = deployment.getId(); - - if (outputs != null) - this.outputs = outputs.getOutputs(); - - if (deployment.getInputs() != null) - this.inputs = deployment.getInputs(); - - if (execution != null) { - this.lastAction = execution.getWorkflowId(); - this.actionStatus = execution.getStatus(); - this.errorMessage = execution.getError(); - - // Compute the status based on the last workflow - if (lastAction.equals("install")) { - if (actionStatus.equals("terminated")) - this.status = DeploymentStatus.INSTALLED; - else if (actionStatus.equals("failed")) - this.status = DeploymentStatus.FAILED; - else if (actionStatus.equals("started") || actionStatus.equals("pending")) - this.status = DeploymentStatus.INSTALLING; - else - this.status = DeploymentStatus.UNKNOWN; - } - else if (lastAction.equals("uninstall")) { - if (actionStatus.equals("terminated")) - this.status = DeploymentStatus.CREATED; - else if (actionStatus.equals("failed")) - this.status = DeploymentStatus.FAILED; - else if (actionStatus.equals("started") || actionStatus.equals("pending")) - this.status = DeploymentStatus.UNINSTALLING; - else - this.status = DeploymentStatus.UNKNOWN; - } - else { - // Could have more cases in the future for different actions. - this.status = DeploymentStatus.UNKNOWN; - } - } - else { - this.status = DeploymentStatus.CREATED; - } - } - - public String getId() { - return id; - } - - public void setId (String id) { - this.id = id; - } - - public DeploymentStatus getStatus() { - return status; - } - - public void setStatus (DeploymentStatus status) { - this.status = status; - } - - public Map getOutputs () { - return outputs; - } - - public void setOutputs (Map outputs) { - this.outputs = outputs; - } - - public Map getInputs () { - return inputs; - } - - public void setInputs (Map inputs) { - this.inputs = inputs; - } - - public String getLastAction() { - return lastAction; - } - - public String getActionStatus() { - return actionStatus; - } - - public String getErrorMessage() { - return errorMessage; - } - - public void saveExecutionStatus (Execution execution) { - this.lastAction = execution.getWorkflowId(); - this.actionStatus = execution.getStatus(); - this.errorMessage = execution.getError(); - } - - @Override +public final class DeploymentInfo { + + private final String id; + private final DeploymentStatus status; + private final Map outputs; + private final Map inputs; + private final String lastAction; + private final String actionStatus; + private final String errorMessage; + + DeploymentInfo(String id, DeploymentStatus deploymentStatus, + Map deploymentOutputs, + Map deploymentInputs, + String lastAction, + String actionStatus, + String errorMessage) { + + this.id = id; + this.status = deploymentStatus; + this.outputs = deploymentOutputs; + this.inputs = deploymentInputs; + this.lastAction = lastAction; + this.actionStatus = actionStatus; + this.errorMessage = errorMessage; + } + + public String getId() { + return id; + } + + public DeploymentStatus getStatus() { + return status; + } + + public Map getOutputs() { + return outputs; + } + + public Map getInputs() { + return inputs; + } + + public String getLastAction() { + return lastAction; + } + + public String getActionStatus() { + return actionStatus; + } + + public String getErrorMessage() { + return errorMessage; + } + + @Override public String toString() { return "DeploymentInfo {" + - "id='" + id + '\'' + - ", inputs='" + inputs + '\'' + - ", outputs='" + outputs + '\'' + - ", lastAction='" + lastAction + '\'' + - ", status='" + status + '\'' + - ", errorMessage='" + errorMessage + '\'' + - '}'; + "id='" + id + '\'' + + ", inputs='" + inputs + '\'' + + ", outputs='" + outputs + '\'' + + ", lastAction='" + lastAction + '\'' + + ", status='" + status + '\'' + + ", errorMessage='" + errorMessage + '\'' + + '}'; } } diff --git a/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/beans/DeploymentInfoBuilder.java b/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/beans/DeploymentInfoBuilder.java new file mode 100644 index 0000000000..2e12869c95 --- /dev/null +++ b/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/beans/DeploymentInfoBuilder.java @@ -0,0 +1,118 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : SO + * ================================================================================ + * Copyright (C) 2018 Nokia. + * ============================================================================= + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.so.cloudify.beans; + +import java.util.HashMap; +import java.util.Map; +import org.onap.so.cloudify.v3.model.Execution; + +public final class DeploymentInfoBuilder { + + private String id = ""; + private DeploymentStatus deploymentStatus = DeploymentStatus.NOTFOUND; + private Map deploymentOutputs = new HashMap<>(); + private Map deploymentInputs = new HashMap<>(); + private String lastAction; + private String actionStatus; + private String errorMessage; + + public DeploymentInfoBuilder withId(String id) { + this.id = id; + return this; + } + + public DeploymentInfoBuilder withStatus(DeploymentStatus deploymentStatus) { + this.deploymentStatus = deploymentStatus; + return this; + } + + public DeploymentInfoBuilder withDeploymentOutputs(Map deploymentOutputs) { + this.deploymentOutputs = deploymentOutputs; + return this; + } + + public DeploymentInfoBuilder withDeploymentInputs(Map deploymentInputs) { + this.deploymentInputs = deploymentInputs; + return this; + } + + public DeploymentInfoBuilder withLastAction(String lastAction) { + this.lastAction = lastAction; + return this; + } + + public DeploymentInfoBuilder withActionStatus(String actionStatus) { + this.actionStatus = actionStatus; + return this; + } + + public DeploymentInfoBuilder withErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + return this; + } + + public DeploymentInfoBuilder fromExecution(Execution execution) { + if (execution != null) { + this.lastAction = execution.getWorkflowId(); + this.actionStatus = execution.getStatus(); + this.errorMessage = execution.getError(); + + // Compute the status based on the last workflow + if (lastAction.equals("install")) { + if (actionStatus.equals("terminated")) { + this.deploymentStatus = DeploymentStatus.INSTALLED; + } else if (actionStatus.equals("failed")) { + this.deploymentStatus = DeploymentStatus.FAILED; + } else if (actionStatus.equals("started") || actionStatus.equals("pending")) { + this.deploymentStatus = DeploymentStatus.INSTALLING; + } else { + this.deploymentStatus = DeploymentStatus.UNKNOWN; + } + } else if (lastAction.equals("uninstall")) { + if (actionStatus.equals("terminated")) { + this.deploymentStatus = DeploymentStatus.CREATED; + } else if (actionStatus.equals("failed")) { + this.deploymentStatus = DeploymentStatus.FAILED; + } else if (actionStatus.equals("started") || actionStatus.equals("pending")) { + this.deploymentStatus = DeploymentStatus.UNINSTALLING; + } else { + this.deploymentStatus = DeploymentStatus.UNKNOWN; + } + } else { + // Could have more cases in the future for different actions. + this.deploymentStatus = DeploymentStatus.UNKNOWN; + } + } else { + this.deploymentStatus = DeploymentStatus.CREATED; + } + + return this; + } + + public DeploymentInfo build() { + return new DeploymentInfo(id, + deploymentStatus, + deploymentOutputs, + deploymentInputs, + lastAction, + actionStatus, + errorMessage); + } +} diff --git a/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/utils/MsoCloudifyUtils.java b/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/utils/MsoCloudifyUtils.java index 677f6395ff..85abf9403c 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/utils/MsoCloudifyUtils.java +++ b/adapters/mso-adapter-utils/src/main/java/org/onap/so/cloudify/utils/MsoCloudifyUtils.java @@ -4,6 +4,8 @@ * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * ================================================================================ + * Copyright (C) 2018 Nokia. + * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -20,6 +22,9 @@ package org.onap.so.cloudify.utils; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -30,7 +35,6 @@ import java.util.Map; import java.util.Optional; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; - import org.onap.so.adapters.vdu.CloudInfo; import org.onap.so.adapters.vdu.PluginAction; import org.onap.so.adapters.vdu.VduArtifact; @@ -42,14 +46,13 @@ import org.onap.so.adapters.vdu.VduPlugin; import org.onap.so.adapters.vdu.VduStateType; import org.onap.so.adapters.vdu.VduStatus; import org.onap.so.cloud.CloudConfig; -import org.onap.so.db.catalog.beans.CloudSite; -import org.onap.so.db.catalog.beans.CloudifyManager; import org.onap.so.cloudify.base.client.CloudifyBaseException; import org.onap.so.cloudify.base.client.CloudifyClientTokenProvider; import org.onap.so.cloudify.base.client.CloudifyConnectException; import org.onap.so.cloudify.base.client.CloudifyRequest; import org.onap.so.cloudify.base.client.CloudifyResponseException; import org.onap.so.cloudify.beans.DeploymentInfo; +import org.onap.so.cloudify.beans.DeploymentInfoBuilder; import org.onap.so.cloudify.beans.DeploymentStatus; import org.onap.so.cloudify.exceptions.MsoCloudifyException; import org.onap.so.cloudify.exceptions.MsoCloudifyManagerNotFound; @@ -77,6 +80,8 @@ import org.onap.so.cloudify.v3.model.Executions; import org.onap.so.cloudify.v3.model.OpenstackConfig; import org.onap.so.cloudify.v3.model.StartExecutionParams; import org.onap.so.config.beans.PoConfig; +import org.onap.so.db.catalog.beans.CloudSite; +import org.onap.so.db.catalog.beans.CloudifyManager; import org.onap.so.db.catalog.beans.HeatTemplateParam; import org.onap.so.logger.MessageEnum; import org.onap.so.logger.MsoAlarmLogger; @@ -93,10 +98,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; -import com.fasterxml.jackson.core.JsonParseException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - @Component public class MsoCloudifyUtils extends MsoCommonUtils implements VduPlugin{ @@ -155,7 +156,6 @@ public class MsoCloudifyUtils extends MsoCommonUtils implements VduPlugin{ * @param inputs A map of key/value inputs * @param pollForCompletion Indicator that polling should be handled in Java vs. in the client * @param timeoutMinutes Timeout after which the "install" will be cancelled - * @param environment An optional yaml-format string to specify environmental parameters * @param backout Flag to delete deployment on install Failure - defaulted to True * @return A DeploymentInfo object * @throws MsoCloudifyException Thrown if the Cloudify API call returns an exception. @@ -256,7 +256,12 @@ public class MsoCloudifyUtils extends MsoCommonUtils implements VduPlugin{ // Success! // Create and return a DeploymentInfo structure. Include the Runtime outputs DeploymentOutputs outputs = getDeploymentOutputs (cloudify, deploymentId); - return new DeploymentInfo (deployment, outputs, installWorkflow); + return new DeploymentInfoBuilder() + .withId(deployment.getId()) + .withDeploymentInputs(deployment.getInputs()) + .withDeploymentOutputs(outputs.getOutputs()) + .fromExecution(installWorkflow) + .build(); } else { // The workflow completed with errors. Must try to back it out. @@ -538,7 +543,6 @@ public class MsoCloudifyUtils extends MsoCommonUtils implements VduPlugin{ * * @param tenantId The Openstack ID of the tenant in which to query * @param cloudSiteId The cloud identifier (may be a region) in which to query - * @param stackName The name of the stack to query (may be simple or canonical) * @return A StackInfo object * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception. */ @@ -556,7 +560,7 @@ public class MsoCloudifyUtils extends MsoCommonUtils implements VduPlugin{ Cloudify cloudify = getCloudifyClient (cloudSite.get()); // Build and send the Cloudify request - Deployment deployment = null; + Deployment deployment = new Deployment(); DeploymentOutputs outputs = null; try { GetDeployment queryDeployment = cloudify.deployments().byId(deploymentId); @@ -573,10 +577,18 @@ public class MsoCloudifyUtils extends MsoCommonUtils implements VduPlugin{ // If no executions, does this give NOT_FOUND or empty set? if (executions.getItems().isEmpty()) { - return new DeploymentInfo (deployment); + return new DeploymentInfoBuilder() + .withId(deployment.getId()) + .withDeploymentInputs(deployment.getInputs()) + .build(); } else { - return new DeploymentInfo (deployment, outputs, executions.getItems().get(0)); + return new DeploymentInfoBuilder() + .withId(deployment.getId()) + .withDeploymentInputs(deployment.getInputs()) + .withDeploymentOutputs(outputs.getOutputs()) + .fromExecution(executions.getItems().get(0)) + .build(); } } catch (CloudifyConnectException ce) { @@ -589,10 +601,14 @@ public class MsoCloudifyUtils extends MsoCommonUtils implements VduPlugin{ // Got a NOT FOUND error. React differently based on deployment vs. execution if (deployment != null) { // Got NOT_FOUND on the executions. Assume this is a valid "empty" set - return new DeploymentInfo (deployment, outputs, null); + return new DeploymentInfoBuilder() + .withId(deployment.getId()) + .withDeploymentInputs(deployment.getInputs()) + .withDeploymentOutputs(outputs.getOutputs()) + .build(); } else { // Deployment not found. Default status of a DeploymentInfo object is NOTFOUND - return new DeploymentInfo (deploymentId); + return new DeploymentInfoBuilder().withId(deploymentId).build(); } } throw new MsoCloudifyException (re.getStatus(), re.getMessage(), re.getLocalizedMessage(), re); @@ -615,8 +631,6 @@ public class MsoCloudifyUtils extends MsoCommonUtils implements VduPlugin{ * * @param tenantId The Openstack ID of the tenant in which to perform the delete * @param cloudSiteId The cloud identifier (may be a region) from which to delete the stack. - * @param stackName The name/id of the stack to delete. May be simple or canonical - * @param pollForCompletion Indicator that polling should be handled in Java vs. in the client * @return A StackInfo object * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception. * @throws MsoCloudSiteNotFound @@ -651,7 +665,10 @@ public class MsoCloudifyUtils extends MsoCommonUtils implements VduPlugin{ // Deployment doesn't exist. Return a "NOTFOUND" DeploymentInfo object // TODO: Should return NULL? LOGGER.debug("Deployment requested for deletion does not exist: " + deploymentId); - return new DeploymentInfo (deploymentId, DeploymentStatus.NOTFOUND); + return new DeploymentInfoBuilder() + .withId(deploymentId) + .withStatus(DeploymentStatus.NOTFOUND) + .build(); } else { // Convert the CloudifyResponseException to an MsoOpenstackException LOGGER.debug("ERROR STATUS = " + e.getStatus() + ",\n" + e.getMessage() + "\n" + e.getLocalizedMessage()); @@ -741,7 +758,12 @@ public class MsoCloudifyUtils extends MsoCommonUtils implements VduPlugin{ } // Return the deleted deployment info (with runtime outputs) along with the completed uninstall workflow status - return new DeploymentInfo (deployment, outputs, uninstallWorkflow); + return new DeploymentInfoBuilder() + .withId(deployment.getId()) + .withDeploymentInputs(deployment.getInputs()) + .withDeploymentOutputs(outputs.getOutputs()) + .fromExecution(uninstallWorkflow) + .build(); } -- cgit 1.2.3-korg