2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
\r
7 * ================================================================================
\r
8 * Modifications Copyright (c) 2019 Samsung
\r
9 * ================================================================================
\r
10 * Licensed under the Apache License, Version 2.0 (the "License");
\r
11 * you may not use this file except in compliance with the License.
\r
12 * You may obtain a copy of the License at
\r
14 * http://www.apache.org/licenses/LICENSE-2.0
\r
16 * Unless required by applicable law or agreed to in writing, software
\r
17 * distributed under the License is distributed on an "AS IS" BASIS,
\r
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
19 * See the License for the specific language governing permissions and
\r
20 * limitations under the License.
\r
21 * ============LICENSE_END============================================
\r
22 * ===================================================================
\r
26 package org.onap.clamp.clds.client;
\r
28 import com.att.eelf.configuration.EELFLogger;
\r
29 import com.att.eelf.configuration.EELFManager;
\r
31 import com.google.gson.JsonObject;
\r
32 import java.io.IOException;
\r
33 import java.util.Date;
\r
35 import org.json.simple.JSONObject;
\r
36 import org.json.simple.parser.JSONParser;
\r
37 import org.json.simple.parser.ParseException;
\r
38 import org.onap.clamp.clds.config.ClampProperties;
\r
39 import org.onap.clamp.clds.exception.dcae.DcaeDeploymentException;
\r
40 import org.onap.clamp.clds.util.LoggingUtils;
\r
41 import org.onap.clamp.util.HttpConnectionManager;
\r
42 import org.springframework.beans.factory.annotation.Autowired;
\r
43 import org.springframework.stereotype.Component;
\r
46 * This class implements the communication with DCAE for the service
\r
50 public class DcaeDispatcherServices {
\r
52 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeDispatcherServices.class);
\r
53 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
\r
54 private final ClampProperties refProp;
\r
55 private final HttpConnectionManager dcaeHttpConnectionManager;
\r
56 private static final String STATUS_URL_LOG = "Status URL extracted: ";
\r
57 private static final String DCAE_URL_PREFIX = "/dcae-deployments/";
\r
58 private static final String DCAE_URL_PROPERTY_NAME = "dcae.dispatcher.url";
\r
59 private static final String DCAE_LINK_FIELD = "links";
\r
60 private static final String DCAE_STATUS_FIELD = "status";
\r
63 public DcaeDispatcherServices(ClampProperties refProp, HttpConnectionManager dcaeHttpConnectionManager) {
\r
64 this.refProp = refProp;
\r
65 this.dcaeHttpConnectionManager = dcaeHttpConnectionManager;
\r
69 * Get the Operation Status from a specified URL with retry.
\r
70 * @param operationStatusUrl
\r
71 * The URL of the DCAE
\r
72 * @return The status
\r
73 * @throws InterruptedException Exception during the retry
\r
75 public String getOperationStatusWithRetry(String operationStatusUrl) throws InterruptedException {
\r
76 String operationStatus = "";
\r
77 for (int i = 0; i < Integer.valueOf(refProp.getStringValue("dcae.dispatcher.retry.limit")); i++) {
\r
78 logger.info("Trying to get Operation status on DCAE for url:" + operationStatusUrl);
\r
79 operationStatus = getOperationStatus(operationStatusUrl);
\r
80 logger.info("Current Status is:" + operationStatus);
\r
81 if (!"processing".equalsIgnoreCase(operationStatus)) {
\r
82 return operationStatus;
\r
84 Thread.sleep(Integer.valueOf(refProp.getStringValue("dcae.dispatcher.retry.interval")));
\r
87 logger.warn("Number of attempts on DCAE is over, stopping the getOperationStatus method");
\r
88 return operationStatus;
\r
92 * Get the Operation Status from a specified URL.
\r
94 * The URL provided by a previous DCAE Query
\r
95 * @return The status
\r
97 public String getOperationStatus(String statusUrl) {
\r
98 // Assigning processing status to monitor operation status further
\r
99 String opStatus = "processing";
\r
100 Date startTime = new Date();
\r
101 LoggingUtils.setTargetContext("DCAE", "getOperationStatus");
\r
103 String responseStr = dcaeHttpConnectionManager.doHttpRequest(statusUrl, "GET", null, null, "DCAE", null, null);
\r
104 JSONObject jsonObj = parseResponse(responseStr);
\r
105 String operationType = (String) jsonObj.get("operationType");
\r
106 String status = (String) jsonObj.get(DCAE_STATUS_FIELD);
\r
107 logger.info("Operation Type - " + operationType + ", Status " + status);
\r
108 LoggingUtils.setResponseContext("0", "Get operation status success", this.getClass().getName());
\r
110 } catch (Exception e) {
\r
111 LoggingUtils.setResponseContext("900", "Get operation status failed", this.getClass().getName());
\r
112 LoggingUtils.setErrorContext("900", "Get operation status error");
\r
113 logger.error("Exception occurred during getOperationStatus Operation with DCAE", e);
\r
115 LoggingUtils.setTimeContext(startTime, new Date());
\r
116 metricsLogger.info("getOperationStatus complete");
\r
122 * Returns status URL for createNewDeployment operation.
\r
123 * @param deploymentId
\r
124 * The deployment ID
\r
125 * @param serviceTypeId
\r
127 * @param blueprintInputJson
\r
128 * The value for each blueprint parameters in a flat JSON
\r
129 * @return The status URL
\r
131 public String createNewDeployment(String deploymentId, String serviceTypeId, JsonObject blueprintInputJson) {
\r
132 Date startTime = new Date();
\r
133 LoggingUtils.setTargetContext("DCAE", "createNewDeployment");
\r
135 JsonObject rootObject = refProp.getJsonTemplate("dcae.deployment.template").getAsJsonObject();
\r
136 rootObject.addProperty("serviceTypeId", serviceTypeId);
\r
137 if (blueprintInputJson != null) {
\r
138 rootObject.add("inputs", blueprintInputJson);
\r
140 String apiBodyString = rootObject.toString();
\r
141 logger.info("Dcae api Body String - " + apiBodyString);
\r
142 String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;
\r
143 String statusUrl = getDcaeResponse(url, "PUT", apiBodyString, "application/json", DCAE_LINK_FIELD,
\r
144 DCAE_STATUS_FIELD);
\r
145 LoggingUtils.setResponseContext("0", "Create new deployment failed", this.getClass().getName());
\r
147 } catch (Exception e) {
\r
148 LoggingUtils.setResponseContext("900", "Create new deployment failed", this.getClass().getName());
\r
149 LoggingUtils.setErrorContext("900", "Create new deployment error");
\r
150 logger.error("Exception occurred during createNewDeployment Operation with DCAE", e);
\r
151 throw new DcaeDeploymentException("Exception occurred during createNewDeployment Operation with DCAE", e);
\r
153 LoggingUtils.setTimeContext(startTime, new Date());
\r
154 metricsLogger.info("createNewDeployment complete");
\r
159 * Returns status URL for deleteExistingDeployment operation.
\r
160 * @param deploymentId
\r
161 * The deployment ID
\r
162 * @param serviceTypeId
\r
163 * The service Type ID
\r
164 * @return The status URL
\r
166 public String deleteExistingDeployment(String deploymentId, String serviceTypeId) {
\r
167 Date startTime = new Date();
\r
168 LoggingUtils.setTargetContext("DCAE", "deleteExistingDeployment");
\r
170 String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}";
\r
171 logger.info("Dcae api Body String - " + apiBodyString);
\r
172 String url = refProp.getStringValue(DCAE_URL_PROPERTY_NAME) + DCAE_URL_PREFIX + deploymentId;
\r
173 String statusUrl = getDcaeResponse(url, "DELETE", apiBodyString, "application/json", DCAE_LINK_FIELD,
\r
174 DCAE_STATUS_FIELD);
\r
175 LoggingUtils.setResponseContext("0", "Delete existing deployment success", this.getClass().getName());
\r
178 } catch (Exception e) {
\r
179 LoggingUtils.setResponseContext("900", "Delete existing deployment failed", this.getClass().getName());
\r
180 LoggingUtils.setErrorContext("900", "Delete existing deployment error");
\r
181 logger.error("Exception occurred during deleteExistingDeployment Operation with DCAE", e);
\r
182 throw new DcaeDeploymentException("Exception occurred during deleteExistingDeployment Operation with DCAE",
\r
185 LoggingUtils.setTimeContext(startTime, new Date());
\r
186 metricsLogger.info("deleteExistingDeployment complete");
\r
190 private String getDcaeResponse(String url, String requestMethod, String payload, String contentType, String node,
\r
191 String nodeAttr) throws IOException, ParseException {
\r
192 Date startTime = new Date();
\r
194 String responseStr = dcaeHttpConnectionManager.doHttpRequest(url, requestMethod, payload, contentType, "DCAE", null, null);
\r
195 JSONObject jsonObj = parseResponse(responseStr);
\r
196 JSONObject linksObj = (JSONObject) jsonObj.get(node);
\r
197 String statusUrl = (String) linksObj.get(nodeAttr);
\r
198 logger.info(STATUS_URL_LOG + statusUrl);
\r
200 } catch (IOException | ParseException e) {
\r
201 logger.error("Exception occurred getting response from DCAE", e);
\r
204 LoggingUtils.setTimeContext(startTime, new Date());
\r
205 metricsLogger.info("getDcaeResponse complete");
\r
209 private JSONObject parseResponse(String responseStr) throws ParseException {
\r
210 JSONParser parser = new JSONParser();
\r
211 Object obj0 = parser.parse(responseStr);
\r
212 JSONObject jsonObj = (JSONObject) obj0;
\r