Global Read only role, Support VID specific Roles
[vid.git] / vid-app-common / src / main / java / org / openecomp / vid / controller / MsoController.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.controller;\r
22 \r
23 \r
24 import com.fasterxml.jackson.databind.ObjectMapper;\r
25 import org.glassfish.jersey.client.ClientResponse;\r
26 import org.json.simple.JSONArray;\r
27 import org.json.simple.JSONObject;\r
28 import org.json.simple.parser.JSONParser;\r
29 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;\r
30 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
31 import org.openecomp.portalsdk.core.util.SystemProperties;\r
32 import org.openecomp.vid.model.ExceptionResponse;\r
33 import org.openecomp.vid.mso.*;\r
34 import org.openecomp.vid.mso.rest.RequestDetails;\r
35 import org.openecomp.vid.roles.RoleProvider;\r
36 import org.openecomp.vid.roles.RoleValidator;\r
37 import org.springframework.http.HttpStatus;\r
38 import org.springframework.http.ResponseEntity;\r
39 import org.springframework.web.bind.annotation.*;\r
40 \r
41 import javax.servlet.http.HttpServletRequest;\r
42 import javax.servlet.http.HttpServletResponse;\r
43 import javax.servlet.http.HttpSession;\r
44 import java.io.IOException;\r
45 import java.io.PrintWriter;\r
46 import java.io.StringWriter;\r
47 import java.text.DateFormat;\r
48 import java.text.SimpleDateFormat;\r
49 import java.util.ArrayList;\r
50 import java.util.Date;\r
51 import java.util.List;\r
52 import java.util.Map;\r
53 \r
54 /**\r
55  * The Class MsoController.\r
56  */\r
57 @RestController\r
58 @RequestMapping("mso")\r
59 public class MsoController extends RestrictedBaseController {\r
60 \r
61     /**\r
62      * The Constant SVC_INSTANCE_ID.\r
63      */\r
64     public final static String SVC_INSTANCE_ID = "<service_instance_id>";\r
65     /**\r
66      * The Constant VNF_INSTANCE_ID.\r
67      */\r
68     public final static String VNF_INSTANCE_ID = "<vnf_instance_id>";\r
69     /**\r
70      * The Constant dateFormat.\r
71      */\r
72     final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");\r
73     /**\r
74      * The view name.\r
75      */\r
76     String viewName;\r
77     /**\r
78      * The logger.\r
79      */\r
80     EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MsoController.class);\r
81 \r
82     /**\r
83      * Welcome.\r
84      *\r
85      * @param request the request\r
86      * @return the model and view\r
87 \r
88     public ModelAndView welcome(HttpServletRequest request) {\r
89     logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== MsoController welcome start");\r
90     logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + " MSO_SERVER_URL=" +\r
91     SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) );\r
92     return new ModelAndView(getViewName());\r
93     }\r
94      */\r
95 \r
96         /* (non-Javadoc)\r
97      * @see org.openecomp.portalsdk.core.controller.RestrictedBaseController#getViewName()\r
98          \r
99         public String getViewName() {\r
100                 return viewName;\r
101         }\r
102         */\r
103 \r
104         /* (non-Javadoc)\r
105      * @see org.openecomp.portalsdk.core.controller.RestrictedBaseController#setViewName(java.lang.String)\r
106          \r
107         public void setViewName(String viewName) {\r
108                 this.viewName = viewName;\r
109         }\r
110         */\r
111 \r
112     /**\r
113      * Creates the svc instance.\r
114      *\r
115      * @param request the request\r
116      * @return the response entity\r
117      * @throws Exception the exception\r
118      */\r
119     @RequestMapping(value = "/mso_create_svc_instance", method = RequestMethod.POST)\r
120     public ResponseEntity<String> createSvcInstance(HttpServletRequest request, @RequestBody RequestDetails mso_request) throws Exception {\r
121 \r
122 \r
123         String methodName = "createSvcInstance";\r
124 \r
125         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
126 \r
127         if (!userIsPermmited(request, mso_request)) {\r
128             return new ResponseEntity<String>(HttpStatus.FORBIDDEN);\r
129         }\r
130 //              mso_request = retrieveRequestObject (request, mso_request);\r
131 \r
132         String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE);\r
133         String userId = "";\r
134         HttpSession session = request.getSession();\r
135         System.out.println((new ObjectMapper().writeValueAsString(session.getAttribute("roles"))));\r
136         MsoResponseWrapper w = createInstance(mso_request, p);\r
137         return (new ResponseEntity<String>(w.getResponse(), HttpStatus.OK));\r
138         // always return OK, the MSO status code is embedded in the body\r
139 \r
140 \r
141     }\r
142 \r
143     private boolean userIsPermmited(HttpServletRequest request, RequestDetails mso_request) {\r
144 \r
145         RoleValidator roleValidator = new RoleValidator(new RoleProvider().getUserRoles(request));\r
146         boolean isPermitted = roleValidator.isMsoRequestValid(mso_request);\r
147         if (!isPermitted) {\r
148             return false;\r
149         } else {\r
150             return true;\r
151         }\r
152     }\r
153 \r
154     /**\r
155      * Creates the vnf.\r
156      *\r
157      * @param serviceInstanceId the service instance id\r
158      * @param request           the request\r
159      * @return the response entity\r
160      * @throws Exception the exception\r
161      */\r
162     @RequestMapping(value = "/mso_create_vnf_instance/{serviceInstanceId}", method = RequestMethod.POST)\r
163     public ResponseEntity<String> createVnf(@PathVariable("serviceInstanceId") String serviceInstanceId, HttpServletRequest request, @RequestBody RequestDetails mso_request) throws Exception {\r
164 //        if (!userIsPermmited(request, mso_request)) {\r
165             String instanceId = (String) ((Map)((Map)((ArrayList)((Map) mso_request.getAdditionalProperties().get("requestDetails")).get("relatedInstanceList")).get(0)).get("relatedInstance")).get("instanceId");\r
166             ResponseEntity<String> a = new AaiController(request.getServletContext()).doGetServiceInstance(instanceId,"Service Instance id");\r
167 //            return new ResponseEntity<String>(HttpStatus.FORBIDDEN);\r
168 //        }\r
169         String methodName = "createVnf";\r
170         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
171 \r
172         //RequestDetails mso_request = retrieveRequestObject (request);\r
173         String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_VNF_INSTANCE);\r
174 \r
175         if (p == null || p.isEmpty()) {\r
176             throw new Exception("Vnf instance path is not defined");\r
177         }\r
178         // /serviceInstances/v2/<service_instance_id>/vnfs\r
179         String vnf_path = p.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);\r
180         MsoResponseWrapper w = createInstance(mso_request, vnf_path);\r
181 \r
182         // always return OK, the MSO status code is embedded in the body\r
183 \r
184         return (new ResponseEntity<String>(w.getResponse(), HttpStatus.OK));\r
185 \r
186     }\r
187 \r
188     /**\r
189      * Creates the nw instance.\r
190      *\r
191      * @param serviceInstanceId the service instance id\r
192      * @param request           the request\r
193      * @return the response entity\r
194      * @throws Exception the exception\r
195      */\r
196     @RequestMapping(value = "/mso_create_nw_instance/{serviceInstanceId}", method = RequestMethod.POST)\r
197     public ResponseEntity<String> createNwInstance(@PathVariable("serviceInstanceId") String serviceInstanceId, HttpServletRequest request, @RequestBody RequestDetails mso_request) throws Exception {\r
198 \r
199         String methodName = "createNwInstance";\r
200         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start, serviceInstanceId = " + serviceInstanceId);\r
201 \r
202         //RequestDetails mso_request = retrieveRequestObject (request);\r
203 \r
204         String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);\r
205 \r
206         if (p == null || p.isEmpty()) {\r
207             throw new Exception("Network instance path is not defined");\r
208         }\r
209         // /serviceInstances/v2/<serviceInstanceId>/networks/\r
210 \r
211         String nw_path = p.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);\r
212         MsoResponseWrapper w = createInstance(mso_request, nw_path);\r
213 \r
214         // always return OK, the MSO status code is embedded in the body\r
215 \r
216         return (new ResponseEntity<String>(w.getResponse(), HttpStatus.OK));\r
217 \r
218     }\r
219 \r
220     /**\r
221      * Creates the volume group instance.\r
222      *\r
223      * @param serviceInstanceId the service instance id\r
224      * @param vnfInstanceId     the vnf instance id\r
225      * @param request           the request\r
226      * @return the response entity\r
227      * @throws Exception the exception\r
228      */\r
229     @RequestMapping(value = "/mso_create_volumegroup_instance/{serviceInstanceId}/vnfs/{vnfInstanceId}", method = RequestMethod.POST)\r
230     public ResponseEntity<String> createVolumeGroupInstance(@PathVariable("serviceInstanceId") String serviceInstanceId, @PathVariable("vnfInstanceId") String vnfInstanceId,\r
231                                                             HttpServletRequest request, @RequestBody RequestDetails mso_request) throws Exception {\r
232         String methodName = "createVolumeGroupInstance";\r
233         if (!userIsPermmited(request, mso_request)) {\r
234             return new ResponseEntity<String>(HttpStatus.FORBIDDEN);\r
235         }\r
236         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
237 \r
238         //RequestDetails mso_request = retrieveRequestObject (request);\r
239         String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);\r
240 \r
241         if (p == null || p.isEmpty()) {\r
242             throw new Exception("Volume group instance path is not defined");\r
243         }\r
244         String path = p.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);\r
245         path = path.replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);\r
246 \r
247         MsoResponseWrapper w = createInstance(mso_request, path);\r
248 \r
249         // always return OK, the MSO status code is embedded in the body\r
250         return (new ResponseEntity<String>(w.getResponse(), HttpStatus.OK));\r
251     }\r
252 \r
253     /**\r
254      * Creates the vf module instance.\r
255      *\r
256      * @param serviceInstanceId the service instance id\r
257      * @param vnfInstanceId     the vnf instance id\r
258      * @param request           the request\r
259      * @return the response entity\r
260      * @throws Exception the exception\r
261      */\r
262     @RequestMapping(value = "/mso_create_vfmodule_instance/{serviceInstanceId}/vnfs/{vnfInstanceId}", method = RequestMethod.POST)\r
263     public ResponseEntity<String> createVfModuleInstance(@PathVariable("serviceInstanceId") String serviceInstanceId,\r
264                                                          @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletRequest request, @RequestBody RequestDetails mso_request) throws Exception {\r
265         String methodName = "createVfModuleInstance";\r
266 \r
267         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
268         if (!userIsPermmited(request, mso_request)) {\r
269             return new ResponseEntity<String>(HttpStatus.FORBIDDEN);\r
270         }\r
271         //RequestDetails mso_request = retrieveRequestObject (request);\r
272         String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);\r
273 \r
274         if (p == null || p.isEmpty()) {\r
275             throw new Exception("VF module instance path is not defined");\r
276         }\r
277         // /serviceInstances/v2/<serviceInstanceId>/vnfs/<vnfInstanceId>/vfmodules\r
278         String path = p.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);\r
279         path = path.replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);\r
280 \r
281         MsoResponseWrapper w = createInstance(mso_request, path);\r
282 \r
283         // always return OK, the MSO status code is embedded in the body\r
284 \r
285         return (new ResponseEntity<String>(w.getResponse(), HttpStatus.OK));\r
286     }\r
287 \r
288     /**\r
289      * Creates the instance.\r
290      *\r
291      * @param request the request\r
292      * @param path    the path\r
293      * @return the mso response wrapper\r
294      * @throws Exception the exception\r
295      */\r
296     protected MsoResponseWrapper createInstance(RequestDetails request, String path) throws Exception {\r
297         String methodName = "createInstance";\r
298         logger.debug(dateFormat.format(new Date()) + "<== " + methodName + " start");\r
299 \r
300         try {\r
301             MsoRestInterfaceIfc restController = MsoRestInterfaceFactory.getInstance();\r
302             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " calling Post, request = (" + request + ")");\r
303 \r
304             RestObject<String> restObjStr = new RestObject<String>();\r
305             String str = new String();\r
306             restObjStr.set(str);\r
307             restController.<String>Post(str, request, "", path, restObjStr);\r
308             MsoResponseWrapper w = MsoUtil.wrapResponse(restObjStr);\r
309 \r
310             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " w=" + w.getResponse());\r
311             return w;\r
312         } catch (Exception e) {\r
313             logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());\r
314             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());\r
315             throw e;\r
316         }\r
317     }\r
318 \r
319     /**\r
320      * Delete svc instance.\r
321      *\r
322      * @param serviceInstanceId the service instance id\r
323      * @param request           the request\r
324      * @return the response entity\r
325      * @throws Exception the exception\r
326      */\r
327     @RequestMapping(value = "/mso_delete_svc_instance/{serviceInstanceId}", method = RequestMethod.POST)\r
328     public ResponseEntity<String> deleteSvcInstance(@PathVariable("serviceInstanceId") String serviceInstanceId,\r
329                                                     HttpServletRequest request, @RequestBody RequestDetails mso_request) throws Exception {\r
330 \r
331         String methodName = "deleteSvcInstance";\r
332         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
333 \r
334         //RequestDetails mso_request = retrieveRequestObject (request);\r
335         String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_SVC_INSTANCE);\r
336         String path = p + "/" + serviceInstanceId;\r
337         MsoResponseWrapper w = deleteInstance(mso_request, path);\r
338 \r
339         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " w=" + w.getResponse());\r
340         // always return OK, the MSO status code is embedded in the body\r
341 \r
342         return (new ResponseEntity<String>(w.getResponse(), HttpStatus.OK));\r
343 \r
344     }\r
345 \r
346     /**\r
347      * Delete vnf.\r
348      *\r
349      * @param serviceInstanceId the service instance id\r
350      * @param vnfInstanceId     the vnf instance id\r
351      * @param request           the request\r
352      * @return the response entity\r
353      * @throws Exception the exception\r
354      */\r
355     @RequestMapping(value = "/mso_delete_vnf_instance/{serviceInstanceId}/vnfs/{vnfInstanceId}", method = RequestMethod.POST)\r
356 \r
357     public ResponseEntity<String> deleteVnf(@PathVariable("serviceInstanceId") String serviceInstanceId, @PathVariable("vnfInstanceId") String vnfInstanceId,\r
358                                             HttpServletRequest request, @RequestBody RequestDetails mso_request) throws Exception {\r
359         String methodName = "deleteVnf";\r
360 \r
361         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
362         if (!userIsPermmited(request, mso_request)) {\r
363             return new ResponseEntity<String>(HttpStatus.FORBIDDEN);\r
364         }\r
365         //RequestDetails mso_request = retrieveRequestObject (request);\r
366         String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_VNF_INSTANCE);\r
367         if (p == null || p.isEmpty()) {\r
368             throw new Exception("Vnf instance path is not defined");\r
369         }\r
370         // /serviceInstances/v2/<service_instance_id>/vnfs/\r
371         String vnf_path = p.replaceFirst(SVC_INSTANCE_ID, vnfInstanceId);\r
372         MsoResponseWrapper w = deleteInstance(mso_request, vnf_path + "/" + vnfInstanceId);\r
373 \r
374         // always return OK, the MSO status code is embedded in the body\r
375         return (new ResponseEntity<String>(w.getResponse(), HttpStatus.OK));\r
376 \r
377     }\r
378 \r
379     /**\r
380      * Delete vf module.\r
381      *\r
382      * @param serviceInstanceId the service instance id\r
383      * @param vnfInstanceId     the vnf instance id\r
384      * @param vfModuleId        the vf module id\r
385      * @param request           the request\r
386      * @return the response entity\r
387      * @throws Exception the exception\r
388      */\r
389     //mso_delete_vf_module/bc305d54-75b4-431b-adb2-eb6b9e546014/vnfs/fe9000-0009-9999/vfmodules/abeeee-abeeee-abeeee\r
390     @RequestMapping(value = "/mso_delete_vfmodule_instance/{serviceInstanceId}/vnfs/{vnfInstanceId}/vfModules/{vfModuleId}", method = RequestMethod.POST)\r
391     public ResponseEntity<String> deleteVfModule(\r
392             @PathVariable("serviceInstanceId") String serviceInstanceId, @PathVariable("vnfInstanceId") String vnfInstanceId,\r
393             @PathVariable("vfModuleId") String vfModuleId, HttpServletRequest request, @RequestBody RequestDetails mso_request) throws Exception {\r
394 \r
395         String methodName = "deleteVfModule";\r
396 \r
397         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
398         if (!userIsPermmited(request, mso_request)) {\r
399             return new ResponseEntity<String>(HttpStatus.FORBIDDEN);\r
400         }\r
401         //RequestDetails mso_request = new RequestDetails();\r
402         //mso_request = retrieveRequestObject (request);\r
403         String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_VF_MODULE_INSTANCE);\r
404         if (p == null || p.isEmpty()) {\r
405             throw new Exception("VF Module instance path is not defined");\r
406         }\r
407         // /serviceInstances/v2/<serviceInstanceId>/vnfs/<vnfInstanceId>/vfmodules\r
408         String path = p.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);\r
409         path = path.replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);\r
410         MsoResponseWrapper w = deleteInstance(mso_request, path + "/" + vfModuleId);\r
411 \r
412         // always return OK, the MSO status code is embedded in the body\r
413         return (new ResponseEntity<String>(w.getResponse(), HttpStatus.OK));\r
414 \r
415     }\r
416 \r
417     /**\r
418      * Delete volume group instance.\r
419      *\r
420      * @param serviceInstanceId the service instance id\r
421      * @param vnfInstanceId     the vnf instance id\r
422      * @param volumeGroupId     the volume group id\r
423      * @param request           the request\r
424      * @return the response entity\r
425      * @throws Exception the exception\r
426      */\r
427     @RequestMapping(value = "/mso_delete_volumegroup_instance/{serviceInstanceId}/vnfs/{vnfInstanceId}/volumeGroups/{volumeGroupId}", method = RequestMethod.POST)\r
428     public ResponseEntity<String> deleteVolumeGroupInstance(\r
429             @PathVariable("serviceInstanceId") String serviceInstanceId, @PathVariable("vnfInstanceId") String vnfInstanceId, @PathVariable("volumeGroupId") String volumeGroupId,\r
430             HttpServletRequest request, @RequestBody RequestDetails mso_request) throws Exception {\r
431 \r
432         String methodName = "deleteVolumeGroupInstance";\r
433 \r
434         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
435         if (!userIsPermmited(request, mso_request)) {\r
436             return new ResponseEntity<String>(HttpStatus.FORBIDDEN);\r
437         }\r
438         String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_VOLUME_GROUP_INSTANCE);\r
439         if (p == null || p.isEmpty()) {\r
440             throw new Exception("Volume group instance path is not defined");\r
441         }\r
442         // /serviceInstances/v2/{serviceInstanceId}/volumeGroups\r
443         String path = p.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);\r
444         path = path.replaceFirst(VNF_INSTANCE_ID, vnfInstanceId);\r
445         MsoResponseWrapper w = deleteInstance(mso_request, path + "/" + volumeGroupId);\r
446 \r
447         // always return OK, the MSO status code is embedded in the body\r
448         return (new ResponseEntity<String>(w.getResponse(), HttpStatus.OK));\r
449     }\r
450 \r
451     /**\r
452      * Delete nw instance.\r
453      *\r
454      * @param serviceInstanceId the service instance id\r
455      * @param networkInstanceId the network instance id\r
456      * @param request           the request\r
457      * @return the response entity\r
458      * @throws Exception the exception\r
459      */\r
460     @RequestMapping(value = "/mso_delete_nw_instance/{serviceInstanceId}/networks/{networkInstanceId}", method = RequestMethod.POST)\r
461     public ResponseEntity<String> deleteNwInstance(@PathVariable("serviceInstanceId") String serviceInstanceId,\r
462                                                    @PathVariable("networkInstanceId") String networkInstanceId, HttpServletRequest request, @RequestBody RequestDetails mso_request) throws Exception {\r
463 \r
464         String methodName = "deleteNwInstance";\r
465         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
466 \r
467         //RequestDetails mso_request = retrieveRequestObject (request);\r
468         if (!userIsPermmited(request, mso_request)) {\r
469             return new ResponseEntity<String>(HttpStatus.FORBIDDEN);\r
470         }\r
471         String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_NETWORK_INSTANCE);\r
472         if (p == null || p.isEmpty()) {\r
473             throw new Exception("Network instance path is not defined");\r
474         }\r
475         // /serviceInstances/v2/<service_instance_id>/networks\r
476         String path = p.replaceFirst(SVC_INSTANCE_ID, serviceInstanceId);\r
477         MsoResponseWrapper w = deleteInstance(mso_request, path + "/" + networkInstanceId);\r
478 \r
479         // always return OK, the MSO status code is embedded in the body\r
480         return (new ResponseEntity<String>(w.getResponse(), HttpStatus.OK));\r
481 \r
482     }\r
483 \r
484     /**\r
485      * Delete instance.\r
486      *\r
487      * @param request the request\r
488      * @param path    the path\r
489      * @return the mso response wrapper\r
490      * @throws Exception the exception\r
491      */\r
492     protected MsoResponseWrapper deleteInstance(RequestDetails request, String path) throws Exception {\r
493         String methodName = "deleteInstance";\r
494         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
495 \r
496         try {\r
497             MsoRestInterfaceIfc restController = MsoRestInterfaceFactory.getInstance();\r
498             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " calling Delete, path =[" + path + "]");\r
499 \r
500             RestObject<String> restObjStr = new RestObject<String>();\r
501             String str = new String();\r
502             restObjStr.set(str);\r
503             restController.<String>Delete(str, request, "", path, restObjStr);\r
504             MsoResponseWrapper w = MsoUtil.wrapResponse(restObjStr);\r
505 \r
506             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " w=" + w.getResponse());\r
507             return w;\r
508 \r
509         } catch (Exception e) {\r
510             logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());\r
511             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());\r
512             throw e;\r
513         }\r
514 \r
515     }\r
516 \r
517     /**\r
518      * Gets the orchestration request.\r
519      *\r
520      * @param requestId the request id\r
521      * @param request   the request\r
522      * @return the orchestration request\r
523      * @throws Exception the exception\r
524      */\r
525     @RequestMapping(value = "/mso_get_orch_req/{requestId}", method = RequestMethod.GET)\r
526     public ResponseEntity<String> getOrchestrationRequest(@PathVariable("requestId") String requestId,\r
527                                                           HttpServletRequest request) throws Exception {\r
528 \r
529         String methodName = "getOrchestrationRequest";\r
530         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
531         MsoResponseWrapper w = null;\r
532         try {\r
533             MsoRestInterfaceIfc restController = MsoRestInterfaceFactory.getInstance();\r
534             String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQ);\r
535             String path = p + "/" + requestId;\r
536 \r
537             RestObject<String> restObjStr = new RestObject<String>();\r
538             String str = new String();\r
539             restObjStr.set(str);\r
540 \r
541             restController.<String>Get(str, "", path, restObjStr);\r
542 \r
543             w = MsoUtil.wrapResponse(restObjStr);\r
544             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " w=" + w.getResponse());\r
545             // always return OK, the MSO status code is embedded in the body\r
546 \r
547         } catch (Exception e) {\r
548             logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());\r
549             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());\r
550             throw e;\r
551         }\r
552         // always return OK, the MSO status code is embedded in the body\r
553         return (new ResponseEntity<String>(w.getResponse(), HttpStatus.OK));\r
554     }\r
555 \r
556 \r
557     /**\r
558      * Gets the orchestration requests.\r
559      *\r
560      * @param filterString the filter string\r
561      * @param request      the request\r
562      * @return the orchestration requests\r
563      * @throws Exception the exception\r
564      */\r
565     @RequestMapping(value = "/mso_get_orch_reqs/{filterString}", method = RequestMethod.GET)\r
566     public ResponseEntity<String> getOrchestrationRequests(@PathVariable("filterString") String filterString,\r
567                                                            HttpServletRequest request) throws Exception {\r
568 \r
569         String methodName = "getOrchestrationRequests";\r
570         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
571         MsoResponseWrapper w = null;\r
572         try {\r
573             MsoRestInterfaceIfc restController = MsoRestInterfaceFactory.getInstance();\r
574             String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQS);\r
575             String path = p + filterString;\r
576 \r
577             RestObject<String> restObjStr = new RestObject<String>();\r
578             String str = new String();\r
579             restObjStr.set(str);\r
580 \r
581             restController.<String>Get(str, "", path, restObjStr);\r
582 \r
583             w = MsoUtil.wrapResponse(restObjStr);\r
584             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " w=" + w.getResponse());\r
585         } catch (Exception e) {\r
586             logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());\r
587             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());\r
588             throw e;\r
589         }\r
590         // always return OK, the MSO status code is embedded in the body\r
591         return (new ResponseEntity<String>(w.getResponse(), HttpStatus.OK));\r
592     }\r
593 \r
594     /**\r
595      * Gets the orchestration requests for svc instance.\r
596      *\r
597      * @param svc_instance_id the svc instance id\r
598      * @return the orchestration requests for svc instance\r
599      * @throws Exception the exception\r
600      */\r
601     public MsoResponseWrapper getOrchestrationRequestsForSvcInstance(String svc_instance_id) throws Exception {\r
602 \r
603         String methodName = "getOrchestrationRequestsForSvcInstance";\r
604         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
605         MsoResponseWrapper w = null;\r
606 \r
607         try {\r
608             MsoRestInterfaceIfc restController = MsoRestInterfaceFactory.getInstance();\r
609             String p = SystemProperties.getProperty(MsoProperties.MSO_REST_API_GET_ORC_REQS);\r
610             String path = p + svc_instance_id;\r
611 \r
612             RestObject<String> restObjStr = new RestObject<String>();\r
613             String str = new String();\r
614             restObjStr.set(str);\r
615 \r
616             restController.<String>Get(str, "", path, restObjStr);\r
617             w = MsoUtil.wrapResponse(restObjStr);\r
618             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " w=" + w.getResponse());\r
619 \r
620         } catch (Exception e) {\r
621             logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());\r
622             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());\r
623             throw e;\r
624         }\r
625         return w;\r
626     }\r
627 \r
628     /**\r
629      * Exception handler.\r
630      *\r
631      * @param e        the e\r
632      * @param response the response\r
633      * @throws IOException Signals that an I/O exception has occurred.\r
634      */\r
635     @ExceptionHandler(Exception.class)\r
636     private void exceptionHandler(Exception e, HttpServletResponse response) throws IOException {\r
637 \r
638                 /*\r
639                  * The following "logger.error" lines "should" be sufficient for logging the exception.\r
640                  * However, the console output in my Eclipse environment is NOT showing ANY of the\r
641                  * logger statements in this class. Thus the temporary "e.printStackTrace" statement\r
642                  * is also included.\r
643                  */\r
644 \r
645         String methodName = "exceptionHandler";\r
646         logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());\r
647         StringWriter sw = new StringWriter();\r
648         e.printStackTrace(new PrintWriter(sw));\r
649         logger.error(EELFLoggerDelegate.errorLogger, sw.toString());\r
650 \r
651                 /*\r
652                  *  Temporary - IF the above  mentioned "logger.error" glitch is resolved ...\r
653                  *  this statement could be removed since it would then likely result in duplicate\r
654                  *  trace output. \r
655                  */\r
656         e.printStackTrace(System.err);\r
657 \r
658         response.setContentType("application/json; charset=UTF-8");\r
659         response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);\r
660 \r
661         ExceptionResponse exceptionResponse = new ExceptionResponse();\r
662         exceptionResponse.setException(e.getClass().toString().replaceFirst("^.*\\.", ""));\r
663         exceptionResponse.setMessage(e.getMessage());\r
664 \r
665         response.getWriter().write(new ObjectMapper().writeValueAsString(exceptionResponse));\r
666 \r
667         response.flushBuffer();\r
668 \r
669     }\r
670 \r
671     /**\r
672      * Parses the orchestration requests for svc instance.\r
673      *\r
674      * @param resp the resp\r
675      * @return the list\r
676      * @throws Exception the exception\r
677      */\r
678     @SuppressWarnings("unchecked")\r
679     public List<JSONObject> parseOrchestrationRequestsForSvcInstance(ClientResponse resp) throws org.json.simple.parser.ParseException, Exception {\r
680 \r
681         String methodName = "parseOrchestrationRequestsForSvcInstance";\r
682 \r
683         ArrayList<JSONObject> json_list = new ArrayList<JSONObject>();\r
684 \r
685         String rlist_str = resp.readEntity(String.class);\r
686         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + " Response string: " + rlist_str);\r
687 \r
688         JSONParser parser = new JSONParser();\r
689         try {\r
690             Object obj = parser.parse(rlist_str);\r
691 \r
692             JSONObject jsonObject = (JSONObject) obj;\r
693 \r
694             JSONArray requestList = (JSONArray) jsonObject.get("requestList");\r
695 \r
696             if (requestList != null && !(requestList.isEmpty()))\r
697                 for (Object container : requestList) {\r
698 \r
699                     JSONObject containerJsonObj = (JSONObject) container;\r
700                     //logger.debug(dateFormat.format(new Date()) +  "<== " + "." + methodName + " reqJsonObj: " + containerJsonObj.toJSONString());\r
701                     JSONObject reqJsonObj = (JSONObject) containerJsonObj.get("request");\r
702 \r
703                     //logger.debug(dateFormat.format(new Date()) +  "<== " + "." + methodName + " reqJsonObj.requestId: " +\r
704                     //  reqJsonObj.get("requestId") );\r
705                     JSONObject result = new JSONObject();\r
706 \r
707                     result.put("requestId", reqJsonObj.get("requestId"));\r
708                     if (reqJsonObj.get("requestType") != null) {\r
709                         result.put("requestType", (reqJsonObj.get("requestType").toString()));\r
710                     }\r
711                     JSONObject req_status = (JSONObject) reqJsonObj.get("requestStatus");\r
712                     if (req_status != null) {\r
713                         result.put("timestamp", (req_status.get("timestamp")));\r
714                         result.put("requestState", (req_status.get("requestState")));\r
715                         result.put("statusMessage", (req_status.get("statusMessage")));\r
716                         result.put("percentProgress", (req_status.get("percentProgress")));\r
717                     }\r
718                     json_list.add(result);\r
719                 }\r
720         } catch (org.json.simple.parser.ParseException pe) {\r
721             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + " Parse exception: " + pe.toString());\r
722             throw pe;\r
723         } catch (Exception e) {\r
724             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + " Exception: " + e.toString());\r
725             throw e;\r
726         }\r
727         return (json_list);\r
728     }\r
729 \r
730     /**\r
731      * Retrieve request object.\r
732      *\r
733      * @param request the request\r
734      * @return the request details\r
735      * @throws Exception the exception\r
736      */\r
737     public RequestDetails retrieveRequestObject(HttpServletRequest request, @RequestBody RequestDetails mso_request) throws Exception {\r
738 \r
739         String methodName = "retrieveRequestObject";\r
740         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
741 \r
742         ObjectMapper mapper = new ObjectMapper();\r
743         //JSON from String to Object\r
744         //RequestDetails mso_request;\r
745 \r
746         try {\r
747             //mso_request = new RequestDetails();\r
748             //mso_request = mapper.readValue(request.getInputStream(), RequestDetails.class);\r
749         } catch (Exception e) {\r
750             logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + " Unable to read json object RequestDetails e=" + e.getMessage());\r
751             throw e;\r
752         }\r
753         if (mso_request == null) {\r
754             logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + " mso_request is null");\r
755             throw new Exception("RequestDetails is missing");\r
756         }\r
757         try {\r
758             String json_req = mapper.writeValueAsString(mso_request);\r
759             logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " request=[" + json_req + "]");\r
760         } catch (Exception e) {\r
761             logger.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + " Unable to convert RequestDetails to json string e=" + e.getMessage());\r
762             throw e;\r
763         }\r
764         return (mso_request);\r
765     }\r
766 }\r