Update deploy-loop route
[clamp.git] / src / main / java / org / onap / clamp / loop / components / external / DcaeComponent.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop.components.external;
25
26 import com.google.gson.JsonObject;
27
28 import java.util.UUID;
29
30 import org.apache.camel.Exchange;
31 import org.onap.clamp.clds.model.dcae.DcaeOperationStatusResponse;
32 import org.onap.clamp.clds.util.JsonUtils;
33 import org.onap.clamp.loop.Loop;
34 import org.onap.clamp.policy.microservice.MicroServicePolicy;
35
36 public class DcaeComponent extends ExternalComponent {
37
38     private static final String DCAE_DEPLOYMENT_PREFIX = "CLAMP_";
39     private static final String DEPLOYMENT_PARAMETER = "dcaeDeployParameters";
40     private static final String DCAE_SERVICETYPE_ID = "serviceTypeId";
41     private static final String DCAE_INPUTS = "inputs";
42
43     public static final ExternalComponentState BLUEPRINT_DEPLOYED = new ExternalComponentState("BLUEPRINT_DEPLOYED",
44             "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop");
45     public static final ExternalComponentState PROCESSING_MICROSERVICE_INSTALLATION = new ExternalComponentState(
46             "PROCESSING_MICROSERVICE_INSTALLATION", "Clamp has requested DCAE to install the microservices "
47                     + "defined in the DCAE blueprint and it's currently processing the request");
48     public static final ExternalComponentState MICROSERVICE_INSTALLATION_FAILED = new ExternalComponentState(
49             "MICROSERVICE_INSTALLATION_FAILED",
50             "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it failed");
51     public static final ExternalComponentState MICROSERVICE_INSTALLED_SUCCESSFULLY = new ExternalComponentState(
52             "MICROSERVICE_INSTALLED_SUCCESSFULLY",
53             "Clamp has requested DCAE to install the DCAE blueprint and it has been installed successfully");
54     public static final ExternalComponentState PROCESSING_MICROSERVICE_UNINSTALLATION = new ExternalComponentState(
55             "PROCESSING_MICROSERVICE_UNINSTALLATION", "Clamp has requested DCAE to uninstall the microservices "
56                     + "defined in the DCAE blueprint and it's currently processing the request");
57     public static final ExternalComponentState MICROSERVICE_UNINSTALLATION_FAILED = new ExternalComponentState(
58             "MICROSERVICE_UNINSTALLATION_FAILED",
59             "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it failed");
60     public static final ExternalComponentState MICROSERVICE_UNINSTALLED_SUCCESSFULLY = new ExternalComponentState(
61             "MICROSERVICE_UNINSTALLED_SUCCESSFULLY",
62             "Clamp has requested DCAE to uninstall the DCAE blueprint and it has been uninstalled successfully");
63     public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
64             "There was an error during the request done to DCAE, look at the logs or try again");
65
66     public DcaeComponent() {
67         super(BLUEPRINT_DEPLOYED);
68     }
69
70     @Override
71     public String getComponentName() {
72         return "DCAE";
73     }
74
75     /**
76      * Convert the json response to a DcaeOperationStatusResponse.
77      * 
78      * @param responseBody The DCAE response Json paylaod
79      * @return The dcae object provisioned
80      */
81     public static DcaeOperationStatusResponse convertDcaeResponse(String responseBody) {
82         if (responseBody != null && !responseBody.isEmpty()) {
83             return JsonUtils.GSON_JPA_MODEL.fromJson(responseBody, DcaeOperationStatusResponse.class);
84         } else {
85             return null;
86         }
87     }
88     /**
89      * Generate the deployment id, it's random.
90      *
91      * @return The deployment id
92      */
93     public static String generateDeploymentId() {
94         return DCAE_DEPLOYMENT_PREFIX + UUID.randomUUID();
95     }
96
97     /**
98      * This method prepare the url returned by DCAE to check the status if fine. It
99      * extracts it from the dcaeResponse.
100      *
101      * @param dcaeResponse The dcae response object
102      * @return the Right Url modified if needed
103      */
104     public static String getStatusUrl(DcaeOperationStatusResponse dcaeResponse) {
105         return dcaeResponse.getLinks().getStatus().replaceAll("http:", "http4:").replaceAll("https:", "https4:");
106     }
107
108     /**
109      * Return the deploy payload for DCAE.
110      *
111      * @param loop The loop object
112      * @return The payload used to send deploy closed loop request
113      */
114     public static String getDeployPayload(Loop loop) {
115         JsonObject globalProp = loop.getGlobalPropertiesJson();
116         JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER);
117
118         String serviceTypeId = loop.getDcaeBlueprintId();
119
120         JsonObject rootObject = new JsonObject();
121         rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
122         if (deploymentProp != null) {
123             rootObject.add(DCAE_INPUTS, deploymentProp);
124         }
125         return rootObject.toString();
126     }
127
128     /**
129      * Return the deploy payload for DCAE.
130      *
131      * @param loop The loop object
132      * @param microServiceName The micro service name
133      * @return The payload used to send deploy closed loop request
134      */
135     public static String getDeployPayload(Loop loop, String microServiceName) {
136         JsonObject globalProp = loop.getGlobalPropertiesJson();
137         JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER).getAsJsonObject(microServiceName);
138
139         String serviceTypeId = loop.getDcaeBlueprintId();
140
141         JsonObject rootObject = new JsonObject();
142         rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
143         if (deploymentProp != null) {
144             rootObject.add(DCAE_INPUTS, deploymentProp);
145         }
146         return rootObject.toString();
147     }
148
149     /**
150      * Return the uninstallation payload for DCAE.
151      *
152      * @param loop The loop object
153      * @return The payload in string (json)
154      */
155     public static String getUndeployPayload(Loop loop) {
156         JsonObject rootObject = new JsonObject();
157         rootObject.addProperty(DCAE_SERVICETYPE_ID, loop.getDcaeBlueprintId());
158         return rootObject.toString();
159     }
160
161     @Override
162     public ExternalComponentState computeState(Exchange camelExchange) {
163
164         DcaeOperationStatusResponse dcaeResponse = (DcaeOperationStatusResponse) camelExchange.getIn().getExchange()
165                 .getProperty("dcaeResponse");
166
167         if (dcaeResponse == null) {
168             setState(BLUEPRINT_DEPLOYED);
169         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("succeeded")) {
170             setState(MICROSERVICE_INSTALLED_SUCCESSFULLY);
171         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("processing")) {
172             setState(PROCESSING_MICROSERVICE_INSTALLATION);
173         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("failed")) {
174             setState(MICROSERVICE_INSTALLATION_FAILED);
175         } else if (dcaeResponse.getOperationType().equals("uninstall")
176                 && dcaeResponse.getStatus().equals("succeeded")) {
177             setState(MICROSERVICE_UNINSTALLED_SUCCESSFULLY);
178         } else if (dcaeResponse.getOperationType().equals("uninstall")
179                 && dcaeResponse.getStatus().equals("processing")) {
180             setState(PROCESSING_MICROSERVICE_UNINSTALLATION);
181         } else if (dcaeResponse.getOperationType().equals("uninstall") && dcaeResponse.getStatus().equals("failed")) {
182             setState(MICROSERVICE_UNINSTALLATION_FAILED);
183         } else {
184             setState(IN_ERROR);
185         }
186         return this.getState();
187     }
188 }