2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 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
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
24 package org.onap.clamp.clds.client;
\r
26 import java.io.BufferedReader;
\r
27 import java.io.DataOutputStream;
\r
28 import java.io.InputStream;
\r
29 import java.io.InputStreamReader;
\r
30 import java.net.URL;
\r
31 import java.util.stream.Collectors;
\r
33 import javax.net.ssl.HttpsURLConnection;
\r
35 import org.json.simple.JSONObject;
\r
36 import org.json.simple.parser.JSONParser;
\r
37 import org.onap.clamp.clds.model.refprop.RefProp;
\r
38 import org.springframework.beans.factory.annotation.Autowired;
\r
40 import com.att.eelf.configuration.EELFLogger;
\r
41 import com.att.eelf.configuration.EELFManager;
\r
47 public class DcaeDispatcherServices {
\r
48 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(DcaeDispatcherServices.class);
\r
49 protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
\r
52 private RefProp refProp;
\r
56 * @param deploymentId
\r
60 public String deleteDeployment(String deploymentId) throws Exception {
\r
62 String statusUrl = null;
\r
63 InputStream in = null;
\r
65 String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments/" + deploymentId;
\r
66 logger.info("Dcae Dispatcher url - " + url);
\r
67 URL obj = new URL(url);
\r
68 HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
\r
69 conn.setRequestMethod("DELETE");
\r
70 int responseCode = conn.getResponseCode();
\r
72 boolean requestFailed = true;
\r
73 logger.info("responseCode=" + responseCode);
\r
74 if (responseCode == 200 || responseCode == 202) {
\r
75 requestFailed = false;
\r
78 InputStream inStream = conn.getErrorStream();
\r
79 if (inStream == null) {
\r
80 inStream = conn.getInputStream();
\r
83 String responseStr = null;
\r
84 if (inStream != null) {
\r
85 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inStream));
\r
86 String inputLine = null;
\r
87 StringBuffer response = new StringBuffer();
\r
88 while ((inputLine = bufferedReader.readLine()) != null) {
\r
89 response.append(inputLine);
\r
91 responseStr = response.toString();
\r
94 if (responseStr != null) {
\r
95 if (requestFailed) {
\r
96 logger.error("requestFailed - responseStr=" + responseStr);
\r
97 throw new Exception(responseStr);
\r
101 logger.debug("response code " + responseCode);
\r
102 in = conn.getInputStream();
\r
103 logger.debug("res:" + responseStr);
\r
104 JSONParser parser = new JSONParser();
\r
105 Object obj0 = parser.parse(responseStr);
\r
106 JSONObject jsonObj = (JSONObject) obj0;
\r
107 JSONObject linksObj = (JSONObject) jsonObj.get("links");
\r
108 statusUrl = (String) linksObj.get("status");
\r
109 logger.debug("Status URL: " + statusUrl);
\r
111 } catch (Exception e) {
\r
112 logger.error(e.getClass().getName() + " " + e.getMessage());
\r
128 * @throws Exception
\r
130 public String getOperationStatus(String statusUrl) throws Exception {
\r
132 //Assigning processing status to monitor operation status further
\r
133 String opStatus = "processing";
\r
134 InputStream in = null;
\r
136 URL obj = new URL(statusUrl);
\r
137 HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
\r
138 conn.setRequestMethod("GET");
\r
139 int responseCode = conn.getResponseCode();
\r
140 logger.debug("Deployment operation status response code - " + responseCode);
\r
141 if(responseCode == 200){
\r
142 in = conn.getInputStream();
\r
143 String res = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));
\r
144 JSONParser parser = new JSONParser();
\r
145 Object obj0 = parser.parse(res);
\r
146 JSONObject jsonObj = (JSONObject) obj0;
\r
147 String operationType = (String) jsonObj.get("operationType");
\r
148 String status = (String) jsonObj.get("status");
\r
149 logger.debug("Operation Type - " + operationType + ", Status " + status);
\r
152 } catch (Exception e) {
\r
153 logger.debug(e.getClass().getName() + " " + e.getMessage());
\r
154 logger.debug(e.getMessage()
\r
155 + " : got exception while retrieving status, trying again until we get 200 response code");
\r
167 * @throws Exception
\r
169 public void getDeployments() throws Exception {
\r
170 InputStream in = null;
\r
172 String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments";
\r
173 logger.info("Dcae Dispatcher deployments url - " + url);
\r
174 URL obj = new URL(url);
\r
175 HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
\r
176 conn.setRequestMethod("GET");
\r
177 int responseCode = conn.getResponseCode();
\r
178 logger.debug("response code " + responseCode);
\r
179 in = conn.getInputStream();
\r
180 String res = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));
\r
181 logger.debug("res:" + res);
\r
182 } catch (Exception e) {
\r
183 logger.error("Exception occurred during DCAE communication", e);
\r
193 * Returns status URL for deployment operation
\r
195 * @param deploymentId
\r
196 * @param serviceTypeId
\r
198 * @throws Exception
\r
200 public String createNewDeployment(String deploymentId, String serviceTypeId) throws Exception {
\r
202 String statusUrl = null;
\r
203 InputStream inStream = null;
\r
204 BufferedReader in = null;
\r
206 String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}";
\r
207 logger.info("Dcae api Body String - " + apiBodyString);
\r
208 String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments/" + deploymentId;
\r
209 logger.info("Dcae Dispatcher Service url - " + url);
\r
210 URL obj = new URL(url);
\r
211 HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
\r
212 conn.setRequestMethod("PUT");
\r
213 conn.setRequestProperty("Content-Type", "application/json");
\r
214 conn.setDoOutput(true);
\r
215 try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {
\r
216 wr.writeBytes(apiBodyString);
\r
220 boolean requestFailed = true;
\r
221 int responseCode = conn.getResponseCode();
\r
222 logger.info("responseCode=" + responseCode);
\r
223 if (responseCode == 200 || responseCode == 202) {
\r
224 requestFailed = false;
\r
227 inStream = conn.getErrorStream();
\r
228 if (inStream == null) {
\r
229 inStream = conn.getInputStream();
\r
232 String responseStr = null;
\r
233 if (inStream != null) {
\r
234 in = new BufferedReader(new InputStreamReader(inStream));
\r
236 String inputLine = null;
\r
238 StringBuffer response = new StringBuffer();
\r
240 while ((inputLine = in.readLine()) != null) {
\r
241 response.append(inputLine);
\r
244 responseStr = response.toString();
\r
247 if (responseStr != null) {
\r
248 if (requestFailed) {
\r
249 logger.error("requestFailed - responseStr=" + responseStr);
\r
250 throw new Exception(responseStr);
\r
254 logger.debug("response code " + responseCode);
\r
255 JSONParser parser = new JSONParser();
\r
256 Object obj0 = parser.parse(responseStr);
\r
257 JSONObject jsonObj = (JSONObject) obj0;
\r
258 JSONObject linksObj = (JSONObject) jsonObj.get("links");
\r
259 statusUrl = (String) linksObj.get("status");
\r
260 logger.debug("Status URL: " + statusUrl);
\r
261 } catch (Exception e) {
\r
262 logger.error("Exception occurred during the DCAE communication", e);
\r
265 if (inStream != null) {
\r
277 * @param deploymentId
\r
278 * @param serviceTypeId
\r
280 * @throws Exception
\r
282 public String deleteExistingDeployment(String deploymentId, String serviceTypeId) throws Exception {
\r
284 String statusUrl = null;
\r
285 InputStream in = null;
\r
287 String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}";
\r
288 logger.debug(apiBodyString);
\r
289 String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments/" + deploymentId;
\r
290 logger.info("Dcae Dispatcher deployments url - " + url);
\r
291 URL obj = new URL(url);
\r
292 HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();
\r
293 conn.setRequestMethod("DELETE");
\r
294 conn.setRequestProperty("Content-Type", "application/json");
\r
295 conn.setDoOutput(true);
\r
296 DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
\r
297 wr.writeBytes(apiBodyString);
\r
300 int responseCode = conn.getResponseCode();
\r
301 logger.debug("response code " + responseCode);
\r
302 in = conn.getInputStream();
\r
303 String res = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));
\r
304 logger.debug("res:" + res);
\r
305 JSONParser parser = new JSONParser();
\r
306 Object obj0 = parser.parse(res);
\r
307 JSONObject jsonObj = (JSONObject) obj0;
\r
308 JSONObject linksObj = (JSONObject) jsonObj.get("links");
\r
309 statusUrl = (String) linksObj.get("status");
\r
310 logger.debug("Status URL: " + statusUrl);
\r
311 } catch (Exception e) {
\r
312 logger.error("Exception occurred during DCAE communication", e);
\r