18006295f6859d5e107663da2f3802e2b2af8e45
[aai/aai-common.git] / aai-core / src / main / java / org / openecomp / aai / ingestModel / CreateWidgetModels.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.openecomp.aai
4  * ================================================================================
5  * Copyright (C) 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
21 package org.openecomp.aai.ingestModel;
22
23 import java.io.File;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.util.ArrayList;
27 import java.util.Map;
28 import java.util.UUID;
29
30 import javax.xml.transform.stream.StreamSource;
31
32 import org.eclipse.persistence.dynamic.DynamicEntity;
33 import org.eclipse.persistence.jaxb.JAXBMarshaller;
34 import org.eclipse.persistence.jaxb.JAXBUnmarshaller;
35 import org.eclipse.persistence.jaxb.MarshallerProperties;
36 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
37
38 import org.openecomp.aai.domain.model.AAIResource;
39 import org.openecomp.aai.domain.model.AAIResources;
40 import org.openecomp.aai.util.AAIConfig;
41 import org.openecomp.aai.util.AAIConstants;
42 import com.google.common.base.CaseFormat;
43
44 /**
45  * The Class CreateWidgetModels.
46  */
47 public class CreateWidgetModels
48 {
49         /**
50          * The main method.
51          *
52          * @param args the arguments
53          * @throws Exception the exception
54          */
55         public static void main(String[] args) throws Exception {
56
57                 String _apiVersion = AAIConfig.get(AAIConstants.AAI_DEFAULT_API_VERSION_PROP);
58                 String widgetJsonDir = null;
59                 String modelVersion = null;
60                 if (args.length > 0) { 
61                         if (args[0] != null) {
62                                 _apiVersion = args[0];
63                         }
64                         if (args[1] != null) { 
65                                 widgetJsonDir = args[1];
66                         }
67                         if (args[2] != null) { 
68                                 modelVersion = args[2];
69                         }
70                 }
71
72                 if (widgetJsonDir == null) { 
73                         System.err.println("You must specify a directory for widgetModelJson");
74                         System.exit(0);
75                 }
76                 if (modelVersion == null) { 
77                         System.err.println("You must specify a modelVersion");
78                         System.exit(0);
79                 }
80
81                 ArrayList<String> apiVersions = new ArrayList<String>();
82                 apiVersions.add(_apiVersion);
83                 final IngestModelMoxyOxm m = new IngestModelMoxyOxm();
84                 m.init(apiVersions, false);
85
86                 AAIResources aaiResources = IngestModelMoxyOxm.aaiResourceContainer.get(_apiVersion);
87
88                 DynamicJAXBContext jaxbContext = aaiResources.getJaxbContext();
89
90                 // iterate the collection of resources
91
92                 ArrayList<String> processedWidgets = new ArrayList<String>();
93                 for (Map.Entry<String, AAIResource> aaiResEnt : aaiResources.getAaiResources().entrySet()) { 
94                         DynamicEntity meObject = jaxbContext.newDynamicEntity("inventory.aai.openecomp.org." + _apiVersion + ".Model");
95                         // no need for a ModelVers DynamicEntity
96
97                         AAIResource aaiRes = aaiResEnt.getValue();
98
99                         if (aaiRes.getResourceType().equals("node")) {
100                                 String resource = aaiRes.getSimpleName();
101
102                                 if (processedWidgets.contains(resource)) {
103                                         continue;
104                                 }
105                                 processedWidgets.add(resource);
106
107                                 String widgetName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, resource);
108                                 String filePathString = widgetJsonDir + "/" + widgetName + "-" + modelVersion + ".json";
109                                 File f = new File(filePathString);
110
111                                 String filePathString2 = widgetJsonDir + "/../widget-model-json-old/" + widgetName + "-" + modelVersion + ".json";
112                                 File f2 = new File(filePathString2);
113
114                                 if(!f.exists() && !f.isDirectory()) { 
115
116                                         if (f2.exists()) { 
117                                                 System.out.println("Using old file for " + resource + ".");
118
119                                                 JAXBUnmarshaller unmarshaller = jaxbContext.createUnmarshaller();
120                                                 unmarshaller.setProperty("eclipselink.media-type", "application/json");
121                                                 unmarshaller.setProperty("eclipselink.json.include-root", false);
122                                                 Class<? extends DynamicEntity> resultClass = meObject.getClass();
123                                                 meObject = (DynamicEntity) unmarshaller.unmarshal(new StreamSource(f2), resultClass).getValue();
124                                                 // override, some of them are wrong
125                                                 meObject.set("modelVersion", modelVersion);
126                                         } else { 
127
128                                                 System.out.println("Making new file for " + resource + ".");
129                                                 meObject.set("modelInvariantId", UUID.randomUUID().toString());
130                                                 meObject.set("modelType", "widget");
131                                                 DynamicEntity mevObject = jaxbContext.newDynamicEntity("inventory.aai.openecomp.org." + _apiVersion + ".ModelVer");
132                                                 DynamicEntity mevsObject = jaxbContext.newDynamicEntity("inventory.aai.openecomp.org." + _apiVersion + ".ModelVers");
133                                                 mevObject.set("modelVersionId", UUID.randomUUID().toString());
134                                                 mevObject.set("modelVersion", modelVersion);
135                                                 mevObject.set("modelName", widgetName);
136                                                 // make a list of dynamic Entities
137                                                 ArrayList<DynamicEntity> mevsList = new ArrayList<DynamicEntity>();
138                                                 // add this one, it will be the only one in the list in this case
139                                                 mevsList.add(mevObject);
140                                                 mevsObject.set("modelVer", mevsList);
141                                                 // Have to figure out how to add my mev object to the mevsObject, 
142                                                 // the modelVers is a list of dynamic entities so we can just attach the array here
143                                                 meObject.set("modelVers",mevsObject);
144                                         }
145
146                                         // put it out as JSON
147
148                                         JAXBMarshaller marshaller = jaxbContext.createMarshaller();
149                                         marshaller.setProperty(JAXBMarshaller.JAXB_FORMATTED_OUTPUT, true);
150
151                                         marshaller.setProperty("eclipselink.media-type", "application/json");
152                                         marshaller.setProperty("eclipselink.json.include-root", false);
153                                         marshaller.setProperty(MarshallerProperties.JSON_MARSHAL_EMPTY_COLLECTIONS, Boolean.FALSE) ;
154
155                                         StringWriter writer = new StringWriter();
156                                         marshaller.marshal(meObject, writer);
157                                         PrintWriter out = new PrintWriter(f);
158                                         out.println(writer.toString());
159                                         out.close();
160
161                                 } else { 
162                                         System.out.println("File already exists for " + resource + ".  Skipping.");
163                                 }
164                         }
165                 }
166                 System.exit(0);
167         }
168 }