Checkstyle fixes
[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", "Clamp has requested DCAE to install the microservices "
46                     + "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", "Clamp has requested DCAE to uninstall the microservices "
55                     + "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     /**
75      * Convert the json response to a DcaeOperationStatusResponse.
76      * 
77      * @param responseBody The DCAE response Json paylaod
78      * @return The dcae object provisioned
79      */
80     public static DcaeOperationStatusResponse convertDcaeResponse(String responseBody) {
81         if (responseBody != null && !responseBody.isEmpty()) {
82             return JsonUtils.GSON_JPA_MODEL.fromJson(responseBody, DcaeOperationStatusResponse.class);
83         } else {
84             return null;
85         }
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 uninstallation payload for DCAE.
130      *
131      * @param loop The loop object
132      * @return The payload in string (json)
133      */
134     public static String getUndeployPayload(Loop loop) {
135         JsonObject rootObject = new JsonObject();
136         rootObject.addProperty(DCAE_SERVICETYPE_ID, loop.getDcaeBlueprintId());
137         return rootObject.toString();
138     }
139
140     @Override
141     public ExternalComponentState computeState(Exchange camelExchange) {
142
143         DcaeOperationStatusResponse dcaeResponse = (DcaeOperationStatusResponse) camelExchange.getIn().getExchange()
144                 .getProperty("dcaeResponse");
145
146         if (dcaeResponse == null) {
147             setState(BLUEPRINT_DEPLOYED);
148         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("succeeded")) {
149             setState(MICROSERVICE_INSTALLED_SUCCESSFULLY);
150         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("processing")) {
151             setState(PROCESSING_MICROSERVICE_INSTALLATION);
152         } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("failed")) {
153             setState(MICROSERVICE_INSTALLATION_FAILED);
154         } else if (dcaeResponse.getOperationType().equals("uninstall")
155                 && dcaeResponse.getStatus().equals("succeeded")) {
156             setState(MICROSERVICE_UNINSTALLED_SUCCESSFULLY);
157         } else if (dcaeResponse.getOperationType().equals("uninstall")
158                 && dcaeResponse.getStatus().equals("processing")) {
159             setState(PROCESSING_MICROSERVICE_UNINSTALLATION);
160         } else if (dcaeResponse.getOperationType().equals("uninstall") && dcaeResponse.getStatus().equals("failed")) {
161             setState(MICROSERVICE_UNINSTALLATION_FAILED);
162         } else {
163             setState(IN_ERROR);
164         }
165         return this.getState();
166     }
167 }