2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.loop.components.external;
26 import com.google.gson.JsonObject;
28 import java.util.Iterator;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.UUID;
33 import org.apache.camel.Exchange;
34 import org.json.simple.JSONArray;
35 import org.json.simple.JSONObject;
36 import org.json.simple.parser.JSONParser;
37 import org.json.simple.parser.ParseException;
38 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;
39 import org.onap.clamp.clds.model.dcae.DcaeOperationStatusResponse;
40 import org.onap.clamp.clds.util.JsonUtils;
41 import org.onap.clamp.loop.Loop;
42 import org.onap.clamp.policy.microservice.MicroServicePolicy;
44 public class DcaeComponent extends ExternalComponent {
46 private static final String DCAE_DEPLOYMENT_PREFIX = "CLAMP_";
47 private static final String DEPLOYMENT_PARAMETER = "dcaeDeployParameters";
48 private static final String DCAE_SERVICETYPE_ID = "serviceTypeId";
49 private static final String DCAE_INPUTS = "inputs";
51 public static final ExternalComponentState BLUEPRINT_DEPLOYED = new ExternalComponentState("BLUEPRINT_DEPLOYED",
52 "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop");
53 public static final ExternalComponentState PROCESSING_MICROSERVICE_INSTALLATION = new ExternalComponentState(
54 "PROCESSING_MICROSERVICE_INSTALLATION", "Clamp has requested DCAE to install the microservices "
55 + "defined in the DCAE blueprint and it's currently processing the request");
56 public static final ExternalComponentState MICROSERVICE_INSTALLATION_FAILED = new ExternalComponentState(
57 "MICROSERVICE_INSTALLATION_FAILED",
58 "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it failed");
59 public static final ExternalComponentState MICROSERVICE_INSTALLED_SUCCESSFULLY = new ExternalComponentState(
60 "MICROSERVICE_INSTALLED_SUCCESSFULLY",
61 "Clamp has requested DCAE to install the DCAE blueprint and it has been installed successfully");
62 public static final ExternalComponentState PROCESSING_MICROSERVICE_UNINSTALLATION = new ExternalComponentState(
63 "PROCESSING_MICROSERVICE_UNINSTALLATION", "Clamp has requested DCAE to uninstall the microservices "
64 + "defined in the DCAE blueprint and it's currently processing the request");
65 public static final ExternalComponentState MICROSERVICE_UNINSTALLATION_FAILED = new ExternalComponentState(
66 "MICROSERVICE_UNINSTALLATION_FAILED",
67 "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it failed");
68 public static final ExternalComponentState MICROSERVICE_UNINSTALLED_SUCCESSFULLY = new ExternalComponentState(
69 "MICROSERVICE_UNINSTALLED_SUCCESSFULLY",
70 "Clamp has requested DCAE to uninstall the DCAE blueprint and it has been uninstalled successfully");
71 public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
72 "There was an error during the request done to DCAE, look at the logs or try again");
74 public DcaeComponent() {
75 super(BLUEPRINT_DEPLOYED);
79 public String getComponentName() {
84 * Convert the json response to a DcaeOperationStatusResponse.
86 * @param responseBody The DCAE response Json paylaod
87 * @return The dcae object provisioned
89 public static DcaeOperationStatusResponse convertDcaeResponse(String responseBody) {
90 if (responseBody != null && !responseBody.isEmpty()) {
91 return JsonUtils.GSON_JPA_MODEL.fromJson(responseBody, DcaeOperationStatusResponse.class);
98 * Generate the deployment id, it's random.
100 * @return The deployment id
102 public static String generateDeploymentId() {
103 return DCAE_DEPLOYMENT_PREFIX + UUID.randomUUID();
107 * This method prepare the url returned by DCAE to check the status if fine. It
108 * extracts it from the dcaeResponse.
110 * @param dcaeResponse The dcae response object
111 * @return the Right Url modified if needed
113 public static String getStatusUrl(DcaeOperationStatusResponse dcaeResponse) {
114 return dcaeResponse.getLinks().getStatus().replaceAll("http:", "http4:").replaceAll("https:", "https4:");
118 * Return the deploy payload for DCAE.
120 * @param loop The loop object
121 * @return The payload used to send deploy closed loop request
123 public static String getDeployPayload(Loop loop) {
124 JsonObject globalProp = loop.getGlobalPropertiesJson();
125 JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER);
127 String serviceTypeId = loop.getLoopTemplate().getDcaeBlueprintId();
129 JsonObject rootObject = new JsonObject();
130 rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
131 if (deploymentProp != null) {
132 rootObject.add(DCAE_INPUTS, deploymentProp);
134 return rootObject.toString();
138 * Return the deploy payload for DCAE.
140 * @param loop The loop object
141 * @param microServiceName The micro service name
142 * @return The payload used to send deploy closed loop request
144 public static String getDeployPayload(Loop loop, String microServiceName) {
145 JsonObject globalProp = loop.getGlobalPropertiesJson();
146 JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER).getAsJsonObject(microServiceName);
148 String serviceTypeId = loop.getLoopTemplate().getDcaeBlueprintId();
150 JsonObject rootObject = new JsonObject();
151 rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
152 if (deploymentProp != null) {
153 rootObject.add(DCAE_INPUTS, deploymentProp);
155 return rootObject.toString();
159 * Return the uninstallation payload for DCAE.
161 * @param loop The loop object
162 * @return The payload in string (json)
164 public static String getUndeployPayload(Loop loop) {
165 JsonObject rootObject = new JsonObject();
166 rootObject.addProperty(DCAE_SERVICETYPE_ID, loop.getLoopTemplate().getDcaeBlueprintId());
167 return rootObject.toString();
171 * Return the uninstallation payload for DCAE.
173 * @param microServicePolicy The microServicePolicy object
174 * @return The payload in string (json)
176 public static String getUndeployPayload(MicroServicePolicy policy) {
177 JsonObject rootObject = new JsonObject();
178 rootObject.addProperty(DCAE_SERVICETYPE_ID, policy.getDcaeBlueprintId());
179 return rootObject.toString();
183 public ExternalComponentState computeState(Exchange camelExchange) {
185 DcaeOperationStatusResponse dcaeResponse = (DcaeOperationStatusResponse) camelExchange.getIn().getExchange()
186 .getProperty("dcaeResponse");
188 if (dcaeResponse == null) {
189 setState(BLUEPRINT_DEPLOYED);
190 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("succeeded")) {
191 setState(MICROSERVICE_INSTALLED_SUCCESSFULLY);
192 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("processing")) {
193 setState(PROCESSING_MICROSERVICE_INSTALLATION);
194 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("failed")) {
195 setState(MICROSERVICE_INSTALLATION_FAILED);
196 } else if (dcaeResponse.getOperationType().equals("uninstall")
197 && dcaeResponse.getStatus().equals("succeeded")) {
198 setState(MICROSERVICE_UNINSTALLED_SUCCESSFULLY);
199 } else if (dcaeResponse.getOperationType().equals("uninstall")
200 && dcaeResponse.getStatus().equals("processing")) {
201 setState(PROCESSING_MICROSERVICE_UNINSTALLATION);
202 } else if (dcaeResponse.getOperationType().equals("uninstall") && dcaeResponse.getStatus().equals("failed")) {
203 setState(MICROSERVICE_UNINSTALLATION_FAILED);
207 return this.getState();
211 * Convert the json response to a DcaeInventoryResponse.
213 * @param responseBody The DCAE response Json paylaod
214 * @return list of DcaeInventoryResponse
215 * @throws ParseException In case of issues with the Json parsing
217 public static List<DcaeInventoryResponse> convertToDcaeInventoryResponse(String responseBody)
218 throws ParseException {
219 JSONParser parser = new JSONParser();
220 JSONObject jsonObj = (JSONObject) parser.parse(responseBody);
221 JSONArray itemsArray = (JSONArray) jsonObj.get("items");
222 Iterator it = itemsArray.iterator();
223 List<DcaeInventoryResponse> inventoryResponseList = new LinkedList<>();
224 while (it.hasNext()) {
225 JSONObject item = (JSONObject) it.next();
226 DcaeInventoryResponse response = JsonUtils.GSON.fromJson(item.toString(), DcaeInventoryResponse.class);
227 inventoryResponseList.add(response);
229 return inventoryResponseList;