X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-interactions%2Fmodel-impl%2Faai%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Faai%2FAaiCqResponse.java;h=7a6eb6859b1496377e535193a02e164ed5984f7e;hb=e53df8d3f8ab0464b0876bdb339fa91dc9085cd2;hp=ca0c1867f7a3d31fe9425acbaa686b2b7fb1494a;hpb=c8857aca1f6dee63b634559c0a06599fb53ecc99;p=policy%2Fmodels.git diff --git a/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/AaiCqResponse.java b/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/AaiCqResponse.java index ca0c1867f..7a6eb6859 100644 --- a/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/AaiCqResponse.java +++ b/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/AaiCqResponse.java @@ -2,7 +2,8 @@ * ============LICENSE_START======================================================= * * ================================================================================ - * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2023 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,19 +21,13 @@ package org.onap.policy.aai; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.annotations.SerializedName; -import java.io.StringReader; +import java.io.Serializable; import java.util.ArrayList; -import java.util.HashMap; import java.util.LinkedList; import java.util.List; -import java.util.Map; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; -import javax.xml.transform.stream.StreamSource; -import org.eclipse.persistence.jaxb.JAXBContextFactory; -import org.eclipse.persistence.jaxb.JAXBContextProperties; import org.json.JSONArray; import org.json.JSONObject; import org.onap.aai.domain.yang.CloudRegion; @@ -44,179 +39,152 @@ import org.onap.aai.domain.yang.ServiceInstance; import org.onap.aai.domain.yang.Tenant; import org.onap.aai.domain.yang.VfModule; 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); - private static JAXBContext jaxbContext; - private static Unmarshaller unmarshaller; - - // JABX initial stuff - static { - Map properties = new HashMap<>(); - properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json"); - 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); - unmarshaller = jaxbContext.createUnmarshaller(); - } catch (JAXBException e) { - LOGGER.error("Could not initialize JAXBContext", e); - LOGGER.info("Problem initiatlizing JAXBContext", e); - } - } @SerializedName("results") - private List inventoryResponseItems = new LinkedList<>(); + private List inventoryResponseItems = new LinkedList<>(); + + private final Gson gson; /** * 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) { + gson = new GsonBuilder() + .setFieldNamingStrategy(new XmlElementFieldNamingStrategy()) + .create(); // Read JSON String and add all AaiObjects - JSONObject responseObj = new JSONObject(jsonString); - JSONArray resultsArray = new JSONArray(); + var responseObj = new JSONObject(jsonString); + var resultsArray = new JSONArray(); if (responseObj.has("results")) { resultsArray = (JSONArray) responseObj.get("results"); } - for (int i = 0; i < resultsArray.length(); i++) { - // Object is a vserver - if (resultsArray.getJSONObject(i).has("vserver")) { - - // Create the StreamSource by creating StringReader using the - // JSON input - 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); - this.inventoryResponseItems.add(vserver); - } - - // Object is a Generic VNF - 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())); - - // Getting the generic vnf pojo again from the json - GenericVnf genericVnf = this.getAaiObject(json, GenericVnf.class); - - this.inventoryResponseItems.add(genericVnf); - } - - // Object is a Service Instance - if (resultsArray.getJSONObject(i).has("service-instance")) { - - // Create the StreamSource by creating StringReader using the - // JSON input - 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); + for (var i = 0; i < resultsArray.length(); i++) { + final var resultObject = resultsArray.getJSONObject(i); + + extractVserver(resultObject); + extractGenericVnf(resultObject); + extractServiceInstance(resultObject); + extractVfModule(resultObject); + extractCloudRegion(resultObject); + extractTenant(resultObject); + extractModelVer(resultObject); + } + } - this.inventoryResponseItems.add(serviceInstance); - } + private void extractVserver(final JSONObject resultObject) { + if (resultObject.has("vserver")) { - // Object is a VF Module - 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())); + // Create the StreamSource by creating StringReader using the + // JSON input + var json = resultObject.getJSONObject("vserver").toString(); - // Getting the vf module pojo again from the json - VfModule vfModule = this.getAaiObject(json, VfModule.class); + // Getting the vserver pojo again from the json + var vserver = gson.fromJson(json, Vserver.class); + this.inventoryResponseItems.add(vserver); + } + } - this.inventoryResponseItems.add(vfModule); - } + private void extractGenericVnf(final JSONObject resultObject) { + if (resultObject.has(GENERIC_VNF)) { + // Create the StreamSource by creating StringReader using the + // JSON input + var json = resultObject.getJSONObject(GENERIC_VNF).toString(); - // Object is a CloudRegion - 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())); + // Getting the generic vnf pojo again from the json + var genericVnf = gson.fromJson(json, GenericVnf.class); + this.inventoryResponseItems.add(genericVnf); + } + } - // Getting the cloud region pojo again from the json - CloudRegion cloudRegion = this.getAaiObject(json, CloudRegion.class); + private void extractServiceInstance(final JSONObject resultObject) { + if (resultObject.has("service-instance")) { - this.inventoryResponseItems.add(cloudRegion); - } + // Create the StreamSource by creating StringReader using the + // JSON input + var json = resultObject.getJSONObject("service-instance").toString(); - // Object is a Tenant - 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())); + // Getting the employee pojo again from the json + var serviceInstance = gson.fromJson(json, ServiceInstance.class); + this.inventoryResponseItems.add(serviceInstance); + } + } - // Getting the tenant pojo again from the json - Tenant tenant = this.getAaiObject(json, Tenant.class); + private void extractVfModule(final JSONObject resultObject) { + if (resultObject.has(VF_MODULE)) { + // Create the StreamSource by creating StringReader using the + // JSON input + var json = resultObject.getJSONObject(VF_MODULE).toString(); - this.inventoryResponseItems.add(tenant); - } + // Getting the vf module pojo again from the json + var vfModule = gson.fromJson(json, VfModule.class); + this.inventoryResponseItems.add(vfModule); + } + } - // Object is a ModelVer - 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())); + private void extractCloudRegion(final JSONObject resultObject) { + if (resultObject.has("cloud-region")) { + // Create the StreamSource by creating StringReader using the + // JSON input + var json = resultObject.getJSONObject("cloud-region").toString(); - // Getting the ModelVer pojo again from the json - ModelVer modelVer = this.getAaiObject(json, ModelVer.class); + // Getting the cloud region pojo again from the json + var cloudRegion = gson.fromJson(json, CloudRegion.class); + this.inventoryResponseItems.add(cloudRegion); + } + } - this.inventoryResponseItems.add(modelVer); - } + private void extractTenant(final JSONObject resultObject) { + if (resultObject.has("tenant")) { + // Create the StreamSource by creating StringReader using the + // JSON input + var json = resultObject.getJSONObject("tenant").toString(); + // Getting the tenant pojo again from the json + var tenant = gson.fromJson(json, Tenant.class); + this.inventoryResponseItems.add(tenant); } - } - private T getAaiObject(StreamSource json, final Class classOfResponse) { - try { - return unmarshaller.unmarshal(json, classOfResponse).getValue(); - } catch (JAXBException e) { - LOGGER.error("JAXBCOntext error", e); - return null; + private void extractModelVer(final JSONObject resultObject) { + if (resultObject.has("model-ver")) { + // Create the StreamSource by creating StringReader using the + // JSON input + var json = resultObject.getJSONObject("model-ver").toString(); + + // Getting the ModelVer pojo again from the json + var modelVer = gson.fromJson(json, ModelVer.class); + this.inventoryResponseItems.add(modelVer); } } - public List getInventoryResponseItems() { + public List getInventoryResponseItems() { return inventoryResponseItems; } - public void setInventoryResponseItems(List inventoryResponseItems) { + public void setInventoryResponseItems(List 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 List getItemListByType(Class classOfResponse) { List returnItemList = new ArrayList<>(); - for (Object i : this.inventoryResponseItems) { + for (Serializable i : this.inventoryResponseItems) { if (i.getClass() == classOfResponse) { returnItemList.add((T) i); } @@ -232,7 +200,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 +216,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 +232,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 +248,7 @@ public class AaiCqResponse { */ public List getGenericVnfs() { List genericVnfList = new ArrayList<>(); - for (Object i : this.inventoryResponseItems) { + for (Serializable i : this.inventoryResponseItems) { if (i.getClass() == GenericVnf.class) { genericVnfList.add((GenericVnf) i); } @@ -292,14 +260,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 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 +285,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 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 +310,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) { @@ -363,6 +328,24 @@ public class AaiCqResponse { return null; } + /** + * Returns the VNF given the vnf-id. + * + * @param vnfId The vnf-id + * @return generic Vnf + */ + public GenericVnf getGenericVnfByVnfId(String vnfId) { + List genericVnfList = this.getGenericVnfs(); + + for (GenericVnf genVnf : genericVnfList) { + if (vnfId.equals(genVnf.getVnfId())) { + return genVnf; + } + } + + return null; + } + /** * Get the generic vnf associated with the vserver in the custom query. * @@ -372,13 +355,13 @@ public class AaiCqResponse { GenericVnf genericVnf = null; // Get the vserver associated with the query - Vserver vserver = this.getVserver(); + var vserver = this.getVserver(); // Get the relationships of the vserver List relations = vserver.getRelationshipList().getRelationship(); // Find the relationship of the genericVNF - String genericVnfId = ""; + var genericVnfId = ""; List relationshipData = null; // Iterate through the list of relationships and get generic vnf @@ -420,13 +403,13 @@ public class AaiCqResponse { VfModule vfModule = null; // Get the vserver associated with the query - Vserver vserver = this.getVserver(); + var vserver = this.getVserver(); // Get the relationships of the vserver List relations = vserver.getRelationshipList().getRelationship(); // Find the relationship of VfModule - String vfModuleId = ""; + var vfModuleId = ""; List relationshipData = null; // Iterate through the list of relationships and get vf module @@ -502,8 +485,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; } @@ -518,7 +500,7 @@ public class AaiCqResponse { */ public Vserver getVserver() { Vserver vserver = null; - int index = 0; + var index = 0; while (this.inventoryResponseItems.get(index).getClass() != Vserver.class) { index = index + 1; } @@ -534,7 +516,7 @@ public class AaiCqResponse { */ public List getAllModelVer() { List modelVerList = new ArrayList<>(); - for (Object i : this.inventoryResponseItems) { + for (Serializable i : this.inventoryResponseItems) { if (i.getClass() == ModelVer.class) { modelVerList.add((ModelVer) i); } @@ -560,28 +542,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 vfModuleList = this.getAllVfModules(); - int count = 0; + var 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) - && vfModule.getModelVersionId().equals(verId)) { - count = count + 1; - } + 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; } } return count;