2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.vid.controller;
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;
31 import java.util.UUID;
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;
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;
57 * Controller to handle a&ai requests.
61 public class AaiController extends RestrictedBaseController{
67 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AaiController.class);
69 /** The Constant dateFormat. */
70 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
72 /** The from app id. */
73 protected String fromAppId = "VidAaiController";
76 private Map<String,Object> model = new HashMap<String,Object>();
78 /** The servlet context. */
79 private @Autowired ServletContext servletContext;
84 * @param request the request
85 * @return ModelAndView The view
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());
94 * @see org.openecomp.portalsdk.core.controller.RestrictedBaseController#getViewName()
96 public String getViewName() {
101 * @see org.openecomp.portalsdk.core.controller.RestrictedBaseController#setViewName(java.lang.String)
103 public void setViewName(String viewName) {
104 this.viewName = viewName;
108 * Get services from a&ai.
110 * @return ResponseEntity<String> The response entity
111 * @throws IOException Signals that an I/O exception has occurred.
112 * @throws InterruptedException the interrupted exception
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();
118 String resp = doAaiGet(certiPath.getAbsolutePath(), "service-design-and-creation/services?depth=0", false);;
120 return new ResponseEntity<String>(resp, HttpStatus.OK);
126 * Get services from a&ai.
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
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);
144 * Obtain the subscriber list from a&ai.
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
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 {
154 String res1 = getSubscribers(false);
156 return new ResponseEntity<String>(res1, HttpStatus.OK);
161 * Obtain the full subscriber list from a&ai.
163 * @return ResponseEntity The response entity
164 * @throws IOException Signals that an I/O exception has occurred.
165 * @throws InterruptedException the interrupted exception
167 @RequestMapping(value="/aai_get_full_subscribers",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
168 public ResponseEntity<String> getFullSubscriberList() throws IOException, InterruptedException {
170 String res1 = getSubscribers(true);
172 return new ResponseEntity<String>(res1, HttpStatus.OK);
177 * Refresh the subscriber list from a&ai.
179 * @return ResponseEntity The response entity
180 * @throws IOException Signals that an I/O exception has occurred.
182 @RequestMapping(value="/aai_refresh_subscribers",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
183 public ResponseEntity<String> doRefreshSubscriberList() throws IOException {
185 String res1 = getSubscribers(false);
187 // refresh the services too
188 String resp = getServices();
190 return new ResponseEntity<String>(res1, HttpStatus.OK);
194 * Refresh the full subscriber list from a&ai.
196 * @return ResponseEntity The response entity
197 * @throws IOException Signals that an I/O exception has occurred.
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;
203 String res1 = getSubscribers(isFullSet);
205 // refresh the services too
206 String resp = getServices();
208 return new ResponseEntity<String>(res1, HttpStatus.OK);
212 * Get subscriber details from a&ai.
214 * @param subscriberId the subscriber id
215 * @return ResponseEntity The response entity
217 @RequestMapping(value="/aai_sub_details/{subscriberId}", method = RequestMethod.GET)
218 public ResponseEntity<String> GetSubscriber(@PathVariable("subscriberId") String subscriberId) {
220 String res1 = getSubscriberDetails(subscriberId);
221 return new ResponseEntity<String>(res1, HttpStatus.OK);
226 * Issue a named query to a&ai.
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
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) {
241 String componentListPayload = getComponentListPutPayload(namedQueryId, globalCustomerId, serviceType, serviceInstance);
242 File certiPath = GetCertificatesPath();
244 String getComponentListOutput = doAaiPost(certiPath.getAbsolutePath(), "search/named-query", componentListPayload, false);
245 System.out.println(getComponentListOutput);
246 return new ResponseEntity<String>(getComponentListOutput, HttpStatus.OK);
251 * Parses the for tenants.
253 * @param resp the resp
256 private String parseForTenants(String resp)
258 String tenantList = "";
262 JSONParser jsonParser = new JSONParser();
264 JSONObject jsonObject = (JSONObject) jsonParser.parse(resp);
266 return parseCustomerObjectForTenants(jsonObject);
268 catch (Exception ex) {
276 * Parses the for tenants by service subscription.
278 * @param resp the resp
281 private String parseForTenantsByServiceSubscription(String resp)
283 String tenantList = "";
287 JSONParser jsonParser = new JSONParser();
289 JSONObject jsonObject = (JSONObject) jsonParser.parse(resp);
291 return parseServiceSubscriptionObjectForTenants(jsonObject);
293 catch (Exception ex) {
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);
308 * Obtain tenants for a given service type.
310 * @param globalCustomerId the global customer id
311 * @param serviceType the service type
312 * @return ResponseEntity The response entity
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) {
318 return new ResponseEntity<String>(getTenantsFromServiceType(globalCustomerId, serviceType), HttpStatus.OK);
326 * @param globalCustomerId the global customer id
327 * @return the tenants
329 private String getTenants(String globalCustomerId)
331 File certiPath = GetCertificatesPath();
332 String resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers/customer/" + globalCustomerId, false);
333 resp = parseForTenants(resp);
334 //model.put("tenants", resp);
339 * Gets the tenants from service type.
341 * @param globalCustomerId the global customer id
342 * @param serviceType the service type
343 * @return the tenants from service type
345 private String getTenantsFromServiceType(String globalCustomerId, String serviceType)
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);
360 * @return the services
362 private String getServices()
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);
372 * Gets the subscribers.
374 * @param isFullSet the is full set
375 * @return the subscribers
377 private String getSubscribers(boolean isFullSet)
379 File certiPath = GetCertificatesPath();
381 if (isFullSet == true) {
384 String resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers?subscriber-type=INFRA&depth=" + depth, false);
385 //model.put("subscribernames" + depth, resp);
390 * Gets the subscriber details.
392 * @param subscriberId the subscriber id
393 * @return the subscriber details
395 private String getSubscriberDetails(String subscriberId)
397 File certiPath = GetCertificatesPath();
398 String resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers/customer/" + subscriberId, false);
403 * Gets the certificates path.
407 private File GetCertificatesPath()
409 if (servletContext != null)
410 return new File( servletContext.getRealPath("/WEB-INF/cert/") );
415 * Send a GET request to a&ai.
417 * @param certiPath the certi path
420 * @return String The response
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");
429 AAIRestInterface restContrller = new AAIRestInterface(certiPath);
430 return restContrller.RestGet(fromAppId, transId, uri, xml);
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());
445 * Send a POST request to a&ai.
447 * @param certiPath the certi path
449 * @param payload the payload
451 * @return String The response
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");
460 AAIRestInterface restContrller = new AAIRestInterface(certiPath);
461 return restContrller.RestPost(fromAppId, transId, uri, payload, xml);
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());
472 * Gets the component list put payload.
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
480 private String getComponentListPutPayload(String namedQueryId, String globalCustomerId, String serviceType, String serviceInstance) {
483 " \"instance-filters\": {" +
484 " \"instance-filter\": [" +
486 // " \"customer\": {" +
487 // " \"global-customer-id\": \"" + globalCustomerId + "\"" +
489 " \"service-instance\": {" +
490 " \"service-instance-id\": \"" + serviceInstance + "\"" +
493 // " \"service-subscription\": {" +
494 // " \"service-type\": \"" + serviceType + "\"" +
499 " \"query-parameters\": {" +
500 " \"named-query\": {" +
501 " \"named-query-uuid\": \"" + namedQueryId + "\"" +
510 * Return tenant details.
512 * @param jsonObject the json object
513 * @return String The parsing results
515 public static String parseCustomerObjectForTenants(JSONObject jsonObject) {
517 JSONArray tenantArray = new JSONArray();
518 boolean bconvert = false;
522 JSONObject serviceSubsObj = (JSONObject) jsonObject.get("service-subscriptions");
524 if (serviceSubsObj != null)
526 JSONArray srvcSubArray = (JSONArray) serviceSubsObj.get("service-subscription");
528 if (srvcSubArray != null)
530 Iterator i = srvcSubArray.iterator();
532 while (i.hasNext()) {
534 JSONObject innerObj = (JSONObject) i.next();
536 if (innerObj == null)
539 JSONObject relationShipListsObj = (JSONObject) innerObj.get("relationship-list");
540 if (relationShipListsObj != null)
542 JSONArray rShipArray = (JSONArray) relationShipListsObj.get("relationship");
543 if (rShipArray != null)
545 Iterator i1 = rShipArray.iterator();
547 while (i1.hasNext()) {
549 JSONObject inner1Obj = (JSONObject) i1.next();
551 if (inner1Obj == null)
554 String relatedTo = checkForNull((String)inner1Obj.get("related-to"));
555 if (relatedTo.equalsIgnoreCase("tenant"))
557 JSONObject tenantNewObj = new JSONObject();
559 String relatedLink = checkForNull((String) inner1Obj.get("related-link"));
560 tenantNewObj.put("link", relatedLink);
562 JSONArray rDataArray = (JSONArray) inner1Obj.get("relationship-data");
563 if (rDataArray != null)
565 Iterator i2 = rDataArray.iterator();
567 while (i2.hasNext()) {
568 JSONObject inner2Obj = (JSONObject) i2.next();
570 if (inner2Obj == null)
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"))
577 tenantNewObj.put("cloudOwner", rShipVal);
579 else if (rShipKey.equalsIgnoreCase("cloud-region.cloud-region-id"))
581 tenantNewObj.put("cloudRegionID", rShipVal);
584 if (rShipKey.equalsIgnoreCase("tenant.tenant-id"))
586 tenantNewObj.put("tenantID", rShipVal);
591 JSONArray relatedTPropArray = (JSONArray) inner1Obj.get("related-to-property");
592 if (relatedTPropArray != null)
594 Iterator i3 = relatedTPropArray.iterator();
596 while (i3.hasNext()) {
597 JSONObject inner3Obj = (JSONObject) i3.next();
599 if (inner3Obj == null)
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"))
606 tenantNewObj.put("tenantName", propVal);
611 tenantArray.add(tenantNewObj);
619 } catch (NullPointerException ex) {
625 return tenantArray.toJSONString();
633 * Retrieve the service subscription from the jsonObject.
635 * @param jsonObject the json object
638 public static String parseServiceSubscriptionObjectForTenants(JSONObject jsonObject) {
640 JSONArray tenantArray = new JSONArray();
641 boolean bconvert = false;
644 JSONObject relationShipListsObj = (JSONObject) jsonObject.get("relationship-list");
645 if (relationShipListsObj != null)
647 JSONArray rShipArray = (JSONArray) relationShipListsObj.get("relationship");
648 if (rShipArray != null)
650 Iterator i1 = rShipArray.iterator();
652 while (i1.hasNext()) {
654 JSONObject inner1Obj = (JSONObject) i1.next();
656 if (inner1Obj == null)
659 String relatedTo = checkForNull((String)inner1Obj.get("related-to"));
660 if (relatedTo.equalsIgnoreCase("tenant"))
662 JSONObject tenantNewObj = new JSONObject();
664 String relatedLink = checkForNull((String) inner1Obj.get("related-link"));
665 tenantNewObj.put("link", relatedLink);
667 JSONArray rDataArray = (JSONArray) inner1Obj.get("relationship-data");
668 if (rDataArray != null)
670 Iterator i2 = rDataArray.iterator();
672 while (i2.hasNext()) {
673 JSONObject inner2Obj = (JSONObject) i2.next();
675 if (inner2Obj == null)
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"))
682 tenantNewObj.put("cloudOwner", rShipVal);
684 else if (rShipKey.equalsIgnoreCase("cloud-region.cloud-region-id"))
686 tenantNewObj.put("cloudRegionID", rShipVal);
689 if (rShipKey.equalsIgnoreCase("tenant.tenant-id"))
691 tenantNewObj.put("tenantID", rShipVal);
696 JSONArray relatedTPropArray = (JSONArray) inner1Obj.get("related-to-property");
697 if (relatedTPropArray != null)
699 Iterator i3 = relatedTPropArray.iterator();
701 while (i3.hasNext()) {
702 JSONObject inner3Obj = (JSONObject) i3.next();
704 if (inner3Obj == null)
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"))
711 tenantNewObj.put("tenantName", propVal);
716 tenantArray.add(tenantNewObj);
722 } catch (NullPointerException ex) {
728 return tenantArray.toJSONString();
737 * @param local the local
740 private static String checkForNull(String local)