Update the license for 2017-2018 license
[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-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
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 package org.onap.aai.ingestModel;
21
22 import java.io.File;
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;
28
29 import javax.xml.transform.stream.StreamSource;
30
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;
38
39 /**
40  * The Class CreateWidgetModels.
41  */
42 public class CreateWidgetModels
43 {
44         /**
45          * The main method.
46          *
47          * @param args the arguments
48          * @throws Exception the exception
49          */
50         public static void main(String[] args) throws Exception {
51
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];
58                         }
59                         if (args[1] != null) { 
60                                 widgetJsonDir = args[1];
61                         }
62                         if (args[2] != null) { 
63                                 modelVersion = args[2];
64                         }
65                 }
66
67                 if (widgetJsonDir == null) { 
68                         System.err.println("You must specify a directory for widgetModelJson");
69                         System.exit(0);
70                 }
71                 if (modelVersion == null) { 
72                         System.err.println("You must specify a modelVersion");
73                         System.exit(0);
74                 }
75
76
77                 Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, Version.valueOf(_apiVersion));
78
79                 // iterate the collection of resources
80
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
85
86                         Introspector aaiRes = aaiResEnt.getValue();
87
88                         if (!(aaiRes.isContainer() || aaiRes.getName().equals("aai-internal"))) {
89                                 String resource = aaiRes.getName();
90
91                                 if (processedWidgets.contains(resource)) {
92                                         continue;
93                                 }
94                                 processedWidgets.add(resource);
95
96                                 String widgetName = resource;
97                                 String filePathString = widgetJsonDir + "/" + widgetName + "-" + modelVersion + ".json";
98                                 File f = new File(filePathString);
99
100                                 String filePathString2 = widgetJsonDir + "/../widget-model-json-old/" + widgetName + "-" + modelVersion + ".json";
101                                 File f2 = new File(filePathString2);
102
103                                 if(!f.exists() && !f.isDirectory()) { 
104
105                                         if (f2.exists()) { 
106                                                 System.out.println("Using old file for " + resource + ".");
107
108                                                 meObject = loader.unmarshal("model", new StreamSource(f2).getReader().toString());
109                                                 // override, some of them are wrong
110                                                 meObject.setValue("model-version", modelVersion);
111                                         } else {
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());
128                                         }
129
130                                         // put it out as JSON
131
132                                         PrintWriter out = new PrintWriter(f);
133                                         out.println(meObject.marshal(true));
134                                         out.close();
135
136                                 } else { 
137                                         System.out.println("File already exists for " + resource + ".  Skipping.");
138                                 }
139                         }
140                 }
141                 System.exit(0);
142         }
143 }