Merge "Move CQ operation name from actor.aai to aai"
[policy/models.git] / models-interactions / model-impl / aai / src / main / java / org / onap / policy / aai / AaiCqResponse.java
index ca0c186..92af217 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  *
  * ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
 package org.onap.policy.aai;
 
 import com.google.gson.annotations.SerializedName;
+import java.io.Serializable;
 import java.io.StringReader;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -47,8 +48,10 @@ import org.onap.aai.domain.yang.Vserver;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class AaiCqResponse {
-
+public class AaiCqResponse implements Serializable {
+    private static final long serialVersionUID = 1L;
+    public static final String CONTEXT_KEY = AaiConstants.CONTEXT_PREFIX + "AaiCqResponse";
+    public static final String OPERATION = "CustomQuery";
     private static final String GENERIC_VNF = "generic-vnf";
     private static final String VF_MODULE = "vf-module";
     private static final Logger LOGGER = LoggerFactory.getLogger(AaiCqResponse.class);
@@ -62,12 +65,17 @@ public class AaiCqResponse {
         properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
         // Define JAXB context
         try {
-            jaxbContext =
-                JAXBContextFactory
-                    .createContext(
-                        new Class[] {Vserver.class, GenericVnf.class, VfModule.class,
-                            CloudRegion.class, ServiceInstance.class, Tenant.class, ModelVer.class},
-                        properties);
+            // @formatter:off
+            jaxbContext = JAXBContextFactory.createContext(new Class[] {
+                Vserver.class,
+                GenericVnf.class,
+                VfModule.class,
+                CloudRegion.class,
+                ServiceInstance.class,
+                Tenant.class,
+                ModelVer.class
+            }, properties);
+            // @formatter:on
             unmarshaller = jaxbContext.createUnmarshaller();
         } catch (JAXBException e) {
             LOGGER.error("Could not initialize JAXBContext", e);
@@ -76,13 +84,12 @@ public class AaiCqResponse {
     }
 
     @SerializedName("results")
-    private List<Object> inventoryResponseItems = new LinkedList<>();
+    private List<Serializable> inventoryResponseItems = new LinkedList<>();
 
     /**
      * Constructor creates a custom query response from a valid json string.
      *
-     * @param jsonString
-     *        A&AI Custom Query response JSON string
+     * @param jsonString A&AI Custom Query response JSON string
      */
     public AaiCqResponse(String jsonString) {
 
@@ -98,8 +105,8 @@ public class AaiCqResponse {
 
                 // Create the StreamSource by creating StringReader using the
                 // JSON input
-                StreamSource json = new StreamSource(new StringReader(
-                    resultsArray.getJSONObject(i).getJSONObject("vserver").toString()));
+                StreamSource json = new StreamSource(
+                        new StringReader(resultsArray.getJSONObject(i).getJSONObject("vserver").toString()));
 
                 // Getting the vserver pojo again from the json
                 Vserver vserver = this.getAaiObject(json, Vserver.class);
@@ -110,8 +117,8 @@ public class AaiCqResponse {
             if (resultsArray.getJSONObject(i).has(GENERIC_VNF)) {
                 // Create the StreamSource by creating StringReader using the
                 // JSON input
-                StreamSource json = new StreamSource(new StringReader(
-                    resultsArray.getJSONObject(i).getJSONObject(GENERIC_VNF).toString()));
+                StreamSource json = new StreamSource(
+                        new StringReader(resultsArray.getJSONObject(i).getJSONObject(GENERIC_VNF).toString()));
 
                 // Getting the generic vnf pojo again from the json
                 GenericVnf genericVnf = this.getAaiObject(json, GenericVnf.class);
@@ -124,8 +131,8 @@ public class AaiCqResponse {
 
                 // Create the StreamSource by creating StringReader using the
                 // JSON input
-                StreamSource json = new StreamSource(new StringReader(
-                    resultsArray.getJSONObject(i).getJSONObject("service-instance").toString()));
+                StreamSource json = new StreamSource(
+                        new StringReader(resultsArray.getJSONObject(i).getJSONObject("service-instance").toString()));
 
                 // Getting the employee pojo again from the json
                 ServiceInstance serviceInstance = this.getAaiObject(json, ServiceInstance.class);
@@ -137,8 +144,8 @@ public class AaiCqResponse {
             if (resultsArray.getJSONObject(i).has(VF_MODULE)) {
                 // Create the StreamSource by creating StringReader using the
                 // JSON input
-                StreamSource json = new StreamSource(new StringReader(
-                    resultsArray.getJSONObject(i).getJSONObject(VF_MODULE).toString()));
+                StreamSource json = new StreamSource(
+                        new StringReader(resultsArray.getJSONObject(i).getJSONObject(VF_MODULE).toString()));
 
                 // Getting the vf module pojo again from the json
                 VfModule vfModule = this.getAaiObject(json, VfModule.class);
@@ -150,8 +157,8 @@ public class AaiCqResponse {
             if (resultsArray.getJSONObject(i).has("cloud-region")) {
                 // Create the StreamSource by creating StringReader using the
                 // JSON input
-                StreamSource json = new StreamSource(new StringReader(
-                    resultsArray.getJSONObject(i).getJSONObject("cloud-region").toString()));
+                StreamSource json = new StreamSource(
+                        new StringReader(resultsArray.getJSONObject(i).getJSONObject("cloud-region").toString()));
 
                 // Getting the cloud region pojo again from the json
                 CloudRegion cloudRegion = this.getAaiObject(json, CloudRegion.class);
@@ -163,8 +170,8 @@ public class AaiCqResponse {
             if (resultsArray.getJSONObject(i).has("tenant")) {
                 // Create the StreamSource by creating StringReader using the
                 // JSON input
-                StreamSource json = new StreamSource(new StringReader(
-                    resultsArray.getJSONObject(i).getJSONObject("tenant").toString()));
+                StreamSource json = new StreamSource(
+                        new StringReader(resultsArray.getJSONObject(i).getJSONObject("tenant").toString()));
 
                 // Getting the tenant pojo again from the json
                 Tenant tenant = this.getAaiObject(json, Tenant.class);
@@ -176,8 +183,8 @@ public class AaiCqResponse {
             if (resultsArray.getJSONObject(i).has("model-ver")) {
                 // Create the StreamSource by creating StringReader using the
                 // JSON input
-                StreamSource json = new StreamSource(new StringReader(
-                    resultsArray.getJSONObject(i).getJSONObject("model-ver").toString()));
+                StreamSource json = new StreamSource(
+                        new StringReader(resultsArray.getJSONObject(i).getJSONObject("model-ver").toString()));
 
                 // Getting the ModelVer pojo again from the json
                 ModelVer modelVer = this.getAaiObject(json, ModelVer.class);
@@ -198,25 +205,24 @@ public class AaiCqResponse {
         }
     }
 
-    public List<Object> getInventoryResponseItems() {
+    public List<Serializable> getInventoryResponseItems() {
         return inventoryResponseItems;
     }
 
-    public void setInventoryResponseItems(List<Object> inventoryResponseItems) {
+    public void setInventoryResponseItems(List<Serializable> inventoryResponseItems) {
         this.inventoryResponseItems = inventoryResponseItems;
     }
 
     /**
      * Get list of A&AI objects in the custom query.
      *
-     * @param classOfResponse
-     *        Class of the type of A&AI objects to be returned
+     * @param classOfResponse Class of the type of A&AI objects to be returned
      * @return List A&AI objects matching the class
      */
     @SuppressWarnings("unchecked")
     public <T> List<T> getItemListByType(Class<T> classOfResponse) {
         List<T> returnItemList = new ArrayList<>();
-        for (Object i : this.inventoryResponseItems) {
+        for (Serializable i : this.inventoryResponseItems) {
             if (i.getClass() == classOfResponse) {
                 returnItemList.add((T) i);
             }
@@ -232,7 +238,7 @@ public class AaiCqResponse {
      */
     public ServiceInstance getServiceInstance() {
         ServiceInstance serviceInstance = null;
-        for (Object i : this.inventoryResponseItems) {
+        for (Serializable i : this.inventoryResponseItems) {
             if (i.getClass() == ServiceInstance.class) {
                 serviceInstance = (ServiceInstance) i;
             }
@@ -248,7 +254,7 @@ public class AaiCqResponse {
      */
     public Tenant getDefaultTenant() {
         Tenant tenant = null;
-        for (Object i : this.inventoryResponseItems) {
+        for (Serializable i : this.inventoryResponseItems) {
             if (i.getClass() == Tenant.class) {
                 tenant = (Tenant) i;
             }
@@ -264,7 +270,7 @@ public class AaiCqResponse {
      */
     public CloudRegion getDefaultCloudRegion() {
         CloudRegion cloudRegion = null;
-        for (Object i : this.inventoryResponseItems) {
+        for (Serializable i : this.inventoryResponseItems) {
             if (i.getClass() == CloudRegion.class) {
                 cloudRegion = (CloudRegion) i;
             }
@@ -280,7 +286,7 @@ public class AaiCqResponse {
      */
     public List<GenericVnf> getGenericVnfs() {
         List<GenericVnf> genericVnfList = new ArrayList<>();
-        for (Object i : this.inventoryResponseItems) {
+        for (Serializable i : this.inventoryResponseItems) {
             if (i.getClass() == GenericVnf.class) {
                 genericVnfList.add((GenericVnf) i);
             }
@@ -292,14 +298,13 @@ public class AaiCqResponse {
     /**
      * Returns a generic Vnf matching vnf name.
      *
-     * @param vnfName
-     *        Name of the vnf to match
+     * @param vnfName Name of the vnf to match
      * @return generic Vnf
      */
     public GenericVnf getGenericVnfByVnfName(String vnfName) {
         List<GenericVnf> genericVnfList = new ArrayList<>();
         GenericVnf genericVnf = null;
-        for (Object i : this.inventoryResponseItems) {
+        for (Serializable i : this.inventoryResponseItems) {
             if (i.getClass() == GenericVnf.class) {
                 genericVnfList.add((GenericVnf) i);
             }
@@ -318,14 +323,13 @@ public class AaiCqResponse {
     /**
      * Returns a generic Vnf matching model invariant ID.
      *
-     * @param modelInvariantId
-     *        Name of the vnf to match
+     * @param modelInvariantId Name of the vnf to match
      * @return generic Vnf
      */
     public GenericVnf getGenericVnfByModelInvariantId(String modelInvariantId) {
         List<GenericVnf> genericVnfList = new ArrayList<>();
         GenericVnf genericVnf = null;
-        for (Object i : this.inventoryResponseItems) {
+        for (Serializable i : this.inventoryResponseItems) {
             if (i.getClass() == GenericVnf.class) {
                 genericVnfList.add((GenericVnf) i);
             }
@@ -344,8 +348,7 @@ public class AaiCqResponse {
     /**
      * Returns a generic Vnf of a given VF Module ID.
      *
-     * @param vfModuleModelInvariantId
-     *        of the vf module for which vnf is to be returned
+     * @param vfModuleModelInvariantId of the vf module for which vnf is to be returned
      * @return generic Vnf
      */
     public GenericVnf getGenericVnfByVfModuleModelInvariantId(String vfModuleModelInvariantId) {
@@ -355,7 +358,7 @@ public class AaiCqResponse {
             // Iterate through all the vfModules of that generic Vnf
             for (VfModule vfMod : genVnf.getVfModules().getVfModule()) {
                 if (vfMod.getModelInvariantId() != null
-                    && vfMod.getModelInvariantId().equals(vfModuleModelInvariantId)) {
+                        && vfMod.getModelInvariantId().equals(vfModuleModelInvariantId)) {
                     return genVnf;
                 }
             }
@@ -502,8 +505,7 @@ public class AaiCqResponse {
         VfModule vfModule = null;
 
         for (VfModule vfMod : this.getAllVfModules()) {
-            if (vfMod.getModelInvariantId() != null
-                && vfModelInvariantId.equals(vfMod.getModelInvariantId())) {
+            if (vfMod.getModelInvariantId() != null && vfModelInvariantId.equals(vfMod.getModelInvariantId())) {
                 vfModule = vfMod;
             }
 
@@ -534,7 +536,7 @@ public class AaiCqResponse {
      */
     public List<ModelVer> getAllModelVer() {
         List<ModelVer> modelVerList = new ArrayList<>();
-        for (Object i : this.inventoryResponseItems) {
+        for (Serializable i : this.inventoryResponseItems) {
             if (i.getClass() == ModelVer.class) {
                 modelVerList.add((ModelVer) i);
             }
@@ -560,28 +562,25 @@ public class AaiCqResponse {
     }
 
     /**
-     * Get the count of vfModules matching customizationId, InvariantId and
-     * VersionId.
+     * Get the count of vfModules matching customizationId, InvariantId and VersionId.
      *
-     * @param custId
-     *        ModelCustomizationId
-     * @param invId
-     *        ModelInvariantId
-     * @param verId
-     *        ModelVersionId
+     * @param custId ModelCustomizationId
+     * @param invId ModelInvariantId
+     * @param verId ModelVersionId
      * @return Returns the count of vf modules
      */
     public int getVfModuleCount(String custId, String invId, String verId) {
         List<VfModule> vfModuleList = this.getAllVfModules();
         int count = 0;
         for (VfModule vfModule : vfModuleList) {
-            if (vfModule.getModelCustomizationId() != null && vfModule.getModelInvariantId() != null
-                && vfModule.getModelVersionId() != null) {
-                if (vfModule.getModelCustomizationId().equals(custId)
-                    && vfModule.getModelInvariantId().equals(invId)
+            if (vfModule.getModelCustomizationId() == null || vfModule.getModelInvariantId() == null
+                    || vfModule.getModelVersionId() == null) {
+                continue;
+            }
+
+            if (vfModule.getModelCustomizationId().equals(custId) && vfModule.getModelInvariantId().equals(invId)
                     && vfModule.getModelVersionId().equals(verId)) {
-                    count = count + 1;
-                }
+                count = count + 1;
             }
         }
         return count;