98b0dc4470b8aefd3f3e7b60627bbe34d0ede6f8
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / ingestModel / CreateWidgetModels.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  *
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.ingestModel;
23
24 import java.io.File;
25 import java.io.PrintWriter;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Map.Entry;
29 import java.util.UUID;
30
31 import javax.xml.transform.stream.StreamSource;
32
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.util.AAIConfig;
39 import org.onap.aai.util.AAIConstants;
40
41 /**
42  * The Class CreateWidgetModels.
43  */
44 public class CreateWidgetModels
45 {
46         /**
47          * The main method.
48          *
49          * @param args the arguments
50          * @throws Exception the exception
51          */
52         public static void main(String[] args) throws Exception {
53
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];
60                         }
61                         if (args[1] != null) { 
62                                 widgetJsonDir = args[1];
63                         }
64                         if (args[2] != null) { 
65                                 modelVersion = args[2];
66                         }
67                 }
68
69                 if (widgetJsonDir == null) { 
70                         System.err.println("You must specify a directory for widgetModelJson");
71                         System.exit(0);
72                 }
73                 if (modelVersion == null) { 
74                         System.err.println("You must specify a modelVersion");
75                         System.exit(0);
76                 }
77
78
79                 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.valueOf(_apiVersion));
80
81                 // iterate the collection of resources
82
83                 ArrayList<String> processedWidgets = new ArrayList<String>();
84                 for (Entry<String, Introspector> aaiResEnt : loader.getAllObjects().entrySet()) {
85                         Introspector meObject = loader.introspectorFromName("model");
86                         // no need for a ModelVers DynamicEntity
87
88                         Introspector aaiRes = aaiResEnt.getValue();
89
90                         if (!(aaiRes.isContainer() || aaiRes.getName().equals("aai-internal"))) {
91                                 String resource = aaiRes.getName();
92
93                                 if (processedWidgets.contains(resource)) {
94                                         continue;
95                                 }
96                                 processedWidgets.add(resource);
97
98                                 String widgetName = resource;
99                                 String filePathString = widgetJsonDir + "/" + widgetName + "-" + modelVersion + ".json";
100                                 File f = new File(filePathString);
101
102                                 String filePathString2 = widgetJsonDir + "/../widget-model-json-old/" + widgetName + "-" + modelVersion + ".json";
103                                 File f2 = new File(filePathString2);
104
105                                 if(!f.exists() && !f.isDirectory()) { 
106
107                                         if (f2.exists()) { 
108                                                 System.out.println("Using old file for " + resource + ".");
109
110                                                 meObject = loader.unmarshal("model", new StreamSource(f2).getReader().toString());
111                                                 // override, some of them are wrong
112                                                 meObject.setValue("model-version", modelVersion);
113                                         } else {
114                                                 System.out.println("Making new file for " + resource + ".");
115                                                 meObject.setValue("model-invariant-id", UUID.randomUUID().toString());
116                                                 meObject.setValue("model-type", "widget");
117                                                 Introspector mevObject = loader.introspectorFromName("model-ver");
118                                                 Introspector mevsObject = loader.introspectorFromName("model-vers");
119                                                 mevObject.setValue("model-version-id", UUID.randomUUID().toString());
120                                                 mevObject.setValue("model-version", modelVersion);
121                                                 mevObject.setValue("model-Name", widgetName);
122                                                 // make a list of dynamic Entities
123                                                 List<Object> mevsList = new ArrayList<>();
124                                                 // add this one, it will be the only one in the list in this case
125                                                 mevsList.add(mevObject.getUnderlyingObject());
126                                                 mevsObject.setValue("model-ver", mevsList);
127                                                 // Have to figure out how to add my mev object to the mevsObject, 
128                                                 // the modelVers is a list of dynamic entities so we can just attach the array here
129                                                 meObject.setValue("model-vers",mevsObject.getUnderlyingObject());
130                                         }
131
132                                         // put it out as JSON
133
134                                         PrintWriter out = new PrintWriter(f);
135                                         out.println(meObject.marshal(true));
136                                         out.close();
137
138                                 } else { 
139                                         System.out.println("File already exists for " + resource + ".  Skipping.");
140                                 }
141                         }
142                 }
143                 System.exit(0);
144         }
145 }