[VID-12] Delivery of remaining features for v1.1
[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          * @param serviceInstance the service instance\r
306          * @return ResponseEntity The response entity\r
307          */\r
308         @RequestMapping(value="/aai_get_models_by_service_type/{namedQueryId}/{globalCustomerId}/{serviceType}", method = RequestMethod.GET)\r
309         public ResponseEntity<String> viewEditGetComponentList(\r
310                         @PathVariable("namedQueryId") String namedQueryId,\r
311                         @PathVariable("globalCustomerId") String globalCustomerId,\r
312                         @PathVariable("serviceType") String serviceType) { \r
313 \r
314                 String componentListPayload = getModelsByServiceTypePayload(namedQueryId, globalCustomerId, serviceType);\r
315                 File certiPath = GetCertificatesPath();\r
316 \r
317                 Response resp = doAaiPost(certiPath.getAbsolutePath(), "search/named-query", componentListPayload, false); \r
318         return convertResponseToResponseEntity(resp);\r
319         }\r
320         \r
321         /**\r
322          * Parses the for tenants.\r
323          *\r
324          * @param resp the resp\r
325          * @return the string\r
326          */\r
327         private String parseForTenants(String resp)\r
328         {\r
329                 String tenantList = "";\r
330 \r
331                 try\r
332                 {\r
333                         JSONParser jsonParser = new JSONParser();\r
334 \r
335                         JSONObject jsonObject = (JSONObject) jsonParser.parse(resp);\r
336 \r
337                         return  parseCustomerObjectForTenants(jsonObject);\r
338                 }\r
339                 catch (Exception ex) {\r
340 \r
341                 }\r
342 \r
343                 return tenantList;\r
344         }\r
345 \r
346         /**\r
347          * Parses the for tenants by service subscription.\r
348          *\r
349          * @param resp the resp\r
350          * @return the string\r
351          */\r
352         private String parseForTenantsByServiceSubscription(String resp)\r
353         {\r
354                 String tenantList = "";\r
355 \r
356                 try\r
357                 {\r
358                         JSONParser jsonParser = new JSONParser();\r
359 \r
360                         JSONObject jsonObject = (JSONObject) jsonParser.parse(resp);\r
361 \r
362                         return  parseServiceSubscriptionObjectForTenants(jsonObject);\r
363                 }\r
364                 catch (Exception ex) {\r
365 \r
366                 }\r
367 \r
368                 return tenantList;\r
369         }\r
370 \r
371 \r
372         //      @RequestMapping(value="/aai_get_tenants/{global-customer-id}", method = RequestMethod.GET)\r
373         //      public ResponseEntity<String> viewEditGetComponentList(\r
374         //                      @PathVariable("global-customer-id") String globalCustomerId) {\r
375         //              return new ResponseEntity<String>(getTenants(globalCustomerId), HttpStatus.OK);\r
376         //      }\r
377 \r
378         /**\r
379          * Obtain tenants for a given service type.\r
380          *\r
381          * @param globalCustomerId the global customer id\r
382          * @param serviceType the service type\r
383          * @return ResponseEntity The response entity\r
384          */\r
385         @RequestMapping(value="/aai_get_tenants/{global-customer-id}/{service-type}", method = RequestMethod.GET)\r
386         public ResponseEntity<String> viewEditGetTenantsFromServiceType(\r
387                         @PathVariable("global-customer-id") String globalCustomerId, @PathVariable("service-type") String serviceType) {\r
388         return getTenantsFromServiceType(globalCustomerId, serviceType);\r
389         }\r
390 \r
391         private ResponseEntity<String> convertResponseToResponseEntity(Response resp) { \r
392                 ResponseEntity<String> respEnt;\r
393                 if (resp == null) { \r
394                         respEnt = new ResponseEntity<String>("Failed to fetch data from A&AI, check server logs for details.", HttpStatus.INTERNAL_SERVER_ERROR);\r
395                 } else { \r
396                         respEnt = new ResponseEntity<String>((String)resp.readEntity(String.class), HttpStatus.valueOf(resp.getStatus()));\r
397                 }\r
398                 return respEnt;\r
399         }\r
400 \r
401         /**\r
402          * Gets the tenants.\r
403          *\r
404          * @param globalCustomerId the global customer id\r
405          * @return the tenants\r
406          */\r
407         private ResponseEntity<String> getTenants(String globalCustomerId)\r
408         {\r
409                 File certiPath = GetCertificatesPath();\r
410         Response resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers/customer/" + globalCustomerId, false);\r
411         \r
412         ResponseEntity<String> respEnt;\r
413         if (resp.getStatus() >= 200 && resp.getStatus() <= 299) { \r
414             respEnt = new ResponseEntity<String>(parseForTenants((String)resp.readEntity(String.class)), HttpStatus.OK);\r
415         } else {\r
416             respEnt = new ResponseEntity<String>((String)resp.readEntity(String.class), HttpStatus.valueOf(resp.getStatus()));\r
417         }\r
418         return respEnt;\r
419 \r
420         }\r
421 \r
422         /**\r
423          * Gets the tenants from service type.\r
424          *\r
425          * @param globalCustomerId the global customer id\r
426          * @param serviceType the service type\r
427          * @return the tenants from service type\r
428          */\r
429         private ResponseEntity<String> getTenantsFromServiceType(String globalCustomerId, String serviceType)\r
430         {\r
431                 File certiPath = GetCertificatesPath();\r
432         String url = "business/customers/customer/" + globalCustomerId + "/service-subscriptions/service-subscription/" + serviceType;\r
433 \r
434         Response resp = doAaiGet(certiPath.getAbsolutePath(), url, false);\r
435         \r
436         ResponseEntity<String> respEnt;\r
437         if (resp.getStatus() >= 200 && resp.getStatus() <= 299) { \r
438             respEnt = new ResponseEntity<String>(parseForTenantsByServiceSubscription((String)resp.readEntity(String.class)), HttpStatus.OK);\r
439         } else {\r
440             respEnt = new ResponseEntity<String>((String)resp.readEntity(String.class), HttpStatus.valueOf(resp.getStatus()));\r
441         }\r
442         return respEnt;\r
443 \r
444         }\r
445 \r
446         /**\r
447          * Gets the services.\r
448          *\r
449          * @return the services\r
450          */\r
451         private Response getServices()\r
452         {\r
453                 File certiPath = GetCertificatesPath();\r
454         Response resp  = doAaiGet(certiPath.getAbsolutePath(), "service-design-and-creation/services", false);\r
455         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "getServices() resp=" + resp.getStatusInfo());\r
456 \r
457                 //model.put("aai_get_services", resp);\r
458                 return resp;\r
459         }\r
460 \r
461 \r
462         /**\r
463          * Gets the subscribers.\r
464          *\r
465          * @param isFullSet the is full set\r
466          * @return the subscribers\r
467          */\r
468         private Response getSubscribers(boolean isFullSet)\r
469         {\r
470                 File certiPath = GetCertificatesPath();\r
471                 String depth = "0";\r
472 \r
473         Response resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers?subscriber-type=INFRA&depth=" + depth, false);\r
474         if (resp != null) { \r
475                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "getSubscribers() resp=" + resp.getStatusInfo().toString());\r
476         }\r
477                 return resp;\r
478         }\r
479 \r
480         \r
481         \r
482         /**\r
483          * Gets the subscriber details.\r
484          *\r
485          * @param subscriberId the subscriber id\r
486          * @return the subscriber details\r
487          */\r
488         private Response getSubscriberDetails(String subscriberId)\r
489         {\r
490                 File certiPath = GetCertificatesPath();\r
491                 Response resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers/customer/" + subscriberId + "?depth=2", false);\r
492                 //String resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers/customer/" + subscriberId, false);\r
493                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "getSubscriberDetails() resp=" + resp.getStatusInfo().toString());\r
494                 return resp;\r
495         }\r
496 \r
497         /**\r
498          * Gets the certificates path.\r
499          *\r
500          * @return the file\r
501          */\r
502         private File GetCertificatesPath()\r
503         {\r
504                 if (servletContext != null)\r
505                         return new File( servletContext.getRealPath("/WEB-INF/cert/") );\r
506                 return null;\r
507         }\r
508 \r
509         /**\r
510          * Send a GET request to a&ai.\r
511          *\r
512          * @param certiPath the certi path\r
513          * @param uri the uri\r
514          * @param xml the xml\r
515          * @return String The response\r
516          */\r
517         protected Response doAaiGet(String certiPath, String uri, boolean xml) {\r
518                 String methodName = "getSubscriberList";                \r
519                 String transId = UUID.randomUUID().toString();\r
520                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
521 \r
522                 Response resp = null;\r
523                 try {\r
524 \r
525                         AAIRestInterface restContrller = new AAIRestInterface(certiPath);\r
526                         resp = restContrller.RestGet(fromAppId, transId, uri, xml);\r
527 \r
528                 } catch (WebApplicationException e) {\r
529                         final String message = ((BadRequestException) e).getResponse().readEntity(String.class);\r
530                         logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + message);\r
531                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + message);\r
532                 } catch (Exception e) {\r
533                         logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());\r
534                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());\r
535                 }\r
536 \r
537                 return resp;\r
538         }\r
539 \r
540         /**\r
541          * Send a POST request to a&ai.\r
542          *\r
543          * @param certiPath the certi path\r
544          * @param uri the uri\r
545          * @param payload the payload\r
546          * @param xml the xml\r
547          * @return String The response\r
548          */\r
549         protected Response doAaiPost(String certiPath, String uri, String payload, boolean xml) {\r
550                 String methodName = "getSubscriberList";                \r
551                 String transId = UUID.randomUUID().toString();\r
552                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");\r
553 \r
554                 Response resp = null;\r
555                 try {\r
556 \r
557                         AAIRestInterface restContrller = new AAIRestInterface(certiPath);\r
558                         resp = restContrller.RestPost(fromAppId, transId, uri, payload, xml);\r
559 \r
560                 } catch (Exception e) {\r
561                         logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());\r
562                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());\r
563                 }\r
564 \r
565                 return resp;\r
566         }\r
567 \r
568         /**\r
569          * Gets the component list put payload.\r
570          *\r
571          * @param namedQueryId the named query id\r
572          * @param globalCustomerId the global customer id\r
573          * @param serviceType the service type\r
574          * @param serviceInstance the service instance\r
575          * @return the component list put payload\r
576          */\r
577         private String getComponentListPutPayload(String namedQueryId, String globalCustomerId, String serviceType, String serviceInstance) {\r
578                 return \r
579                                 "               {" +\r
580                                 "    \"instance-filters\": {" +\r
581                                 "        \"instance-filter\": [" +\r
582                                 "            {" +\r
583                                 "                \"customer\": {" +\r
584                                 "                    \"global-customer-id\": \"" + globalCustomerId + "\"" +\r
585                                 "                }," +\r
586                                 "                \"service-instance\": {" +\r
587                                 "                    \"service-instance-id\": \"" + serviceInstance + "\"" +\r
588                                 "                }," +\r
589                                 "                \"service-subscription\": {" +\r
590                                 "                    \"service-type\": \"" + serviceType + "\"" +\r
591                                 "                }" +\r
592                                 "            }" +\r
593                                 "        ]" +\r
594                                 "    }," +\r
595                                 "    \"query-parameters\": {" +\r
596                                 "        \"named-query\": {" +\r
597                                 "            \"named-query-uuid\": \"" + namedQueryId + "\"" +\r
598                                 "        }" +\r
599                                 "    }" +\r
600                                 "}";\r
601 \r
602         }\r
603         private String getModelsByServiceTypePayload(String namedQueryId, String globalCustomerId, String serviceType) {\r
604                 // TODO Auto-generated method stub\r
605                 return                  "               {" +\r
606                 "    \"instance-filters\": {" +\r
607                 "        \"instance-filter\": [" +\r
608                 "            {" +\r
609                 "                \"customer\": {" +\r
610                 "                    \"global-customer-id\": \"" + globalCustomerId + "\"" +\r
611                 "                }," +\r
612                 "                \"service-subscription\": {" +\r
613                 "                    \"service-type\": \"" + serviceType + "\"" +\r
614                 "                }" +\r
615                 "            }" +\r
616                 "        ]" +\r
617                 "    }," +\r
618                 "    \"query-parameters\": {" +\r
619                 "        \"named-query\": {" +\r
620                 "            \"named-query-uuid\": \"" + namedQueryId + "\"" +\r
621                 "        }" +\r
622                 "    }" +\r
623                 "}";\r
624         \r
625         }\r
626 \r
627         /**\r
628          * Return tenant details.\r
629          *\r
630          * @param jsonObject the json object\r
631          * @return String The parsing results\r
632          */\r
633         public static String parseCustomerObjectForTenants(JSONObject jsonObject) {\r
634 \r
635                 JSONArray tenantArray = new JSONArray();\r
636                 boolean bconvert = false;\r
637 \r
638                 try {\r
639 \r
640                         JSONObject serviceSubsObj = (JSONObject) jsonObject.get("service-subscriptions");\r
641 \r
642                         if (serviceSubsObj != null)\r
643                         {\r
644                                 JSONArray srvcSubArray = (JSONArray) serviceSubsObj.get("service-subscription");\r
645 \r
646                                 if (srvcSubArray != null)\r
647                                 {\r
648                                         Iterator i = srvcSubArray.iterator();\r
649 \r
650                                         while (i.hasNext()) {\r
651 \r
652                                                 JSONObject innerObj = (JSONObject) i.next();\r
653 \r
654                                                 if (innerObj == null)\r
655                                                         continue;\r
656 \r
657                                                 JSONObject relationShipListsObj = (JSONObject) innerObj.get("relationship-list");\r
658                                                 if (relationShipListsObj != null)\r
659                                                 {\r
660                                                         JSONArray rShipArray = (JSONArray) relationShipListsObj.get("relationship");\r
661                                                         if (rShipArray != null)\r
662                                                         {\r
663                                                                 Iterator i1 = rShipArray.iterator();\r
664 \r
665                                                                 while (i1.hasNext()) {\r
666 \r
667                                                                         JSONObject inner1Obj = (JSONObject) i1.next();\r
668 \r
669                                                                         if (inner1Obj == null)\r
670                                                                                 continue;\r
671 \r
672                                                                         String relatedTo = checkForNull((String)inner1Obj.get("related-to"));\r
673                                                                         if (relatedTo.equalsIgnoreCase("tenant"))\r
674                                                                         {\r
675                                                                                 JSONObject tenantNewObj = new JSONObject();\r
676 \r
677                                                                                 String relatedLink = checkForNull((String) inner1Obj.get("related-link"));\r
678                                                                                 tenantNewObj.put("link", relatedLink);\r
679 \r
680                                                                                 JSONArray rDataArray = (JSONArray) inner1Obj.get("relationship-data");\r
681                                                                                 if (rDataArray != null)\r
682                                                                                 {\r
683                                                                                         Iterator i2 = rDataArray.iterator();\r
684 \r
685                                                                                         while (i2.hasNext()) {\r
686                                                                                                 JSONObject inner2Obj = (JSONObject) i2.next();\r
687 \r
688                                                                                                 if (inner2Obj == null)\r
689                                                                                                         continue;\r
690 \r
691                                                                                                 String rShipKey = checkForNull((String)inner2Obj.get("relationship-key"));\r
692                                                                                                 String rShipVal = checkForNull((String)inner2Obj.get("relationship-value"));\r
693                                                                                                 if (rShipKey.equalsIgnoreCase("cloud-region.cloud-owner"))\r
694                                                                                                 {\r
695                                                                                                         tenantNewObj.put("cloudOwner", rShipVal);\r
696                                                                                                 }\r
697                                                                                                 else if (rShipKey.equalsIgnoreCase("cloud-region.cloud-region-id"))\r
698                                                                                                 {\r
699                                                                                                         tenantNewObj.put("cloudRegionID", rShipVal);\r
700                                                                                                 }\r
701 \r
702                                                                                                 if (rShipKey.equalsIgnoreCase("tenant.tenant-id"))\r
703                                                                                                 {\r
704                                                                                                         tenantNewObj.put("tenantID", rShipVal);\r
705                                                                                                 }\r
706                                                                                         }\r
707                                                                                 }\r
708 \r
709                                                                                 JSONArray relatedTPropArray = (JSONArray) inner1Obj.get("related-to-property");\r
710                                                                                 if (relatedTPropArray != null)\r
711                                                                                 {\r
712                                                                                         Iterator i3 = relatedTPropArray.iterator();\r
713 \r
714                                                                                         while (i3.hasNext()) {\r
715                                                                                                 JSONObject inner3Obj = (JSONObject) i3.next();\r
716 \r
717                                                                                                 if (inner3Obj == null)\r
718                                                                                                         continue;\r
719 \r
720                                                                                                 String propKey = checkForNull((String)inner3Obj.get("property-key"));\r
721                                                                                                 String propVal = checkForNull((String)inner3Obj.get("property-value"));\r
722                                                                                                 if (propKey.equalsIgnoreCase("tenant.tenant-name"))\r
723                                                                                                 {\r
724                                                                                                         tenantNewObj.put("tenantName", propVal);\r
725                                                                                                 }\r
726                                                                                         }\r
727                                                                                 }\r
728                                                                                 bconvert = true;\r
729                                                                                 tenantArray.add(tenantNewObj);\r
730                                                                         }\r
731                                                                 }\r
732                                                         }\r
733                                                 }\r
734                                         }\r
735                                 }\r
736                         }\r
737                 } catch (NullPointerException ex) {\r
738 \r
739 \r
740                 }\r
741 \r
742                 if (bconvert)\r
743                         return tenantArray.toJSONString();\r
744                 else\r
745                         return "";\r
746 \r
747         }\r
748 \r
749 \r
750         /**\r
751          * Retrieve the service subscription from the jsonObject.\r
752          *\r
753          * @param jsonObject the json object\r
754          * @return String\r
755          */\r
756         public static String parseServiceSubscriptionObjectForTenants(JSONObject jsonObject) {\r
757 \r
758                 JSONArray tenantArray = new JSONArray();\r
759                 boolean bconvert = false;\r
760 \r
761                 try {\r
762                         JSONObject relationShipListsObj = (JSONObject) jsonObject.get("relationship-list");\r
763                         if (relationShipListsObj != null)\r
764                         {\r
765                                 JSONArray rShipArray = (JSONArray) relationShipListsObj.get("relationship");\r
766                                 if (rShipArray != null)\r
767                                 {\r
768                                         Iterator i1 = rShipArray.iterator();\r
769 \r
770                                         while (i1.hasNext()) {\r
771 \r
772                                                 JSONObject inner1Obj = (JSONObject) i1.next();\r
773 \r
774                                                 if (inner1Obj == null)\r
775                                                         continue;\r
776 \r
777                                                 String relatedTo = checkForNull((String)inner1Obj.get("related-to"));\r
778                                                 if (relatedTo.equalsIgnoreCase("tenant"))\r
779                                                 {\r
780                                                         JSONObject tenantNewObj = new JSONObject();\r
781 \r
782                                                         String relatedLink = checkForNull((String) inner1Obj.get("related-link"));\r
783                                                         tenantNewObj.put("link", relatedLink);\r
784 \r
785                                                         JSONArray rDataArray = (JSONArray) inner1Obj.get("relationship-data");\r
786                                                         if (rDataArray != null)\r
787                                                         {\r
788                                                                 Iterator i2 = rDataArray.iterator();\r
789 \r
790                                                                 while (i2.hasNext()) {\r
791                                                                         JSONObject inner2Obj = (JSONObject) i2.next();\r
792 \r
793                                                                         if (inner2Obj == null)\r
794                                                                                 continue;\r
795 \r
796                                                                         String rShipKey = checkForNull((String)inner2Obj.get("relationship-key"));\r
797                                                                         String rShipVal = checkForNull((String)inner2Obj.get("relationship-value"));\r
798                                                                         if (rShipKey.equalsIgnoreCase("cloud-region.cloud-owner"))\r
799                                                                         {\r
800                                                                                 tenantNewObj.put("cloudOwner", rShipVal);\r
801                                                                         }\r
802                                                                         else if (rShipKey.equalsIgnoreCase("cloud-region.cloud-region-id"))\r
803                                                                         {\r
804                                                                                 tenantNewObj.put("cloudRegionID", rShipVal);\r
805                                                                         }\r
806 \r
807                                                                         if (rShipKey.equalsIgnoreCase("tenant.tenant-id"))\r
808                                                                         {\r
809                                                                                 tenantNewObj.put("tenantID", rShipVal);\r
810                                                                         }\r
811                                                                 }\r
812                                                         }\r
813 \r
814                                                         JSONArray relatedTPropArray = (JSONArray) inner1Obj.get("related-to-property");\r
815                                                         if (relatedTPropArray != null)\r
816                                                         {\r
817                                                                 Iterator i3 = relatedTPropArray.iterator();\r
818 \r
819                                                                 while (i3.hasNext()) {\r
820                                                                         JSONObject inner3Obj = (JSONObject) i3.next();\r
821 \r
822                                                                         if (inner3Obj == null)\r
823                                                                                 continue;\r
824 \r
825                                                                         String propKey = checkForNull((String)inner3Obj.get("property-key"));\r
826                                                                         String propVal = checkForNull((String)inner3Obj.get("property-value"));\r
827                                                                         if (propKey.equalsIgnoreCase("tenant.tenant-name"))\r
828                                                                         {\r
829                                                                                 tenantNewObj.put("tenantName", propVal);\r
830                                                                         }\r
831                                                                 }\r
832                                                         }\r
833                                                         bconvert = true;\r
834                                                         tenantArray.add(tenantNewObj);\r
835                                                 }\r
836                                         }\r
837 \r
838                                 }\r
839                         }\r
840                 } catch (NullPointerException ex) {\r
841 \r
842 \r
843                 }\r
844 \r
845                 if (bconvert)\r
846                         return tenantArray.toJSONString();\r
847                 else\r
848                         return "";\r
849 \r
850         }\r
851 \r
852         /**\r
853          * Check for null.\r
854          *\r
855          * @param local the local\r
856          * @return the string\r
857          */\r
858         private static String checkForNull(String local)\r
859         {\r
860                 if (local != null)\r
861                         return local;\r
862                 else\r
863                         return "";\r
864 \r
865         }\r
866 }\r
867 \r