Merge "Code improvement"
[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         //Assigning processing status to monitor operation status further\r
133         String opStatus = "processing";\r
134         InputStream in = null;\r
135         try {\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
150                 opStatus = status;\r
151             }\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
156         } finally {\r
157             if (in != null) {\r
158                 in.close();\r
159             }\r
160         }\r
161 \r
162         return opStatus;\r
163     }\r
164 \r
165     /**\r
166      *\r
167      * @throws Exception\r
168      */\r
169     public void getDeployments() throws Exception {\r
170         InputStream in = null;\r
171         try {\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
184             throw e;\r
185         } finally {\r
186             if (in != null) {\r
187                 in.close();\r
188             }\r
189         }\r
190     }\r
191 \r
192     /**\r
193      * Returns status URL for deployment operation\r
194      *\r
195      * @param deploymentId\r
196      * @param serviceTypeId\r
197      * @return\r
198      * @throws Exception\r
199      */\r
200     public String createNewDeployment(String deploymentId, String serviceTypeId) throws Exception {\r
201 \r
202         String statusUrl = null;\r
203         InputStream inStream = null;\r
204         BufferedReader in = null;\r
205         try {\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
217                 wr.flush();\r
218             }\r
219 \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
225             }\r
226 \r
227             inStream = conn.getErrorStream();\r
228             if (inStream == null) {\r
229                 inStream = conn.getInputStream();\r
230             }\r
231 \r
232             String responseStr = null;\r
233             if (inStream != null) {\r
234                 in = new BufferedReader(new InputStreamReader(inStream));\r
235 \r
236                 String inputLine = null;\r
237 \r
238                 StringBuffer response = new StringBuffer();\r
239 \r
240                 while ((inputLine = in.readLine()) != null) {\r
241                     response.append(inputLine);\r
242                 }\r
243 \r
244                 responseStr = response.toString();\r
245             }\r
246 \r
247             if (responseStr != null) {\r
248                 if (requestFailed) {\r
249                     logger.error("requestFailed - responseStr=" + responseStr);\r
250                     throw new Exception(responseStr);\r
251                 }\r
252             }\r
253 \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
263             throw e;\r
264         } finally {\r
265             if (inStream != null) {\r
266                 inStream.close();\r
267             }\r
268             if (in != null) {\r
269                 in.close();\r
270             }\r
271         }\r
272         return statusUrl;\r
273     }\r
274 \r
275     /**\r
276      *\r
277      * @param deploymentId\r
278      * @param serviceTypeId\r
279      * @return\r
280      * @throws Exception\r
281      */\r
282     public String deleteExistingDeployment(String deploymentId, String serviceTypeId) throws Exception {\r
283 \r
284         String statusUrl = null;\r
285         InputStream in = null;\r
286         try {\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
298             wr.flush();\r
299 \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
313             throw e;\r
314         } finally {\r
315             if (in != null) {\r
316                 in.close();\r
317             }\r
318         }\r
319         return statusUrl;\r
320     }\r
321 \r
322 }