From e5d32da735e111adb84269e9e65335831b93a801 Mon Sep 17 00:00:00 2001 From: jimmy Date: Tue, 29 Aug 2017 11:19:40 -0400 Subject: Built named-query for policy needs Issue-ID: AAI-220 Change-Id: I3dc8e8b88062328b8226d9b2fa3841e125b98851 Signed-off-by: jimmy --- .../vnf-to-service-instance-1.0.json | 45 ++++ .../org/openecomp/aai/util/MakeNamedQuery.java | 233 +++++++++++++++++++++ 2 files changed, 278 insertions(+) create mode 100644 aai-traversal/bundleconfig-local/etc/scriptdata/named-query-json/vnf-to-service-instance-1.0.json create mode 100644 aai-traversal/src/main/java/org/openecomp/aai/util/MakeNamedQuery.java diff --git a/aai-traversal/bundleconfig-local/etc/scriptdata/named-query-json/vnf-to-service-instance-1.0.json b/aai-traversal/bundleconfig-local/etc/scriptdata/named-query-json/vnf-to-service-instance-1.0.json new file mode 100644 index 0000000..53e6f26 --- /dev/null +++ b/aai-traversal/bundleconfig-local/etc/scriptdata/named-query-json/vnf-to-service-instance-1.0.json @@ -0,0 +1,45 @@ +{ + "named-query-uuid" : "a93ac487-409c-4e8c-9e5f-334ae8f99087", + "named-query-name" : "vnf-to-service-instance", + "named-query-version" : "1.0", + "description" : "Named Query - VNF to Service Instance", + "named-query-elements" : { + "named-query-element" : [ { + "named-query-elements" : { + "named-query-element" : [ { + "named-query-elements" : { + "named-query-element" : [ { + "relationship-list" : { + "relationship" : [ { + "related-to" : "model", + "relationship-data" : [ { + "relationship-key" : "model.model-invariant-id", + "relationship-value" : "acc6edd8-a8d4-4b93-afaa-0994068be14c" + } ] + } ] + } + } ] + }, + "relationship-list" : { + "relationship" : [ { + "related-to" : "model", + "relationship-data" : [ { + "relationship-key" : "model.model-invariant-id", + "relationship-value" : "82194af1-3c2c-485a-8f44-420e22a9eaa4" + } ] + } ] + } + } ] + }, + "relationship-list" : { + "relationship" : [ { + "related-to" : "model", + "relationship-data" : [ { + "relationship-key" : "model.model-invariant-id", + "relationship-value" : "acc6edd8-a8d4-4b93-afaa-0994068be14c" + } ] + } ] + } + } ] + } +} \ No newline at end of file diff --git a/aai-traversal/src/main/java/org/openecomp/aai/util/MakeNamedQuery.java b/aai-traversal/src/main/java/org/openecomp/aai/util/MakeNamedQuery.java new file mode 100644 index 0000000..3f984a3 --- /dev/null +++ b/aai-traversal/src/main/java/org/openecomp/aai/util/MakeNamedQuery.java @@ -0,0 +1,233 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.aai.util; +import java.io.File; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map.Entry; +import java.util.UUID; + +import org.apache.commons.io.FileUtils; + +import org.openecomp.aai.introspection.Introspector; +import org.openecomp.aai.introspection.Loader; +import org.openecomp.aai.introspection.LoaderFactory; +import org.openecomp.aai.introspection.ModelType; +import org.openecomp.aai.introspection.Version; +import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException; +import org.openecomp.aai.util.AAIConfig; +import org.openecomp.aai.util.AAIConstants; + +public class MakeNamedQuery { + + public static void main(String[] args) throws Exception { + String _apiVersion = AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP); + String widgetJsonDir = null; + String modelVersion = null; + String namedQueryUuid = UUID.randomUUID().toString(); + if (args.length > 0) { + if (args[0] != null) { + _apiVersion = args[0]; + } + if (args[1] != null) { + widgetJsonDir = args[1]; + } + if (args[2] != null) { + modelVersion = args[2]; + } + if (args[3] != null) { + namedQueryUuid = args[3]; + } + } + + if (widgetJsonDir == null) { + System.err.println("You must specify a directory for widgetModelJson"); + System.exit(0); + } + if (modelVersion == null) { + System.err.println("You must specify a modelVersion"); + System.exit(0); + } + + + Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.valueOf(_apiVersion)); + + // iterate the collection of resources + + ArrayList processedWidgets = new ArrayList(); + + + HashMap widgetToRelationship = new HashMap(); + for (Entry aaiResEnt : loader.getAllObjects().entrySet()) { + Introspector meObject = loader.introspectorFromName("model"); + // no need for a ModelVers DynamicEntity + + Introspector aaiRes = aaiResEnt.getValue(); + + if (!(aaiRes.isContainer() || aaiRes.getName().equals("aai-internal"))) { + String resource = aaiRes.getName(); + + if (processedWidgets.contains(resource)) { + continue; + } + processedWidgets.add(resource); + + String widgetName = resource; + String filePathString = widgetJsonDir + "/" + widgetName + "-" + modelVersion + ".json"; + File f = new File(filePathString); + if (f.exists()) { + System.out.println(f.toString()); + String json = FileUtils.readFileToString(f); + + meObject = loader.unmarshal("Model", json); + String modelInvariantId = meObject.getValue("model-invariant-id"); + if (meObject.hasProperty("model-vers")) { + Introspector modelVers = meObject.getWrappedValue("model-vers"); + List modelVerList = (List) modelVers.getWrappedListValue("model-ver"); + for (Introspector modelVer : modelVerList) { + widgetToRelationship.put(widgetName, makeWidgetRelationship(loader, modelInvariantId, + modelVer.getValue("model-version-id").toString())); + } + } + } + } + } + //source vnf-id, related service-instance-id, all related vnfs in this service-instance-id + Introspector genericVnfRelationship = widgetToRelationship.get("generic-vnf"); + Introspector serviceInstanceRelationship = widgetToRelationship.get("service-instance"); + Introspector vceRelationship = widgetToRelationship.get("vce"); + + Introspector namedQueryObj = loader.introspectorFromName("named-query"); + namedQueryObj.setValue("named-query-uuid", namedQueryUuid); + namedQueryObj.setValue("named-query-name", "vnf-to-service-instance"); + namedQueryObj.setValue("named-query-version", "1.0"); + namedQueryObj.setValue("description", "Named Query - VNF to Service Instance"); + + List genericVnfRels = new ArrayList(); + genericVnfRels.add(genericVnfRelationship); + List serviceInstanceRels = new ArrayList(); + serviceInstanceRels.add(serviceInstanceRelationship); + List vceRels = new ArrayList(); + vceRels.add(vceRelationship); + + + Introspector genericVnfNQE = setupNQElements(namedQueryObj, genericVnfRels); + + //Introspector vceNQE2 = setupNQElements(namedQueryObj, vceRels); + + Introspector serviceInstanceNQE = setupNQElements(genericVnfNQE, serviceInstanceRels); + + Introspector newGenericVnfNQE = setupNQElements(serviceInstanceNQE, genericVnfRels); + + System.out.println(namedQueryObj.marshal(true)); + + System.exit(0); + + } + + private static Introspector setupNQElements (Introspector nqeObj, List listOfRelationships) { + Introspector newNQElement = null; + try { + Introspector newNQElements = null; + List nqElementList = null; + if (nqeObj.getWrappedValue("named-query-elements") != null) { + newNQElements = nqeObj.getWrappedValue("named-query-elements"); + nqElementList = newNQElements.getValue("named-query-element"); + } else { + newNQElements = nqeObj.newIntrospectorInstanceOfProperty("named-query-elements"); + nqeObj.setValue("named-query-elements", newNQElements.getUnderlyingObject()); + nqElementList = (List)newNQElements.getValue("named-query-element"); + } + newNQElement = loadNQElement(newNQElements, listOfRelationships); + nqElementList.add(newNQElement.getUnderlyingObject()); + + } catch (AAIUnknownObjectException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return newNQElement; + } + + private static Introspector loadNQElement (Introspector nqElements, List listOfRelationships) { + Introspector newNqElement = null; + try { + newNqElement = nqElements.getLoader().introspectorFromName("named-query-element"); + + //newNqElement.setValue("named-query-element-uuid", UUID.randomUUID().toString()); + + Introspector newRelationshipList = newNqElement.getLoader().introspectorFromName("relationship-list"); + newNqElement.setValue("relationship-list", newRelationshipList.getUnderlyingObject()); + + List newRelationshipListList = (List)newRelationshipList.getValue("relationship"); + + for (Introspector rel : listOfRelationships) { + newRelationshipListList.add(rel.getUnderlyingObject()); + } + + } catch (AAIUnknownObjectException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return newNqElement; + + } + private static Introspector makeWidgetRelationship(Loader loader, String modelInvariantId, String modelVersionId) { + + Introspector newRelationship = null; + try { + newRelationship = loader.introspectorFromName("relationship"); + + List newRelationshipData = (List)newRelationship.getValue("relationship-data"); + + newRelationship.setValue("related-to", "model"); + + Introspector newRelationshipDatum1 = newRelationship.getLoader().introspectorFromName("relationship-data"); + Introspector newRelationshipDatum2 = newRelationship.getLoader().introspectorFromName("relationship-data"); + + + newRelationshipDatum1.setValue("relationship-key", "model.model-invariant-id"); + newRelationshipDatum1.setValue("relationship-value", modelInvariantId); + + //newRelationshipDatum2.setValue("relationship-key", "model-ver.model-version-id"); + //newRelationshipDatum2.setValue("relationship-value", modelVersionId); + + newRelationshipData.add(newRelationshipDatum1.getUnderlyingObject()); + //newRelationshipData.add(newRelationshipDatum2.getUnderlyingObject()); + } catch (AAIUnknownObjectException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + return newRelationship; + } + +} \ No newline at end of file -- cgit 1.2.3-korg