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;
35 public class DcaeComponent extends ExternalComponent {
37 private static final String DCAE_DEPLOYMENT_PREFIX = "CLAMP_";
38 private static final String DEPLOYMENT_PARAMETER = "dcaeDeployParameters";
39 private static final String DCAE_SERVICETYPE_ID = "serviceTypeId";
40 private static final String DCAE_INPUTS = "inputs";
42 public static final ExternalComponentState BLUEPRINT_DEPLOYED = new ExternalComponentState("BLUEPRINT_DEPLOYED",
43 "The DCAE blueprint has been found in the DCAE inventory but not yet instancianted for this loop");
44 public static final ExternalComponentState PROCESSING_MICROSERVICE_INSTALLATION = new ExternalComponentState(
45 "PROCESSING_MICROSERVICE_INSTALLATION",
46 "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it's currently processing the request");
47 public static final ExternalComponentState MICROSERVICE_INSTALLATION_FAILED = new ExternalComponentState(
48 "MICROSERVICE_INSTALLATION_FAILED",
49 "Clamp has requested DCAE to install the microservices defined in the DCAE blueprint and it failed");
50 public static final ExternalComponentState MICROSERVICE_INSTALLED_SUCCESSFULLY = new ExternalComponentState(
51 "MICROSERVICE_INSTALLED_SUCCESSFULLY",
52 "Clamp has requested DCAE to install the DCAE blueprint and it has been installed successfully");
53 public static final ExternalComponentState PROCESSING_MICROSERVICE_UNINSTALLATION = new ExternalComponentState(
54 "PROCESSING_MICROSERVICE_UNINSTALLATION",
55 "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it's currently processing the request");
56 public static final ExternalComponentState MICROSERVICE_UNINSTALLATION_FAILED = new ExternalComponentState(
57 "MICROSERVICE_UNINSTALLATION_FAILED",
58 "Clamp has requested DCAE to uninstall the microservices defined in the DCAE blueprint and it failed");
59 public static final ExternalComponentState MICROSERVICE_UNINSTALLED_SUCCESSFULLY = new ExternalComponentState(
60 "MICROSERVICE_UNINSTALLED_SUCCESSFULLY",
61 "Clamp has requested DCAE to uninstall the DCAE blueprint and it has been uninstalled successfully");
62 public static final ExternalComponentState IN_ERROR = new ExternalComponentState("IN_ERROR",
63 "There was an error during the request done to DCAE, look at the logs or try again");
65 public DcaeComponent() {
66 super(BLUEPRINT_DEPLOYED);
70 public String getComponentName() {
74 public static DcaeOperationStatusResponse convertDcaeResponse(String responseBody) {
75 if (responseBody != null && !responseBody.isEmpty()) {
76 return JsonUtils.GSON_JPA_MODEL.fromJson(responseBody, DcaeOperationStatusResponse.class);
83 * Generate the deployment id, it's random
85 * @return The deployment id
87 public static String generateDeploymentId() {
88 return DCAE_DEPLOYMENT_PREFIX + UUID.randomUUID();
92 * This method prepare the url returned by DCAE to check the status if fine.
95 * @return the Right Url modified if needed
97 public static String getStatusUrl(DcaeOperationStatusResponse dcaeResponse) {
98 return dcaeResponse.getLinks().getStatus().replaceAll("http:", "http4:").replaceAll("https:", "https4:");
102 * Return the deploy payload for DCAE.
106 * @return The payload used to send deploy closed loop request
108 public static String getDeployPayload(Loop loop) {
109 JsonObject globalProp = loop.getGlobalPropertiesJson();
110 JsonObject deploymentProp = globalProp.getAsJsonObject(DEPLOYMENT_PARAMETER);
112 String serviceTypeId = loop.getDcaeBlueprintId();
114 JsonObject rootObject = new JsonObject();
115 rootObject.addProperty(DCAE_SERVICETYPE_ID, serviceTypeId);
116 if (deploymentProp != null) {
117 rootObject.add(DCAE_INPUTS, deploymentProp);
119 return rootObject.toString();
123 * Return the uninstallation payload for DCAE.
127 * @return The payload in string (json)
129 public static String getUndeployPayload(Loop loop) {
130 JsonObject rootObject = new JsonObject();
131 rootObject.addProperty(DCAE_SERVICETYPE_ID, loop.getDcaeBlueprintId());
132 return rootObject.toString();
136 public ExternalComponentState computeState(Exchange camelExchange) {
138 DcaeOperationStatusResponse dcaeResponse = (DcaeOperationStatusResponse) camelExchange.getIn().getExchange()
139 .getProperty("dcaeResponse");
141 if (dcaeResponse == null) {
142 setState(BLUEPRINT_DEPLOYED);
143 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("succeeded")) {
144 setState(MICROSERVICE_INSTALLED_SUCCESSFULLY);
145 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("processing")) {
146 setState(PROCESSING_MICROSERVICE_INSTALLATION);
147 } else if (dcaeResponse.getOperationType().equals("install") && dcaeResponse.getStatus().equals("failed")) {
148 setState(MICROSERVICE_INSTALLATION_FAILED);
149 } else if (dcaeResponse.getOperationType().equals("uninstall")
150 && dcaeResponse.getStatus().equals("succeeded")) {
151 setState(MICROSERVICE_UNINSTALLED_SUCCESSFULLY);
152 } else if (dcaeResponse.getOperationType().equals("uninstall")
153 && dcaeResponse.getStatus().equals("processing")) {
154 setState(PROCESSING_MICROSERVICE_UNINSTALLATION);
155 } else if (dcaeResponse.getOperationType().equals("uninstall") && dcaeResponse.getStatus().equals("failed")) {
156 setState(MICROSERVICE_UNINSTALLATION_FAILED);
160 return this.getState();