[VID-3] Setting docker image tag
[vid.git] / vid / src / main / java / org / openecomp / vid / controller / AaiController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.vid.controller;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.text.DateFormat;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.Map;
31 import java.util.UUID;
32
33 import javax.servlet.ServletContext;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.ws.rs.BadRequestException;
36 import javax.ws.rs.DefaultValue;
37 import javax.ws.rs.QueryParam;
38 import javax.ws.rs.WebApplicationException;
39
40 import org.json.simple.JSONArray;
41 import org.json.simple.JSONObject;
42 import org.json.simple.parser.JSONParser;
43 import org.openecomp.aai.util.AAIRestInterface;
44 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
45 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.http.MediaType;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.web.bind.annotation.PathVariable;
51 import org.springframework.web.bind.annotation.RequestMapping;
52 import org.springframework.web.bind.annotation.RequestMethod;
53 import org.springframework.web.bind.annotation.RestController;
54 import org.springframework.web.servlet.ModelAndView;
55
56 /**
57  * Controller to handle a&ai requests.
58  */
59
60 @RestController
61 public class AaiController extends RestrictedBaseController{
62         
63         /** The view name. */
64         String viewName;
65         
66         /** The logger. */
67         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AaiController.class);
68         
69         /** The Constant dateFormat. */
70         final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
71
72         /** The from app id. */
73         protected String fromAppId = "VidAaiController";
74
75         /** The model. */
76         private Map<String,Object> model = new HashMap<String,Object>();
77
78         /** The servlet context. */
79         private @Autowired ServletContext servletContext;
80         
81         /**
82          * Welcome method.
83          *
84          * @param request the request
85          * @return ModelAndView The view
86          */
87         @RequestMapping(value = {"/subscriberSearch" }, method = RequestMethod.GET)
88         public ModelAndView welcome(HttpServletRequest request) {
89                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== AaiController welcome start");
90                 return new ModelAndView(getViewName());         
91         }
92
93         /* (non-Javadoc)
94          * @see org.openecomp.portalsdk.core.controller.RestrictedBaseController#getViewName()
95          */
96         public String getViewName() {
97                 return viewName;
98         }
99         
100         /* (non-Javadoc)
101          * @see org.openecomp.portalsdk.core.controller.RestrictedBaseController#setViewName(java.lang.String)
102          */
103         public void setViewName(String viewName) {
104                 this.viewName = viewName;
105         }
106
107         /**
108          * Get services from a&ai.
109          *
110          * @return ResponseEntity<String> The response entity
111          * @throws IOException Signals that an I/O exception has occurred.
112          * @throws InterruptedException the interrupted exception
113          */
114 @RequestMapping(value="/aai_get_services",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
115         public ResponseEntity<String> doGetServices() throws IOException, InterruptedException {        
116                 File certiPath = GetCertificatesPath();
117                 
118                 String resp = doAaiGet(certiPath.getAbsolutePath(), "service-design-and-creation/services?depth=0", false);;
119                 
120                 return new ResponseEntity<String>(resp, HttpStatus.OK);
121         }
122         
123         
124
125         /**
126          * Get services from a&ai.
127          *
128          * @param globalCustomerId the global customer id
129          * @param serviceSubscriptionId the service subscription id
130          * @return ResponseEntity The response entity
131          * @throws IOException Signals that an I/O exception has occurred.
132          * @throws InterruptedException the interrupted exception
133          */
134         @RequestMapping(value="/aai_get_service_subscription/{global-customer-id}/{service-subscription-id}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) 
135         public ResponseEntity<String> doGetServices(@PathVariable("global-customer-id") String globalCustomerId,
136                         @PathVariable("service-subscription-id") String serviceSubscriptionId) throws IOException, InterruptedException {       
137                 File certiPath = GetCertificatesPath();
138                 String resp = doAaiGet(certiPath.getAbsolutePath(), "/business/customers/customer/" + globalCustomerId 
139                                 + "/service-subscriptions/service-subscription/" + serviceSubscriptionId + "?depth=0", false);
140                 return new ResponseEntity<String>(resp, HttpStatus.OK);
141         }
142
143         /**
144          * Obtain the subscriber list from a&ai.
145          *
146          * @param fullSet the full set
147          * @return ResponseEntity The response entity
148          * @throws IOException Signals that an I/O exception has occurred.
149          * @throws InterruptedException the interrupted exception
150          */
151         @RequestMapping(value="/aai_get_subscribers",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)   
152         public ResponseEntity<String> doGetSubscriberList(@DefaultValue("n") @QueryParam("fullSet") String fullSet) throws IOException, InterruptedException {
153
154                 String res1 = getSubscribers(false);
155
156                 return new ResponseEntity<String>(res1, HttpStatus.OK);
157
158         }
159
160         /**
161          * Obtain the full subscriber list from a&ai.
162          *
163          * @return ResponseEntity The response entity
164          * @throws IOException Signals that an I/O exception has occurred.
165          * @throws InterruptedException the interrupted exception
166          */
167         @RequestMapping(value="/aai_get_full_subscribers",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)      
168         public ResponseEntity<String> getFullSubscriberList() throws IOException, InterruptedException {
169
170                 String res1 = getSubscribers(true);
171
172                 return new ResponseEntity<String>(res1, HttpStatus.OK);
173
174         }
175         
176         /**
177          * Refresh the subscriber list from a&ai.
178          *
179          * @return ResponseEntity The response entity
180          * @throws IOException Signals that an I/O exception has occurred.
181          */
182         @RequestMapping(value="/aai_refresh_subscribers",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)       
183         public ResponseEntity<String> doRefreshSubscriberList() throws IOException {
184                 
185                 String res1 = getSubscribers(false);
186                 
187                 // refresh the services too
188                 String resp  = getServices();
189                 
190                 return new ResponseEntity<String>(res1, HttpStatus.OK);
191         }
192         
193         /**
194          * Refresh the full subscriber list from a&ai.
195          *
196          * @return ResponseEntity The response entity
197          * @throws IOException Signals that an I/O exception has occurred.
198          */
199         @RequestMapping(value="/aai_refresh_full_subscribers",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)          
200         public ResponseEntity<String> doRefreshFullSubscriberList() throws IOException {
201                 boolean isFullSet = true;
202         
203                 String res1 = getSubscribers(isFullSet);
204
205                 // refresh the services too
206                 String resp  = getServices();
207         
208                 return new ResponseEntity<String>(res1, HttpStatus.OK);
209         }
210
211         /**
212          * Get subscriber details from a&ai.
213          *
214          * @param subscriberId the subscriber id
215          * @return ResponseEntity The response entity
216          */
217         @RequestMapping(value="/aai_sub_details/{subscriberId}", method = RequestMethod.GET)
218         public ResponseEntity<String> GetSubscriber(@PathVariable("subscriberId") String subscriberId) {
219                 
220                 String  res1 = getSubscriberDetails(subscriberId);
221                 return new ResponseEntity<String>(res1, HttpStatus.OK);
222
223         }
224
225         /**
226          * Issue a named query to a&ai.
227          *
228          * @param namedQueryId the named query id
229          * @param globalCustomerId the global customer id
230          * @param serviceType the service type
231          * @param serviceInstance the service instance
232          * @return ResponseEntity The response entity
233          */
234         @RequestMapping(value="/aai_sub_viewedit/{namedQueryId}/{globalCustomerId}/{serviceType}/{serviceInstance}", method = RequestMethod.GET)
235         public ResponseEntity<String> viewEditGetComponentList(
236                         @PathVariable("namedQueryId") String namedQueryId,
237                         @PathVariable("globalCustomerId") String globalCustomerId,
238                         @PathVariable("serviceType") String serviceType,
239                         @PathVariable("serviceInstance") String serviceInstance) { 
240
241                 String componentListPayload = getComponentListPutPayload(namedQueryId, globalCustomerId, serviceType, serviceInstance);
242                 File certiPath = GetCertificatesPath();
243
244                 String getComponentListOutput = doAaiPost(certiPath.getAbsolutePath(), "search/named-query", componentListPayload, false); 
245                 System.out.println(getComponentListOutput);
246                 return new ResponseEntity<String>(getComponentListOutput, HttpStatus.OK);
247
248         }
249
250         /**
251          * Parses the for tenants.
252          *
253          * @param resp the resp
254          * @return the string
255          */
256         private String parseForTenants(String resp)
257         {
258                 String tenantList = "";
259
260                 try
261                 {
262                         JSONParser jsonParser = new JSONParser();
263
264                         JSONObject jsonObject = (JSONObject) jsonParser.parse(resp);
265
266                         return  parseCustomerObjectForTenants(jsonObject);
267                 }
268                 catch (Exception ex) {
269
270                 }
271
272                 return tenantList;
273         }
274         
275         /**
276          * Parses the for tenants by service subscription.
277          *
278          * @param resp the resp
279          * @return the string
280          */
281         private String parseForTenantsByServiceSubscription(String resp)
282         {
283                 String tenantList = "";
284
285                 try
286                 {
287                         JSONParser jsonParser = new JSONParser();
288
289                         JSONObject jsonObject = (JSONObject) jsonParser.parse(resp);
290
291                         return  parseServiceSubscriptionObjectForTenants(jsonObject);
292                 }
293                 catch (Exception ex) {
294
295                 }
296
297                 return tenantList;
298         }
299         
300
301 //      @RequestMapping(value="/aai_get_tenants/{global-customer-id}", method = RequestMethod.GET)
302 //      public ResponseEntity<String> viewEditGetComponentList(
303 //                      @PathVariable("global-customer-id") String globalCustomerId) {
304 //              return new ResponseEntity<String>(getTenants(globalCustomerId), HttpStatus.OK);
305 //      }
306         
307         /**
308  * Obtain tenants for a given service type.
309  *
310  * @param globalCustomerId the global customer id
311  * @param serviceType the service type
312  * @return ResponseEntity The response entity
313  */
314         @RequestMapping(value="/aai_get_tenants/{global-customer-id}/{service-type}", method = RequestMethod.GET)
315         public ResponseEntity<String> viewEditGetTenantsFromServiceType(
316                         @PathVariable("global-customer-id") String globalCustomerId, @PathVariable("service-type") String serviceType) {
317                 
318                 return new ResponseEntity<String>(getTenantsFromServiceType(globalCustomerId, serviceType), HttpStatus.OK);
319         }
320
321         
322
323         /**
324          * Gets the tenants.
325          *
326          * @param globalCustomerId the global customer id
327          * @return the tenants
328          */
329         private String getTenants(String globalCustomerId)
330         {
331                 File certiPath = GetCertificatesPath();
332                 String resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers/customer/" + globalCustomerId, false);
333                 resp = parseForTenants(resp);
334                 //model.put("tenants", resp);
335                 return resp;
336         }
337         
338         /**
339          * Gets the tenants from service type.
340          *
341          * @param globalCustomerId the global customer id
342          * @param serviceType the service type
343          * @return the tenants from service type
344          */
345         private String getTenantsFromServiceType(String globalCustomerId, String serviceType)
346         {
347                 File certiPath = GetCertificatesPath();
348                 String url = "business/customers/customer/" + globalCustomerId + "/service-subscriptions/service-subscription/" + serviceType;
349                 System.out.println("URL: " + url);
350                 String resp = doAaiGet(certiPath.getAbsolutePath(), url, false);
351                 System.out.println("URL: " + url + "RES: " + resp);
352                 resp = parseForTenantsByServiceSubscription(resp);
353                 //model.put("tenants", resp);
354                 return resp;
355         }
356         
357         /**
358          * Gets the services.
359          *
360          * @return the services
361          */
362         private String getServices()
363         {
364                 File certiPath = GetCertificatesPath();
365                 String resp  = doAaiGet(certiPath.getAbsolutePath(), "/service-design-and-creation/services?depth=0", false);
366                 //model.put("aai_get_services", resp);
367                 return resp;
368         }
369
370
371         /**
372          * Gets the subscribers.
373          *
374          * @param isFullSet the is full set
375          * @return the subscribers
376          */
377         private String getSubscribers(boolean isFullSet)
378         {
379                 File certiPath = GetCertificatesPath();
380                 String depth = "0";
381                 if (isFullSet == true) { 
382                         depth = "all";
383                 }
384                 String resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers?subscriber-type=INFRA&depth=" + depth, false);
385                 //model.put("subscribernames" + depth, resp);
386                 return resp;
387         }
388
389         /**
390          * Gets the subscriber details.
391          *
392          * @param subscriberId the subscriber id
393          * @return the subscriber details
394          */
395         private String getSubscriberDetails(String subscriberId)
396         {
397                 File certiPath = GetCertificatesPath();
398                 String resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers/customer/" + subscriberId, false);
399                 return resp;
400         }
401
402         /**
403          * Gets the certificates path.
404          *
405          * @return the file
406          */
407         private File GetCertificatesPath()
408         {
409                 if (servletContext != null)
410                         return new File( servletContext.getRealPath("/WEB-INF/cert/") );
411                 return null;
412         }
413         
414         /**
415          * Send a GET request to a&ai.
416          *
417          * @param certiPath the certi path
418          * @param uri the uri
419          * @param xml the xml
420          * @return String The response
421          */
422         protected String doAaiGet(String certiPath, String uri, boolean xml) {
423                 String methodName = "getSubscriberList";                
424                 String transId = UUID.randomUUID().toString();
425                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
426
427                 try {
428
429                         AAIRestInterface restContrller = new AAIRestInterface(certiPath);
430                         return restContrller.RestGet(fromAppId, transId, uri, xml);
431
432                 } catch (WebApplicationException e) {
433                         final String message = ((BadRequestException) e).getResponse().readEntity(String.class);
434                         logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + message);
435                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + message);
436                 } catch (Exception e) {
437                         logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());
438                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());
439                 }
440
441                 return null;
442         }
443         
444         /**
445          * Send a POST request to a&ai.
446          *
447          * @param certiPath the certi path
448          * @param uri the uri
449          * @param payload the payload
450          * @param xml the xml
451          * @return String The response
452          */
453         protected String doAaiPost(String certiPath, String uri, String payload, boolean xml) {
454                 String methodName = "getSubscriberList";                
455                 String transId = UUID.randomUUID().toString();
456                 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
457
458                 try {
459
460                         AAIRestInterface restContrller = new AAIRestInterface(certiPath);
461                         return restContrller.RestPost(fromAppId, transId, uri, payload, xml);
462
463                 } catch (Exception e) {
464                         logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());
465                         logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) +  "<== " + "." + methodName + e.toString());
466                 }
467
468                 return null;
469         }
470
471         /**
472          * Gets the component list put payload.
473          *
474          * @param namedQueryId the named query id
475          * @param globalCustomerId the global customer id
476          * @param serviceType the service type
477          * @param serviceInstance the service instance
478          * @return the component list put payload
479          */
480         private String getComponentListPutPayload(String namedQueryId, String globalCustomerId, String serviceType, String serviceInstance) {
481                 return 
482                                 "               {" +
483                                 "    \"instance-filters\": {" +
484                                 "        \"instance-filter\": [" +
485                                 "            {" +
486 //                              "                \"customer\": {" +
487 //                              "                    \"global-customer-id\": \"" + globalCustomerId + "\"" +
488 //                              "                }," +
489                                 "                \"service-instance\": {" +
490                                 "                    \"service-instance-id\": \"" + serviceInstance + "\"" +
491                                 "                }" + 
492 //                              "                }," +
493 //                              "                \"service-subscription\": {" +
494 //                              "                    \"service-type\": \"" + serviceType + "\"" +
495 //                              "                }" +
496                                 "            }" +
497                                 "        ]" +
498                                 "    }," +
499                                 "    \"query-parameters\": {" +
500                                 "        \"named-query\": {" +
501                                 "            \"named-query-uuid\": \"" + namedQueryId + "\"" +
502                                 "        }" +
503                                 "    }" +
504                                 "}";
505
506         }
507
508
509         /**
510          * Return tenant details.
511          *
512          * @param jsonObject the json object
513          * @return String The parsing results
514          */
515         public static String parseCustomerObjectForTenants(JSONObject jsonObject) {
516
517                 JSONArray tenantArray = new JSONArray();
518                 boolean bconvert = false;
519
520                 try {
521
522                         JSONObject serviceSubsObj = (JSONObject) jsonObject.get("service-subscriptions");
523
524                         if (serviceSubsObj != null)
525                         {
526                                 JSONArray srvcSubArray = (JSONArray) serviceSubsObj.get("service-subscription");
527
528                                 if (srvcSubArray != null)
529                                 {
530                                         Iterator i = srvcSubArray.iterator();
531
532                                         while (i.hasNext()) {
533
534                                                 JSONObject innerObj = (JSONObject) i.next();
535
536                                                 if (innerObj == null)
537                                                         continue;
538
539                                                 JSONObject relationShipListsObj = (JSONObject) innerObj.get("relationship-list");
540                                                 if (relationShipListsObj != null)
541                                                 {
542                                                         JSONArray rShipArray = (JSONArray) relationShipListsObj.get("relationship");
543                                                         if (rShipArray != null)
544                                                         {
545                                                                 Iterator i1 = rShipArray.iterator();
546
547                                                                 while (i1.hasNext()) {
548
549                                                                         JSONObject inner1Obj = (JSONObject) i1.next();
550
551                                                                         if (inner1Obj == null)
552                                                                                 continue;
553
554                                                                         String relatedTo = checkForNull((String)inner1Obj.get("related-to"));
555                                                                         if (relatedTo.equalsIgnoreCase("tenant"))
556                                                                         {
557                                                                                 JSONObject tenantNewObj = new JSONObject();
558
559                                                                                 String relatedLink = checkForNull((String) inner1Obj.get("related-link"));
560                                                                                 tenantNewObj.put("link", relatedLink);
561
562                                                                                 JSONArray rDataArray = (JSONArray) inner1Obj.get("relationship-data");
563                                                                                 if (rDataArray != null)
564                                                                                 {
565                                                                                         Iterator i2 = rDataArray.iterator();
566
567                                                                                         while (i2.hasNext()) {
568                                                                                                 JSONObject inner2Obj = (JSONObject) i2.next();
569
570                                                                                                 if (inner2Obj == null)
571                                                                                                         continue;
572
573                                                                                                 String rShipKey = checkForNull((String)inner2Obj.get("relationship-key"));
574                                                                                                 String rShipVal = checkForNull((String)inner2Obj.get("relationship-value"));
575                                                                                                 if (rShipKey.equalsIgnoreCase("cloud-region.cloud-owner"))
576                                                                                                 {
577                                                                                                         tenantNewObj.put("cloudOwner", rShipVal);
578                                                                                                 }
579                                                                                                 else if (rShipKey.equalsIgnoreCase("cloud-region.cloud-region-id"))
580                                                                                                 {
581                                                                                                         tenantNewObj.put("cloudRegionID", rShipVal);
582                                                                                                 }
583
584                                                                                                 if (rShipKey.equalsIgnoreCase("tenant.tenant-id"))
585                                                                                                 {
586                                                                                                         tenantNewObj.put("tenantID", rShipVal);
587                                                                                                 }
588                                                                                         }
589                                                                                 }
590
591                                                                                 JSONArray relatedTPropArray = (JSONArray) inner1Obj.get("related-to-property");
592                                                                                 if (relatedTPropArray != null)
593                                                                                 {
594                                                                                         Iterator i3 = relatedTPropArray.iterator();
595
596                                                                                         while (i3.hasNext()) {
597                                                                                                 JSONObject inner3Obj = (JSONObject) i3.next();
598
599                                                                                                 if (inner3Obj == null)
600                                                                                                         continue;
601
602                                                                                                 String propKey = checkForNull((String)inner3Obj.get("property-key"));
603                                                                                                 String propVal = checkForNull((String)inner3Obj.get("property-value"));
604                                                                                                 if (propKey.equalsIgnoreCase("tenant.tenant-name"))
605                                                                                                 {
606                                                                                                         tenantNewObj.put("tenantName", propVal);
607                                                                                                 }
608                                                                                         }
609                                                                                 }
610                                                                                 bconvert = true;
611                                                                                 tenantArray.add(tenantNewObj);
612                                                                         }
613                                                                 }
614                                                         }
615                                                 }
616                                         }
617                                 }
618                         }
619                 } catch (NullPointerException ex) {
620
621
622                 }
623
624                 if (bconvert)
625                         return tenantArray.toJSONString();
626                 else
627                         return "";
628
629         }
630
631         
632         /**
633          * Retrieve the service subscription from the jsonObject.
634          *
635          * @param jsonObject the json object
636          * @return String
637          */
638         public static String parseServiceSubscriptionObjectForTenants(JSONObject jsonObject) {
639
640                 JSONArray tenantArray = new JSONArray();
641                 boolean bconvert = false;
642
643                 try {
644                         JSONObject relationShipListsObj = (JSONObject) jsonObject.get("relationship-list");
645                         if (relationShipListsObj != null)
646                         {
647                                 JSONArray rShipArray = (JSONArray) relationShipListsObj.get("relationship");
648                                 if (rShipArray != null)
649                                 {
650                                         Iterator i1 = rShipArray.iterator();
651
652                                         while (i1.hasNext()) {
653
654                                                 JSONObject inner1Obj = (JSONObject) i1.next();
655
656                                                 if (inner1Obj == null)
657                                                         continue;
658
659                                                 String relatedTo = checkForNull((String)inner1Obj.get("related-to"));
660                                                 if (relatedTo.equalsIgnoreCase("tenant"))
661                                                 {
662                                                         JSONObject tenantNewObj = new JSONObject();
663
664                                                         String relatedLink = checkForNull((String) inner1Obj.get("related-link"));
665                                                         tenantNewObj.put("link", relatedLink);
666
667                                                         JSONArray rDataArray = (JSONArray) inner1Obj.get("relationship-data");
668                                                         if (rDataArray != null)
669                                                         {
670                                                                 Iterator i2 = rDataArray.iterator();
671
672                                                                 while (i2.hasNext()) {
673                                                                         JSONObject inner2Obj = (JSONObject) i2.next();
674
675                                                                         if (inner2Obj == null)
676                                                                                 continue;
677
678                                                                         String rShipKey = checkForNull((String)inner2Obj.get("relationship-key"));
679                                                                         String rShipVal = checkForNull((String)inner2Obj.get("relationship-value"));
680                                                                         if (rShipKey.equalsIgnoreCase("cloud-region.cloud-owner"))
681                                                                         {
682                                                                                 tenantNewObj.put("cloudOwner", rShipVal);
683                                                                         }
684                                                                         else if (rShipKey.equalsIgnoreCase("cloud-region.cloud-region-id"))
685                                                                         {
686                                                                                 tenantNewObj.put("cloudRegionID", rShipVal);
687                                                                         }
688
689                                                                         if (rShipKey.equalsIgnoreCase("tenant.tenant-id"))
690                                                                         {
691                                                                                 tenantNewObj.put("tenantID", rShipVal);
692                                                                         }
693                                                                 }
694                                                         }
695
696                                                         JSONArray relatedTPropArray = (JSONArray) inner1Obj.get("related-to-property");
697                                                         if (relatedTPropArray != null)
698                                                         {
699                                                                 Iterator i3 = relatedTPropArray.iterator();
700
701                                                                 while (i3.hasNext()) {
702                                                                         JSONObject inner3Obj = (JSONObject) i3.next();
703
704                                                                         if (inner3Obj == null)
705                                                                                 continue;
706
707                                                                         String propKey = checkForNull((String)inner3Obj.get("property-key"));
708                                                                         String propVal = checkForNull((String)inner3Obj.get("property-value"));
709                                                                         if (propKey.equalsIgnoreCase("tenant.tenant-name"))
710                                                                         {
711                                                                                 tenantNewObj.put("tenantName", propVal);
712                                                                         }
713                                                                 }
714                                                         }
715                                                         bconvert = true;
716                                                         tenantArray.add(tenantNewObj);
717                                                 }
718                                         }
719
720                                 }
721                         }
722                 } catch (NullPointerException ex) {
723
724
725                 }
726
727                 if (bconvert)
728                         return tenantArray.toJSONString();
729                 else
730                         return "";
731
732         }
733
734         /**
735          * Check for null.
736          *
737          * @param local the local
738          * @return the string
739          */
740         private static String checkForNull(String local)
741         {
742                 if (local != null)
743                         return local;
744                 else
745                         return "";
746
747         }
748 }