Replaced all tabs with spaces in java and pom.xml
[so.git] / adapters / mso-adapter-utils / src / main / java / org / onap / so / cloudify / beans / DeploymentInfoBuilder.java
1 /*
2  * ============LICENSE_START======================================================= ONAP : SO
3  * ================================================================================ Copyright (C) 2018 Nokia.
4  * ============================================================================= Licensed under the Apache License,
5  * Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy
6  * of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
11  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
12  * specific language governing permissions and limitations under the License.
13  * ============LICENSE_END=========================================================
14  */
15 package org.onap.so.cloudify.beans;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import org.onap.so.cloudify.v3.model.Execution;
20
21 public final class DeploymentInfoBuilder {
22
23     private String id = "";
24     private DeploymentStatus deploymentStatus = DeploymentStatus.NOTFOUND;
25     private Map<String, Object> deploymentOutputs = new HashMap<>();
26     private Map<String, Object> deploymentInputs = new HashMap<>();
27     private String lastAction;
28     private String actionStatus;
29     private String errorMessage;
30
31     public DeploymentInfoBuilder withId(String id) {
32         this.id = id;
33         return this;
34     }
35
36     public DeploymentInfoBuilder withStatus(DeploymentStatus deploymentStatus) {
37         this.deploymentStatus = deploymentStatus;
38         return this;
39     }
40
41     public DeploymentInfoBuilder withDeploymentOutputs(Map<String, Object> deploymentOutputs) {
42         if (deploymentOutputs != null) {
43             this.deploymentOutputs = deploymentOutputs;
44         }
45         return this;
46     }
47
48     public DeploymentInfoBuilder withDeploymentInputs(Map<String, Object> deploymentInputs) {
49         this.deploymentInputs = deploymentInputs;
50         return this;
51     }
52
53     public DeploymentInfoBuilder withLastAction(String lastAction) {
54         this.lastAction = lastAction;
55         return this;
56     }
57
58     public DeploymentInfoBuilder withActionStatus(String actionStatus) {
59         this.actionStatus = actionStatus;
60         return this;
61     }
62
63     public DeploymentInfoBuilder withErrorMessage(String errorMessage) {
64         this.errorMessage = errorMessage;
65         return this;
66     }
67
68     public DeploymentInfoBuilder fromExecution(Execution execution) {
69         if (execution != null) {
70             this.lastAction = execution.getWorkflowId();
71             this.actionStatus = execution.getStatus();
72             this.errorMessage = execution.getError();
73
74             // Compute the status based on the last workflow
75             if (lastAction.equals("install")) {
76                 if (actionStatus.equals("terminated")) {
77                     this.deploymentStatus = DeploymentStatus.INSTALLED;
78                 } else if (actionStatus.equals("failed")) {
79                     this.deploymentStatus = DeploymentStatus.FAILED;
80                 } else if (actionStatus.equals("started") || actionStatus.equals("pending")) {
81                     this.deploymentStatus = DeploymentStatus.INSTALLING;
82                 } else {
83                     this.deploymentStatus = DeploymentStatus.UNKNOWN;
84                 }
85             } else if (lastAction.equals("uninstall")) {
86                 if (actionStatus.equals("terminated")) {
87                     this.deploymentStatus = DeploymentStatus.CREATED;
88                 } else if (actionStatus.equals("failed")) {
89                     this.deploymentStatus = DeploymentStatus.FAILED;
90                 } else if (actionStatus.equals("started") || actionStatus.equals("pending")) {
91                     this.deploymentStatus = DeploymentStatus.UNINSTALLING;
92                 } else {
93                     this.deploymentStatus = DeploymentStatus.UNKNOWN;
94                 }
95             } else {
96                 // Could have more cases in the future for different actions.
97                 this.deploymentStatus = DeploymentStatus.UNKNOWN;
98             }
99         } else {
100             this.deploymentStatus = DeploymentStatus.CREATED;
101         }
102
103         return this;
104     }
105
106     public DeploymentInfo build() {
107         return new DeploymentInfo(id, deploymentStatus, deploymentOutputs, deploymentInputs, lastAction, actionStatus,
108                 errorMessage);
109     }
110 }