2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 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 package org.onap.aai.util;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map.Entry;
27 import java.util.UUID;
29 import org.apache.commons.io.FileUtils;
31 import org.onap.aai.introspection.Introspector;
32 import org.onap.aai.introspection.Loader;
33 import org.onap.aai.introspection.LoaderFactory;
34 import org.onap.aai.introspection.ModelType;
35 import org.onap.aai.introspection.Version;
36 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
37 import org.onap.aai.util.AAIConfig;
38 import org.onap.aai.util.AAIConstants;
40 public class MakeNamedQuery {
42 public static void main(String[] args) throws Exception {
43 String _apiVersion = AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP);
44 String widgetJsonDir = null;
45 String modelVersion = null;
46 String namedQueryUuid = UUID.randomUUID().toString();
47 if (args.length > 0) {
48 if (args[0] != null) {
49 _apiVersion = args[0];
51 if (args[1] != null) {
52 widgetJsonDir = args[1];
54 if (args[2] != null) {
55 modelVersion = args[2];
57 if (args[3] != null) {
58 namedQueryUuid = args[3];
62 if (widgetJsonDir == null) {
63 System.err.println("You must specify a directory for widgetModelJson");
66 if (modelVersion == null) {
67 System.err.println("You must specify a modelVersion");
72 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.valueOf(_apiVersion));
74 // iterate the collection of resources
76 ArrayList<String> processedWidgets = new ArrayList<>();
79 HashMap<String, List<Introspector>> widgetToRelationship = new HashMap<String, List<Introspector>>();
80 for (Entry<String, Introspector> aaiResEnt : loader.getAllObjects().entrySet()) {
81 Introspector meObject = loader.introspectorFromName("model");
82 // no need for a ModelVers DynamicEntity
84 Introspector aaiRes = aaiResEnt.getValue();
86 if (!(aaiRes.isContainer() || aaiRes.getName().equals("aai-internal"))) {
87 String resource = aaiRes.getName();
89 if (processedWidgets.contains(resource)) {
92 processedWidgets.add(resource);
94 String widgetName = resource;
95 String filePathString = widgetJsonDir + "/" + widgetName + "-" + modelVersion + ".json";
96 File f = new File(filePathString);
98 System.out.println(f.toString());
99 String json = FileUtils.readFileToString(f);
101 meObject = loader.unmarshal("Model", json);
102 String modelInvariantId = meObject.getValue("model-invariant-id");
103 if (meObject.hasProperty("model-vers")) {
104 Introspector modelVers = meObject.getWrappedValue("model-vers");
105 List<Introspector> modelVerList = (List<Introspector>) modelVers.getWrappedListValue("model-ver");
106 for (Introspector modelVer : modelVerList) {
108 List<Introspector> relList = new ArrayList<Introspector>();
109 Introspector widgetRelationship = makeWidgetRelationship(loader, modelInvariantId,
110 modelVer.getValue("model-version-id").toString());
111 relList.add(widgetRelationship);
113 widgetToRelationship.put(widgetName, relList);
120 // esr-system-info-from-vnf=builder.store('x').union(\
121 // builder.newInstance().createEdgeTraversal(EdgeType.COUSIN, 'generic-vnf', 'vserver').store('x').union(\
122 // builder.newInstance().createEdgeTraversal(EdgeType.TREE, 'vserver', 'tenant').store('x')\
123 // .createEdgeTraversal(EdgeType.TREE, 'tenant', 'cloud-region').store('x')\
124 // .createEdgeTraversal(EdgeType.TREE, 'cloud-region', 'esr-system-info').store('x')\
125 // )).cap('x').unfold.dedup()
127 //source vnf-id, related service-instance-id, all related vnfs in this service-instance-id
129 //this should be abstracted and moved to a file
131 HashMap<String, List<Introspector>> relationshipMap = new HashMap<String, List<Introspector>>();
133 List<Introspector> genericVnfRelationship = widgetToRelationship.get("generic-vnf");
134 List<Introspector> vserverRelationship = widgetToRelationship.get("vserver");
135 List<Introspector> tenantRelationship = widgetToRelationship.get("tenant");
136 List<Introspector> cloudRegionRelationship = widgetToRelationship.get("cloud-region");
137 List<Introspector> esrSystemInfoRelationship = widgetToRelationship.get("esr-system-info");
139 Introspector namedQueryObj = loader.introspectorFromName("named-query");
140 namedQueryObj.setValue("named-query-uuid", namedQueryUuid);
141 namedQueryObj.setValue("named-query-name", "vnf-to-esr-system-info");
142 namedQueryObj.setValue("named-query-version", "1.0");
143 namedQueryObj.setValue("description", "Named Query - VNF to ESR System Info");
145 Introspector genericVnfNQE = setupNQElements(namedQueryObj, genericVnfRelationship);
147 Introspector vserverNQE = setupNQElements(genericVnfNQE, vserverRelationship);
149 Introspector tenantNQE = setupNQElements(vserverNQE, tenantRelationship);
151 Introspector cloudRegionNQE = setupNQElements(tenantNQE, cloudRegionRelationship);
153 Introspector esrSystemInfoNQE = setupNQElements(cloudRegionNQE, esrSystemInfoRelationship);
155 System.out.println(namedQueryObj.marshal(true));
160 private static List<Introspector> getRels(String widgetName, HashMap<String, Introspector> widgetToRelationship) {
161 List<Introspector> relList = new ArrayList<Introspector>();
162 Introspector genericVnfRelationship = widgetToRelationship.get(widgetName);
163 relList.add(genericVnfRelationship);
167 private static Introspector setupNQElements (Introspector nqeObj, List<Introspector> listOfRelationships) {
168 Introspector newNQElement = null;
170 Introspector newNQElements = null;
171 List<Object> nqElementList = null;
172 if (nqeObj.getWrappedValue("named-query-elements") != null) {
173 newNQElements = nqeObj.getWrappedValue("named-query-elements");
174 nqElementList = newNQElements.getValue("named-query-element");
176 newNQElements = nqeObj.newIntrospectorInstanceOfProperty("named-query-elements");
177 nqeObj.setValue("named-query-elements", newNQElements.getUnderlyingObject());
178 nqElementList = (List<Object>)newNQElements.getValue("named-query-element");
180 newNQElement = loadNQElement(newNQElements, listOfRelationships);
181 nqElementList.add(newNQElement.getUnderlyingObject());
183 } catch (AAIUnknownObjectException e) {
184 // TODO Auto-generated catch block
186 } catch (IllegalArgumentException e) {
187 // TODO Auto-generated catch block
193 private static Introspector loadNQElement (Introspector nqElements, List<Introspector> listOfRelationships) {
194 Introspector newNqElement = null;
196 newNqElement = nqElements.getLoader().introspectorFromName("named-query-element");
198 //newNqElement.setValue("named-query-element-uuid", UUID.randomUUID().toString());
200 Introspector newRelationshipList = newNqElement.getLoader().introspectorFromName("relationship-list");
201 newNqElement.setValue("relationship-list", newRelationshipList.getUnderlyingObject());
203 List<Object> newRelationshipListList = (List<Object>)newRelationshipList.getValue("relationship");
205 for (Introspector rel : listOfRelationships) {
206 newRelationshipListList.add(rel.getUnderlyingObject());
209 } catch (AAIUnknownObjectException e) {
210 // TODO Auto-generated catch block
212 } catch (IllegalArgumentException e) {
213 // TODO Auto-generated catch block
219 private static Introspector makeWidgetRelationship(Loader loader, String modelInvariantId, String modelVersionId) {
221 Introspector newRelationship = null;
223 newRelationship = loader.introspectorFromName("relationship");
225 List<Object> newRelationshipData = (List<Object>)newRelationship.getValue("relationship-data");
227 newRelationship.setValue("related-to", "model");
229 Introspector newRelationshipDatum1 = newRelationship.getLoader().introspectorFromName("relationship-data");
230 Introspector newRelationshipDatum2 = newRelationship.getLoader().introspectorFromName("relationship-data");
233 newRelationshipDatum1.setValue("relationship-key", "model.model-invariant-id");
234 newRelationshipDatum1.setValue("relationship-value", modelInvariantId);
236 //newRelationshipDatum2.setValue("relationship-key", "model-ver.model-version-id");
237 //newRelationshipDatum2.setValue("relationship-value", modelVersionId);
239 newRelationshipData.add(newRelationshipDatum1.getUnderlyingObject());
240 //newRelationshipData.add(newRelationshipDatum2.getUnderlyingObject());
241 } catch (AAIUnknownObjectException e) {
242 // TODO Auto-generated catch block
244 } catch (IllegalArgumentException e) {
245 // TODO Auto-generated catch block
249 return newRelationship;