[VID-6] Initial rebase push
[vid.git] / vid-app-common / src / main / java / org / openecomp / vid / controller / AaiController.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 import java.io.File;\r
24 import java.io.IOException;\r
25 import java.text.DateFormat;\r
26 import java.text.SimpleDateFormat;\r
27 import java.util.Date;\r
28 import java.util.HashMap;\r
29 import java.util.Iterator;\r
30 import java.util.Map;\r
31 import java.util.UUID;\r
32 \r
33 import javax.servlet.ServletContext;\r
34 import javax.servlet.http.HttpServletRequest;\r
35 import javax.servlet.http.HttpSession;\r
36 import javax.ws.rs.BadRequestException;\r
37 import javax.ws.rs.DefaultValue;\r
38 import javax.ws.rs.QueryParam;\r
39 import javax.ws.rs.WebApplicationException;\r
40 import javax.ws.rs.core.Response;\r
41 \r
42 import org.json.simple.JSONArray;\r
43 import org.json.simple.JSONObject;\r
44 import org.json.simple.parser.JSONParser;\r
45 import org.openecomp.aai.util.AAIRestInterface;\r
46 import org.springframework.beans.factory.annotation.Autowired;\r
47 import org.springframework.http.HttpStatus;\r
48 import org.springframework.http.MediaType;\r
49 import org.springframework.http.ResponseEntity;\r
50 import org.springframework.web.bind.annotation.PathVariable;\r
51 import org.springframework.web.bind.annotation.RequestMapping;\r
52 import org.springframework.web.bind.annotation.RequestMethod;\r
53 import org.springframework.web.bind.annotation.RestController;\r
54 import org.springframework.web.servlet.ModelAndView;\r
55 \r
56 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;\r
57 import org.openecomp.portalsdk.core.domain.User;\r
58 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;\r
59 import org.openecomp.portalsdk.core.util.SystemProperties;\r
60 \r
61 /**\r
62  * Controller to handle a&ai requests.\r
63  */\r
64 \r
65 @RestController\r
66 public class AaiController extends RestrictedBaseController{\r
67 \r
68         /** The view name. */\r
69         String viewName;\r
70 \r
71         /** The logger. */\r
72         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AaiController.class);\r
73 \r
74         /** The Constant dateFormat. */\r
75         final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");\r
76 \r
77         /** The from app id. */\r
78         protected String fromAppId = "VidAaiController";\r
79 \r
80         /** The model. */\r
81         private Map<String,Object> model = new HashMap<String,Object>();\r
82 \r
83         /** The servlet context. */\r
84         private @Autowired ServletContext servletContext;\r
85 \r
86         /**\r
87          * Welcome method.\r
88          *\r
89          * @param request the request\r
90          * @return ModelAndView The view\r
91          */\r
92         @RequestMapping(value = {"/subscriberSearch" }, method = RequestMethod.GET)\r
93         public ModelAndView welcome(HttpServletRequest request) {\r
94                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== AaiController welcome start");\r
95                 return new ModelAndView(getViewName());         \r
96         }\r
97 \r
98         /* (non-Javadoc)\r
99          * @see org.openecomp.portalsdk.core.controller.RestrictedBaseController#getViewName()\r
100          */\r
101         public String getViewName() {\r
102                 return viewName;\r
103         }\r
104 \r
105         /* (non-Javadoc)\r
106          * @see org.openecomp.portalsdk.core.controller.RestrictedBaseController#setViewName(java.lang.String)\r
107          */\r
108         public void setViewName(String viewName) {\r
109                 this.viewName = viewName;\r
110         }\r
111 \r
112         /**\r
113          * Get services from a&ai.\r
114          *\r
115          * @return ResponseEntity<String> The response entity with the logged in user uuid.\r
116          * @throws IOException Signals that an I/O exception has occurred.\r
117          * @throws InterruptedException the interrupted exception\r
118          */\r
119         @RequestMapping(value = {"/getuserID" }, method = RequestMethod.GET)\r
120         public ResponseEntity<String> getUserID(HttpServletRequest request) throws IOException, InterruptedException {\r
121 \r
122                 String userId = "";\r
123                 HttpSession session = request.getSession();\r
124                 if (session != null)\r
125                 {\r
126                         User user = (User) session.getAttribute(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME));\r
127                         if (user != null)\r
128                         {\r
129                                 userId = user.getHrid();\r
130                         }\r
131                 }\r
132 \r
133                 return new ResponseEntity<String>( userId, HttpStatus.OK);\r
134         }\r
135 \r
136 \r
137         /**\r
138          * Get services from a&ai.\r
139          *\r
140          * @return ResponseEntity<String> The response entity\r
141          * @throws IOException Signals that an I/O exception has occurred.\r
142          * @throws InterruptedException the interrupted exception\r
143          */\r
144         @RequestMapping(value="/aai_get_services",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) \r
145         public ResponseEntity<String> doGetServices() throws IOException, InterruptedException {        \r
146                 File certiPath = GetCertificatesPath();\r
147                 Response resp = doAaiGet(certiPath.getAbsolutePath(), "service-design-and-creation/services", false);\r
148 \r
149                 return convertResponseToResponseEntity(resp);\r
150         }\r
151 \r
152         /**\r
153          * Lookup single service instance in a&ai.  Get the service-subscription and customer, too, i guess?\r
154          *\r
155          * @param serviceInstanceId the service instance Id\r
156          * @return ResponseEntity The response entity\r
157          * @throws IOException Signals that an I/O exception has occurred.\r
158          * @throws InterruptedException the interrupted exception\r
159          */\r
160         @RequestMapping(value="/aai_get_service_instance/{service-instance-id}/{service-instance-type}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) \r
161         public ResponseEntity<String> doGetServiceInstance(@PathVariable("service-instance-id") String serviceInstanceId,@PathVariable("service-instance-type") String serviceInstanceType) throws IOException, InterruptedException {  \r
162                 File certiPath = GetCertificatesPath();\r
163                 Response resp=null;\r
164 \r
165                 if(serviceInstanceType.equalsIgnoreCase("Service Instance Id")){\r
166                         resp = doAaiGet( certiPath.getAbsolutePath(), \r
167                                         "search/nodes-query?search-node-type=service-instance&filter=service-instance-id:EQUALS:" \r
168                                                         + serviceInstanceId, false);\r
169                 } else {\r
170                         resp = doAaiGet( certiPath.getAbsolutePath(), \r
171                                         "search/nodes-query?search-node-type=service-instance&filter=service-instance-name:EQUALS:" \r
172                                                         + serviceInstanceId, false);\r
173                 }\r
174                 return convertResponseToResponseEntity(resp);\r
175         }\r
176 \r
177 \r
178         /**\r
179          * Get services from a&ai.\r
180          *\r
181          * @param globalCustomerId the global customer id\r
182          * @param serviceSubscriptionId the service subscription id\r
183          * @return ResponseEntity The response entity\r
184          * @throws IOException Signals that an I/O exception has occurred.\r
185          * @throws InterruptedException the interrupted exception\r
186          */\r
187         @RequestMapping(value="/aai_get_service_subscription/{global-customer-id}/{service-subscription-id}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) \r
188         public ResponseEntity<String> doGetServices(@PathVariable("global-customer-id") String globalCustomerId,\r
189                         @PathVariable("service-subscription-id") String serviceSubscriptionId) throws IOException, InterruptedException {       \r
190                 File certiPath = GetCertificatesPath();\r
191                 Response resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers/customer/" + globalCustomerId \r
192                                 + "/service-subscriptions/service-subscription/" + serviceSubscriptionId + "?depth=0", false);\r
193                 return convertResponseToResponseEntity(resp);\r
194         }\r
195 \r
196         /**\r
197          * Obtain the subscriber list from a&ai.\r
198          *\r
199          * @param fullSet the full set\r
200          * @return ResponseEntity The response entity\r
201          * @throws IOException Signals that an I/O exception has occurred.\r
202          * @throws InterruptedException the interrupted exception\r
203          */\r
204         @RequestMapping(value="/aai_get_subscribers",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)   \r
205         public ResponseEntity<String> doGetSubscriberList(@DefaultValue("n") @QueryParam("fullSet") String fullSet) throws IOException, InterruptedException {\r
206                 Response resp = getSubscribers(false);\r
207                 return convertResponseToResponseEntity(resp);\r
208         }\r
209 \r
210         /**\r
211          * Obtain the Target Prov Status from the System.Properties file.\r
212          *\r
213          * @return ResponseEntity The response entity\r
214          * @throws IOException Signals that an I/O exception has occurred.\r
215          * @throws InterruptedException the interrupted exception\r
216          */     \r
217         @RequestMapping(value="/get_system_prop_vnf_prov_status",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)       \r
218         public ResponseEntity<String> getTargetProvStatus() throws IOException, InterruptedException {\r
219                 String p = SystemProperties.getProperty("aai.vnf.provstatus");\r
220                 return new ResponseEntity<String>(p, HttpStatus.OK);\r
221         }\r
222 \r
223         /**\r
224          * Obtain the full subscriber list from a&ai.\r
225          *\r
226          * @return ResponseEntity The response entity\r
227          * @throws IOException Signals that an I/O exception has occurred.\r
228          * @throws InterruptedException the interrupted exception\r
229          */\r
230         @RequestMapping(value="/aai_get_full_subscribers",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)      \r
231         public ResponseEntity<String> getFullSubscriberList() throws IOException, InterruptedException {\r
232                 Response resp = getSubscribers(true);\r
233                 return convertResponseToResponseEntity(resp);\r
234         }\r
235 \r
236 \r
237         /**\r
238          * Refresh the subscriber list from a&ai.\r
239          *\r
240          * @return ResponseEntity The response entity\r
241          * @throws IOException Signals that an I/O exception has occurred.\r
242          */\r
243         @RequestMapping(value="/aai_refresh_subscribers",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)       \r
244         public ResponseEntity<String> doRefreshSubscriberList() throws IOException {\r
245                 Response resp = getSubscribers(false);\r
246                 return convertResponseToResponseEntity(resp);\r
247         }\r
248 \r
249         /**\r
250          * Refresh the full subscriber list from a&ai.\r
251          *\r
252          * @return ResponseEntity The response entity\r
253          * @throws IOException Signals that an I/O exception has occurred.\r
254          */\r
255         @RequestMapping(value="/aai_refresh_full_subscribers",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)          \r
256         public ResponseEntity<String> doRefreshFullSubscriberList() throws IOException {\r
257                 Response resp = getSubscribers(false);\r
258                 return convertResponseToResponseEntity(resp);\r
259         }\r
260 \r
261         /**\r
262          * Get subscriber details from a&ai.\r
263          *\r
264          * @param subscriberId the subscriber id\r
265          * @return ResponseEntity The response entity\r
266          */\r
267         @RequestMapping(value="/aai_sub_details/{subscriberId}", method = RequestMethod.GET)\r
268         public ResponseEntity<String> GetSubscriber(@PathVariable("subscriberId") String subscriberId) {\r
269                 Response resp = getSubscriberDetails(subscriberId);\r
270                 return convertResponseToResponseEntity(resp);\r
271         }\r
272 \r
273         /**\r
274          * Issue a named query to a&ai.\r
275          *\r
276          * @param namedQueryId the named query id\r
277          * @param globalCustomerId the global customer id\r
278          * @param serviceType the service type\r
279          * @param serviceInstance the service instance\r
280          * @return ResponseEntity The response entity\r
281          */\r
282         @RequestMapping(value="/aai_sub_viewedit/{namedQueryId}/{globalCustomerId}/{serviceType}/{serviceInstance}", method = RequestMethod.GET)\r
283         public ResponseEntity<String> viewEditGetComponentList(\r
284                         @PathVariable("namedQueryId") String namedQueryId,\r
285                         @PathVariable("globalCustomerId") String globalCustomerId,\r
286                         @PathVariable("serviceType") String serviceType,\r
287                         @PathVariable("serviceInstance") String serviceInstance) { \r
288 \r
289                 String componentListPayload = getComponentListPutPayload(namedQueryId, globalCustomerId, serviceType, serviceInstance);\r
290                 File certiPath = GetCertificatesPath();\r
291 \r
292                 Response resp = doAaiPost(certiPath.getAbsolutePath(), "search/named-query", componentListPayload, false); \r
293         return convertResponseToResponseEntity(resp);\r
294         }\r
295 \r
296         /**\r
297          * Issue a named query to a&ai.\r
298          *\r
299          * @param namedQueryId the named query id\r
300          * @param globalCustomerId the global customer id\r
301          * @param serviceType the service type\r
302          * @param serviceInstance the service instance\r
303          * @return ResponseEntity The response entity\r
304          */\r
305         @RequestMapping(value="/aai_get_models_by_service_type/{namedQueryId}/{globalCustomerId}/{serviceType}", method = RequestMethod.GET)\r
306         public ResponseEntity<String> viewEditGetComponentList(\r
307                         @PathVariable("namedQueryId") String namedQueryId,\r
308                         @PathVariable("globalCustomerId") String globalCustomerId,\r
309                         @PathVariable("serviceType") String serviceType) { \r
310 \r
311                 String componentListPayload = getModelsByServiceTypePayload(namedQueryId, globalCustomerId, serviceType);\r
312                 File certiPath = GetCertificatesPath();\r
313 \r
314                 Response resp = doAaiPost(certiPath.getAbsolutePath(), "search/named-query", componentListPayload, false); \r
315         return convertResponseToResponseEntity(resp);\r
316         }\r
317         \r
318         /**\r
319          * Parses the for tenants.\r
320          *\r
321          * @param resp the resp\r
322          * @return the string\r
323          */\r
324         private String parseForTenants(String resp)\r
325         {\r
326                 String tenantList = "";\r
327 \r
328                 try\r
329                 {\r
330                         JSONParser jsonParser = new JSONParser();\r
331 \r
332                         JSONObject jsonObject = (JSONObject) jsonParser.parse(resp);\r
333 \r
334                         return  parseCustomerObjectForTenants(jsonObject);\r
335                 }\r
336                 catch (Exception ex) {\r
337 \r
338                 }\r
339 \r
340                 return tenantList;\r
341         }\r
342 \r
343         /**\r
344          * Parses the for tenants by service subscription.\r
345          *\r
346          * @param resp the resp\r
347          * @return the string\r
348          */\r
349         private String parseForTenantsByServiceSubscription(String resp)\r
350         {\r
351                 String tenantList = "";\r
352 \r
353                 try\r
354                 {\r
355                         JSONParser jsonParser = new JSONParser();\r
356 \r
357                         JSONObject jsonObject = (JSONObject) jsonParser.parse(resp);\r
358 \r
359                         return  parseServiceSubscriptionObjectForTenants(jsonObject);\r
360                 }\r
361                 catch (Exception ex) {\r
362 \r
363                 }\r
364 \r
365                 return tenantList;\r
366         }\r
367 \r
368 \r
369         //      @RequestMapping(value="/aai_get_tenants/{global-customer-id}", method = RequestMethod.GET)\r
370         //      public ResponseEntity<String> viewEditGetComponentList(\r
371         //                      @PathVariable("global-customer-id") String globalCustomerId) {\r
372         //              return new ResponseEntity<String>(getTenants(globalCustomerId), HttpStatus.OK);\r
373         //      }\r
374 \r
375         /**\r
376          * Obtain tenants for a given service type.\r
377          *\r
378          * @param globalCustomerId the global customer id\r
379          * @param serviceType the service type\r
380          * @return ResponseEntity The response entity\r
381          */\r
382         @RequestMapping(value="/aai_get_tenants/{global-customer-id}/{service-type}", method = RequestMethod.GET)\r
383         public ResponseEntity<String> viewEditGetTenantsFromServiceType(\r
384                         @PathVariable("global-customer-id") String globalCustomerId, @PathVariable("service-type") String serviceType) {\r
385         return getTenantsFromServiceType(globalCustomerId, serviceType);\r
386         }\r
387 \r
388         private ResponseEntity<String> convertResponseToResponseEntity(Response resp) { \r
389                 ResponseEntity<String> respEnt;\r
390                 if (resp == null) { \r
391                         respEnt = new ResponseEntity<String>("Failed to fetch data from A&AI, check server logs for details.", HttpStatus.INTERNAL_SERVER_ERROR);\r
392                 } else { \r
393                         respEnt = new ResponseEntity<String>((String)resp.readEntity(String.class), HttpStatus.valueOf(resp.getStatus()));\r
394                 }\r
395                 return respEnt;\r
396         }\r
397 \r
398         /**\r
399          * Gets the tenants.\r
400          *\r
401          * @param globalCustomerId the global customer id\r
402          * @return the tenants\r
403          */\r
404         private ResponseEntity<String> getTenants(String globalCustomerId)\r
405         {\r
406                 File certiPath = GetCertificatesPath();\r
407         Response resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers/customer/" + globalCustomerId, false);\r
408         \r
409         ResponseEntity<String> respEnt;\r
410         if (resp.getStatus() >= 200 && resp.getStatus() <= 299) { \r
411             respEnt = new ResponseEntity<String>(parseForTenants((String)resp.readEntity(String.class)), HttpStatus.OK);\r
412         } else {\r
413             respEnt = new ResponseEntity<String>((String)resp.readEntity(String.class), HttpStatus.valueOf(resp.getStatus()));\r
414         }\r
415         return respEnt;\r
416 \r
417         }\r
418 \r
419         /**\r
420          * Gets the tenants from service type.\r
421          *\r
422          * @param globalCustomerId the global customer id\r
423          * @param serviceType the service type\r
424          * @return the tenants from service type\r
425          */\r
426         private ResponseEntity<String> getTenantsFromServiceType(String globalCustomerId, String serviceType)\r
427         {\r
428                 File certiPath = GetCertificatesPath();\r
429         String url = "business/customers/customer/" + globalCustomerId + "/service-subscriptions/service-subscription/" + serviceType;\r
430 \r
431         Response resp = doAaiGet(certiPath.getAbsolutePath(), url, false);\r
432         \r
433         ResponseEntity<String> respEnt;\r
434         if (resp.getStatus() >= 200 && resp.getStatus() <= 299) { \r
435             respEnt = new ResponseEntity<String>(parseForTenantsByServiceSubscription((String)resp.readEntity(String.class)), HttpStatus.OK);\r
436         } else {\r
437             respEnt = new ResponseEntity<String>((String)resp.readEntity(String.class), HttpStatus.valueOf(resp.getStatus()));\r
438         }\r
439         return respEnt;\r
440 \r
441         }\r
442 \r
443         /**\r
444          * Gets the services.\r
445          *\r
446          * @return the services\r
447          */\r
448         private Response getServices()\r
449         {\r
450                 File certiPath = GetCertificatesPath();\r
451         Response resp  = doAaiGet(certiPath.getAbsolutePath(), "service-design-and-creation/services", false);\r
452         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "getServices() resp=" + resp.getStatusInfo());\r
453 \r
454                 //model.put("aai_get_services", resp);\r
455                 return resp;\r
456         }\r
457 \r
458 \r
459         /**\r
460          * Gets the subscribers.\r
461          *\r
462          * @param isFullSet the is full set\r
463          * @return the subscribers\r
464          */\r
465         private Response getSubscribers(boolean isFullSet)\r
466         {\r
467                 File certiPath = GetCertificatesPath();\r
468                 String depth = "0";\r
469 \r
470         Response resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers?subscriber-type=INFRA&depth=" + depth, false);\r
471         if (resp != null) { \r
472                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "getSubscribers() resp=" + resp.getStatusInfo().toString());\r
473         }\r
474                 return resp;\r
475         }\r
476 \r
477         \r
478         \r
479         /**\r
480          * Gets the subscriber details.\r
481          *\r
482          * @param subscriberId the subscriber id\r
483          * @return the subscriber details\r
484          */\r
485         private Response getSubscriberDetails(String subscriberId)\r
486         {\r
487                 File certiPath = GetCertificatesPath();\r
488                 Response resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers/customer/" + subscriberId + "?depth=2", false);\r
489                 //String resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers/customer/" + subscriberId, false);\r
490                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "getSubscriberDetails() resp=" + resp.getStatusInfo().toString());\r
491                 return resp;\r
492         }\r
493 \r
494         /**\r
495          * Gets the certificates path.\r
496          *\r
497          * @return the file\r
498          */\r
499         private File GetCertificatesPath()\r
500         {\r
501                 if (servletContext != null)\r
502                         return new File( servletContext.getRealPath("/WEB-INF/cert/") );\r
503                 return null;\r
504         }\r
505 \r
506         /**\r
507          * Send a GET request to a&ai.\r
508          *\r
509          * @param certiPath the certi path\r
510          * @param uri the uri\r
511          * @param xml the xml\r
512          * @return String The response\r
513          */\r
514         protected Response doAaiGet(String certiPath, String uri, boolean xml) {\r
515                 String methodName = "getSubscriberList";                \r
516                 String transId = UUID.randomUUID().toString();\r
517                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
518 \r
519                 Response resp = null;\r
520                 try {\r
521 \r
522                         AAIRestInterface restContrller = new AAIRestInterface(certiPath);\r
523                         resp = restContrller.RestGet(fromAppId, transId, uri, xml);\r
524 \r
525                 } catch (WebApplicationException e) {\r
526                         final String message = ((BadRequestException) e).getResponse().readEntity(String.class);\r
527                         logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + message);\r
528                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + message);\r
529                 } catch (Exception e) {\r
530                         logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());\r
531                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());\r
532                 }\r
533 \r
534                 return resp;\r
535         }\r
536 \r
537         /**\r
538          * Send a POST request to a&ai.\r
539          *\r
540          * @param certiPath the certi path\r
541          * @param uri the uri\r
542          * @param payload the payload\r
543          * @param xml the xml\r
544          * @return String The response\r
545          */\r
546         protected Response doAaiPost(String certiPath, String uri, String payload, boolean xml) {\r
547                 String methodName = "getSubscriberList";                \r
548                 String transId = UUID.randomUUID().toString();\r
549                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
550 \r
551                 Response resp = null;\r
552                 try {\r
553 \r
554                         AAIRestInterface restContrller = new AAIRestInterface(certiPath);\r
555                         resp = restContrller.RestPost(fromAppId, transId, uri, payload, xml);\r
556 \r
557                 } catch (Exception e) {\r
558                         logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());\r
559                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());\r
560                 }\r
561 \r
562                 return resp;\r
563         }\r
564 \r
565         /**\r
566          * Gets the component list put payload.\r
567          *\r
568          * @param namedQueryId the named query id\r
569          * @param globalCustomerId the global customer id\r
570          * @param serviceType the service type\r
571          * @param serviceInstance the service instance\r
572          * @return the component list put payload\r
573          */\r
574         private String getComponentListPutPayload(String namedQueryId, String globalCustomerId, String serviceType, String serviceInstance) {\r
575                 return \r
576                                 "               {" +\r
577                                 "    \"instance-filters\": {" +\r
578                                 "        \"instance-filter\": [" +\r
579                                 "            {" +\r
580                                 "                \"customer\": {" +\r
581                                 "                    \"global-customer-id\": \"" + globalCustomerId + "\"" +\r
582                                 "                }," +\r
583                                 "                \"service-instance\": {" +\r
584                                 "                    \"service-instance-id\": \"" + serviceInstance + "\"" +\r
585                                 "                }," +\r
586                                 "                \"service-subscription\": {" +\r
587                                 "                    \"service-type\": \"" + serviceType + "\"" +\r
588                                 "                }" +\r
589                                 "            }" +\r
590                                 "        ]" +\r
591                                 "    }," +\r
592                                 "    \"query-parameters\": {" +\r
593                                 "        \"named-query\": {" +\r
594                                 "            \"named-query-uuid\": \"" + namedQueryId + "\"" +\r
595                                 "        }" +\r
596                                 "    }" +\r
597                                 "}";\r
598 \r
599         }\r
600         private String getModelsByServiceTypePayload(String namedQueryId, String globalCustomerId, String serviceType) {\r
601                 // TODO Auto-generated method stub\r
602                 return                  "               {" +\r
603                 "    \"instance-filters\": {" +\r
604                 "        \"instance-filter\": [" +\r
605                 "            {" +\r
606                 "                \"customer\": {" +\r
607                 "                    \"global-customer-id\": \"" + globalCustomerId + "\"" +\r
608                 "                }," +\r
609                 "                \"service-subscription\": {" +\r
610                 "                    \"service-type\": \"" + serviceType + "\"" +\r
611                 "                }" +\r
612                 "            }" +\r
613                 "        ]" +\r
614                 "    }," +\r
615                 "    \"query-parameters\": {" +\r
616                 "        \"named-query\": {" +\r
617                 "            \"named-query-uuid\": \"" + namedQueryId + "\"" +\r
618                 "        }" +\r
619                 "    }" +\r
620                 "}";\r
621         \r
622         }\r
623 \r
624         /**\r
625          * Return tenant details.\r
626          *\r
627          * @param jsonObject the json object\r
628          * @return String The parsing results\r
629          */\r
630         public static String parseCustomerObjectForTenants(JSONObject jsonObject) {\r
631 \r
632                 JSONArray tenantArray = new JSONArray();\r
633                 boolean bconvert = false;\r
634 \r
635                 try {\r
636 \r
637                         JSONObject serviceSubsObj = (JSONObject) jsonObject.get("service-subscriptions");\r
638 \r
639                         if (serviceSubsObj != null)\r
640                         {\r
641                                 JSONArray srvcSubArray = (JSONArray) serviceSubsObj.get("service-subscription");\r
642 \r
643                                 if (srvcSubArray != null)\r
644                                 {\r
645                                         Iterator i = srvcSubArray.iterator();\r
646 \r
647                                         while (i.hasNext()) {\r
648 \r
649                                                 JSONObject innerObj = (JSONObject) i.next();\r
650 \r
651                                                 if (innerObj == null)\r
652                                                         continue;\r
653 \r
654                                                 JSONObject relationShipListsObj = (JSONObject) innerObj.get("relationship-list");\r
655                                                 if (relationShipListsObj != null)\r
656                                                 {\r
657                                                         JSONArray rShipArray = (JSONArray) relationShipListsObj.get("relationship");\r
658                                                         if (rShipArray != null)\r
659                                                         {\r
660                                                                 Iterator i1 = rShipArray.iterator();\r
661 \r
662                                                                 while (i1.hasNext()) {\r
663 \r
664                                                                         JSONObject inner1Obj = (JSONObject) i1.next();\r
665 \r
666                                                                         if (inner1Obj == null)\r
667                                                                                 continue;\r
668 \r
669                                                                         String relatedTo = checkForNull((String)inner1Obj.get("related-to"));\r
670                                                                         if (relatedTo.equalsIgnoreCase("tenant"))\r
671                                                                         {\r
672                                                                                 JSONObject tenantNewObj = new JSONObject();\r
673 \r
674                                                                                 String relatedLink = checkForNull((String) inner1Obj.get("related-link"));\r
675                                                                                 tenantNewObj.put("link", relatedLink);\r
676 \r
677                                                                                 JSONArray rDataArray = (JSONArray) inner1Obj.get("relationship-data");\r
678                                                                                 if (rDataArray != null)\r
679                                                                                 {\r
680                                                                                         Iterator i2 = rDataArray.iterator();\r
681 \r
682                                                                                         while (i2.hasNext()) {\r
683                                                                                                 JSONObject inner2Obj = (JSONObject) i2.next();\r
684 \r
685                                                                                                 if (inner2Obj == null)\r
686                                                                                                         continue;\r
687 \r
688                                                                                                 String rShipKey = checkForNull((String)inner2Obj.get("relationship-key"));\r
689                                                                                                 String rShipVal = checkForNull((String)inner2Obj.get("relationship-value"));\r
690                                                                                                 if (rShipKey.equalsIgnoreCase("cloud-region.cloud-owner"))\r
691                                                                                                 {\r
692                                                                                                         tenantNewObj.put("cloudOwner", rShipVal);\r
693                                                                                                 }\r
694                                                                                                 else if (rShipKey.equalsIgnoreCase("cloud-region.cloud-region-id"))\r
695                                                                                                 {\r
696                                                                                                         tenantNewObj.put("cloudRegionID", rShipVal);\r
697                                                                                                 }\r
698 \r
699                                                                                                 if (rShipKey.equalsIgnoreCase("tenant.tenant-id"))\r
700                                                                                                 {\r
701                                                                                                         tenantNewObj.put("tenantID", rShipVal);\r
702                                                                                                 }\r
703                                                                                         }\r
704                                                                                 }\r
705 \r
706                                                                                 JSONArray relatedTPropArray = (JSONArray) inner1Obj.get("related-to-property");\r
707                                                                                 if (relatedTPropArray != null)\r
708                                                                                 {\r
709                                                                                         Iterator i3 = relatedTPropArray.iterator();\r
710 \r
711                                                                                         while (i3.hasNext()) {\r
712                                                                                                 JSONObject inner3Obj = (JSONObject) i3.next();\r
713 \r
714                                                                                                 if (inner3Obj == null)\r
715                                                                                                         continue;\r
716 \r
717                                                                                                 String propKey = checkForNull((String)inner3Obj.get("property-key"));\r
718                                                                                                 String propVal = checkForNull((String)inner3Obj.get("property-value"));\r
719                                                                                                 if (propKey.equalsIgnoreCase("tenant.tenant-name"))\r
720                                                                                                 {\r
721                                                                                                         tenantNewObj.put("tenantName", propVal);\r
722                                                                                                 }\r
723                                                                                         }\r
724                                                                                 }\r
725                                                                                 bconvert = true;\r
726                                                                                 tenantArray.add(tenantNewObj);\r
727                                                                         }\r
728                                                                 }\r
729                                                         }\r
730                                                 }\r
731                                         }\r
732                                 }\r
733                         }\r
734                 } catch (NullPointerException ex) {\r
735 \r
736 \r
737                 }\r
738 \r
739                 if (bconvert)\r
740                         return tenantArray.toJSONString();\r
741                 else\r
742                         return "";\r
743 \r
744         }\r
745 \r
746 \r
747         /**\r
748          * Retrieve the service subscription from the jsonObject.\r
749          *\r
750          * @param jsonObject the json object\r
751          * @return String\r
752          */\r
753         public static String parseServiceSubscriptionObjectForTenants(JSONObject jsonObject) {\r
754 \r
755                 JSONArray tenantArray = new JSONArray();\r
756                 boolean bconvert = false;\r
757 \r
758                 try {\r
759                         JSONObject relationShipListsObj = (JSONObject) jsonObject.get("relationship-list");\r
760                         if (relationShipListsObj != null)\r
761                         {\r
762                                 JSONArray rShipArray = (JSONArray) relationShipListsObj.get("relationship");\r
763                                 if (rShipArray != null)\r
764                                 {\r
765                                         Iterator i1 = rShipArray.iterator();\r
766 \r
767                                         while (i1.hasNext()) {\r
768 \r
769                                                 JSONObject inner1Obj = (JSONObject) i1.next();\r
770 \r
771                                                 if (inner1Obj == null)\r
772                                                         continue;\r
773 \r
774                                                 String relatedTo = checkForNull((String)inner1Obj.get("related-to"));\r
775                                                 if (relatedTo.equalsIgnoreCase("tenant"))\r
776                                                 {\r
777                                                         JSONObject tenantNewObj = new JSONObject();\r
778 \r
779                                                         String relatedLink = checkForNull((String) inner1Obj.get("related-link"));\r
780                                                         tenantNewObj.put("link", relatedLink);\r
781 \r
782                                                         JSONArray rDataArray = (JSONArray) inner1Obj.get("relationship-data");\r
783                                                         if (rDataArray != null)\r
784                                                         {\r
785                                                                 Iterator i2 = rDataArray.iterator();\r
786 \r
787                                                                 while (i2.hasNext()) {\r
788                                                                         JSONObject inner2Obj = (JSONObject) i2.next();\r
789 \r
790                                                                         if (inner2Obj == null)\r
791                                                                                 continue;\r
792 \r
793                                                                         String rShipKey = checkForNull((String)inner2Obj.get("relationship-key"));\r
794                                                                         String rShipVal = checkForNull((String)inner2Obj.get("relationship-value"));\r
795                                                                         if (rShipKey.equalsIgnoreCase("cloud-region.cloud-owner"))\r
796                                                                         {\r
797                                                                                 tenantNewObj.put("cloudOwner", rShipVal);\r
798                                                                         }\r
799                                                                         else if (rShipKey.equalsIgnoreCase("cloud-region.cloud-region-id"))\r
800                                                                         {\r
801                                                                                 tenantNewObj.put("cloudRegionID", rShipVal);\r
802                                                                         }\r
803 \r
804                                                                         if (rShipKey.equalsIgnoreCase("tenant.tenant-id"))\r
805                                                                         {\r
806                                                                                 tenantNewObj.put("tenantID", rShipVal);\r
807                                                                         }\r
808                                                                 }\r
809                                                         }\r
810 \r
811                                                         JSONArray relatedTPropArray = (JSONArray) inner1Obj.get("related-to-property");\r
812                                                         if (relatedTPropArray != null)\r
813                                                         {\r
814                                                                 Iterator i3 = relatedTPropArray.iterator();\r
815 \r
816                                                                 while (i3.hasNext()) {\r
817                                                                         JSONObject inner3Obj = (JSONObject) i3.next();\r
818 \r
819                                                                         if (inner3Obj == null)\r
820                                                                                 continue;\r
821 \r
822                                                                         String propKey = checkForNull((String)inner3Obj.get("property-key"));\r
823                                                                         String propVal = checkForNull((String)inner3Obj.get("property-value"));\r
824                                                                         if (propKey.equalsIgnoreCase("tenant.tenant-name"))\r
825                                                                         {\r
826                                                                                 tenantNewObj.put("tenantName", propVal);\r
827                                                                         }\r
828                                                                 }\r
829                                                         }\r
830                                                         bconvert = true;\r
831                                                         tenantArray.add(tenantNewObj);\r
832                                                 }\r
833                                         }\r
834 \r
835                                 }\r
836                         }\r
837                 } catch (NullPointerException ex) {\r
838 \r
839 \r
840                 }\r
841 \r
842                 if (bconvert)\r
843                         return tenantArray.toJSONString();\r
844                 else\r
845                         return "";\r
846 \r
847         }\r
848 \r
849         /**\r
850          * Check for null.\r
851          *\r
852          * @param local the local\r
853          * @return the string\r
854          */\r
855         private static String checkForNull(String local)\r
856         {\r
857                 if (local != null)\r
858                         return local;\r
859                 else\r
860                         return "";\r
861 \r
862         }\r
863 }\r
864 \r