Merge "Validate ids"
[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
35 public class DcaeComponent extends ExternalComponent {
36
37     private static final String DCAE_DEPLOYMENT_PREFIX = "CLAMP_";
38     private static final String DEPLOYMENT_PARAMETER = "dcaeDeployParameters";
39     private static final String DCAE_SERVICETYPE_ID = "serviceTypeId";
40     private static final String DCAE_INPUTS = "inputs";
41
42     public static final ExternalComponentState BLUEPRINT_DEPLOYED = new ExternalComponentState("BLUEPRINT_DEPLOYED",
43         "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop");
44     public static final ExternalComponentState PROCESSING_MICROSERVICE_INSTALLATION = new ExternalComponentState(
45         "PROCESSING_MICROSERVICE_INSTALLATION",
46         "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it's currently processing the request");
47     public static final ExternalComponentState MICROSERVICE_INSTALLATION_FAILED = new ExternalComponentState(
48         "MICROSERVICE_INSTALLATION_FAILED",
49         "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it failed");
50     public static final ExternalComponentState MICROSERVICE_INSTALLED_SUCCESSFULLY = new ExternalComponentState(
51         "MICROSERVICE_INSTALLED_SUCCESSFULLY",
52         "Clamp has requested DCAE to install the DCAE blueprint and it has been installed successfully");
53     public static final ExternalComponentState PROCESSING_MICROSERVICE_UNINSTALLATION = new ExternalComponentState(
54         "PROCESSING_MICROSERVICE_UNINSTALLATION",
55         "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it's currently processing the request");
56     public static final ExternalComponentState MICROSERVICE_UNINSTALLATION_FAILED = new ExternalComponentState(
57         "MICROSERVICE_UNINSTALLATION_FAILED",
58         "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it failed");
59     public static final ExternalComponentState MICROSERVICE_UNINSTALLED_SUCCESSFULLY = new ExternalComponentState(
60         "MICROSERVICE_UNINSTALLED_SUCCESSFULLY",
61         "Clamp has requested DCAE to uninstall the DCAE blueprint and it has been uninstalled successfully");
62     public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
63         "There was an error during the request done to DCAE, look at the logs or try again");
64
65     public DcaeComponent() {
66         super(BLUEPRINT_DEPLOYED);
67     }
68
69     @Override
70     public String getComponentName() {
71         return "DCAE";
72     }
73
74     public static DcaeOperationStatusResponse convertDcaeResponse(String responseBody) {
75         if (responseBody != null && !responseBody.isEmpty()) {
76             return JsonUtils.GSON_JPA_MODEL.fromJson(responseBody, DcaeOperationStatusResponse.class);
77         } else {
78             return null;
79         }
80     }
81
82     /**
83      * Generate the deployment id, it's random
84      *
85      * @return The deployment id
86      */
87     public static String generateDeploymentId() {
88         return DCAE_DEPLOYMENT_PREFIX + UUID.randomUUID();
89     }
90
91     /**
92      * This method prepare the url returned by DCAE to check the status if fine.
93      *
94      * @param statusUrl
95      * @return the Right Url modified if needed
96      */
97     public static String getStatusUrl(DcaeOperationStatusResponse dcaeResponse) {
98         return dcaeResponse.getLinks().getStatus().replaceAll("http:", "http4:").replaceAll("https:", "https4:");
99     }
100
101     /**
102      * Return the deploy payload for DCAE.
103      *
104      * @param loop
105      *        The loop object
106      * @return The payload used to send deploy closed loop request
107      */
108     public static String getDeployPayload(Loop loop) {
109         JsonObject globalProp = loop.getGlobalPropertiesJson();
110         JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER);
111
112         String serviceTypeId = loop.getDcaeBlueprintId();
113
114         JsonObject rootObject = new JsonObject();
115         rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
116         if (deploymentProp != null) {
117             rootObject.add(DCAE_INPUTS, deploymentProp);
118         }
119         return rootObject.toString();
120     }
121
122     /**
123      * Return the uninstallation payload for DCAE.
124      *
125      * @param loop
126      *        The loop object
127      * @return The payload in string (json)
128      */
129     public static String getUndeployPayload(Loop loop) {
130         JsonObject rootObject = new JsonObject();
131         rootObject.addProperty(DCAE_SERVICETYPE_ID, loop.getDcaeBlueprintId());
132         return rootObject.toString();
133     }
134
135     @Override
136     public ExternalComponentState computeState(Exchange camelExchange) {
137
138         DcaeOperationStatusResponse dcaeResponse = (DcaeOperationStatusResponse) camelExchange.getIn().getExchange()
139             .getProperty("dcaeResponse");
140
141         if (dcaeResponse == null) {
142             setState(BLUEPRINT_DEPLOYED);
143         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("succeeded")) {
144             setState(MICROSERVICE_INSTALLED_SUCCESSFULLY);
145         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("processing")) {
146             setState(PROCESSING_MICROSERVICE_INSTALLATION);
147         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("failed")) {
148             setState(MICROSERVICE_INSTALLATION_FAILED);
149         } else if (dcaeResponse.getOperationType().equals("uninstall")
150             && dcaeResponse.getStatus().equals("succeeded")) {
151             setState(MICROSERVICE_UNINSTALLED_SUCCESSFULLY);
152         } else if (dcaeResponse.getOperationType().equals("uninstall")
153             && dcaeResponse.getStatus().equals("processing")) {
154             setState(PROCESSING_MICROSERVICE_UNINSTALLATION);
155         } else if (dcaeResponse.getOperationType().equals("uninstall") && dcaeResponse.getStatus().equals("failed")) {
156             setState(MICROSERVICE_UNINSTALLATION_FAILED);
157         } else {
158             setState(IN_ERROR);
159         }
160         return this.getState();
161     }
162 }