2 * ============LICENSE_START=======================================================
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
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=========================================================
21 package org.openecomp.aai.ingestModel;
24 import java.io.PrintWriter;
25 import java.io.StringWriter;
26 import java.util.ArrayList;
28 import java.util.UUID;
30 import javax.xml.transform.stream.StreamSource;
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;
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;
45 * The Class CreateWidgetModels.
47 public class CreateWidgetModels
52 * @param args the arguments
53 * @throws Exception the exception
55 public static void main(String[] args) throws Exception {
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];
64 if (args[1] != null) {
65 widgetJsonDir = args[1];
67 if (args[2] != null) {
68 modelVersion = args[2];
72 if (widgetJsonDir == null) {
73 System.err.println("You must specify a directory for widgetModelJson");
76 if (modelVersion == null) {
77 System.err.println("You must specify a modelVersion");
81 ArrayList<String> apiVersions = new ArrayList<String>();
82 apiVersions.add(_apiVersion);
83 final IngestModelMoxyOxm m = new IngestModelMoxyOxm();
84 m.init(apiVersions, false);
86 AAIResources aaiResources = IngestModelMoxyOxm.aaiResourceContainer.get(_apiVersion);
88 DynamicJAXBContext jaxbContext = aaiResources.getJaxbContext();
90 // iterate the collection of resources
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
97 AAIResource aaiRes = aaiResEnt.getValue();
99 if (aaiRes.getResourceType().equals("node")) {
100 String resource = aaiRes.getSimpleName();
102 if (processedWidgets.contains(resource)) {
105 processedWidgets.add(resource);
107 String widgetName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, resource);
108 String filePathString = widgetJsonDir + "/" + widgetName + "-" + modelVersion + ".json";
109 File f = new File(filePathString);
111 String filePathString2 = widgetJsonDir + "/../widget-model-json-old/" + widgetName + "-" + modelVersion + ".json";
112 File f2 = new File(filePathString2);
114 if(!f.exists() && !f.isDirectory()) {
117 System.out.println("Using old file for " + resource + ".");
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);
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);
146 // put it out as JSON
148 JAXBMarshaller marshaller = jaxbContext.createMarshaller();
149 marshaller.setProperty(JAXBMarshaller.JAXB_FORMATTED_OUTPUT, true);
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) ;
155 StringWriter writer = new StringWriter();
156 marshaller.marshal(meObject, writer);
157 PrintWriter out = new PrintWriter(f);
158 out.println(writer.toString());
162 System.out.println("File already exists for " + resource + ". Skipping.");