Rework of the DCAE client
[clamp.git] / src / main / java / org / onap / clamp / clds / client / DcaeDispatcherServices.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP CLAMP\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights\r
6  *                             reserved.\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
11  *\r
12  * http://www.apache.org/licenses/LICENSE-2.0\r
13  *\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
22  */\r
23 \r
24 package org.onap.clamp.clds.client;\r
25 \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
32 \r
33 import javax.net.ssl.HttpsURLConnection;\r
34 \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
39 \r
40 import com.att.eelf.configuration.EELFLogger;\r
41 import com.att.eelf.configuration.EELFManager;\r
42 \r
43 /**\r
44  *\r
45  *\r
46  */\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
50 \r
51     @Autowired\r
52     private RefProp                 refProp;\r
53 \r
54     /**\r
55      *\r
56      * @param deploymentId\r
57      * @return\r
58      * @throws Exception\r
59      */\r
60     public String deleteDeployment(String deploymentId) throws Exception {\r
61 \r
62         String statusUrl = null;\r
63         InputStream in = null;\r
64         try {\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
71 \r
72             boolean requestFailed = true;\r
73             logger.info("responseCode=" + responseCode);\r
74             if (responseCode == 200 || responseCode == 202) {\r
75                 requestFailed = false;\r
76             }\r
77 \r
78             InputStream inStream = conn.getErrorStream();\r
79             if (inStream == null) {\r
80                 inStream = conn.getInputStream();\r
81             }\r
82 \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
90                 }\r
91                 responseStr = response.toString();\r
92             }\r
93 \r
94             if (responseStr != null) {\r
95                 if (requestFailed) {\r
96                     logger.error("requestFailed - responseStr=" + responseStr);\r
97                     throw new Exception(responseStr);\r
98                 }\r
99             }\r
100 \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
110 \r
111         } catch (Exception e) {\r
112             logger.error(e.getClass().getName() + " " + e.getMessage());\r
113             throw e;\r
114         } finally {\r
115             if (in != null) {\r
116                 in.close();\r
117             }\r
118         }\r
119 \r
120         return statusUrl;\r
121 \r
122     }\r
123 \r
124     /**\r
125      *\r
126      * @param statusUrl\r
127      * @return\r
128      * @throws Exception\r
129      */\r
130     public String getOperationStatus(String statusUrl) throws Exception {\r
131 \r
132         String opStatus = null;\r
133         InputStream in = null;\r
134         try {\r
135             URL obj = new URL(statusUrl);\r
136             HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();\r
137             conn.setRequestMethod("GET");\r
138             int responseCode = conn.getResponseCode();\r
139             logger.debug("response code " + responseCode);\r
140             in = conn.getInputStream();\r
141             String res = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));\r
142             JSONParser parser = new JSONParser();\r
143             Object obj0 = parser.parse(res);\r
144             JSONObject jsonObj = (JSONObject) obj0;\r
145             String operationType = (String) jsonObj.get("operationType");\r
146             String status = (String) jsonObj.get("status");\r
147             logger.debug("Operation Type " + operationType);\r
148             logger.debug("Status " + status);\r
149             opStatus = status;\r
150 \r
151         } catch (Exception e) {\r
152             logger.debug(e.getClass().getName() + " " + e.getMessage());\r
153             logger.debug(e.getMessage()\r
154                     + " : got exception while retrieving status, trying again until we get 200 response code");\r
155         } finally {\r
156             if (in != null) {\r
157                 in.close();\r
158             }\r
159         }\r
160 \r
161         return opStatus;\r
162     }\r
163 \r
164     /**\r
165      *\r
166      * @throws Exception\r
167      */\r
168     public void getDeployments() throws Exception {\r
169         InputStream in = null;\r
170         try {\r
171             String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments";\r
172             logger.info("Dcae Dispatcher deployments url - " + url);\r
173             URL obj = new URL(url);\r
174             HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();\r
175             conn.setRequestMethod("GET");\r
176             int responseCode = conn.getResponseCode();\r
177             logger.debug("response code " + responseCode);\r
178             in = conn.getInputStream();\r
179             String res = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));\r
180             logger.debug("res:" + res);\r
181         } catch (Exception e) {\r
182             logger.error("Exception occurred during DCAE communication", e);\r
183             throw e;\r
184         } finally {\r
185             if (in != null) {\r
186                 in.close();\r
187             }\r
188         }\r
189     }\r
190 \r
191     /**\r
192      * Returns status URL for deployment operation\r
193      *\r
194      * @param deploymentId\r
195      * @param serviceTypeId\r
196      * @return\r
197      * @throws Exception\r
198      */\r
199     public String createNewDeployment(String deploymentId, String serviceTypeId) throws Exception {\r
200 \r
201         String statusUrl = null;\r
202         InputStream inStream = null;\r
203         BufferedReader in = null;\r
204         try {\r
205             String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}";\r
206             logger.info("Dcae api Body String - " + apiBodyString);\r
207             String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments/" + deploymentId;\r
208             logger.info("Dcae Dispatcher Service url - " + url);\r
209             URL obj = new URL(url);\r
210             HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();\r
211             conn.setRequestMethod("PUT");\r
212             conn.setRequestProperty("Content-Type", "application/json");\r
213             conn.setDoOutput(true);\r
214             try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) {\r
215                 wr.writeBytes(apiBodyString);\r
216                 wr.flush();\r
217             }\r
218 \r
219             boolean requestFailed = true;\r
220             int responseCode = conn.getResponseCode();\r
221             logger.info("responseCode=" + responseCode);\r
222             if (responseCode == 200 || responseCode == 202) {\r
223                 requestFailed = false;\r
224             }\r
225 \r
226             inStream = conn.getErrorStream();\r
227             if (inStream == null) {\r
228                 inStream = conn.getInputStream();\r
229             }\r
230 \r
231             String responseStr = null;\r
232             if (inStream != null) {\r
233                 in = new BufferedReader(new InputStreamReader(inStream));\r
234 \r
235                 String inputLine = null;\r
236 \r
237                 StringBuffer response = new StringBuffer();\r
238 \r
239                 while ((inputLine = in.readLine()) != null) {\r
240                     response.append(inputLine);\r
241                 }\r
242 \r
243                 responseStr = response.toString();\r
244             }\r
245 \r
246             if (responseStr != null) {\r
247                 if (requestFailed) {\r
248                     logger.error("requestFailed - responseStr=" + responseStr);\r
249                     throw new Exception(responseStr);\r
250                 }\r
251             }\r
252 \r
253             logger.debug("response code " + responseCode);\r
254             JSONParser parser = new JSONParser();\r
255             Object obj0 = parser.parse(responseStr);\r
256             JSONObject jsonObj = (JSONObject) obj0;\r
257             JSONObject linksObj = (JSONObject) jsonObj.get("links");\r
258             statusUrl = (String) linksObj.get("status");\r
259             logger.debug("Status URL: " + statusUrl);\r
260         } catch (Exception e) {\r
261             logger.error("Exception occurred during the DCAE communication", e);\r
262             throw e;\r
263         } finally {\r
264             if (inStream != null) {\r
265                 inStream.close();\r
266             }\r
267             if (in != null) {\r
268                 in.close();\r
269             }\r
270         }\r
271         return statusUrl;\r
272     }\r
273 \r
274     /**\r
275      *\r
276      * @param deploymentId\r
277      * @param serviceTypeId\r
278      * @return\r
279      * @throws Exception\r
280      */\r
281     public String deleteExistingDeployment(String deploymentId, String serviceTypeId) throws Exception {\r
282 \r
283         String statusUrl = null;\r
284         InputStream in = null;\r
285         try {\r
286             String apiBodyString = "{\"serviceTypeId\": \"" + serviceTypeId + "\"}";\r
287             logger.debug(apiBodyString);\r
288             String url = refProp.getStringValue("DCAE_DISPATCHER_URL") + "/dcae-deployments/" + deploymentId;\r
289             logger.info("Dcae Dispatcher deployments url - " + url);\r
290             URL obj = new URL(url);\r
291             HttpsURLConnection conn = (HttpsURLConnection) obj.openConnection();\r
292             conn.setRequestMethod("DELETE");\r
293             conn.setRequestProperty("Content-Type", "application/json");\r
294             conn.setDoOutput(true);\r
295             DataOutputStream wr = new DataOutputStream(conn.getOutputStream());\r
296             wr.writeBytes(apiBodyString);\r
297             wr.flush();\r
298 \r
299             int responseCode = conn.getResponseCode();\r
300             logger.debug("response code " + responseCode);\r
301             in = conn.getInputStream();\r
302             String res = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("\n"));\r
303             logger.debug("res:" + res);\r
304             JSONParser parser = new JSONParser();\r
305             Object obj0 = parser.parse(res);\r
306             JSONObject jsonObj = (JSONObject) obj0;\r
307             JSONObject linksObj = (JSONObject) jsonObj.get("links");\r
308             statusUrl = (String) linksObj.get("status");\r
309             logger.debug("Status URL: " + statusUrl);\r
310         } catch (Exception e) {\r
311             logger.error("Exception occurred during DCAE communication", e);\r
312             throw e;\r
313         } finally {\r
314             if (in != null) {\r
315                 in.close();\r
316             }\r
317         }\r
318         return statusUrl;\r
319     }\r
320 \r
321 }