aboutsummaryrefslogtreecommitdiffstats
path: root/aai-core/src/main/java/org/onap/aai/util/GenerateXsd.java
blob: f44663e02064dd847cc4119caae9255b493de90e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.util;


import org.onap.aai.config.SpringContextAware;
import org.onap.aai.setup.SchemaVersion;
import org.onap.aai.setup.SchemaVersions;
import org.onap.aai.util.genxsd.HTMLfromOXM;
import org.onap.aai.util.genxsd.NodesYAMLfromOXM;

import org.onap.aai.util.genxsd.YAMLfromOXM;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.w3c.dom.*;


import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GenerateXsd {

	private static final Logger logger = LoggerFactory.getLogger("GenerateXsd.class");
	protected static String apiVersion = null;
	public static AnnotationConfigApplicationContext ctx = null;
	static String apiVersionFmt = null;
	static boolean useAnnotationsInXsd = false;
	static String responsesUrl = null;
	static String responsesLabel = null;
	static String jsonEdges = null;
	static Map<String, String> generatedJavaType;
	static Map<String, String> appliedPaths;
	static String RELEASE = System.getProperty("aai.release", "onap");


	static NodeList javaTypeNodes;
	static Map<String,String> javaTypeDefinitions = createJavaTypeDefinitions();
    private static Map<String, String> createJavaTypeDefinitions()
    {
    	StringBuffer aaiInternal = new StringBuffer();
    	Map<String,String> javaTypeDefinitions = new HashMap<String, String>();
    	aaiInternal.append("  aai-internal:\n");
    	aaiInternal.append("    properties:\n");
    	aaiInternal.append("      property-name:\n");
    	aaiInternal.append("        type: string\n");
    	aaiInternal.append("      property-value:\n");
    	aaiInternal.append("        type: string\n");
//    	javaTypeDefinitions.put("aai-internal", aaiInternal.toString());
    	return javaTypeDefinitions;
    }

	public static final int VALUE_NONE = 0;
	public static final int VALUE_DESCRIPTION = 1;
	public static final int VALUE_INDEXED_PROPS = 2;
	public static final int VALUE_CONTAINER = 3;

	private static final String generateTypeXSD = "xsd";
	private static final String generateTypeYAML = "yaml";

	private final static String nodeDir = System.getProperty("nodes.configuration.location");
	private final static String edgeDir = System.getProperty("edges.configuration.location");
	private static final String baseRoot = "aai-schema/";
	private static final String baseAutoGenRoot = "aai-schema/";

	private static final String root = baseRoot + "src/main/resources";
	private static final String autoGenRoot = baseAutoGenRoot + "src/main/resources";

	private static final String normalStartDir = "aai-core";
	private static final String xsd_dir = root + "/" + RELEASE +"/aai_schema";

	private static final String yaml_dir = (((System.getProperty("user.dir") != null) && (!System.getProperty("user.dir").contains(normalStartDir))) ? autoGenRoot : root) + "/" + RELEASE + "/aai_swagger_yaml";

	/* These three strings are for yaml auto-generation from aai-common class*/

	private static int swaggerSupportStartsVersion = 1; // minimum version to support swagger documentation


	private static boolean validVersion(String versionToGen) {

		if ("ALL".equalsIgnoreCase(versionToGen)) {
			return true;
		}

		SchemaVersions schemaVersions = (SchemaVersions) SpringContextAware.getBean("schemaVersions");
		for (SchemaVersion v : schemaVersions.getVersions()) {
	        if (v.equals(versionToGen)) {
	            return true;
	        }
	    }

	    return false;
	}

	private static boolean versionSupportsSwagger( String version) {
		if (new Integer(version.substring(1)).intValue() >= swaggerSupportStartsVersion ) {
			return true;
		}
		return false;
	}

	public static String getAPIVersion() {
		return apiVersion;
	}

	public static String getYamlDir() {
		return yaml_dir;
	}

	public static String getResponsesUrl() {
		return responsesUrl;
	}
	public static void main(String[] args) throws IOException {
		String versionToGen = System.getProperty("gen_version").toLowerCase();
		String fileTypeToGen = System.getProperty("gen_type").toLowerCase();

		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
				"org.onap.aai.config",
				"org.onap.aai.setup"
		);

		SchemaVersions schemaVersions = ctx.getBean(SchemaVersions.class);

		if ( fileTypeToGen == null ) {
			fileTypeToGen = generateTypeXSD;
		}

		if ( !fileTypeToGen.equals( generateTypeXSD ) && !fileTypeToGen.equals( generateTypeYAML )) {
			System.err.println("Invalid gen_type passed. " + fileTypeToGen);
			System.exit(1);
		}

		String responsesLabel = System.getProperty("yamlresponses_url");
		responsesUrl = responsesLabel;

		List<SchemaVersion> versionsToGen = new ArrayList<>();
		if ( versionToGen == null ) {
			System.err.println("Version is required, ie v<n> or ALL.");
			System.exit(1);
		}
		else if (!"ALL".equalsIgnoreCase(versionToGen) && !versionToGen.matches("v\\d+") && !validVersion(versionToGen)) {
			System.err.println("Invalid version passed. " + versionToGen);
			System.exit(1);
		}
		else if ("ALL".equalsIgnoreCase(versionToGen)) {
			versionsToGen = schemaVersions.getVersions();
			Collections.sort(versionsToGen);
			Collections.reverse(versionsToGen);
		} else {
			versionsToGen.add(new SchemaVersion(versionToGen));
		}

		//process file type System property
		fileTypeToGen = (fileTypeToGen == null ? generateTypeXSD : fileTypeToGen.toLowerCase());
		if ( !fileTypeToGen.equals( generateTypeXSD ) && !fileTypeToGen.equals( generateTypeYAML )) {
			System.err.println("Invalid gen_type passed. " + fileTypeToGen);
			System.exit(1);
		} else if ( fileTypeToGen.equals(generateTypeYAML) ) {
			if ( responsesUrl == null || responsesUrl.length() < 1
					|| responsesLabel == null || responsesLabel.length() < 1 ) {
				System.err.println("generating swagger yaml file requires yamlresponses_url and yamlresponses_label properties" );
				System.exit(1);
			} else {
				responsesUrl = "description: "+ "Response codes found in [response codes]("+responsesLabel+ ").\n";
			}
		}
		/*
		 * TODO: Oxm Path is config driveb
		 */
		String oxmPath;
		if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(normalStartDir)) {
			oxmPath = baseAutoGenRoot + nodeDir;
		}
		else {
			oxmPath = baseRoot + nodeDir;
		}

		String outfileName = null;
		File outfile;
		String nodesfileName = null;
		File nodesfile;
		String fileContent = null;
		String nodesContent = null;


		for (SchemaVersion v : versionsToGen) {
			apiVersion = v.toString();
			logger.debug("YAMLdir = "+yaml_dir);
			logger.debug("Generating " + apiVersion + " " + fileTypeToGen);
			apiVersionFmt = "." + apiVersion + ".";
			generatedJavaType = new HashMap<String, String>();
			appliedPaths = new HashMap<String, String>();
			File edgeRuleFile = null;
			String fileName = edgeDir + "DbEdgeRules_" + apiVersion + ".json";
			logger.debug("user.dir = "+System.getProperty("user.dir"));
			if(System.getProperty("user.dir") != null && !System.getProperty("user.dir").contains(normalStartDir)) {
				fileName = baseAutoGenRoot + fileName;

			}
			else {
				fileName = baseRoot + fileName;

			}
			edgeRuleFile = new File( fileName);
//			Document doc = ni.getSchema(translateVersion(v));

			if ( fileTypeToGen.equals(generateTypeXSD) ) {
				outfileName = xsd_dir + "/aai_schema_" + apiVersion + "." + generateTypeXSD;
				try {
					HTMLfromOXM swagger = ctx.getBean(HTMLfromOXM.class);
					swagger.setVersion(v);
					fileContent = swagger.process();
					if ( fileContent.startsWith("Schema format issue")) {
						throw new Exception(fileContent);
					}
				} catch(Exception e) {
		        	logger.error( "Exception creating output file " + outfileName);
		        	logger.error( e.getMessage());
		        	e.printStackTrace();
		        	System.exit(-1);
				}
			} else if ( versionSupportsSwagger(apiVersion )) {
				outfileName = yaml_dir + "/aai_swagger_" + apiVersion + "." + generateTypeYAML;
				nodesfileName = yaml_dir + "/aai_swagger_" + apiVersion + "." + "nodes"+"."+generateTypeYAML;
				try {
					YAMLfromOXM swagger = (YAMLfromOXM) ctx.getBean(YAMLfromOXM.class);
					swagger.setVersion(v);
					fileContent = swagger.process();
					Map combinedJavaTypes = swagger.getCombinedJavaTypes();
					NodesYAMLfromOXM nodesSwagger = ctx.getBean(NodesYAMLfromOXM.class);
					nodesSwagger.setVersion(v);
					nodesSwagger.setCombinedJavaTypes(combinedJavaTypes);
					nodesContent = nodesSwagger.process();
				} catch(Exception e) {
		        	logger.error( "Exception creating output file " + outfileName);
		        	e.printStackTrace();
				}
			} else {
				continue;
			}
			outfile = new File(outfileName);
			File parentDir = outfile.getParentFile();
			if(! parentDir.exists())
			      parentDir.mkdirs();
			if(nodesfileName != null) {
				BufferedWriter nodesBW = null;
				nodesfile = new File(nodesfileName);
				parentDir = nodesfile.getParentFile();
				if(! parentDir.exists())
				      parentDir.mkdirs();
			    try {
			        nodesfile.createNewFile();
			    } catch (IOException e) {
		        	logger.error( "Exception creating output file " + nodesfileName);
		        	e.printStackTrace();
			    }
	        	try {
	        		Charset charset = Charset.forName("UTF-8");
	        		Path path = Paths.get(nodesfileName);
	        		nodesBW = Files.newBufferedWriter(path, charset);
	        		nodesBW.write(nodesContent);
	        	} catch ( IOException e) {
	        		logger.error( "Exception writing output file " + outfileName);
	        		e.printStackTrace();
	        	} finally {
	        		if ( nodesBW != null ) {
	        			nodesBW.close();
	        		}
	        	}
			}

		    try {
		        outfile.createNewFile();
		    } catch (IOException e) {
	        	logger.error( "Exception creating output file " + outfileName);
	        	e.printStackTrace();
		    }
		    BufferedWriter bw = null;
	        try {
	        	Charset charset = Charset.forName("UTF-8");
	        	Path path = Paths.get(outfileName);
	        	bw = Files.newBufferedWriter(path, charset);
	        	bw.write(fileContent);
	        } catch ( IOException e) {
	        	logger.error( "Exception writing output file " + outfileName);
	        	e.printStackTrace();
	        } finally {
	        	if ( bw != null ) {
	        		bw.close();
	        	}
	        }
			logger.debug( "GeneratedXSD successful, saved in " + outfileName);
		}

	}

}