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.ingestModel;
23 import java.io.PrintWriter;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Map.Entry;
27 import java.util.UUID;
29 import javax.xml.transform.stream.StreamSource;
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.util.AAIConfig;
37 import org.onap.aai.util.AAIConstants;
40 * The Class CreateWidgetModels.
42 public class CreateWidgetModels
47 * @param args the arguments
48 * @throws Exception the exception
50 public static void main(String[] args) throws Exception {
52 String _apiVersion = AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP);
53 String widgetJsonDir = null;
54 String modelVersion = null;
55 if (args.length > 0) {
56 if (args[0] != null) {
57 _apiVersion = args[0];
59 if (args[1] != null) {
60 widgetJsonDir = args[1];
62 if (args[2] != null) {
63 modelVersion = args[2];
67 if (widgetJsonDir == null) {
68 System.err.println("You must specify a directory for widgetModelJson");
71 if (modelVersion == null) {
72 System.err.println("You must specify a modelVersion");
77 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.valueOf(_apiVersion));
79 // iterate the collection of resources
81 ArrayList<String> processedWidgets = new ArrayList<String>();
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 String filePathString2 = widgetJsonDir + "/../widget-model-json-old/" + widgetName + "-" + modelVersion + ".json";
101 File f2 = new File(filePathString2);
103 if(!f.exists() && !f.isDirectory()) {
106 System.out.println("Using old file for " + resource + ".");
108 meObject = loader.unmarshal("model", new StreamSource(f2).getReader().toString());
109 // override, some of them are wrong
110 meObject.setValue("model-version", modelVersion);
112 System.out.println("Making new file for " + resource + ".");
113 meObject.setValue("model-invariant-id", UUID.randomUUID().toString());
114 meObject.setValue("model-type", "widget");
115 Introspector mevObject = loader.introspectorFromName("model-ver");
116 Introspector mevsObject = loader.introspectorFromName("model-vers");
117 mevObject.setValue("model-version-id", UUID.randomUUID().toString());
118 mevObject.setValue("model-version", modelVersion);
119 mevObject.setValue("model-Name", widgetName);
120 // make a list of dynamic Entities
121 List<Object> mevsList = new ArrayList<>();
122 // add this one, it will be the only one in the list in this case
123 mevsList.add(mevObject.getUnderlyingObject());
124 mevsObject.setValue("model-ver", mevsList);
125 // Have to figure out how to add my mev object to the mevsObject,
126 // the modelVers is a list of dynamic entities so we can just attach the array here
127 meObject.setValue("model-vers",mevsObject.getUnderlyingObject());
130 // put it out as JSON
132 PrintWriter out = new PrintWriter(f);
133 out.println(meObject.marshal(true));
137 System.out.println("File already exists for " + resource + ". Skipping.");