Sync up the changes for v15
[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.Set;
28 import java.util.UUID;
29
30 import javax.xml.transform.stream.StreamSource;
31
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.setup.SchemaVersion;
38 import org.onap.aai.util.AAIConfig;
39 import org.onap.aai.util.AAIConstants;
40 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
41
42 /**
43  * The Class CreateWidgetModels.
44  */
45 public class CreateWidgetModels
46 {
47         /**
48          * The main method.
49          *
50          * @param args the arguments
51          * @throws Exception the exception
52          */
53         public static void main(String[] args) throws Exception {
54
55                 String _apiVersion = AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP);
56                 String widgetJsonDir = null;
57                 String modelVersion = null;
58                 if (args.length > 0) { 
59                         if (args[0] != null) {
60                                 _apiVersion = args[0];
61                         }
62                         if (args[1] != null) { 
63                                 widgetJsonDir = args[1];
64                         }
65                         if (args[2] != null) { 
66                                 modelVersion = args[2];
67                         }
68                 }
69
70                 if (widgetJsonDir == null) { 
71                         System.err.println("You must specify a directory for widgetModelJson");
72                         System.exit(0);
73                 }
74                 if (modelVersion == null) { 
75                         System.err.println("You must specify a modelVersion");
76                         System.exit(0);
77                 }
78
79                 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
80                                 "org.onap.aai.config",
81                                 "org.onap.aai.setup"
82                 );
83
84                 LoaderFactory loaderFactory = ctx.getBean(LoaderFactory.class);
85                 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, new SchemaVersion(_apiVersion));
86
87                 // iterate the collection of resources
88
89                 ArrayList<String> processedWidgets = new ArrayList<String>();
90                 for (Entry<String, Introspector> aaiResEnt : loader.getAllObjects().entrySet()) {
91                         Introspector meObject = loader.introspectorFromName("model");
92                         // no need for a ModelVers DynamicEntity
93
94                         Introspector aaiRes = aaiResEnt.getValue();
95
96                         if (!(aaiRes.isContainer() || aaiRes.getName().equals("aai-internal"))) {
97                                 String resource = aaiRes.getName();
98
99                                 if (processedWidgets.contains(resource)) {
100                                         continue;
101                                 }
102
103                                 Set<String> introspectorProperties = aaiRes.getProperties();
104
105                                 if(!(introspectorProperties.contains("model-version-id") && introspectorProperties.contains("model-invariant-id"))){
106                                     System.out.println(aaiRes.getDbName() + " does not contain model properties so skipping");
107                                 }
108                                 processedWidgets.add(resource);
109
110                                 String widgetName = resource;
111                                 String filePathString = widgetJsonDir + "/" + widgetName + "-" + modelVersion + ".json";
112                                 File f = new File(filePathString);
113
114                                 String filePathString2 = widgetJsonDir + "/../widget-model-json-old/" + widgetName + "-" + modelVersion + ".json";
115                                 File f2 = new File(filePathString2);
116
117                                 if(!f.exists() && !f.isDirectory()) { 
118
119                                         if (f2.exists()) { 
120                                                 System.out.println("Using old file for " + resource + ".");
121
122                                                 meObject = loader.unmarshal("model", new StreamSource(f2).getReader().toString());
123                                                 // override, some of them are wrong
124                                                 meObject.setValue("model-version", modelVersion);
125                                         } else {
126                                                 System.out.println("Making new file for " + resource + ".");
127                                                 meObject.setValue("model-invariant-id", UUID.randomUUID().toString());
128                                                 meObject.setValue("model-type", "widget");
129                                                 Introspector mevObject = loader.introspectorFromName("model-ver");
130                                                 Introspector mevsObject = loader.introspectorFromName("model-vers");
131                                                 mevObject.setValue("model-version-id", UUID.randomUUID().toString());
132                                                 mevObject.setValue("model-version", modelVersion);
133                                                 mevObject.setValue("model-Name", widgetName);
134                                                 // make a list of dynamic Entities
135                                                 List<Object> mevsList = new ArrayList<>();
136                                                 // add this one, it will be the only one in the list in this case
137                                                 mevsList.add(mevObject.getUnderlyingObject());
138                                                 mevsObject.setValue("model-ver", mevsList);
139                                                 // Have to figure out how to add my mev object to the mevsObject, 
140                                                 // the modelVers is a list of dynamic entities so we can just attach the array here
141                                                 meObject.setValue("model-vers",mevsObject.getUnderlyingObject());
142                                         }
143
144                                         // put it out as JSON
145
146                                         PrintWriter out = new PrintWriter(f);
147                                         out.println(meObject.marshal(true));
148                                         out.close();
149
150                                 } else { 
151                                         System.out.println("File already exists for " + resource + ".  Skipping.");
152                                 }
153                         }
154                 }
155                 System.exit(0);
156         }
157 }