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;
41 import org.onap.clamp.policy.microservice.MicroServicePolicy;
43 public class DcaeComponent extends ExternalComponent {
45 private static final String DCAE_DEPLOYMENT_PREFIX = "CLAMP_";
46 private static final String DEPLOYMENT_PARAMETER = "dcaeDeployParameters";
47 private static final String DCAE_SERVICETYPE_ID = "serviceTypeId";
48 private static final String DCAE_INPUTS = "inputs";
50 public static final ExternalComponentState BLUEPRINT_DEPLOYED = new ExternalComponentState("BLUEPRINT_DEPLOYED",
51 "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop");
52 public static final ExternalComponentState PROCESSING_MICROSERVICE_INSTALLATION = new ExternalComponentState(
53 "PROCESSING_MICROSERVICE_INSTALLATION", "Clamp has requested DCAE to install the microservices "
54 + "defined in the DCAE blueprint and it's currently processing the request");
55 public static final ExternalComponentState MICROSERVICE_INSTALLATION_FAILED = new ExternalComponentState(
56 "MICROSERVICE_INSTALLATION_FAILED",
57 "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it failed");
58 public static final ExternalComponentState MICROSERVICE_INSTALLED_SUCCESSFULLY = new ExternalComponentState(
59 "MICROSERVICE_INSTALLED_SUCCESSFULLY",
60 "Clamp has requested DCAE to install the DCAE blueprint and it has been installed successfully");
61 public static final ExternalComponentState PROCESSING_MICROSERVICE_UNINSTALLATION = new ExternalComponentState(
62 "PROCESSING_MICROSERVICE_UNINSTALLATION", "Clamp has requested DCAE to uninstall the microservices "
63 + "defined in the DCAE blueprint and it's currently processing the request");
64 public static final ExternalComponentState MICROSERVICE_UNINSTALLATION_FAILED = new ExternalComponentState(
65 "MICROSERVICE_UNINSTALLATION_FAILED",
66 "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it failed");
67 public static final ExternalComponentState MICROSERVICE_UNINSTALLED_SUCCESSFULLY = new ExternalComponentState(
68 "MICROSERVICE_UNINSTALLED_SUCCESSFULLY",
69 "Clamp has requested DCAE to uninstall the DCAE blueprint and it has been uninstalled successfully");
70 public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
71 "There was an error during the request done to DCAE, look at the logs or try again");
73 public DcaeComponent() {
74 super(BLUEPRINT_DEPLOYED);
78 public String getComponentName() {
83 * Convert the json response to a DcaeOperationStatusResponse.
85 * @param responseBody The DCAE response Json paylaod
86 * @return The dcae object provisioned
88 public static DcaeOperationStatusResponse convertDcaeResponse(String responseBody) {
89 if (responseBody != null && !responseBody.isEmpty()) {
90 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 deploy payload for DCAE.
138 * @param loop The loop object
139 * @param microServiceName The micro service name
140 * @return The payload used to send deploy closed loop request
142 public static String getDeployPayload(Loop loop, String microServiceName) {
143 JsonObject globalProp = loop.getGlobalPropertiesJson();
144 JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER).getAsJsonObject(microServiceName);
146 String serviceTypeId = loop.getDcaeBlueprintId();
148 JsonObject rootObject = new JsonObject();
149 rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
150 if (deploymentProp != null) {
151 rootObject.add(DCAE_INPUTS, deploymentProp);
153 return rootObject.toString();
157 * Return the uninstallation payload for DCAE.
159 * @param loop The loop object
160 * @return The payload in string (json)
162 public static String getUndeployPayload(Loop loop) {
163 JsonObject rootObject = new JsonObject();
164 rootObject.addProperty(DCAE_SERVICETYPE_ID, loop.getDcaeBlueprintId());
165 return rootObject.toString();
169 public ExternalComponentState computeState(Exchange camelExchange) {
171 DcaeOperationStatusResponse dcaeResponse = (DcaeOperationStatusResponse) camelExchange.getIn().getExchange()
172 .getProperty("dcaeResponse");
174 if (dcaeResponse == null) {
175 setState(BLUEPRINT_DEPLOYED);
176 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("succeeded")) {
177 setState(MICROSERVICE_INSTALLED_SUCCESSFULLY);
178 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("processing")) {
179 setState(PROCESSING_MICROSERVICE_INSTALLATION);
180 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("failed")) {
181 setState(MICROSERVICE_INSTALLATION_FAILED);
182 } else if (dcaeResponse.getOperationType().equals("uninstall")
183 && dcaeResponse.getStatus().equals("succeeded")) {
184 setState(MICROSERVICE_UNINSTALLED_SUCCESSFULLY);
185 } else if (dcaeResponse.getOperationType().equals("uninstall")
186 && dcaeResponse.getStatus().equals("processing")) {
187 setState(PROCESSING_MICROSERVICE_UNINSTALLATION);
188 } else if (dcaeResponse.getOperationType().equals("uninstall") && dcaeResponse.getStatus().equals("failed")) {
189 setState(MICROSERVICE_UNINSTALLATION_FAILED);
193 return this.getState();
197 * Convert the json response to a DcaeInventoryResponse.
199 * @param responseBody The DCAE response Json paylaod
200 * @return list of DcaeInventoryResponse
201 * @throws ParseException In case of issues with the Json parsing
203 public static List<DcaeInventoryResponse> convertToDcaeInventoryResponse(String responseBody)
204 throws ParseException {
205 JSONParser parser = new JSONParser();
206 JSONObject jsonObj = (JSONObject) parser.parse(responseBody);
207 JSONArray itemsArray = (JSONArray) jsonObj.get("items");
208 Iterator it = itemsArray.iterator();
209 List<DcaeInventoryResponse> inventoryResponseList = new LinkedList<>();
210 while (it.hasNext()) {
211 JSONObject item = (JSONObject) it.next();
212 DcaeInventoryResponse response = JsonUtils.GSON.fromJson(item.toString(), DcaeInventoryResponse.class);
213 inventoryResponseList.add(response);
215 return inventoryResponseList;