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.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.google.gson.JsonObject;
30 import java.util.Iterator;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.UUID;
35 import javax.persistence.Transient;
36 import org.apache.camel.Exchange;
37 import org.json.simple.JSONArray;
38 import org.json.simple.JSONObject;
39 import org.json.simple.parser.JSONParser;
40 import org.json.simple.parser.ParseException;
41 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;
42 import org.onap.clamp.clds.model.dcae.DcaeOperationStatusResponse;
43 import org.onap.clamp.clds.util.JsonUtils;
44 import org.onap.clamp.loop.Loop;
45 import org.onap.clamp.policy.microservice.MicroServicePolicy;
47 public class DcaeComponent extends ExternalComponent {
50 private static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeComponent.class);
52 private static final String DCAE_DEPLOYMENT_PREFIX = "CLAMP_";
53 private static final String DEPLOYMENT_PARAMETER = "dcaeDeployParameters";
54 private static final String DCAE_SERVICETYPE_ID = "serviceTypeId";
55 private static final String DCAE_INPUTS = "inputs";
56 public static final String UNIQUE_BLUEPRINT_PARAMETERS = "uniqueBlueprintParameters";
60 public static final ExternalComponentState BLUEPRINT_DEPLOYED = new ExternalComponentState("BLUEPRINT_DEPLOYED",
61 "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop");
62 public static final ExternalComponentState PROCESSING_MICROSERVICE_INSTALLATION = new ExternalComponentState(
63 "PROCESSING_MICROSERVICE_INSTALLATION", "Clamp has requested DCAE to install the microservices "
64 + "defined in the DCAE blueprint and it's currently processing the request");
65 public static final ExternalComponentState MICROSERVICE_INSTALLATION_FAILED = new ExternalComponentState(
66 "MICROSERVICE_INSTALLATION_FAILED",
67 "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it failed");
68 public static final ExternalComponentState MICROSERVICE_INSTALLED_SUCCESSFULLY = new ExternalComponentState(
69 "MICROSERVICE_INSTALLED_SUCCESSFULLY",
70 "Clamp has requested DCAE to install the DCAE blueprint and it has been installed successfully");
71 public static final ExternalComponentState PROCESSING_MICROSERVICE_UNINSTALLATION = new ExternalComponentState(
72 "PROCESSING_MICROSERVICE_UNINSTALLATION", "Clamp has requested DCAE to uninstall the microservices "
73 + "defined in the DCAE blueprint and it's currently processing the request");
74 public static final ExternalComponentState MICROSERVICE_UNINSTALLATION_FAILED = new ExternalComponentState(
75 "MICROSERVICE_UNINSTALLATION_FAILED",
76 "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it failed");
77 public static final ExternalComponentState MICROSERVICE_UNINSTALLED_SUCCESSFULLY = new ExternalComponentState(
78 "MICROSERVICE_UNINSTALLED_SUCCESSFULLY",
79 "Clamp has requested DCAE to uninstall the DCAE blueprint and it has been uninstalled successfully");
80 public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
81 "There was an error during the request done to DCAE, look at the logs or try again");
83 public DcaeComponent() {
84 super(BLUEPRINT_DEPLOYED);
88 public DcaeComponent(String name) {
89 super(BLUEPRINT_DEPLOYED);
90 this.name = "DCAE_" + name;
94 public String getComponentName() {
100 * Convert the json response to a DcaeOperationStatusResponse.
102 * @param responseBody The DCAE response Json paylaod
103 * @return The dcae object provisioned
105 public static DcaeOperationStatusResponse convertDcaeResponse(String responseBody) {
106 if (responseBody != null && !responseBody.isEmpty()) {
107 return JsonUtils.GSON_JPA_MODEL.fromJson(responseBody, DcaeOperationStatusResponse.class);
114 * Generate the deployment id, it's random.
116 * @return The deployment id
118 public static String generateDeploymentId() {
119 return DCAE_DEPLOYMENT_PREFIX + UUID.randomUUID();
123 * This method prepare the url returned by DCAE to check the status if fine. It
124 * extracts it from the dcaeResponse.
126 * @param dcaeResponse The dcae response object
127 * @return the Right Url modified if needed
129 public static String getStatusUrl(DcaeOperationStatusResponse dcaeResponse) {
130 return dcaeResponse.getLinks().getStatus().replaceAll("http:", "http4:").replaceAll("https:", "https4:");
134 * Return the deploy payload for DCAE.
136 * @param loop The loop object
137 * @return The payload used to send deploy closed loop request
139 public static String getDeployPayload(Loop loop) {
140 JsonObject globalProp = loop.getGlobalPropertiesJson();
141 JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER).getAsJsonObject(
142 UNIQUE_BLUEPRINT_PARAMETERS);
144 String serviceTypeId = loop.getLoopTemplate().getDcaeBlueprintId();
146 JsonObject rootObject = new JsonObject();
147 rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
148 if (deploymentProp != null) {
149 rootObject.add(DCAE_INPUTS, deploymentProp);
151 logger.info("DCAE Deploy payload for unique blueprint: " + rootObject.toString());
152 return rootObject.toString();
156 * Return the deploy payload for DCAE.
158 * @param loop The loop object
159 * @param microServicePolicy The micro service policy
160 * @return The payload used to send deploy closed loop request
162 public static String getDeployPayload(Loop loop, MicroServicePolicy microServicePolicy) {
163 JsonObject globalProp = loop.getGlobalPropertiesJson();
164 JsonObject deploymentProp =
165 globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER).getAsJsonObject(microServicePolicy.getName());
167 String serviceTypeId = microServicePolicy.getDcaeBlueprintId();
169 JsonObject rootObject = new JsonObject();
170 rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
171 if (deploymentProp != null) {
172 rootObject.add(DCAE_INPUTS, deploymentProp);
174 logger.info("DCAE Deploy payload for multiple blueprints: " + rootObject.toString());
175 return rootObject.toString();
179 * Return the uninstallation payload for DCAE.
181 * @param loop The loop object
182 * @return The payload in string (json)
184 public static String getUndeployPayload(Loop loop) {
185 JsonObject rootObject = new JsonObject();
186 rootObject.addProperty(DCAE_SERVICETYPE_ID, loop.getLoopTemplate().getDcaeBlueprintId());
187 logger.info("DCAE Undeploy payload for unique blueprint: " + rootObject.toString());
188 return rootObject.toString();
192 * Return the uninstallation payload for DCAE.
194 * @param policy The microServicePolicy object
195 * @return The payload in string (json)
197 public static String getUndeployPayload(MicroServicePolicy policy) {
198 JsonObject rootObject = new JsonObject();
199 rootObject.addProperty(DCAE_SERVICETYPE_ID, policy.getDcaeBlueprintId());
200 logger.info("DCAE Undeploy payload for multiple blueprints: " + rootObject.toString());
201 return rootObject.toString();
205 public ExternalComponentState computeState(Exchange camelExchange) {
207 DcaeOperationStatusResponse dcaeResponse = (DcaeOperationStatusResponse) camelExchange.getIn().getExchange()
208 .getProperty("dcaeResponse");
210 if (dcaeResponse == null) {
211 setState(BLUEPRINT_DEPLOYED);
212 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("succeeded")) {
213 setState(MICROSERVICE_INSTALLED_SUCCESSFULLY);
214 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("processing")) {
215 setState(PROCESSING_MICROSERVICE_INSTALLATION);
216 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("failed")) {
217 setState(MICROSERVICE_INSTALLATION_FAILED);
218 } else if (dcaeResponse.getOperationType().equals("uninstall")
219 && dcaeResponse.getStatus().equals("succeeded")) {
220 setState(MICROSERVICE_UNINSTALLED_SUCCESSFULLY);
221 } else if (dcaeResponse.getOperationType().equals("uninstall")
222 && dcaeResponse.getStatus().equals("processing")) {
223 setState(PROCESSING_MICROSERVICE_UNINSTALLATION);
224 } else if (dcaeResponse.getOperationType().equals("uninstall") && dcaeResponse.getStatus().equals("failed")) {
225 setState(MICROSERVICE_UNINSTALLATION_FAILED);
229 return this.getState();
233 * Convert the json response to a DcaeInventoryResponse.
235 * @param responseBody The DCAE response Json paylaod
236 * @return list of DcaeInventoryResponse
237 * @throws ParseException In case of issues with the Json parsing
239 public static List<DcaeInventoryResponse> convertToDcaeInventoryResponse(String responseBody)
240 throws ParseException {
241 JSONParser parser = new JSONParser();
242 JSONObject jsonObj = (JSONObject) parser.parse(responseBody);
243 JSONArray itemsArray = (JSONArray) jsonObj.get("items");
244 Iterator it = itemsArray.iterator();
245 List<DcaeInventoryResponse> inventoryResponseList = new LinkedList<>();
246 while (it.hasNext()) {
247 JSONObject item = (JSONObject) it.next();
248 DcaeInventoryResponse response = JsonUtils.GSON.fromJson(item.toString(), DcaeInventoryResponse.class);
249 inventoryResponseList.add(response);
251 return inventoryResponseList;