2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
\r
7 * ================================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
19 * ============LICENSE_END============================================
\r
20 * ===================================================================
\r
24 package org.onap.clamp.clds.client;
\r
26 import com.att.eelf.configuration.EELFLogger;
\r
27 import com.att.eelf.configuration.EELFManager;
\r
29 import com.google.gson.JsonObject;
\r
30 import java.io.IOException;
\r
31 import java.util.Date;
\r
33 import org.json.simple.JSONObject;
\r
34 import org.json.simple.parser.JSONParser;
\r
35 import org.json.simple.parser.ParseException;
\r
36 import org.onap.clamp.clds.config.ClampProperties;
\r
37 import org.onap.clamp.clds.exception.dcae.DcaeDeploymentException;
\r
38 import org.onap.clamp.clds.util.LoggingUtils;
\r
39 import org.springframework.beans.factory.annotation.Autowired;
\r
40 import org.springframework.stereotype.Component;
\r
43 * This class implements the communication with DCAE for the service
\r
47 public class DcaeDispatcherServices {
\r
49 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeDispatcherServices.class);
\r
50 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
\r
51 private final ClampProperties refProp;
\r
52 private final DcaeHttpConnectionManager dcaeHttpConnectionManager;
\r
53 private static final String STATUS_URL_LOG = "Status URL extracted: ";
\r
54 private static final String DCAE_URL_PREFIX = "/dcae-deployments/";
\r
55 private static final String DCAE_URL_PROPERTY_NAME = "dcae.dispatcher.url";
\r
56 public static final String DCAE_REQUESTID_PROPERTY_NAME = "dcae.header.requestId";
\r
57 private static final String DCAE_LINK_FIELD = "links";
\r
58 private static final String DCAE_STATUS_FIELD = "status";
\r
61 public DcaeDispatcherServices(ClampProperties refProp, DcaeHttpConnectionManager dcaeHttpConnectionManager) {
\r
62 this.refProp = refProp;
\r
63 this.dcaeHttpConnectionManager = dcaeHttpConnectionManager;
\r
66 public String getOperationStatusWithRetry(String operationStatusUrl) throws InterruptedException {
\r
67 String operationStatus = "";
\r
68 for (int i = 0; i < Integer.valueOf(refProp.getStringValue("dcae.dispatcher.retry.limit")); i++) {
\r
69 logger.info("Trying to get Operation status on DCAE for url:" + operationStatusUrl);
\r
70 operationStatus = getOperationStatus(operationStatusUrl);
\r
71 logger.info("Current Status is:" + operationStatus);
\r
72 if (!"processing".equalsIgnoreCase(operationStatus)) {
\r
73 return operationStatus;
\r
75 Thread.sleep(Integer.valueOf(refProp.getStringValue("dcae.dispatcher.retry.interval")));
\r
78 logger.warn("Number of attempts on DCAE is over, stopping the getOperationStatus method");
\r
79 return operationStatus;
\r
83 * Get the Operation Status from a specified URL.
\r
86 * The URL provided by a previous DCAE Query
\r
87 * @return The status
\r
89 public String getOperationStatus(String statusUrl) {
\r
90 // Assigning processing status to monitor operation status further
\r
91 String opStatus = "processing";
\r
92 Date startTime = new Date();
\r
93 LoggingUtils.setTargetContext("DCAE", "getOperationStatus");
\r
95 String responseStr = dcaeHttpConnectionManager.doDcaeHttpQuery(statusUrl, "GET", null, null);
\r
96 JSONObject jsonObj = parseResponse(responseStr);
\r
97 String operationType = (String) jsonObj.get("operationType");
\r
98 String status = (String) jsonObj.get(DCAE_STATUS_FIELD);
\r
99 logger.info("Operation Type - " + operationType + ", Status " + status);
\r
100 LoggingUtils.setResponseContext("0", "Get operation status success", this.getClass().getName());
\r
102 } catch (Exception e) {
\r
103 LoggingUtils.setResponseContext("900", "Get operation status failed", this.getClass().getName());
\r
104 LoggingUtils.setErrorContext("900", "Get operation status error");
\r
105 logger.error("Exception occurred during getOperationStatus Operation with DCAE", e);
\r
107 LoggingUtils.setTimeContext(startTime, new Date());
\r
108 metricsLogger.info("getOperationStatus complete");
\r
114 * Returns status URL for createNewDeployment operation.
\r
116 * @param deploymentId
\r
117 * The deployment ID
\r
118 * @param serviceTypeId
\r
120 * @param blueprintInputJson
\r
121 * The value for each blueprint parameters in a flat JSON
\r
122 * @return The status URL
\r
124 public String createNewDeployment(String deploymentId, String serviceTypeId, JsonObject blueprintInputJson) {
\r
125 Date startTime = new Date();
\r
126 LoggingUtils.setTargetContext("DCAE", "createNewDeployment");
\r
128 JsonObject rootObject = refProp.getJsonTemplate("dcae.deployment.template").getAsJsonObject();
\r
129 rootObject.addProperty("serviceTypeId", serviceTypeId);
\r
130 if (blueprintInputJson != null) {
\r
131 rootObject.add("inputs", blueprintInputJson);
\r
133 String apiBodyString = rootObject.toString();
\r
134 logger.info("Dcae api Body String - " + apiBodyString);
\r
135 String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;
\r
136 String statusUrl = getDcaeResponse(url, "PUT", apiBodyString, "application/json", DCAE_LINK_FIELD,
\r
137 DCAE_STATUS_FIELD);
\r
138 LoggingUtils.setResponseContext("0", "Create new deployment failed", this.getClass().getName());
\r
140 } catch (Exception e) {
\r
141 LoggingUtils.setResponseContext("900", "Create new deployment failed", this.getClass().getName());
\r
142 LoggingUtils.setErrorContext("900", "Create new deployment error");
\r
143 logger.error("Exception occurred during createNewDeployment Operation with DCAE", e);
\r
144 throw new DcaeDeploymentException("Exception occurred during createNewDeployment Operation with DCAE", e);
\r
146 LoggingUtils.setTimeContext(startTime, new Date());
\r
147 metricsLogger.info("createNewDeployment complete");
\r
152 * Returns status URL for deleteExistingDeployment operation.
\r
154 * @param deploymentId
\r
155 * The deployment ID
\r
156 * @param serviceTypeId
\r
157 * The service Type ID
\r
158 * @return The status URL
\r
160 public String deleteExistingDeployment(String deploymentId, String serviceTypeId) {
\r
161 Date startTime = new Date();
\r
162 LoggingUtils.setTargetContext("DCAE", "deleteExistingDeployment");
\r
164 String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}";
\r
165 logger.info("Dcae api Body String - " + apiBodyString);
\r
166 String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;
\r
167 String statusUrl = getDcaeResponse(url, "DELETE", apiBodyString, "application/json", DCAE_LINK_FIELD,
\r
168 DCAE_STATUS_FIELD);
\r
169 LoggingUtils.setResponseContext("0", "Delete existing deployment success", this.getClass().getName());
\r
172 } catch (Exception e) {
\r
173 LoggingUtils.setResponseContext("900", "Delete existing deployment failed", this.getClass().getName());
\r
174 LoggingUtils.setErrorContext("900", "Delete existing deployment error");
\r
175 logger.error("Exception occurred during deleteExistingDeployment Operation with DCAE", e);
\r
176 throw new DcaeDeploymentException("Exception occurred during deleteExistingDeployment Operation with DCAE",
\r
179 LoggingUtils.setTimeContext(startTime, new Date());
\r
180 metricsLogger.info("deleteExistingDeployment complete");
\r
184 private String getDcaeResponse(String url, String requestMethod, String payload, String contentType, String node,
\r
185 String nodeAttr) throws IOException, ParseException {
\r
186 Date startTime = new Date();
\r
188 String responseStr = dcaeHttpConnectionManager.doDcaeHttpQuery(url, requestMethod, payload, contentType);
\r
189 JSONObject jsonObj = parseResponse(responseStr);
\r
190 JSONObject linksObj = (JSONObject) jsonObj.get(node);
\r
191 String statusUrl = (String) linksObj.get(nodeAttr);
\r
192 logger.info(STATUS_URL_LOG + statusUrl);
\r
194 } catch (IOException | ParseException e) {
\r
195 logger.error("Exception occurred getting response from DCAE", e);
\r
198 LoggingUtils.setTimeContext(startTime, new Date());
\r
199 metricsLogger.info("getDcaeResponse complete");
\r
203 private JSONObject parseResponse(String responseStr) throws ParseException {
\r
204 JSONParser parser = new JSONParser();
\r
205 Object obj0 = parser.parse(responseStr);
\r
206 JSONObject jsonObj = (JSONObject) obj0;
\r