Upgrade sonar plugin
[vid.git] / vid-app-common / src / main / java / org / openecomp / vid / mso / MsoRestInterface.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * VID\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.vid.mso;\r
22 \r
23 \r
24 import java.text.DateFormat;\r
25 import java.text.SimpleDateFormat;\r
26 import java.util.Collections;\r
27 import java.util.Date;\r
28 \r
29 import javax.ws.rs.client.Client;\r
30 import javax.ws.rs.client.Entity;\r
31 import javax.ws.rs.core.MediaType;\r
32 import javax.ws.rs.core.MultivaluedHashMap;\r
33 import javax.ws.rs.core.Response;\r
34 \r
35 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
36 import org.openecomp.portalsdk.core.util.SystemProperties;\r
37 \r
38 import org.apache.commons.codec.binary.Base64;\r
39 import org.eclipse.jetty.util.security.Password;\r
40 import org.openecomp.vid.changeManagement.ChangeManagementRequest;\r
41 import org.openecomp.vid.client.HttpBasicClient;\r
42 import org.openecomp.vid.client.HttpsBasicClient;\r
43 import org.openecomp.vid.mso.rest.RequestDetails;\r
44 \r
45 /**\r
46  * The Class MsoRestInterface.\r
47  */\r
48 public class MsoRestInterface extends MsoRestInt implements MsoRestInterfaceIfc {\r
49 \r
50         /** The logger. */\r
51         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MsoRestInterface.class);\r
52         \r
53         /** The Constant dateFormat. */\r
54         final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");\r
55         \r
56         /** The client. */\r
57         private static Client client = null;\r
58         \r
59         /** The common headers. */\r
60         private MultivaluedHashMap<String, Object> commonHeaders;\r
61 \r
62         /**\r
63          * Instantiates a new mso rest interface.\r
64          */\r
65         public MsoRestInterface() {\r
66                 super();\r
67         }\r
68         \r
69         /* (non-Javadoc)\r
70          * @see org.openecomp.vid.mso.MsoRestInterfaceIfc#initRestClient()\r
71          */\r
72         public void initMsoClient()\r
73         {\r
74                 final String methodname = "initRestClient()";\r
75                 \r
76                 final String username = SystemProperties.getProperty(MsoProperties.MSO_USER_NAME);\r
77                 final String password = SystemProperties.getProperty(MsoProperties.MSO_PASSWORD);\r
78                 final String mso_url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL);\r
79                 final String decrypted_password = Password.deobfuscate(password);\r
80                 \r
81                 String authString = username + ":" + decrypted_password;\r
82                 \r
83                 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());\r
84                 String authStringEnc = new String(authEncBytes);\r
85 \r
86                 commonHeaders = new MultivaluedHashMap<String, Object> ();\r
87                 commonHeaders.put("Authorization",  Collections.singletonList((Object) ("Basic " + authStringEnc)));\r
88                 //Pass calling application identifier to SO\r
89                 commonHeaders.put("X-FromAppId",\r
90                                 Collections.singletonList(SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME)));\r
91 \r
92                 boolean use_ssl = true;\r
93                 if ( (mso_url != null) && ( !(mso_url.isEmpty()) ) ) {\r
94                         if ( mso_url.startsWith("https")) {\r
95                                 use_ssl = true;\r
96                         }\r
97                         else {\r
98                                 use_ssl = false;\r
99                         }\r
100                 }\r
101                 if (client == null) {\r
102                         \r
103                         try {\r
104                                 if ( use_ssl ) { \r
105                                         //logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  methodname + " getting HttpsBasicClient with username=" + username\r
106                                         //              + " password=" + password);\r
107                                         client = HttpsBasicClient.getClient();\r
108                                 }\r
109                                 else {\r
110                                         //logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  methodname + " getting HttpsBasicClient with username=" + username\r
111                                         //              + " password=" + password);\r
112                                         client = HttpBasicClient.getClient();\r
113                                 }\r
114                         } catch (Exception e) {\r
115                                 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  methodname + " Unable to get the SSL client");\r
116                         }\r
117                 }\r
118         }\r
119         \r
120         /* (non-Javadoc)\r
121          * @see org.openecomp.vid.mso.MsoRestInterfaceIfc#Get(java.lang.Object, java.lang.String, java.lang.String, org.openecomp.vid.mso.RestObject)\r
122          */\r
123         @SuppressWarnings("unchecked")\r
124         public <T> void  Get (T t, String sourceId, String path, RestObject<T> restObject ) throws Exception {\r
125                 String methodName = "Get";\r
126                 \r
127                 logger.debug(EELFLoggerDelegate.debugLogger, methodName + " start");\r
128                 \r
129                 String url="";\r
130                 restObject.set(t);\r
131                 \r
132                 url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;\r
133         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " +  methodName + " sending request to url= " + url);\r
134                 \r
135         initMsoClient();\r
136                 \r
137                 final Response cres = client.target(url)\r
138                          .request()\r
139                  .accept("application/json")\r
140                  .headers(commonHeaders)\r
141                  .get();\r
142                 \r
143                 int status = cres.getStatus();\r
144                 restObject.setStatusCode (status);\r
145                 \r
146                 if (status == 200) {\r
147                          t = (T) cres.readEntity(t.getClass());\r
148                          restObject.set(t);\r
149                          logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " REST api was successfull!");\r
150                     \r
151                  } else {\r
152                      throw new Exception(methodName + " with status="+ status + ", url= " + url );\r
153                  }\r
154 \r
155                 logger.debug(EELFLoggerDelegate.debugLogger,methodName + " received status=" + status );\r
156                 \r
157                 return;\r
158         }\r
159    \r
160    /* (non-Javadoc)\r
161     * @see org.openecomp.vid.mso.MsoRestInterfaceIfc#Delete(java.lang.Object, org.openecomp.vid.mso.rest.RequestDetails, java.lang.String, java.lang.String, org.openecomp.vid.mso.RestObject)\r
162     */\r
163    @SuppressWarnings("unchecked")\r
164          public <T> void Delete(T t, RequestDetails r, String sourceID, String path, RestObject<T> restObject) {\r
165          \r
166                 String methodName = "Delete";\r
167                 String url="";\r
168                 Response cres = null;\r
169                 \r
170                 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " +  methodName + " start");\r
171                 logRequest (r);\r
172 \r
173                 try {\r
174                         initMsoClient();\r
175                         \r
176                         url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;\r
177                         logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + " methodName sending request to: " + url);\r
178         \r
179                         cres = client.target(url)\r
180                                          .request()\r
181                                  .accept("application/json")\r
182                                  .headers(commonHeaders)\r
183                                  //.entity(r)\r
184                                  .build("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON)).invoke();\r
185                                //  .method("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON));\r
186                                  //.delete(Entity.entity(r, MediaType.APPLICATION_JSON));\r
187                         \r
188                         int status = cres.getStatus();\r
189                 restObject.setStatusCode (status);\r
190                 \r
191                         if (status == 404) { // resource not found\r
192                                 String msg = "Resource does not exist...: " + cres.getStatus();\r
193                                 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);\r
194                         } else if (status == 200  || status == 204){\r
195                                 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + "Resource " + url + " deleted");\r
196                         } else if (status == 202) {\r
197                                 String msg = "Delete in progress: " + status;\r
198                                 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);\r
199                         }\r
200                         else {\r
201                                 String msg = "Deleting Resource failed: " + status;\r
202                                         logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);\r
203                         }\r
204                         \r
205                         try {\r
206                                 t = (T) cres.readEntity(t.getClass());\r
207                                 restObject.set(t);\r
208             }\r
209             catch ( Exception e ) {\r
210                 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " No response entity, this is probably ok, e="\r
211                                 + e.getMessage());\r
212             }\r
213    \r
214         } \r
215                 catch (Exception e)\r
216         {\r
217                  logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with url="+url+ ", Exception: " + e.toString());\r
218                  throw e;\r
219         \r
220         }\r
221         }\r
222         \r
223         /* (non-Javadoc)\r
224          * @see org.openecomp.vid.mso.MsoRestInterfaceIfc#Post(java.lang.Object, org.openecomp.vid.mso.rest.RequestDetails, java.lang.String, java.lang.String, org.openecomp.vid.mso.RestObject)\r
225          */\r
226         @SuppressWarnings("unchecked")\r
227         public <T> void Post(T t, RequestDetails r, String sourceID, String path, RestObject<T> restObject) throws Exception {\r
228 \r
229         String methodName = "Post";\r
230         String url="";\r
231         \r
232         logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " +  methodName + " start");\r
233        \r
234         logRequest (r);\r
235         try {\r
236 \r
237                         initMsoClient();\r
238 \r
239             url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;\r
240             logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " +  methodName + " sending request to url= " + url);\r
241             // Change the content length\r
242             final Response cres = client.target(url)\r
243                  .request()\r
244                  .accept("application/json")\r
245                          .headers(commonHeaders)\r
246                  //.header("content-length", 201)\r
247                  //.header("X-FromAppId",  sourceID)\r
248                  .post(Entity.entity(r, MediaType.APPLICATION_JSON));\r
249             \r
250             try {\r
251                                 t = (T) cres.readEntity(t.getClass());\r
252                                 restObject.set(t);\r
253             }\r
254             catch ( Exception e ) {\r
255                 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " No response entity, this is probably ok, e="\r
256                                 + e.getMessage());\r
257             }\r
258 \r
259             int status = cres.getStatus();\r
260                 restObject.setStatusCode (status);\r
261                 \r
262                 if ( status >= 200 && status <= 299 ) {\r
263                         logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!");\r
264                         logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!");\r
265                 \r
266              } else {\r
267                  logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with status="+status+", url="+url);\r
268              }    \r
269    \r
270         } catch (Exception e)\r
271         {\r
272                  logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with url="+url+ ", Exception: " + e.toString());\r
273                  throw e;\r
274         \r
275         }\r
276     }\r
277         \r
278         \r
279         /* (non-Javadoc)\r
280          * @see org.openecomp.vid.mso.MsoRestInterfaceIfc#Put(java.lang.Object, org.openecomp.vid.mso.rest.RequestDetails, java.lang.String, java.lang.String, org.openecomp.vid.mso.RestObject)\r
281          */\r
282         @SuppressWarnings("unchecked")\r
283         public <T> void Put(T t, ChangeManagementRequest r, String sourceID, String path, RestObject<T> restObject) throws Exception {\r
284 \r
285         String methodName = "Put";\r
286         String url="";\r
287         \r
288         logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " +  methodName + " start");\r
289        \r
290 //        logRequest (r);\r
291         try {\r
292             \r
293             initMsoClient();    \r
294     \r
295             url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;\r
296             logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " +  methodName + " sending request to url= " + url);\r
297             // Change the content length\r
298             final Response cres = client.target(url)\r
299                  .request()\r
300                  .accept("application/json")\r
301                          .headers(commonHeaders)\r
302                  //.header("content-length", 201)\r
303                  //.header("X-FromAppId",  sourceID)\r
304                  .put(Entity.entity(r, MediaType.APPLICATION_JSON));\r
305             \r
306             try {\r
307                                 t = (T) cres.readEntity(t.getClass());\r
308                                 restObject.set(t);\r
309             }\r
310             catch ( Exception e ) {\r
311                 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " No response entity, this is probably ok, e="\r
312                                 + e.getMessage());\r
313             }\r
314 \r
315             int status = cres.getStatus();\r
316                 restObject.setStatusCode (status);\r
317                 \r
318                 if ( status >= 200 && status <= 299 ) {\r
319                         logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!");\r
320                         logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!");\r
321                 \r
322              } else {\r
323                  logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with status="+status+", url="+url);\r
324              }    \r
325    \r
326         } catch (Exception e)\r
327         {\r
328                  logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with url="+url+ ", Exception: " + e.toString());\r
329                  throw e;\r
330         \r
331         }\r
332     }\r
333 \r
334         \r
335     /**\r
336      * Gets the single instance of MsoRestInterface.\r
337      *\r
338      * @param <T> the generic type\r
339      * @param clazz the clazz\r
340      * @return single instance of MsoRestInterface\r
341      * @throws IllegalAccessException the illegal access exception\r
342      * @throws InstantiationException the instantiation exception\r
343      */\r
344     public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException\r
345         {\r
346                 return clazz.newInstance();\r
347         }\r
348 \r
349 \r
350         \r
351     \r
352 }\r