aboutsummaryrefslogtreecommitdiffstats
path: root/operation-utils/src/main/java/org/openecomp/utils/YamlToJava.java
blob: 4577e06d78d2b633750a6c40fbdfb9756f7a0924 (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
/*-
 * ============LICENSE_START==========================================
 * OPENECOMP - DCAE
 * ===================================================================
 * Copyright (c) 2017 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.openecomp.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Map;
import java.util.Properties;

import org.json.JSONObject;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import groovy.lang.Writable;
import groovy.text.SimpleTemplateEngine;

import org.openecomp.ncomp.utils.PropertyUtil;
import org.openecomp.ncomp.webservice.utils.FileUtils;

public class YamlToJava {
	
//	package org.openecomp.operation.logging.usecases;
//
//	import org.openecomp.logger.EcompMessageEnum;
//	import com.att.eelf.i18n.EELFResourceManager;
//
//	public enum MyMessageEnum implements EcompMessageEnum {
//		// Api Handler Messages
//		FOOBAR;
//
//		static {
//			EELFResourceManager.loadMessageBundle("foobar");
//		}
//	}
	
	@SuppressWarnings("unchecked")
	static public void convert(String yamlFileName, String outputSourceRootDir, String packageName) {
		try {
			if (! (new File(yamlFileName).exists())) {
				System.err.println(yamlFileName + " does not exists");
				return;
			}
			DumperOptions options = new DumperOptions();
			options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
			Yaml y = new Yaml(options);
			Map<String,Object> m = (Map<String, Object>) y.load(FileUtils.filename2stream(yamlFileName, null));
			File f = new File(yamlFileName);
			String name = f.getName().replace(".yaml", "");
			m.put("name", name);
			m.put("packageName", packageName);
			String ofile1 = outputSourceRootDir + "/" + name + ".properties";
			OutputStreamWriter w;
			SimpleTemplateEngine engine = new SimpleTemplateEngine();
			if (m.containsKey("messages")) {
				w = FileUtils.filename2writer(ofile1);
				w.append(engine.createTemplate(getTemplate("properties_template")).make(m).toString());
				w.close();
				String ofile2 = outputSourceRootDir + "/" + name + "MessageEnum.java";
				w = FileUtils.filename2writer(ofile2);
				w.append(engine.createTemplate(getTemplate("messageEnum.java_template")).make(m).toString());
				w.close();
			}
			if (m.containsKey("operations")) {
				String ofile3 = outputSourceRootDir + "/" + name + "OperationEnum.java";
				w = FileUtils.filename2writer(ofile3);
				w.append(engine.createTemplate(getTemplate("operationEnum.java_template")).make(m).toString());
				w.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private static String getTemplate(String res) throws IOException {
		InputStream in = YamlToJava.class.getClassLoader().getResourceAsStream(res);
		if (in == null) {
			throw new RuntimeException("Unable to find resource: " + res);
		}
		ByteArrayOutputStream o = new ByteArrayOutputStream();
		FileUtils.copyStream(in, o);
		return o.toString();
}

	public static void main(String[] args) throws IOException {

		Properties props = new Properties();
		String pname = "GenericMessages.properties";
		String fname = "src/main/resources/GenericMessages.yaml";
		props.load(YamlToJava.class.getClassLoader().getResourceAsStream(pname));
		DumperOptions options = new DumperOptions();
		options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
		Yaml y = new Yaml(options);
		JSONObject j = new JSONObject();
		for (Object k : props.keySet()) {
			String a[] = props.getProperty((String) k).split("\\|");
			JSONObject j1 = new JSONObject();
			j.put((String) k, j1);
			j1.put("errorCode", a[0]);
			j1.put("messageFormat", a[1]);
			j1.put("resolution", a[2]);
			j1.put("description", a[3].trim());
		}

		Object data = y.load(j.toString());
		OutputStreamWriter w = FileUtils.filename2writer(fname);
		w.append(y.dump(data) + "\n");
		w.close();
	}
}