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