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;
27 import java.util.Iterator;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.UUID;
31 import org.apache.camel.Exchange;
32 import org.json.simple.JSONArray;
33 import org.json.simple.JSONObject;
34 import org.json.simple.parser.JSONParser;
35 import org.json.simple.parser.ParseException;
37 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;
38 import org.onap.clamp.clds.model.dcae.DcaeOperationStatusResponse;
39 import org.onap.clamp.clds.util.JsonUtils;
40 import org.onap.clamp.loop.Loop;
42 public class DcaeComponent extends ExternalComponent {
44 private static final String DCAE_DEPLOYMENT_PREFIX = "CLAMP_";
45 private static final String DEPLOYMENT_PARAMETER = "dcaeDeployParameters";
46 private static final String DCAE_SERVICETYPE_ID = "serviceTypeId";
47 private static final String DCAE_INPUTS = "inputs";
49 public static final ExternalComponentState BLUEPRINT_DEPLOYED = new ExternalComponentState("BLUEPRINT_DEPLOYED",
50 "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop");
51 public static final ExternalComponentState PROCESSING_MICROSERVICE_INSTALLATION = new ExternalComponentState(
52 "PROCESSING_MICROSERVICE_INSTALLATION", "Clamp has requested DCAE to install the microservices "
53 + "defined in the DCAE blueprint and it's currently processing the request");
54 public static final ExternalComponentState MICROSERVICE_INSTALLATION_FAILED = new ExternalComponentState(
55 "MICROSERVICE_INSTALLATION_FAILED",
56 "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it failed");
57 public static final ExternalComponentState MICROSERVICE_INSTALLED_SUCCESSFULLY = new ExternalComponentState(
58 "MICROSERVICE_INSTALLED_SUCCESSFULLY",
59 "Clamp has requested DCAE to install the DCAE blueprint and it has been installed successfully");
60 public static final ExternalComponentState PROCESSING_MICROSERVICE_UNINSTALLATION = new ExternalComponentState(
61 "PROCESSING_MICROSERVICE_UNINSTALLATION", "Clamp has requested DCAE to uninstall the microservices "
62 + "defined in the DCAE blueprint and it's currently processing the request");
63 public static final ExternalComponentState MICROSERVICE_UNINSTALLATION_FAILED = new ExternalComponentState(
64 "MICROSERVICE_UNINSTALLATION_FAILED",
65 "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it failed");
66 public static final ExternalComponentState MICROSERVICE_UNINSTALLED_SUCCESSFULLY = new ExternalComponentState(
67 "MICROSERVICE_UNINSTALLED_SUCCESSFULLY",
68 "Clamp has requested DCAE to uninstall the DCAE blueprint and it has been uninstalled successfully");
69 public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
70 "There was an error during the request done to DCAE, look at the logs or try again");
72 public DcaeComponent() {
73 super(BLUEPRINT_DEPLOYED);
77 public String getComponentName() {
82 * Convert the json response to a DcaeOperationStatusResponse.
84 * @param responseBody The DCAE response Json paylaod
85 * @return The dcae object provisioned
87 public static DcaeOperationStatusResponse convertDcaeResponse(String responseBody) {
88 if (responseBody != null && !responseBody.isEmpty()) {
89 return JsonUtils.GSON_JPA_MODEL.fromJson(responseBody, DcaeOperationStatusResponse.class);
96 * Generate the deployment id, it's random.
98 * @return The deployment id
100 public static String generateDeploymentId() {
101 return DCAE_DEPLOYMENT_PREFIX + UUID.randomUUID();
105 * This method prepare the url returned by DCAE to check the status if fine. It
106 * extracts it from the dcaeResponse.
108 * @param dcaeResponse The dcae response object
109 * @return the Right Url modified if needed
111 public static String getStatusUrl(DcaeOperationStatusResponse dcaeResponse) {
112 return dcaeResponse.getLinks().getStatus().replaceAll("http:", "http4:").replaceAll("https:", "https4:");
116 * Return the deploy payload for DCAE.
118 * @param loop The loop object
119 * @return The payload used to send deploy closed loop request
121 public static String getDeployPayload(Loop loop) {
122 JsonObject globalProp = loop.getGlobalPropertiesJson();
123 JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER);
125 String serviceTypeId = loop.getDcaeBlueprintId();
127 JsonObject rootObject = new JsonObject();
128 rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
129 if (deploymentProp != null) {
130 rootObject.add(DCAE_INPUTS, deploymentProp);
132 return rootObject.toString();
136 * Return the uninstallation payload for DCAE.
138 * @param loop The loop object
139 * @return The payload in string (json)
141 public static String getUndeployPayload(Loop loop) {
142 JsonObject rootObject = new JsonObject();
143 rootObject.addProperty(DCAE_SERVICETYPE_ID, loop.getDcaeBlueprintId());
144 return rootObject.toString();
148 public ExternalComponentState computeState(Exchange camelExchange) {
150 DcaeOperationStatusResponse dcaeResponse = (DcaeOperationStatusResponse) camelExchange.getIn().getExchange()
151 .getProperty("dcaeResponse");
153 if (dcaeResponse == null) {
154 setState(BLUEPRINT_DEPLOYED);
155 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("succeeded")) {
156 setState(MICROSERVICE_INSTALLED_SUCCESSFULLY);
157 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("processing")) {
158 setState(PROCESSING_MICROSERVICE_INSTALLATION);
159 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("failed")) {
160 setState(MICROSERVICE_INSTALLATION_FAILED);
161 } else if (dcaeResponse.getOperationType().equals("uninstall")
162 && dcaeResponse.getStatus().equals("succeeded")) {
163 setState(MICROSERVICE_UNINSTALLED_SUCCESSFULLY);
164 } else if (dcaeResponse.getOperationType().equals("uninstall")
165 && dcaeResponse.getStatus().equals("processing")) {
166 setState(PROCESSING_MICROSERVICE_UNINSTALLATION);
167 } else if (dcaeResponse.getOperationType().equals("uninstall") && dcaeResponse.getStatus().equals("failed")) {
168 setState(MICROSERVICE_UNINSTALLATION_FAILED);
172 return this.getState();
176 * Convert the json response to a DcaeInventoryResponse.
178 * @param responseBody The DCAE response Json paylaod
179 * @return list of DcaeInventoryResponse
180 * @throws ParseException In case of issues with the Json parsing
182 public static List<DcaeInventoryResponse> convertToDcaeInventoryResponse(String responseBody)
183 throws ParseException {
184 JSONParser parser = new JSONParser();
185 JSONObject jsonObj = (JSONObject) parser.parse(responseBody);
186 JSONArray itemsArray = (JSONArray) jsonObj.get("items");
187 Iterator it = itemsArray.iterator();
188 List<DcaeInventoryResponse> inventoryResponseList = new LinkedList<>();
189 while (it.hasNext()) {
190 JSONObject item = (JSONObject) it.next();
191 DcaeInventoryResponse response = JsonUtils.GSON.fromJson(item.toString(), DcaeInventoryResponse.class);
192 inventoryResponseList.add(response);
194 return inventoryResponseList;