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
28 import com.fasterxml.jackson.databind.JsonNode;
\r
29 import com.fasterxml.jackson.databind.node.ObjectNode;
\r
31 import java.io.IOException;
\r
32 import java.util.Date;
\r
34 import org.json.simple.JSONObject;
\r
35 import org.json.simple.parser.JSONParser;
\r
36 import org.json.simple.parser.ParseException;
\r
37 import org.onap.clamp.clds.config.ClampProperties;
\r
38 import org.onap.clamp.clds.exception.dcae.DcaeDeploymentException;
\r
39 import org.onap.clamp.clds.util.LoggingUtils;
\r
40 import org.springframework.beans.factory.annotation.Autowired;
\r
41 import org.springframework.stereotype.Component;
\r
44 * This class implements the communication with DCAE for the service
\r
48 public class DcaeDispatcherServices {
\r
50 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeDispatcherServices.class);
\r
51 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
\r
53 private ClampProperties refProp;
\r
54 private static final String STATUS_URL_LOG = "Status URL extracted: ";
\r
55 private static final String DCAE_URL_PREFIX = "/dcae-deployments/";
\r
56 private static final String DCAE_URL_PROPERTY_NAME = "dcae.dispatcher.url";
\r
57 public static final String DCAE_REQUESTID_PROPERTY_NAME = "dcae.header.requestId";
\r
58 private static final String DCAE_LINK_FIELD = "links";
\r
59 private static final String DCAE_STATUS_FIELD = "status";
\r
62 * Delete the deployment on DCAE.
\r
64 * @param deploymentId
\r
66 * @return Return the URL Status
\r
68 public String deleteDeployment(String deploymentId) {
\r
69 Date startTime = new Date();
\r
70 LoggingUtils.setTargetContext("DCAE", "deleteDeployment");
\r
72 String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;
\r
73 String statusUrl = getDcaeResponse(url, "DELETE", null, null, DCAE_LINK_FIELD, DCAE_STATUS_FIELD);
\r
74 logger.info(STATUS_URL_LOG + statusUrl);
\r
75 LoggingUtils.setResponseContext("0", "Delete deployments success", this.getClass().getName());
\r
77 } catch (Exception e) {
\r
78 LoggingUtils.setResponseContext("900", "Delete deployments failed", this.getClass().getName());
\r
79 LoggingUtils.setErrorContext("900", "Delete deployments error");
\r
80 logger.error("Exception occurred during Delete Deployment Operation with DCAE", e);
\r
81 throw new DcaeDeploymentException("Exception occurred during Delete Deployment Operation with DCAE", e);
\r
83 LoggingUtils.setTimeContext(startTime, new Date());
\r
84 metricsLogger.info("deleteDeployment complete");
\r
88 public String getOperationStatusWithRetry(String operationStatusUrl) throws InterruptedException {
\r
89 String operationStatus = "";
\r
90 for (int i = 0; i < Integer.valueOf(refProp.getStringValue("dcae.dispatcher.retry.limit")); i++) {
\r
91 logger.info("Trying to get Operation status on DCAE for url:" + operationStatusUrl);
\r
92 operationStatus = getOperationStatus(operationStatusUrl);
\r
93 logger.info("Current Status is:" + operationStatus);
\r
94 if (!"processing".equalsIgnoreCase(operationStatus)) {
\r
95 return operationStatus;
\r
97 Thread.sleep(Integer.valueOf(refProp.getStringValue("dcae.dispatcher.retry.interval")));
\r
100 logger.warn("Number of attempts on DCAE is over, stopping the getOperationStatus method");
\r
101 return operationStatus;
\r
105 * Get the Operation Status from a specified URL.
\r
108 * The URL provided by a previous DCAE Query
\r
109 * @return The status
\r
111 public String getOperationStatus(String statusUrl) {
\r
112 // Assigning processing status to monitor operation status further
\r
113 String opStatus = "processing";
\r
114 Date startTime = new Date();
\r
115 LoggingUtils.setTargetContext("DCAE", "getOperationStatus");
\r
117 String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(statusUrl, "GET", null, null);
\r
118 JSONObject jsonObj = parseResponse(responseStr);
\r
119 String operationType = (String) jsonObj.get("operationType");
\r
120 String status = (String) jsonObj.get(DCAE_STATUS_FIELD);
\r
121 logger.info("Operation Type - " + operationType + ", Status " + status);
\r
122 LoggingUtils.setResponseContext("0", "Get operation status success", this.getClass().getName());
\r
124 } catch (Exception e) {
\r
125 LoggingUtils.setResponseContext("900", "Get operation status failed", this.getClass().getName());
\r
126 LoggingUtils.setErrorContext("900", "Get operation status error");
\r
127 logger.error("Exception occurred during getOperationStatus Operation with DCAE", e);
\r
129 LoggingUtils.setTimeContext(startTime, new Date());
\r
130 metricsLogger.info("getOperationStatus complete");
\r
136 * This method send a getDeployments operation to DCAE.
\r
138 public void getDeployments() {
\r
139 Date startTime = new Date();
\r
140 LoggingUtils.setTargetContext("DCAE", "getDeployments");
\r
142 String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX;
\r
143 DcaeHttpConnectionManager.doDcaeHttpQuery(url, "GET", null, null);
\r
144 LoggingUtils.setResponseContext("0", "Get deployments success", this.getClass().getName());
\r
145 } catch (Exception e) {
\r
146 LoggingUtils.setResponseContext("900", "Get deployments failed", this.getClass().getName());
\r
147 LoggingUtils.setErrorContext("900", "Get deployments error");
\r
148 logger.error("Exception occurred during getDeployments Operation with DCAE", e);
\r
149 throw new DcaeDeploymentException("Exception occurred during getDeployments Operation with DCAE", e);
\r
151 LoggingUtils.setTimeContext(startTime, new Date());
\r
152 metricsLogger.info("getDeployments complete");
\r
157 * Returns status URL for createNewDeployment operation.
\r
159 * @param deploymentId
\r
160 * The deployment ID
\r
161 * @param serviceTypeId
\r
163 * @param blueprintInputJson
\r
164 * The value for each blueprint parameters in a flat JSON
\r
165 * @return The status URL
\r
167 public String createNewDeployment(String deploymentId, String serviceTypeId, JsonNode blueprintInputJson) {
\r
168 Date startTime = new Date();
\r
169 LoggingUtils.setTargetContext("DCAE", "createNewDeployment");
\r
171 ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("dcae.deployment.template");
\r
172 rootNode.put("serviceTypeId", serviceTypeId);
\r
173 if (blueprintInputJson != null) {
\r
174 rootNode.set("inputs", blueprintInputJson);
\r
176 String apiBodyString = rootNode.toString();
\r
177 logger.info("Dcae api Body String - " + apiBodyString);
\r
178 String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;
\r
179 String statusUrl = getDcaeResponse(url, "PUT", apiBodyString, "application/json", DCAE_LINK_FIELD,
\r
180 DCAE_STATUS_FIELD);
\r
181 LoggingUtils.setResponseContext("0", "Create new deployment failed", this.getClass().getName());
\r
183 } catch (Exception e) {
\r
184 LoggingUtils.setResponseContext("900", "Create new deployment failed", this.getClass().getName());
\r
185 LoggingUtils.setErrorContext("900", "Create new deployment error");
\r
186 logger.error("Exception occurred during createNewDeployment Operation with DCAE", e);
\r
187 throw new DcaeDeploymentException("Exception occurred during createNewDeployment Operation with DCAE", e);
\r
189 LoggingUtils.setTimeContext(startTime, new Date());
\r
190 metricsLogger.info("createNewDeployment complete");
\r
195 * Returns status URL for deleteExistingDeployment operation.
\r
197 * @param deploymentId
\r
198 * The deployment ID
\r
199 * @param serviceTypeId
\r
200 * The service Type ID
\r
201 * @return The status URL
\r
203 public String deleteExistingDeployment(String deploymentId, String serviceTypeId) {
\r
204 Date startTime = new Date();
\r
205 LoggingUtils.setTargetContext("DCAE", "deleteExistingDeployment");
\r
207 String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}";
\r
208 logger.info("Dcae api Body String - " + apiBodyString);
\r
209 String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;
\r
210 String statusUrl = getDcaeResponse(url, "DELETE", apiBodyString, "application/json", DCAE_LINK_FIELD,
\r
211 DCAE_STATUS_FIELD);
\r
212 LoggingUtils.setResponseContext("0", "Delete existing deployment success", this.getClass().getName());
\r
215 } catch (Exception e) {
\r
216 LoggingUtils.setResponseContext("900", "Delete existing deployment failed", this.getClass().getName());
\r
217 LoggingUtils.setErrorContext("900", "Delete existing deployment error");
\r
218 logger.error("Exception occurred during deleteExistingDeployment Operation with DCAE", e);
\r
219 throw new DcaeDeploymentException("Exception occurred during deleteExistingDeployment Operation with DCAE",
\r
222 LoggingUtils.setTimeContext(startTime, new Date());
\r
223 metricsLogger.info("deleteExistingDeployment complete");
\r
227 private String getDcaeResponse(String url, String requestMethod, String payload, String contentType, String node,
\r
228 String nodeAttr) throws IOException, ParseException {
\r
229 Date startTime = new Date();
\r
231 String responseStr = DcaeHttpConnectionManager.doDcaeHttpQuery(url, requestMethod, payload, contentType);
\r
232 JSONObject jsonObj = parseResponse(responseStr);
\r
233 JSONObject linksObj = (JSONObject) jsonObj.get(node);
\r
234 String statusUrl = (String) linksObj.get(nodeAttr);
\r
235 logger.info(STATUS_URL_LOG + statusUrl);
\r
237 } catch (IOException | ParseException e) {
\r
238 logger.error("Exception occurred getting response from DCAE", e);
\r
241 LoggingUtils.setTimeContext(startTime, new Date());
\r
242 metricsLogger.info("getDcaeResponse complete");
\r
246 private JSONObject parseResponse(String responseStr) throws ParseException {
\r
247 JSONParser parser = new JSONParser();
\r
248 Object obj0 = parser.parse(responseStr);
\r
249 JSONObject jsonObj = (JSONObject) obj0;
\r