2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 package org.onap.aai.util;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map.Entry;
29 import java.util.UUID;
31 import org.apache.commons.io.FileUtils;
33 import org.onap.aai.introspection.Introspector;
34 import org.onap.aai.introspection.Loader;
35 import org.onap.aai.introspection.LoaderFactory;
36 import org.onap.aai.introspection.ModelType;
37 import org.onap.aai.introspection.Version;
38 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
39 import org.onap.aai.util.AAIConfig;
40 import org.onap.aai.util.AAIConstants;
42 public class MakeNamedQuery {
44 public static void main(String[] args) throws Exception {
45 String _apiVersion = AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP);
46 String widgetJsonDir = null;
47 String modelVersion = null;
48 String namedQueryUuid = UUID.randomUUID().toString();
49 if (args.length > 0) {
50 if (args[0] != null) {
51 _apiVersion = args[0];
53 if (args[1] != null) {
54 widgetJsonDir = args[1];
56 if (args[2] != null) {
57 modelVersion = args[2];
59 if (args[3] != null) {
60 namedQueryUuid = args[3];
64 if (widgetJsonDir == null) {
65 System.err.println("You must specify a directory for widgetModelJson");
68 if (modelVersion == null) {
69 System.err.println("You must specify a modelVersion");
74 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.valueOf(_apiVersion));
76 // iterate the collection of resources
78 ArrayList<String> processedWidgets = new ArrayList<>();
81 HashMap<String, List<Introspector>> widgetToRelationship = new HashMap<String, List<Introspector>>();
82 for (Entry<String, Introspector> aaiResEnt : loader.getAllObjects().entrySet()) {
83 Introspector meObject = loader.introspectorFromName("model");
84 // no need for a ModelVers DynamicEntity
86 Introspector aaiRes = aaiResEnt.getValue();
88 if (!(aaiRes.isContainer() || aaiRes.getName().equals("aai-internal"))) {
89 String resource = aaiRes.getName();
91 if (processedWidgets.contains(resource)) {
94 processedWidgets.add(resource);
96 String widgetName = resource;
97 String filePathString = widgetJsonDir + "/" + widgetName + "-" + modelVersion + ".json";
98 File f = new File(filePathString);
100 System.out.println(f.toString());
101 String json = FileUtils.readFileToString(f);
103 meObject = loader.unmarshal("Model", json);
104 String modelInvariantId = meObject.getValue("model-invariant-id");
105 if (meObject.hasProperty("model-vers")) {
106 Introspector modelVers = meObject.getWrappedValue("model-vers");
107 List<Introspector> modelVerList = (List<Introspector>) modelVers.getWrappedListValue("model-ver");
108 for (Introspector modelVer : modelVerList) {
110 List<Introspector> relList = new ArrayList<Introspector>();
111 Introspector widgetRelationship = makeWidgetRelationship(loader, modelInvariantId,
112 modelVer.getValue("model-version-id").toString());
113 relList.add(widgetRelationship);
115 widgetToRelationship.put(widgetName, relList);
122 // esr-system-info-from-vnf=builder.store('x').union(\
123 // builder.newInstance().createEdgeTraversal(EdgeType.COUSIN, 'generic-vnf', 'vserver').store('x').union(\
124 // builder.newInstance().createEdgeTraversal(EdgeType.TREE, 'vserver', 'tenant').store('x')\
125 // .createEdgeTraversal(EdgeType.TREE, 'tenant', 'cloud-region').store('x')\
126 // .createEdgeTraversal(EdgeType.TREE, 'cloud-region', 'esr-system-info').store('x')\
127 // )).cap('x').unfold.dedup()
129 //source vnf-id, related service-instance-id, all related vnfs in this service-instance-id
131 //this should be abstracted and moved to a file
133 HashMap<String, List<Introspector>> relationshipMap = new HashMap<String, List<Introspector>>();
135 List<Introspector> genericVnfRelationship = widgetToRelationship.get("generic-vnf");
136 List<Introspector> vserverRelationship = widgetToRelationship.get("vserver");
137 List<Introspector> tenantRelationship = widgetToRelationship.get("tenant");
138 List<Introspector> cloudRegionRelationship = widgetToRelationship.get("cloud-region");
139 List<Introspector> esrSystemInfoRelationship = widgetToRelationship.get("esr-system-info");
141 Introspector namedQueryObj = loader.introspectorFromName("named-query");
142 namedQueryObj.setValue("named-query-uuid", namedQueryUuid);
143 namedQueryObj.setValue("named-query-name", "vnf-to-esr-system-info");
144 namedQueryObj.setValue("named-query-version", "1.0");
145 namedQueryObj.setValue("description", "Named Query - VNF to ESR System Info");
147 Introspector genericVnfNQE = setupNQElements(namedQueryObj, genericVnfRelationship);
149 Introspector vserverNQE = setupNQElements(genericVnfNQE, vserverRelationship);
151 Introspector tenantNQE = setupNQElements(vserverNQE, tenantRelationship);
153 Introspector cloudRegionNQE = setupNQElements(tenantNQE, cloudRegionRelationship);
155 Introspector esrSystemInfoNQE = setupNQElements(cloudRegionNQE, esrSystemInfoRelationship);
157 System.out.println(namedQueryObj.marshal(true));
162 private static List<Introspector> getRels(String widgetName, HashMap<String, Introspector> widgetToRelationship) {
163 List<Introspector> relList = new ArrayList<Introspector>();
164 Introspector genericVnfRelationship = widgetToRelationship.get(widgetName);
165 relList.add(genericVnfRelationship);
169 private static Introspector setupNQElements (Introspector nqeObj, List<Introspector> listOfRelationships) {
170 Introspector newNQElement = null;
172 Introspector newNQElements = null;
173 List<Object> nqElementList = null;
174 if (nqeObj.getWrappedValue("named-query-elements") != null) {
175 newNQElements = nqeObj.getWrappedValue("named-query-elements");
176 nqElementList = newNQElements.getValue("named-query-element");
178 newNQElements = nqeObj.newIntrospectorInstanceOfProperty("named-query-elements");
179 nqeObj.setValue("named-query-elements", newNQElements.getUnderlyingObject());
180 nqElementList = (List<Object>)newNQElements.getValue("named-query-element");
182 newNQElement = loadNQElement(newNQElements, listOfRelationships);
183 nqElementList.add(newNQElement.getUnderlyingObject());
185 } catch (AAIUnknownObjectException e) {
186 // TODO Auto-generated catch block
188 } catch (IllegalArgumentException e) {
189 // TODO Auto-generated catch block
195 private static Introspector loadNQElement (Introspector nqElements, List<Introspector> listOfRelationships) {
196 Introspector newNqElement = null;
198 newNqElement = nqElements.getLoader().introspectorFromName("named-query-element");
200 //newNqElement.setValue("named-query-element-uuid", UUID.randomUUID().toString());
202 Introspector newRelationshipList = newNqElement.getLoader().introspectorFromName("relationship-list");
203 newNqElement.setValue("relationship-list", newRelationshipList.getUnderlyingObject());
205 List<Object> newRelationshipListList = (List<Object>)newRelationshipList.getValue("relationship");
207 for (Introspector rel : listOfRelationships) {
208 newRelationshipListList.add(rel.getUnderlyingObject());
211 } catch (AAIUnknownObjectException e) {
212 // TODO Auto-generated catch block
214 } catch (IllegalArgumentException e) {
215 // TODO Auto-generated catch block
221 private static Introspector makeWidgetRelationship(Loader loader, String modelInvariantId, String modelVersionId) {
223 Introspector newRelationship = null;
225 newRelationship = loader.introspectorFromName("relationship");
227 List<Object> newRelationshipData = (List<Object>)newRelationship.getValue("relationship-data");
229 newRelationship.setValue("related-to", "model");
231 Introspector newRelationshipDatum1 = newRelationship.getLoader().introspectorFromName("relationship-data");
232 Introspector newRelationshipDatum2 = newRelationship.getLoader().introspectorFromName("relationship-data");
235 newRelationshipDatum1.setValue("relationship-key", "model.model-invariant-id");
236 newRelationshipDatum1.setValue("relationship-value", modelInvariantId);
238 //newRelationshipDatum2.setValue("relationship-key", "model-ver.model-version-id");
239 //newRelationshipDatum2.setValue("relationship-value", modelVersionId);
241 newRelationshipData.add(newRelationshipDatum1.getUnderlyingObject());
242 //newRelationshipData.add(newRelationshipDatum2.getUnderlyingObject());
243 } catch (AAIUnknownObjectException e) {
244 // TODO Auto-generated catch block
246 } catch (IllegalArgumentException e) {
247 // TODO Auto-generated catch block
251 return newRelationship;