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=========================================================
21 package org.onap.aai.ingestModel;
24 import java.io.PrintWriter;
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Map.Entry;
29 import java.util.UUID;
31 import javax.xml.transform.stream.StreamSource;
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.setup.SchemaVersion;
38 import org.onap.aai.util.AAIConfig;
39 import org.onap.aai.util.AAIConstants;
40 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
43 * The Class CreateWidgetModels.
45 public class CreateWidgetModels {
49 * @param args the arguments
50 * @throws Exception the exception
52 public static void main(String[] args) throws Exception {
54 String _apiVersion = AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP);
55 String widgetJsonDir = null;
56 String modelVersion = null;
57 if (args.length > 0) {
58 if (args[0] != null) {
59 _apiVersion = args[0];
61 if (args[1] != null) {
62 widgetJsonDir = args[1];
64 if (args[2] != null) {
65 modelVersion = args[2];
69 if (widgetJsonDir == null) {
70 System.err.println("You must specify a directory for widgetModelJson");
73 if (modelVersion == null) {
74 System.err.println("You must specify a modelVersion");
78 try (AnnotationConfigApplicationContext ctx =
79 new AnnotationConfigApplicationContext("org.onap.aai.config", "org.onap.aai.setup")) {
80 LoaderFactory loaderFactory = ctx.getBean(LoaderFactory.class);
81 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, new SchemaVersion(_apiVersion));
83 // iterate the collection of resources
85 ArrayList<String> processedWidgets = new ArrayList<String>();
86 for (Entry<String, Introspector> aaiResEnt : loader.getAllObjects().entrySet()) {
87 Introspector meObject = loader.introspectorFromName("model");
88 // no need for a ModelVers DynamicEntity
90 Introspector aaiRes = aaiResEnt.getValue();
92 if (!(aaiRes.isContainer() || aaiRes.getName().equals("aai-internal"))) {
93 String resource = aaiRes.getName();
95 if (processedWidgets.contains(resource)) {
99 Set<String> introspectorProperties = aaiRes.getProperties();
101 if (!(introspectorProperties.contains("model-version-id")
102 && introspectorProperties.contains("model-invariant-id"))) {
103 System.out.println(aaiRes.getDbName() + " does not contain model properties so skipping");
105 processedWidgets.add(resource);
107 String widgetName = resource;
108 String filePathString = widgetJsonDir + "/" + widgetName + "-" + modelVersion + ".json";
109 File f = new File(filePathString);
111 String filePathString2 =
112 widgetJsonDir + "/../widget-model-json-old/" + widgetName + "-" + modelVersion + ".json";
113 File f2 = new File(filePathString2);
115 if (!f.exists() && !f.isDirectory()) {
118 System.out.println("Using old file for " + resource + ".");
120 meObject = loader.unmarshal("model", new StreamSource(f2).getReader().toString());
121 // override, some of them are wrong
122 meObject.setValue("model-version", modelVersion);
124 System.out.println("Making new file for " + resource + ".");
125 meObject.setValue("model-invariant-id", UUID.randomUUID().toString());
126 meObject.setValue("model-type", "widget");
127 Introspector mevObject = loader.introspectorFromName("model-ver");
128 Introspector mevsObject = loader.introspectorFromName("model-vers");
129 mevObject.setValue("model-version-id", UUID.randomUUID().toString());
130 mevObject.setValue("model-version", modelVersion);
131 mevObject.setValue("model-Name", widgetName);
132 // make a list of dynamic Entities
133 List<Object> mevsList = new ArrayList<>();
134 // add this one, it will be the only one in the list in this case
135 mevsList.add(mevObject.getUnderlyingObject());
136 mevsObject.setValue("model-ver", mevsList);
137 // Have to figure out how to add my mev object to the mevsObject,
138 // the modelVers is a list of dynamic entities so we can just attach the array here
139 meObject.setValue("model-vers", mevsObject.getUnderlyingObject());
142 // put it out as JSON
144 PrintWriter out = new PrintWriter(f);
145 out.println(meObject.marshal(true));
149 System.out.println("File already exists for " + resource + ". Skipping.");