Upgrade sonar plugin
[vid.git] / vid-app-common / src / main / java / org / openecomp / vid / aai / AaiClient.java
index af1f404..0f07f20 100644 (file)
@@ -7,9 +7,8 @@ import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
 import org.json.simple.parser.JSONParser;
 import org.openecomp.aai.util.AAIRestInterface;
-import org.apache.tiles.request.ApplicationContext;
 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
-import org.openecomp.portalsdk.core.web.support.UserUtils;
+import org.openecomp.vid.aai.model.ServiceRelationships;
 import org.openecomp.vid.aai.model.AaiGetServicesRequestModel.GetServicesAAIRespone;
 import org.openecomp.vid.aai.model.AaiGetTenatns.GetTenantsResponse;
 import org.openecomp.vid.model.SubscriberList;
@@ -25,6 +24,7 @@ import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.Iterator;
+import java.util.List;
 import java.util.UUID;
 
 
@@ -33,252 +33,320 @@ import java.util.UUID;
  */
 public class AaiClient implements AaiClientInterface {
 
-       /**
-        * The Constant dateFormat.
-        */
-       final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
-       protected String fromAppId = "VidAaiController";
-       @Autowired
-       ServletContext servletContext;
-       /**
-        * The logger
-        */
-
-       EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AaiClient.class);
-
-
-       public AaiClient() {
-               //        certiPath = getCertificatesFile().getAbsolutePath();
-               //        depth = "0";
-       }
-
-       public AaiClient(ServletContext context) {
-               servletContext = context;
-       }
-
-
-
-       private static String checkForNull(String local) {
-               if (local != null)
-                       return local;
-               else
-                       return "";
-
-       }
-
+    /**
+     * The Constant dateFormat.
+     */
+    final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
+    protected String fromAppId = "VidAaiController";
+    @Autowired
+    ServletContext servletContext;
+    /**
+     * The logger
+     */
+
+    EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AaiClient.class);
+
+
+    public AaiClient() {
+        //        certiPath = getCertificatesFile().getAbsolutePath();
+        //        depth = "0";
+    }
+
+    public AaiClient(ServletContext context) {
+        servletContext = context;
+    }
+
+
+    private static String checkForNull(String local) {
+        if (local != null)
+            return local;
+        else
+            return "";
+
+    }
+
+    @Override
+    public AaiResponse<SubscriberList> getAllSubscribers() {
+        String certiPath = getCertificatesFile().getAbsolutePath();
+        String depth = "0";
+        Response resp = doAaiGet(certiPath, "business/customers?subscriber-type=INFRA&depth=" + depth, false);
+        return proccessAaiResponse(resp, SubscriberList.class, null);
+    }
+
+
+    @Override
+    public AaiResponse getAllAicZones() {
+        String certiPath = getCertificatesFile().getAbsolutePath();
+        Response resp = doAaiGet(certiPath, "network/zones", false);
+        AaiResponse aaiAicZones = proccessAaiResponse(resp, AicZones.class, null);
+        return aaiAicZones;
+    }
+    
+    
        @Override
-       public AaiResponse<SubscriberList> getAllSubscribers() {
+       public AaiResponse<String> getAicZoneForPnf(String globalCustomerId , String serviceType , String serviceId) {
                String certiPath = getCertificatesFile().getAbsolutePath();
-               String depth = "0";
-               Response resp = doAaiGet(certiPath, "business/customers?subscriber-type=INFRA&depth=" + depth, false);
-               UserUtils userUtils = new UserUtils();
-               return proccessAaiResponse(resp, SubscriberList.class,null);
-       }
-
-
-       @Override
-       public AaiResponse getAllAicZones() {
-               String certiPath = getCertificatesFile().getAbsolutePath();
-               Response resp = doAaiGet(certiPath, "network/zones" , false);
-               AaiResponse aaiAicZones = proccessAaiResponse(resp, AicZones.class,null);
-               return aaiAicZones;
-       }
-
-       @Override
-       public AaiResponse getSubscriberData(String subscriberId) {
-
-               File certiPath = getCertificatesFile();
-               String depth = "2";
-               AaiResponse subscriberDataResponse;
-               Response resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers/customer/" + subscriberId + "?depth=" + depth, false);
-               subscriberDataResponse = proccessAaiResponse(resp, Services.class,null);
-               return subscriberDataResponse;
-       }
-
-       @Override
-       public AaiResponse getServices() {
-               File certiPath = getCertificatesFile();
-               Response resp = doAaiGet(certiPath.getAbsolutePath(), "service-design-and-creation/services", false);
-               AaiResponse<GetServicesAAIRespone> getServicesResponse = proccessAaiResponse(resp, GetServicesAAIRespone.class,null);
-
-               return getServicesResponse;
+               String aicZonePath = "business/customers/customer/" + globalCustomerId + "/service-subscriptions/service-subscription/" + serviceType + "/service-instances/service-instance/" + serviceId;
+               Response resp = doAaiGet(certiPath , aicZonePath , false);
+               AaiResponse<ServiceRelationships> aaiResponse = proccessAaiResponse(resp , ServiceRelationships.class , null);
+               ServiceRelationships serviceRelationships = (ServiceRelationships)aaiResponse.getT();
+               String aicZone = serviceRelationships.getRelationshipList().getRelationship().get(0).getRelatedToPropertyList().get(0).getPropertyValue();
+               AaiResponse<String> aaiAicZonaForPnfResponse = new AaiResponse(aicZone , null ,HttpStatus.SC_OK);
+               return  aaiAicZonaForPnfResponse;
        }
+    
+
+    @Override
+    public AaiResponse getVNFData() {
+        String certiPath = getCertificatesFile().getAbsolutePath();
+        String payload = "{\"start\": [\"/business/customers/customer/e433710f-9217-458d-a79d-1c7aff376d89/service-subscriptions/service-subscription/VIRTUAL%20USP/service-instances/service-instance/3f93c7cb-2fd0-4557-9514-e189b7b04f9d\"],        \"query\": \"query/vnf-topology-fromServiceInstance\"}";
+        Response resp = doAaiPut(certiPath, "query?format=simple", payload, false);
+        return proccessAaiResponse(resp, AaiGetVnfResponse.class, null);
+
+    }
+
+    @Override
+    public Response getVNFData(String globalSubscriberId, String serviceType) {
+        String certiPath = getCertificatesFile().getAbsolutePath();
+        String payload = "{\"start\": [\"business/customers/customer/" + globalSubscriberId + "/service-subscriptions/service-subscription/"+ serviceType +"/service-instances\"]," +
+                "\"query\": \"query/vnf-topology-fromServiceInstance\"}";
+        return doAaiPut(certiPath, "query?format=simple", payload, false);
+
+    }
+
+    @Override
+    public AaiResponse getVNFData(String globalSubscriberId, String serviceType, String serviceInstanceId) {
+        String certiPath = getCertificatesFile().getAbsolutePath();
+        String payload = "{\"start\": [\"/business/customers/customer/" + globalSubscriberId + "/service-subscriptions/service-subscription/" + serviceType + "/service-instances/service-instance/" + serviceInstanceId + "\"],       \"query\": \"query/vnf-topology-fromServiceInstance\"}";
+        Response resp = doAaiPut(certiPath, "query?format=simple", payload, false);
+        return proccessAaiResponse(resp, AaiGetVnfResponse.class, null);
+    }
+
+    @Override
+    public Response getVersionByInvariantId(List<String> modelInvariantId) {
+        File certiPath = getCertificatesFile();
+        StringBuilder sb = new StringBuilder();
+        for (String id : modelInvariantId){
+            sb.append("&model-invariant-id=");
+            sb.append(id);
+
+        }
+        Response resp = doAaiGet(certiPath.getAbsolutePath(), "service-design-and-creation/models?depth=2"+ sb.toString(), false);
+        return resp;
+    }
+
+    @Override
+    public AaiResponse getSubscriberData(String subscriberId) {
+        File certiPath = getCertificatesFile();
+        String depth = "2";
+        AaiResponse subscriberDataResponse;
+        Response resp = doAaiGet(certiPath.getAbsolutePath(), "business/customers/customer/" + subscriberId + "?depth=" + depth, false);
+        subscriberDataResponse = proccessAaiResponse(resp, Services.class, null);
+        return subscriberDataResponse;
+    }
+
+    @Override
+    public AaiResponse getServices() {
+        File certiPath = getCertificatesFile();
+        Response resp = doAaiGet(certiPath.getAbsolutePath(), "service-design-and-creation/services", false);
+        AaiResponse<GetServicesAAIRespone> getServicesResponse = proccessAaiResponse(resp, GetServicesAAIRespone.class, null);
+
+        return getServicesResponse;
+    }
+
+    @Override
+    public AaiResponse getTenants(String globalCustomerId, String serviceType) {
+        File certiPath = getCertificatesFile();
+        String url = "business/customers/customer/" + globalCustomerId + "/service-subscriptions/service-subscription/" + serviceType;
+
+        Response resp = doAaiGet(certiPath.getAbsolutePath(), url, false);
+        String responseAsString = parseForTenantsByServiceSubscription(resp.readEntity(String.class));
+
+        AaiResponse<GetTenantsResponse[]> getTenantsResponse = proccessAaiResponse(resp, GetTenantsResponse[].class, responseAsString);
+        return getTenantsResponse;
+    }
+
+    private AaiResponse proccessAaiResponse(Response resp, Class classType, String responseBody) {
+        AaiResponse subscriberDataResponse;
+        if (resp == null) {
+            subscriberDataResponse = new AaiResponse<>(null, null, HttpStatus.SC_INTERNAL_SERVER_ERROR);
+            logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "Invalid response from AAI");
+        } else {
+            logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "getSubscribers() resp=" + resp.getStatusInfo().toString());
+            if (resp.getStatus() != HttpStatus.SC_OK) {
+                logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "Invalid response from AAI");
+                subscriberDataResponse = new AaiResponse<>(null, resp.readEntity(String.class), resp.getStatus());
+            } else {
+                String finalResponse;
+                try {
+                    if (responseBody != null) {
+                        finalResponse = responseBody;
+                    } else {
+                        finalResponse = resp.readEntity(String.class);
+                    }
+
+                    subscriberDataResponse = new AaiResponse<>((new ObjectMapper().readValue(finalResponse, classType)), null, HttpStatus.SC_OK);
+
+                } catch (IOException e) {
+                    subscriberDataResponse = new AaiResponse<>(null, null, HttpStatus.SC_INTERNAL_SERVER_ERROR);
+                }
+
+            }
+        }
+        return subscriberDataResponse;
+    }
+
+    private File getCertificatesFile() {
+        if (servletContext != null)
+            return new File(servletContext.getRealPath("/WEB-INF/cert/"));
+        return null;
+    }
+
+    @SuppressWarnings("all")
+    protected Response doAaiGet(String certiPath, String uri, boolean xml) {
+        String methodName = "doAaiGet";
+        String transId = UUID.randomUUID().toString();
+        logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
+
+        Response resp = null;
+        try {
+
+            AAIRestInterface restContrller = new AAIRestInterface(certiPath);
+            resp = restContrller.RestGet(fromAppId, transId, uri, xml);
+
+        } catch (WebApplicationException e) {
+            final String message = ((BadRequestException) e).getResponse().readEntity(String.class);
+            logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + message);
+            logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + message);
+        } catch (Exception e) {
+            logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
+            logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
+        }
+
+        return resp;
+    }
+
+    private String parseForTenantsByServiceSubscription(String resp) {
+        String tenantList = "";
+
+        try {
+            JSONParser jsonParser = new JSONParser();
+
+            JSONObject jsonObject = (JSONObject) jsonParser.parse(resp);
+
+            return parseServiceSubscriptionObjectForTenants(jsonObject);
+        } catch (Exception ex) {
+
+        }
+
+        return tenantList;
+    }
+
+    protected Response doAaiPut(String certiPath, String uri, String payload, boolean xml) {
+        String methodName = "doAaiPut";
+        String transId = UUID.randomUUID().toString();
+        logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
+
+        Response resp = null;
+        try {
 
-       @Override
-       public AaiResponse getTenants(String globalCustomerId, String serviceType) {
-               File certiPath = getCertificatesFile();
-               String url = "business/customers/customer/" + globalCustomerId + "/service-subscriptions/service-subscription/" + serviceType;
-
-               Response resp = doAaiGet(certiPath.getAbsolutePath(), url, false);
-               String responseAsString  = parseForTenantsByServiceSubscription(resp.readEntity(String.class));
-
-               AaiResponse<GetTenantsResponse[]> getTenantsResponse = proccessAaiResponse(resp, GetTenantsResponse[].class,responseAsString);
-               return getTenantsResponse;
-       }
-
-       private AaiResponse proccessAaiResponse(Response resp, Class classType,String responseBody) {
-               AaiResponse subscriberDataResponse;
-               if (resp == null) {
-                       subscriberDataResponse = new AaiResponse<>(null, null, HttpStatus.SC_INTERNAL_SERVER_ERROR);
-                       logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "Invalid response from AAI");
-               } else {
-                       logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "getSubscribers() resp=" + resp.getStatusInfo().toString());
-                       if (resp.getStatus() != HttpStatus.SC_OK) {
-                               logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "Invalid response from AAI");
-                               subscriberDataResponse = new AaiResponse<>(null, resp.readEntity(String.class), resp.getStatus());
-                       } else {
-                               String finalResponse;
-                               try {
-                                       if (responseBody != null){
-                                               finalResponse = responseBody;
-                                       }
-                                       else{
-                                               finalResponse =  resp.readEntity(String.class);;
-                                       }
-
-                                       subscriberDataResponse = new AaiResponse<>((new ObjectMapper().readValue(finalResponse, classType)), null, HttpStatus.SC_OK);
-
-                               } catch (IOException e) {
-                                       subscriberDataResponse = new AaiResponse<>(null, null, HttpStatus.SC_INTERNAL_SERVER_ERROR);
-                               }
-
-                       }
-               }
-               return subscriberDataResponse;
-       }
-
-       private File getCertificatesFile() {
-               if (servletContext != null)
-                       return new File(servletContext.getRealPath("/WEB-INF/cert/"));
-               return null;
-       }
-
-       @SuppressWarnings("all")
-       protected Response doAaiGet(String certiPath, String uri, boolean xml) {
-               String methodName = "getSubscriberList";
-               String transId = UUID.randomUUID().toString();
-               logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
-
-               Response resp = null;
-               try {
-
-                       AAIRestInterface restContrller = new AAIRestInterface(certiPath);
-                       resp = restContrller.RestGet(fromAppId, transId, uri, xml);
-
-               } catch (WebApplicationException e) {
-                       final String message = ((BadRequestException) e).getResponse().readEntity(String.class);
-                       logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + message);
-                       logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + message);
-               } catch (Exception e) {
-                       logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
-                       logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
-               }
-
-               return resp;
-       }
-
-       private String parseForTenantsByServiceSubscription(String resp) {
-               String tenantList = "";
-
-               try {
-                       JSONParser jsonParser = new JSONParser();
-
-                       JSONObject jsonObject = (JSONObject) jsonParser.parse(resp);
-
-                       return parseServiceSubscriptionObjectForTenants(jsonObject);
-               } catch (Exception ex) {
-
-               }
-
-               return tenantList;
-       }
+            AAIRestInterface restContrller = new AAIRestInterface(certiPath);
+            resp = restContrller.RestPut(fromAppId, transId, uri, payload, xml);
+
+        } catch (Exception e) {
+            logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
+            logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "." + methodName + e.toString());
+        }
+
+        return resp;
+    }
 
 
+    public static String parseServiceSubscriptionObjectForTenants(JSONObject jsonObject) {
+
+        JSONArray tenantArray = new JSONArray();
+        boolean bconvert = false;
+
+        try {
+            JSONObject relationShipListsObj = (JSONObject) jsonObject.get("relationship-list");
+            if (relationShipListsObj != null) {
+                JSONArray rShipArray = (JSONArray) relationShipListsObj.get("relationship");
+                if (rShipArray != null) {
+                    Iterator i1 = rShipArray.iterator();
 
-       public static String parseServiceSubscriptionObjectForTenants(JSONObject jsonObject) {
+                    while (i1.hasNext()) {
 
-               JSONArray tenantArray = new JSONArray();
-               boolean bconvert = false;
+                        JSONObject inner1Obj = (JSONObject) i1.next();
 
-               try {
-                       JSONObject relationShipListsObj = (JSONObject) jsonObject.get("relationship-list");
-                       if (relationShipListsObj != null) {
-                               JSONArray rShipArray = (JSONArray) relationShipListsObj.get("relationship");
-                               if (rShipArray != null) {
-                                       Iterator i1 = rShipArray.iterator();
+                        if (inner1Obj == null)
+                            continue;
 
-                                       while (i1.hasNext()) {
+                        String relatedTo = checkForNull((String) inner1Obj.get("related-to"));
+                        if (relatedTo.equalsIgnoreCase("tenant")) {
+                            JSONObject tenantNewObj = new JSONObject();
 
-                                               JSONObject inner1Obj = (JSONObject) i1.next();
+                            String relatedLink = checkForNull((String) inner1Obj.get("related-link"));
+                            tenantNewObj.put("link", relatedLink);
 
-                                               if (inner1Obj == null)
-                                                       continue;
+                            JSONArray rDataArray = (JSONArray) inner1Obj.get("relationship-data");
+                            if (rDataArray != null) {
+                                Iterator i2 = rDataArray.iterator();
 
-                                               String relatedTo = checkForNull((String) inner1Obj.get("related-to"));
-                                               if (relatedTo.equalsIgnoreCase("tenant")) {
-                                                       JSONObject tenantNewObj = new JSONObject();
+                                while (i2.hasNext()) {
+                                    JSONObject inner2Obj = (JSONObject) i2.next();
 
-                                                       String relatedLink = checkForNull((String) inner1Obj.get("related-link"));
-                                                       tenantNewObj.put("link", relatedLink);
+                                    if (inner2Obj == null)
+                                        continue;
 
-                                                       JSONArray rDataArray = (JSONArray) inner1Obj.get("relationship-data");
-                                                       if (rDataArray != null) {
-                                                               Iterator i2 = rDataArray.iterator();
+                                    String rShipKey = checkForNull((String) inner2Obj.get("relationship-key"));
+                                    String rShipVal = checkForNull((String) inner2Obj.get("relationship-value"));
+                                    if (rShipKey.equalsIgnoreCase("cloud-region.cloud-owner")) {
+                                        tenantNewObj.put("cloudOwner", rShipVal);
+                                    } else if (rShipKey.equalsIgnoreCase("cloud-region.cloud-region-id")) {
+                                        tenantNewObj.put("cloudRegionID", rShipVal);
+                                    }
 
-                                                               while (i2.hasNext()) {
-                                                                       JSONObject inner2Obj = (JSONObject) i2.next();
+                                    if (rShipKey.equalsIgnoreCase("tenant.tenant-id")) {
+                                        tenantNewObj.put("tenantID", rShipVal);
+                                    }
+                                }
+                            }
 
-                                                                       if (inner2Obj == null)
-                                                                               continue;
+                            JSONArray relatedTPropArray = (JSONArray) inner1Obj.get("related-to-property");
+                            if (relatedTPropArray != null) {
+                                Iterator i3 = relatedTPropArray.iterator();
 
-                                                                       String rShipKey = checkForNull((String) inner2Obj.get("relationship-key"));
-                                                                       String rShipVal = checkForNull((String) inner2Obj.get("relationship-value"));
-                                                                       if (rShipKey.equalsIgnoreCase("cloud-region.cloud-owner")) {
-                                                                               tenantNewObj.put("cloudOwner", rShipVal);
-                                                                       } else if (rShipKey.equalsIgnoreCase("cloud-region.cloud-region-id")) {
-                                                                               tenantNewObj.put("cloudRegionID", rShipVal);
-                                                                       }
+                                while (i3.hasNext()) {
+                                    JSONObject inner3Obj = (JSONObject) i3.next();
 
-                                                                       if (rShipKey.equalsIgnoreCase("tenant.tenant-id")) {
-                                                                               tenantNewObj.put("tenantID", rShipVal);
-                                                                       }
-                                                               }
-                                                       }
+                                    if (inner3Obj == null)
+                                        continue;
 
-                                                       JSONArray relatedTPropArray = (JSONArray) inner1Obj.get("related-to-property");
-                                                       if (relatedTPropArray != null) {
-                                                               Iterator i3 = relatedTPropArray.iterator();
+                                    String propKey = checkForNull((String) inner3Obj.get("property-key"));
+                                    String propVal = checkForNull((String) inner3Obj.get("property-value"));
+                                    if (propKey.equalsIgnoreCase("tenant.tenant-name")) {
+                                        tenantNewObj.put("tenantName", propVal);
+                                    }
+                                }
+                            }
+                            bconvert = true;
+                            tenantArray.add(tenantNewObj);
+                        }
+                    }
 
-                                                               while (i3.hasNext()) {
-                                                                       JSONObject inner3Obj = (JSONObject) i3.next();
+                }
+            }
+        } catch (NullPointerException ex) {
 
-                                                                       if (inner3Obj == null)
-                                                                               continue;
 
-                                                                       String propKey = checkForNull((String) inner3Obj.get("property-key"));
-                                                                       String propVal = checkForNull((String) inner3Obj.get("property-value"));
-                                                                       if (propKey.equalsIgnoreCase("tenant.tenant-name")) {
-                                                                               tenantNewObj.put("tenantName", propVal);
-                                                                       }
-                                                               }
-                                                       }
-                                                       bconvert = true;
-                                                       tenantArray.add(tenantNewObj);
-                                               }
-                                       }
+        }
 
-                               }
-                       }
-               } catch (NullPointerException ex) {
+        if (bconvert)
+            return tenantArray.toJSONString();
+        else
+            return "";
 
+    }
 
-               }
-
-               if (bconvert)
-                       return tenantArray.toJSONString();
-               else
-                       return "";
-
-       }
 
 }
\ No newline at end of file