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