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 com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 import org.apache.commons.io.FileUtils;
32 import org.onap.aai.config.SpringContextAware;
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.exceptions.AAIUnknownObjectException;
38 import org.onap.aai.setup.SchemaVersion;
39 import org.onap.aai.setup.SchemaVersions;
40 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
42 public class MakeNamedQuery {
44 private static final EELFLogger logger = EELFManager.getInstance().getLogger(MakeNamedQuery.class.getName());
46 public static void main(String[] args) throws Exception {
47 String _apiVersion = AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP);
48 String widgetJsonDir = null;
49 String modelVersion = null;
50 String namedQueryUuid = UUID.randomUUID().toString();
51 if (args.length > 0) {
52 if (args[0] != null) {
53 _apiVersion = args[0];
55 if (args[1] != null) {
56 widgetJsonDir = args[1];
58 if (args[2] != null) {
59 modelVersion = args[2];
61 if (args[3] != null) {
62 namedQueryUuid = args[3];
66 if (widgetJsonDir == null) {
67 System.err.println("You must specify a directory for widgetModelJson");
70 if (modelVersion == null) {
71 System.err.println("You must specify a modelVersion");
75 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
76 "org.onap.aai.config",
80 LoaderFactory loaderFactory = ctx.getBean(LoaderFactory.class);
81 SchemaVersions schemaVersions = ctx.getBean(SchemaVersions.class);
83 if(schemaVersions.getVersions().contains(_apiVersion)){
85 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, new SchemaVersion(_apiVersion));
87 // iterate the collection of resources
89 ArrayList<String> processedWidgets = new ArrayList<>();
92 HashMap<String, List<Introspector>> widgetToRelationship = new HashMap<>();
93 for (Entry<String, Introspector> aaiResEnt : loader.getAllObjects().entrySet()) {
94 Introspector meObject = loader.introspectorFromName("model");
95 // no need for a ModelVers DynamicEntity
97 Introspector aaiRes = aaiResEnt.getValue();
99 if (!(aaiRes.isContainer() || aaiRes.getName().equals("aai-internal"))) {
100 String resource = aaiRes.getName();
102 if (processedWidgets.contains(resource)) {
105 processedWidgets.add(resource);
107 String widgetName = resource;
108 String filePathString = widgetJsonDir + "/" + widgetName + "-" + modelVersion + ".json";
109 File f = new File(filePathString);
111 System.out.println(f.toString());
112 String json = FileUtils.readFileToString(f);
114 meObject = loader.unmarshal("Model", json);
115 String modelInvariantId = meObject.getValue("model-invariant-id");
116 if (meObject.hasProperty("model-vers")) {
117 Introspector modelVers = meObject.getWrappedValue("model-vers");
118 List<Introspector> modelVerList = (List<Introspector>) modelVers.getWrappedListValue("model-ver");
119 for (Introspector modelVer : modelVerList) {
121 List<Introspector> relList = new ArrayList<Introspector>();
122 Introspector widgetRelationship = makeWidgetRelationship(loader, modelInvariantId,
123 modelVer.getValue("model-version-id").toString());
124 relList.add(widgetRelationship);
126 widgetToRelationship.put(widgetName, relList);
133 //source vnf-id, related service-instance-id, all related vnfs in this service-instance-id
134 //this should be abstracted and moved to a file
136 HashMap<String, List<Introspector>> relationshipMap = new HashMap<String, List<Introspector>>();
138 List<Introspector> genericVnfRelationship = widgetToRelationship.get("generic-vnf");
139 List<Introspector> vserverRelationship = widgetToRelationship.get("vserver");
140 List<Introspector> tenantRelationship = widgetToRelationship.get("tenant");
141 List<Introspector> cloudRegionRelationship = widgetToRelationship.get("cloud-region");
142 List<Introspector> esrSystemInfoRelationship = widgetToRelationship.get("esr-system-info");
144 Introspector namedQueryObj = loader.introspectorFromName("named-query");
145 namedQueryObj.setValue("named-query-uuid", namedQueryUuid);
146 namedQueryObj.setValue("named-query-name", "vnf-to-esr-system-info");
147 namedQueryObj.setValue("named-query-version", "1.0");
148 namedQueryObj.setValue("description", "Named Query - VNF to ESR System Info");
150 Introspector genericVnfNQE = setupNQElements(namedQueryObj, genericVnfRelationship);
152 Introspector vserverNQE = setupNQElements(genericVnfNQE, vserverRelationship);
154 Introspector tenantNQE = setupNQElements(vserverNQE, tenantRelationship);
156 Introspector cloudRegionNQE = setupNQElements(tenantNQE, cloudRegionRelationship);
158 Introspector esrSystemInfoNQE = setupNQElements(cloudRegionNQE, esrSystemInfoRelationship);
160 System.out.println(namedQueryObj.marshal(true));
167 private static List<Introspector> getRels(String widgetName, HashMap<String, Introspector> widgetToRelationship) {
168 List<Introspector> relList = new ArrayList<Introspector>();
169 Introspector genericVnfRelationship = widgetToRelationship.get(widgetName);
170 relList.add(genericVnfRelationship);
174 private static Introspector setupNQElements (Introspector nqeObj, List<Introspector> listOfRelationships) {
175 Introspector newNQElement = null;
177 Introspector newNQElements = null;
178 List<Object> nqElementList = null;
179 if (nqeObj.getWrappedValue("named-query-elements") != null) {
180 newNQElements = nqeObj.getWrappedValue("named-query-elements");
181 nqElementList = newNQElements.getValue("named-query-element");
183 newNQElements = nqeObj.newIntrospectorInstanceOfProperty("named-query-elements");
184 nqeObj.setValue("named-query-elements", newNQElements.getUnderlyingObject());
185 nqElementList = (List<Object>)newNQElements.getValue("named-query-element");
187 newNQElement = loadNQElement(newNQElements, listOfRelationships);
188 nqElementList.add(newNQElement.getUnderlyingObject());
190 } catch (AAIUnknownObjectException e) {
191 logger.info("AAIUnknownObjectException in MakeNamedQuery.setupNQElements() "+e);
193 } catch (IllegalArgumentException e) {
194 logger.info("IllegalArgumentException in MakeNamedQuery.setupNQElements() "+e);
200 private static Introspector loadNQElement (Introspector nqElements, List<Introspector> listOfRelationships) {
201 Introspector newNqElement = null;
203 newNqElement = nqElements.getLoader().introspectorFromName("named-query-element");
205 //newNqElement.setValue("named-query-element-uuid", UUID.randomUUID().toString());
207 Introspector newRelationshipList = newNqElement.getLoader().introspectorFromName("relationship-list");
208 newNqElement.setValue("relationship-list", newRelationshipList.getUnderlyingObject());
210 List<Object> newRelationshipListList = (List<Object>)newRelationshipList.getValue("relationship");
212 for (Introspector rel : listOfRelationships) {
213 newRelationshipListList.add(rel.getUnderlyingObject());
216 } catch (AAIUnknownObjectException e) {
217 logger.info("AAIUnknownObjectException in MakeNamedQuery.loadNQElement() "+e);
219 } catch (IllegalArgumentException e) {
220 logger.info("IllegalArgumentException in MakeNamedQuery.loadNQElement() "+e);
226 private static Introspector makeWidgetRelationship(Loader loader, String modelInvariantId, String modelVersionId) {
228 Introspector newRelationship = null;
230 newRelationship = loader.introspectorFromName("relationship");
232 List<Object> newRelationshipData = (List<Object>)newRelationship.getValue("relationship-data");
234 newRelationship.setValue("related-to", "model");
236 Introspector newRelationshipDatum1 = newRelationship.getLoader().introspectorFromName("relationship-data");
237 Introspector newRelationshipDatum2 = newRelationship.getLoader().introspectorFromName("relationship-data");
240 newRelationshipDatum1.setValue("relationship-key", "model.model-invariant-id");
241 newRelationshipDatum1.setValue("relationship-value", modelInvariantId);
243 //newRelationshipDatum2.setValue("relationship-key", "model-ver.model-version-id");
244 //newRelationshipDatum2.setValue("relationship-value", modelVersionId);
246 newRelationshipData.add(newRelationshipDatum1.getUnderlyingObject());
247 //newRelationshipData.add(newRelationshipDatum2.getUnderlyingObject());
248 } catch (AAIUnknownObjectException e) {
249 logger.info("AAIUnknownObjectException in MakeNamedQuery.makeWidgetRelationship() "+e);
251 } catch (IllegalArgumentException e) {
252 logger.info("IllegalArgumentException in MakeNamedQuery.makeWidgetRelationship() "+e);
256 return newRelationship;