diff options
Diffstat (limited to 'models-interactions/model-impl/aai')
3 files changed, 63 insertions, 83 deletions
diff --git a/models-interactions/model-impl/aai/pom.xml b/models-interactions/model-impl/aai/pom.xml index de82e9bf2..5f401295f 100644 --- a/models-interactions/model-impl/aai/pom.xml +++ b/models-interactions/model-impl/aai/pom.xml @@ -44,10 +44,6 @@ <version>${project.version}</version> </dependency> <dependency> - <groupId>javax.xml.bind</groupId> - <artifactId>jaxb-api</artifactId> - </dependency> - <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> 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 d638c9d3d..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 @@ -3,6 +3,7 @@ * * ================================================================================ * 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.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.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,8 +39,6 @@ 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 implements Serializable { private static final long serialVersionUID = 1L; @@ -53,46 +46,21 @@ public class AaiCqResponse implements Serializable { 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; - - // 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 - - // verify that we can create an 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 var responseObj = new JSONObject(jsonString); @@ -118,11 +86,10 @@ public class AaiCqResponse implements Serializable { // Create the StreamSource by creating StringReader using the // JSON input - var json = new StreamSource( - new StringReader(resultObject.getJSONObject("vserver").toString())); + var json = resultObject.getJSONObject("vserver").toString(); // Getting the vserver pojo again from the json - var vserver = this.getAaiObject(json, Vserver.class); + var vserver = gson.fromJson(json, Vserver.class); this.inventoryResponseItems.add(vserver); } } @@ -131,12 +98,10 @@ public class AaiCqResponse implements Serializable { if (resultObject.has(GENERIC_VNF)) { // Create the StreamSource by creating StringReader using the // JSON input - var json = new StreamSource( - new StringReader(resultObject.getJSONObject(GENERIC_VNF).toString())); + var json = resultObject.getJSONObject(GENERIC_VNF).toString(); // Getting the generic vnf pojo again from the json - var genericVnf = this.getAaiObject(json, GenericVnf.class); - + var genericVnf = gson.fromJson(json, GenericVnf.class); this.inventoryResponseItems.add(genericVnf); } } @@ -146,12 +111,10 @@ public class AaiCqResponse implements Serializable { // Create the StreamSource by creating StringReader using the // JSON input - var json = new StreamSource( - new StringReader(resultObject.getJSONObject("service-instance").toString())); + var json = resultObject.getJSONObject("service-instance").toString(); // Getting the employee pojo again from the json - var serviceInstance = this.getAaiObject(json, ServiceInstance.class); - + var serviceInstance = gson.fromJson(json, ServiceInstance.class); this.inventoryResponseItems.add(serviceInstance); } } @@ -160,12 +123,10 @@ public class AaiCqResponse implements Serializable { if (resultObject.has(VF_MODULE)) { // Create the StreamSource by creating StringReader using the // JSON input - var json = new StreamSource( - new StringReader(resultObject.getJSONObject(VF_MODULE).toString())); + var json = resultObject.getJSONObject(VF_MODULE).toString(); // Getting the vf module pojo again from the json - var vfModule = this.getAaiObject(json, VfModule.class); - + var vfModule = gson.fromJson(json, VfModule.class); this.inventoryResponseItems.add(vfModule); } } @@ -174,12 +135,10 @@ public class AaiCqResponse implements Serializable { if (resultObject.has("cloud-region")) { // Create the StreamSource by creating StringReader using the // JSON input - var json = new StreamSource( - new StringReader(resultObject.getJSONObject("cloud-region").toString())); + var json = resultObject.getJSONObject("cloud-region").toString(); // Getting the cloud region pojo again from the json - var cloudRegion = this.getAaiObject(json, CloudRegion.class); - + var cloudRegion = gson.fromJson(json, CloudRegion.class); this.inventoryResponseItems.add(cloudRegion); } } @@ -188,12 +147,10 @@ public class AaiCqResponse implements Serializable { if (resultObject.has("tenant")) { // Create the StreamSource by creating StringReader using the // JSON input - var json = new StreamSource( - new StringReader(resultObject.getJSONObject("tenant").toString())); + var json = resultObject.getJSONObject("tenant").toString(); // Getting the tenant pojo again from the json - var tenant = this.getAaiObject(json, Tenant.class); - + var tenant = gson.fromJson(json, Tenant.class); this.inventoryResponseItems.add(tenant); } } @@ -202,25 +159,14 @@ public class AaiCqResponse implements Serializable { if (resultObject.has("model-ver")) { // Create the StreamSource by creating StringReader using the // JSON input - var json = new StreamSource( - new StringReader(resultObject.getJSONObject("model-ver").toString())); + var json = resultObject.getJSONObject("model-ver").toString(); // Getting the ModelVer pojo again from the json - var modelVer = this.getAaiObject(json, ModelVer.class); - + var modelVer = gson.fromJson(json, ModelVer.class); this.inventoryResponseItems.add(modelVer); } } - private <T> T getAaiObject(StreamSource json, final Class<T> classOfResponse) { - try { - return jaxbContext.createUnmarshaller().unmarshal(json, classOfResponse).getValue(); - } catch (JAXBException e) { - LOGGER.error("JAXBCOntext error", e); - return null; - } - } - public List<Serializable> getInventoryResponseItems() { return inventoryResponseItems; } @@ -374,7 +320,7 @@ public class AaiCqResponse implements Serializable { // 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; } } @@ -608,12 +554,12 @@ public class AaiCqResponse implements Serializable { 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; } } diff --git a/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/XmlElementFieldNamingStrategy.java b/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/XmlElementFieldNamingStrategy.java new file mode 100644 index 000000000..fa1236283 --- /dev/null +++ b/models-interactions/model-impl/aai/src/main/java/org/onap/policy/aai/XmlElementFieldNamingStrategy.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.aai; + +import com.google.gson.FieldNamingStrategy; +import java.lang.reflect.Field; +import javax.xml.bind.annotation.XmlElement; + +public class XmlElementFieldNamingStrategy implements FieldNamingStrategy { + @Override + public String translateName(Field field) { + XmlElement annotatedFieldName = field.getAnnotation(XmlElement.class); + + if (annotatedFieldName != null) { + return annotatedFieldName.name(); + } else { + return field.getName(); + } + } +}
\ No newline at end of file |