* ============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.
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;
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<String, Object> 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<Object> inventoryResponseItems = new LinkedList<>();
+ private List<Serializable> 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> T getAaiObject(StreamSource json, final Class<T> 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<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);
}
*/
public ServiceInstance getServiceInstance() {
ServiceInstance serviceInstance = null;
- for (Object i : this.inventoryResponseItems) {
+ for (Serializable i : this.inventoryResponseItems) {
if (i.getClass() == ServiceInstance.class) {
serviceInstance = (ServiceInstance) i;
}
*/
public Tenant getDefaultTenant() {
Tenant tenant = null;
- for (Object i : this.inventoryResponseItems) {
+ for (Serializable i : this.inventoryResponseItems) {
if (i.getClass() == Tenant.class) {
tenant = (Tenant) i;
}
*/
public CloudRegion getDefaultCloudRegion() {
CloudRegion cloudRegion = null;
- for (Object i : this.inventoryResponseItems) {
+ for (Serializable i : this.inventoryResponseItems) {
if (i.getClass() == CloudRegion.class) {
cloudRegion = (CloudRegion) i;
}
*/
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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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) {
return null;
}
+ /**
+ * Returns the VNF given the vnf-id.
+ *
+ * @param vnfId The vnf-id
+ * @return generic Vnf
+ */
+ public GenericVnf getGenericVnfByVnfId(String vnfId) {
+ List<GenericVnf> 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.
*
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<Relationship> relations = vserver.getRelationshipList().getRelationship();
// Find the relationship of the genericVNF
- String genericVnfId = "";
+ var genericVnfId = "";
List<RelationshipData> relationshipData = null;
// Iterate through the list of relationships and get generic vnf
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<Relationship> relations = vserver.getRelationshipList().getRelationship();
// Find the relationship of VfModule
- String vfModuleId = "";
+ var vfModuleId = "";
List<RelationshipData> relationshipData = null;
// Iterate through the list of relationships and get vf module
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;
}
*/
public Vserver getVserver() {
Vserver vserver = null;
- int index = 0;
+ var index = 0;
while (this.inventoryResponseItems.get(index).getClass() != Vserver.class) {
index = index + 1;
}
*/
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);
}
}
/**
- * 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;
+ 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;