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.UUID;
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 import org.onap.clamp.policy.microservice.MicroServicePolicy;
36 public class DcaeComponent extends ExternalComponent {
38 private static final String DCAE_DEPLOYMENT_PREFIX = "CLAMP_";
39 private static final String DEPLOYMENT_PARAMETER = "dcaeDeployParameters";
40 private static final String DCAE_SERVICETYPE_ID = "serviceTypeId";
41 private static final String DCAE_INPUTS = "inputs";
43 public static final ExternalComponentState BLUEPRINT_DEPLOYED = new ExternalComponentState("BLUEPRINT_DEPLOYED",
44 "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop");
45 public static final ExternalComponentState PROCESSING_MICROSERVICE_INSTALLATION = new ExternalComponentState(
46 "PROCESSING_MICROSERVICE_INSTALLATION", "Clamp has requested DCAE to install the microservices "
47 + "defined in the DCAE blueprint and it's currently processing the request");
48 public static final ExternalComponentState MICROSERVICE_INSTALLATION_FAILED = new ExternalComponentState(
49 "MICROSERVICE_INSTALLATION_FAILED",
50 "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it failed");
51 public static final ExternalComponentState MICROSERVICE_INSTALLED_SUCCESSFULLY = new ExternalComponentState(
52 "MICROSERVICE_INSTALLED_SUCCESSFULLY",
53 "Clamp has requested DCAE to install the DCAE blueprint and it has been installed successfully");
54 public static final ExternalComponentState PROCESSING_MICROSERVICE_UNINSTALLATION = new ExternalComponentState(
55 "PROCESSING_MICROSERVICE_UNINSTALLATION", "Clamp has requested DCAE to uninstall the microservices "
56 + "defined in the DCAE blueprint and it's currently processing the request");
57 public static final ExternalComponentState MICROSERVICE_UNINSTALLATION_FAILED = new ExternalComponentState(
58 "MICROSERVICE_UNINSTALLATION_FAILED",
59 "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it failed");
60 public static final ExternalComponentState MICROSERVICE_UNINSTALLED_SUCCESSFULLY = new ExternalComponentState(
61 "MICROSERVICE_UNINSTALLED_SUCCESSFULLY",
62 "Clamp has requested DCAE to uninstall the DCAE blueprint and it has been uninstalled successfully");
63 public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
64 "There was an error during the request done to DCAE, look at the logs or try again");
66 public DcaeComponent() {
67 super(BLUEPRINT_DEPLOYED);
71 public String getComponentName() {
76 * Convert the json response to a DcaeOperationStatusResponse.
78 * @param responseBody The DCAE response Json paylaod
79 * @return The dcae object provisioned
81 public static DcaeOperationStatusResponse convertDcaeResponse(String responseBody) {
82 if (responseBody != null && !responseBody.isEmpty()) {
83 return JsonUtils.GSON_JPA_MODEL.fromJson(responseBody, DcaeOperationStatusResponse.class);
89 * Generate the deployment id, it's random.
91 * @return The deployment id
93 public static String generateDeploymentId() {
94 return DCAE_DEPLOYMENT_PREFIX + UUID.randomUUID();
98 * This method prepare the url returned by DCAE to check the status if fine. It
99 * extracts it from the dcaeResponse.
101 * @param dcaeResponse The dcae response object
102 * @return the Right Url modified if needed
104 public static String getStatusUrl(DcaeOperationStatusResponse dcaeResponse) {
105 return dcaeResponse.getLinks().getStatus().replaceAll("http:", "http4:").replaceAll("https:", "https4:");
109 * Return the deploy payload for DCAE.
111 * @param loop The loop object
112 * @return The payload used to send deploy closed loop request
114 public static String getDeployPayload(Loop loop) {
115 JsonObject globalProp = loop.getGlobalPropertiesJson();
116 JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER);
118 String serviceTypeId = loop.getDcaeBlueprintId();
120 JsonObject rootObject = new JsonObject();
121 rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
122 if (deploymentProp != null) {
123 rootObject.add(DCAE_INPUTS, deploymentProp);
125 return rootObject.toString();
129 * Return the deploy payload for DCAE.
131 * @param loop The loop object
132 * @param microServiceName The micro service name
133 * @return The payload used to send deploy closed loop request
135 public static String getDeployPayload(Loop loop, String microServiceName) {
136 JsonObject globalProp = loop.getGlobalPropertiesJson();
137 JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER).getAsJsonObject(microServiceName);
139 String serviceTypeId = loop.getDcaeBlueprintId();
141 JsonObject rootObject = new JsonObject();
142 rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
143 if (deploymentProp != null) {
144 rootObject.add(DCAE_INPUTS, deploymentProp);
146 return rootObject.toString();
150 * Return the uninstallation payload for DCAE.
152 * @param loop The loop object
153 * @return The payload in string (json)
155 public static String getUndeployPayload(Loop loop) {
156 JsonObject rootObject = new JsonObject();
157 rootObject.addProperty(DCAE_SERVICETYPE_ID, loop.getDcaeBlueprintId());
158 return rootObject.toString();
162 public ExternalComponentState computeState(Exchange camelExchange) {
164 DcaeOperationStatusResponse dcaeResponse = (DcaeOperationStatusResponse) camelExchange.getIn().getExchange()
165 .getProperty("dcaeResponse");
167 if (dcaeResponse == null) {
168 setState(BLUEPRINT_DEPLOYED);
169 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("succeeded")) {
170 setState(MICROSERVICE_INSTALLED_SUCCESSFULLY);
171 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("processing")) {
172 setState(PROCESSING_MICROSERVICE_INSTALLATION);
173 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("failed")) {
174 setState(MICROSERVICE_INSTALLATION_FAILED);
175 } else if (dcaeResponse.getOperationType().equals("uninstall")
176 && dcaeResponse.getStatus().equals("succeeded")) {
177 setState(MICROSERVICE_UNINSTALLED_SUCCESSFULLY);
178 } else if (dcaeResponse.getOperationType().equals("uninstall")
179 && dcaeResponse.getStatus().equals("processing")) {
180 setState(PROCESSING_MICROSERVICE_UNINSTALLATION);
181 } else if (dcaeResponse.getOperationType().equals("uninstall") && dcaeResponse.getStatus().equals("failed")) {
182 setState(MICROSERVICE_UNINSTALLATION_FAILED);
186 return this.getState();