aboutsummaryrefslogtreecommitdiffstats
path: root/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java
blob: 47a08ea70e00787c16491ff93dba469486336fe7 (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
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * 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.sdc.asdctool.main;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.openecomp.sdc.asdctool.configuration.SdcSchemaFileImportConfiguration;
import org.openecomp.sdc.asdctool.enums.SchemaZipFileEnum;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus;
import org.openecomp.sdc.be.dao.cassandra.SdcSchemaFilesCassandraDao;
import org.openecomp.sdc.be.resources.data.SdcSchemaFilesData;
import org.openecomp.sdc.common.api.ConfigurationSource;
import org.openecomp.sdc.common.impl.ExternalConfiguration;
import org.openecomp.sdc.common.impl.FSConfigurationSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class SdcSchemaFileImport {
	
	private static final String SEPARATOR = FileSystems.getDefault().getSeparator();
	
	private static final String TOSCA_VERSION = "tosca_simple_yaml_1_1";
		
	private static String importToscaPath;
	
	private static final byte[] buffer = new byte[1024];
	
	private static final String YAML_EXTENSION = ".yml";

	private static final String DEPLOYMENT_TYPE_ONAP = "onap";
	
	private static String LICENSE_TXT;
	
	private static ZipOutputStream zos; 
	
	public static void main(String[] args) throws Exception {
		
		//Generation flow start - generating SDC from normatives
		System.out.println("Starting SdcSchemaFileImport procedure...");
		final String FILE_NAME = "SDC.zip";
		
		if (args == null || !(args.length ==4 || args.length == 5 )) {
			usageAndExit();
		}
		
		importToscaPath = args[0];
		String sdcReleaseNum = args[1];
		String conformanceLevel = args[2];
		String appConfigDir = args[3];
		String deploymentType=null;
		if(args.length==5){
			deploymentType=args[4];
		}

				
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		zos = new ZipOutputStream(baos);
	
		//Initialize the license text
		try {
			LICENSE_TXT = new String(Files.readAllBytes(Paths.get(appConfigDir + SEPARATOR+"license.txt")));
		}
		catch(Exception e)  {
			System.err.println("Couldn't read license.txt in location :" + appConfigDir+", error: "+e);
			System.exit(1);
		}
		
		//Loop over schema file list and create each yaml file from /import/tosca folder 
		SchemaZipFileEnum[] schemaFileList = SchemaZipFileEnum.values();
		for (SchemaZipFileEnum schemaZipFileEnum : schemaFileList) {
			try {
				//get the source yaml file
				String pathname = importToscaPath + SEPARATOR + schemaZipFileEnum.getSourceFolderName() + SEPARATOR +  schemaZipFileEnum.getSourceFileName() + YAML_EXTENSION;
				System.out.println("Processing file "+pathname+"....");
				InputStream input = new FileInputStream(new File(pathname));
				//Convert the content of file to yaml 
				Yaml yamlFileSource = new Yaml();
			    Object content = yamlFileSource.load(input);
			    
			    createAndSaveSchemaFileYaml(schemaZipFileEnum, content);
			}
			catch(Exception e)  {
				System.err.println("Error in file creation : " + schemaZipFileEnum.getFileName() + ", " + e.getMessage());
				System.exit(1);
			}
		}
		
		createAndSaveNodeSchemaFile(deploymentType);
		
    	try  {
    		//close the ZipOutputStream
    		zos.close();
    		System.out.println("File SDC.zip creation successful");
    		
    	}	catch(Exception ex)  {
    		System.err.println("Failed to pack SDC.zip file, error: "+ex);
    		System.exit(1);
    	}
		
    	//Generation flow end - generating SDC from narratives
				
		AnnotationConfigApplicationContext context = initContext(appConfigDir);
        SdcSchemaFilesCassandraDao schemaFilesCassandraDao = (SdcSchemaFilesCassandraDao) context.getBean("sdc-schema-files-cassandra-dao");
		
		byte[] fileBytes = baos.toByteArray();

		Date date = new Date();
		String md5Hex = DigestUtils.md5Hex(fileBytes);
		
		SdcSchemaFilesData schemeFileData = new SdcSchemaFilesData(sdcReleaseNum, date, conformanceLevel, FILE_NAME, fileBytes, md5Hex);
		CassandraOperationStatus saveSchemaFile = schemaFilesCassandraDao.saveSchemaFile(schemeFileData);
		
		if(!saveSchemaFile.equals(CassandraOperationStatus.OK))  {
			System.err.println("SdcSchemaFileImport failed cassandra error" + saveSchemaFile);
			System.exit(1);
		}
		
		System.out.println("SdcSchemaFileImport successfully completed");
		
		System.exit(0);
	}
	
	public static void createAndSaveSchemaFileYaml(SchemaZipFileEnum schemaZipFileEnum, Object content) {	
		createAndSaveSchemaFileYaml(schemaZipFileEnum.getFileName(), schemaZipFileEnum.getImportFileList(), schemaZipFileEnum.getCollectionTitle(), content);
	}
	
	public static void createAndSaveSchemaFileYaml(String fileName, String[] importFileList, String collectionTitle, Object content) {
	    
		//Initialize the snake yaml dumper option
		DumperOptions options = new DumperOptions();
		options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
		
	    //Create the new yaml
		Yaml yaml = new Yaml(options);
		yaml.setName(fileName);
		 
		//Initialize the yaml contents
		Map<String, Object> data = new LinkedHashMap<>();
					
		data.put("tosca_definitions_version", TOSCA_VERSION);
		
		if (importFileList.length > 0)  {
			data.put("imports", importFileList);
		}
		
		data.put(collectionTitle, content);
		
		//Save the new yaml to file
		try {
		
			FileWriter writer;
			File file = File.createTempFile(fileName, YAML_EXTENSION);
			writer = new FileWriter(file);
			
			//Add the license as comment in top of file
			writer.write(LICENSE_TXT);
			
			yaml.dump(data, writer);
			
			writer.close();
			
			// begin writing a new ZIP entry, positions the stream to the start of the entry data
			ZipEntry entry = new ZipEntry(yaml.getName() + YAML_EXTENSION);
			zos.putNextEntry(entry);
			FileInputStream stream = new FileInputStream(file.getAbsolutePath());
    		int len;
    		while ((len = stream.read(buffer)) > 0) {
    			zos.write(buffer, 0, len);
    		}
    		//close the InputStream
            file.delete();
            stream.close();
    		zos.closeEntry();

    		
		} catch (IOException e) {
			System.out.println("Error in file creation : " + fileName + ", " + e.getMessage());
			System.exit(1);
		}
	}

	/**
	 *the method is responsible for creating and storing the sdc normatives in the DB
	 * @param deploymentType if the deployments type is onap the onap narratives will be add to the zip
	 * @throws IOException thrown in case of issues in reding files.
	 */
	public static void createAndSaveNodeSchemaFile(String deploymentType) throws IOException  {
		
		//Initialize the snake yaml dumper option
		DumperOptions options = new DumperOptions();
		options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
		
		Map<String, Object> nodeTypeList = new LinkedHashMap<>();
		
		String[] importFileList = new String[]{"data.yml", "artifacts.yml", "capabilities.yml", "interfaces.yml", "relationships.yml"}; 
		String collectionTitle = "node_types";
		
		//Create node.yaml - collect all types from normative-types and heat-types directories
		String[] nodeTypesMainFolders = new String[]{"normative-types", "heat-types"};

		if(DEPLOYMENT_TYPE_ONAP.equals(deploymentType)){
            String[] onapNodeTypesMainFolders = new String[]{"nfv-types"};
            nodeTypesMainFolders=ArrayUtils.addAll(nodeTypesMainFolders,onapNodeTypesMainFolders);
		}
		
		for (String nodeTypesMainFolder : nodeTypesMainFolders) {
        try (Stream<Path> paths = Files.walk(Paths.get(importToscaPath + SEPARATOR + nodeTypesMainFolder))) {
            paths.filter(path -> path.getFileName().toString().toLowerCase().endsWith(YAML_EXTENSION))
                .forEach(yamlFile -> {
                    try {
                        String path = yamlFile.toAbsolutePath().toString();
                        System.out.println("Processing node type file " + path + "...");
                        FileInputStream inputStream = new FileInputStream(path);
                        Yaml yaml = new Yaml();
                        Map<String, Object> load = yaml.loadAs(inputStream, Map.class);
                        Map<String, Object> nodeType = (Map<String, Object>) load.get(collectionTitle);
                        nodeTypeList.putAll(nodeType);

                    } catch (Exception e) {
                        System.err.println("Error in opening file " + yamlFile.toAbsolutePath().toString());
                        System.exit(1);
                    }
                });
        }
    }
		createAndSaveSchemaFileYaml("nodes", importFileList, collectionTitle, nodeTypeList);
	}

	private static void usageAndExit()  {
		SdcSchemaFileImportUsage();
		System.exit(1);
	}
	
	private static void SdcSchemaFileImportUsage()  {
		System.err.println("Usage: <file dir/filename> <SDC release number> <Schema conformance level> <configuration dir> <deployment type optional>");
	}
	
	private static AnnotationConfigApplicationContext initContext(String appConfigDir) {
		ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
		new ConfigurationManager(configurationSource);
		return  new AnnotationConfigApplicationContext(SdcSchemaFileImportConfiguration.class);
	}
}