* ============LICENSE_START=======================================================
*
* ================================================================================
- * Copyright (C) 2019-2020 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.Serializable;
-import java.io.StringReader;
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 implements Serializable {
private static final long serialVersionUID = 1L;
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 {
- // @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);
- LOGGER.info("Problem initiatlizing JAXBContext", e);
- }
- }
@SerializedName("results")
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
*/
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()));
+ 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);
+ }
+ }
- // Getting the employee pojo again from the json
- ServiceInstance serviceInstance = this.getAaiObject(json, ServiceInstance.class);
+ private void extractVserver(final JSONObject resultObject) {
+ if (resultObject.has("vserver")) {
- this.inventoryResponseItems.add(serviceInstance);
- }
+ // Create the StreamSource by creating StringReader using the
+ // JSON input
+ var json = resultObject.getJSONObject("vserver").toString();
- // 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()));
+ // Getting the vserver pojo again from the json
+ var vserver = gson.fromJson(json, Vserver.class);
+ this.inventoryResponseItems.add(vserver);
+ }
+ }
- // Getting the vf module pojo again from the json
- VfModule vfModule = this.getAaiObject(json, VfModule.class);
+ 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();
- this.inventoryResponseItems.add(vfModule);
- }
+ // Getting the generic vnf pojo again from the json
+ var genericVnf = gson.fromJson(json, GenericVnf.class);
+ this.inventoryResponseItems.add(genericVnf);
+ }
+ }
- // 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()));
+ private void extractServiceInstance(final JSONObject resultObject) {
+ if (resultObject.has("service-instance")) {
- // Getting the cloud region pojo again from the json
- CloudRegion cloudRegion = this.getAaiObject(json, CloudRegion.class);
+ // Create the StreamSource by creating StringReader using the
+ // JSON input
+ var json = resultObject.getJSONObject("service-instance").toString();
- this.inventoryResponseItems.add(cloudRegion);
- }
-
- // 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);
}
}
// 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;
}
}
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
*/
public Vserver getVserver() {
Vserver vserver = null;
- int index = 0;
+ var index = 0;
while (this.inventoryResponseItems.get(index).getClass() != Vserver.class) {
index = index + 1;
}
*/
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) {
+ || vfModule.getModelVersionId() == null) {
continue;
}
if (vfModule.getModelCustomizationId().equals(custId) && vfModule.getModelInvariantId().equals(invId)
- && vfModule.getModelVersionId().equals(verId)) {
+ && vfModule.getModelVersionId().equals(verId)) {
count = count + 1;
}
}