From cdeda67931317caa0582b6e806aaf2df42616170 Mon Sep 17 00:00:00 2001 From: Bogumil Zebek Date: Thu, 26 Jul 2018 11:05:58 +0200 Subject: Fix potential null pointer places Change-Id: Icd4f2f6034bc8045f5530e89994f31985be5c079 Issue-ID: AAI-1424 Signed-off-by: bogumil_zebek --- .../java/org/onap/aai/util/MakeNamedQuery.java | 32 ++++++++++++++-------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'aai-traversal') diff --git a/aai-traversal/src/main/java/org/onap/aai/util/MakeNamedQuery.java b/aai-traversal/src/main/java/org/onap/aai/util/MakeNamedQuery.java index 136b610..68d204f 100644 --- a/aai-traversal/src/main/java/org/onap/aai/util/MakeNamedQuery.java +++ b/aai-traversal/src/main/java/org/onap/aai/util/MakeNamedQuery.java @@ -24,6 +24,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; +import java.util.Optional; import java.util.UUID; import org.apache.commons.io.FileUtils; @@ -34,8 +35,6 @@ import org.onap.aai.introspection.LoaderFactory; import org.onap.aai.introspection.ModelType; import org.onap.aai.introspection.Version; import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; -import org.onap.aai.util.AAIConfig; -import org.onap.aai.util.AAIConstants; public class MakeNamedQuery { @@ -142,21 +141,30 @@ public class MakeNamedQuery { namedQueryObj.setValue("named-query-version", "1.0"); namedQueryObj.setValue("description", "Named Query - VNF to ESR System Info"); - Introspector genericVnfNQE = setupNQElements(namedQueryObj, genericVnfRelationship); + Optional genericVnfNQE = tryToSetUpNQElements(Optional.of(namedQueryObj), genericVnfRelationship); - Introspector vserverNQE = setupNQElements(genericVnfNQE, vserverRelationship); + Optional vserverNQE = tryToSetUpNQElements(genericVnfNQE, vserverRelationship); - Introspector tenantNQE = setupNQElements(vserverNQE, tenantRelationship); + Optional tenantNQE = tryToSetUpNQElements(vserverNQE, tenantRelationship); - Introspector cloudRegionNQE = setupNQElements(tenantNQE, cloudRegionRelationship); + Optional cloudRegionNQE = tryToSetUpNQElements(tenantNQE, cloudRegionRelationship); - Introspector esrSystemInfoNQE = setupNQElements(cloudRegionNQE, esrSystemInfoRelationship); + Optional esrSystemInfoNQE = tryToSetUpNQElements(cloudRegionNQE, esrSystemInfoRelationship); System.out.println(namedQueryObj.marshal(true)); System.exit(0); - } + } + + private static Optional tryToSetUpNQElements(Optional genericVnfNQE, List vserverRelationship) { + if(genericVnfNQE.isPresent()) { + return Optional.ofNullable(setupNQElements(genericVnfNQE.get(), vserverRelationship)); + } else { + return Optional.empty(); + } + } + private static List getRels(String widgetName, HashMap widgetToRelationship) { List relList = new ArrayList(); Introspector genericVnfRelationship = widgetToRelationship.get(widgetName); @@ -172,14 +180,16 @@ public class MakeNamedQuery { if (nqeObj.getWrappedValue("named-query-elements") != null) { newNQElements = nqeObj.getWrappedValue("named-query-elements"); nqElementList = newNQElements.getValue("named-query-element"); - } else { + } else { newNQElements = nqeObj.newIntrospectorInstanceOfProperty("named-query-elements"); nqeObj.setValue("named-query-elements", newNQElements.getUnderlyingObject()); nqElementList = newNQElements.getValue("named-query-element"); } newNQElement = loadNQElement(newNQElements, listOfRelationships); - nqElementList.add(newNQElement.getUnderlyingObject()); - + if (newNQElement != null) { + nqElementList.add(newNQElement.getUnderlyingObject()); + } + } catch (AAIUnknownObjectException e) { // TODO Auto-generated catch block e.printStackTrace(); -- cgit 1.2.3-korg