diff options
author | Michael Lando <ml636r@att.com> | 2017-07-22 17:10:02 +0300 |
---|---|---|
committer | Michael Lando <ml636r@att.com> | 2017-07-22 17:10:02 +0300 |
commit | 9db4052423f34a76296ffdcda896cdeba2b4aead (patch) | |
tree | 7c3b779c6d09be40283158382dee2cf02767761e /asdctool/src/main | |
parent | 3c3c833897dbfc0e85feaf36086a505f990ecb76 (diff) |
[sdc] 1710 rebase code
Change-Id: Icedeac71c0a9d27518969effcd5d5519de9955a6
Signed-off-by: Michael Lando <ml636r@att.com>
Diffstat (limited to 'asdctool/src/main')
-rw-r--r-- | asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/SchemaZipFileEnum.java | 68 | ||||
-rw-r--r-- | asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java | 194 |
2 files changed, 244 insertions, 18 deletions
diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/SchemaZipFileEnum.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/SchemaZipFileEnum.java new file mode 100644 index 0000000000..6f4c602c2c --- /dev/null +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/enums/SchemaZipFileEnum.java @@ -0,0 +1,68 @@ +package org.openecomp.sdc.asdctool.enums; + +public enum SchemaZipFileEnum { + + DATA("data", "data-types", "dataTypes", "data_types", new String[]{}), + GROUPS("groups", "group-types", "groupTypes", "group_types", new String[]{"data.yml"}), + POLICIES("policies", "policy-types", "policyTypes","policy_types", new String[]{"data.yml"}), + RELATIONSHIPS("relationships","relationship-types","relationshipTypes", "relationship_types", new String[]{"capabilities.yml", "data.yml", "interfaces.yml"}), + ARTIFACTS("artifacts", "artifact-types", "artifactTypes", "artifact_types", new String[]{"data.yml"}), + CAPABILITIES("capabilities", "capability-types", "capabilityTypes", "capability_types" ,new String[]{"data.yml"}), + INTERFACES("interfaces", "interface-lifecycle-types", "interfaceLifecycleTypes", "interface_types", new String[]{"data.yml"}); + + private String fileName; + private String sourceFolderName; + private String sourceFileName; + private String collectionTitle; + private String[] importFileList; + + private SchemaZipFileEnum(String fileName, String sourceFolderName, String sourceFileName, String collectionTitle, + String[] importFileList) { + this.fileName = fileName; + this.sourceFolderName = sourceFolderName; + this.sourceFileName = sourceFileName; + this.collectionTitle = collectionTitle; + this.importFileList = importFileList; + } + + public String getFileName() { + return fileName; + } + + public void setFileName(String fileName) { + this.fileName = fileName; + } + + public String getSourceFolderName() { + return sourceFolderName; + } + + public void setSourceFolderName(String sourceFolderName) { + this.sourceFolderName = sourceFolderName; + } + + public String getSourceFileName() { + return sourceFileName; + } + + public void setSourceFileName(String sourceFileName) { + this.sourceFileName = sourceFileName; + } + + public String getCollectionTitle() { + return collectionTitle; + } + + public void setCollectionTitle(String collectionTitle) { + this.collectionTitle = collectionTitle; + } + + public String[] getImportFileList() { + return importFileList; + } + + public void setImportFileList(String[] importFileList) { + this.importFileList = importFileList; + } + +} diff --git a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java index efc34a7bc0..50563a5412 100644 --- a/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java +++ b/asdctool/src/main/java/org/openecomp/sdc/asdctool/main/SdcSchemaFileImport.java @@ -20,13 +20,24 @@ package org.openecomp.sdc.asdctool.main; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileFilter; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; 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.zip.ZipEntry; +import java.util.zip.ZipOutputStream; import org.apache.commons.codec.digest.DigestUtils; +import org.openecomp.sdc.asdctool.enums.SchemaZipFileEnum; import org.openecomp.sdc.asdctool.impl.EsToCassandraDataMigrationConfig; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.cassandra.CassandraOperationStatus; @@ -36,48 +47,101 @@ 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; public class SdcSchemaFileImport { private static SdcSchemaFilesCassandraDao schemaFilesCassandraDao; + private static final String TOSCA_VERSION = "tosca_simple_yaml_1_1"; + + private static String importToscaPath; + + private static byte[] buffer = new byte[1024]; + + private static String YAML_EXTENSION = ".yml"; + + private static String LICENSE_TXT; + + private static ZipOutputStream zos; + public static void main(String[] args) throws Exception { + //Generation flow start - generating SDC from normatives + final String FILE_NAME = "SDC.zip"; if (args == null || args.length < 4) { usageAndExit(); } - String pathAndFile = args[0]; + importToscaPath = args[0]; String sdcReleaseNum = args[1]; String conformanceLevel = args[2]; String appConfigDir = args[3]; - - File file = new File(pathAndFile); - if(!file.exists()){ - System.out.println("The file or path does not exist"); - System.exit(1); - } else if(!file.isFile()){ - System.out.println("Specify the file name"); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + zos = new ZipOutputStream(baos); + + //Initialize the license text + try { + LICENSE_TXT = new String(Files.readAllBytes(Paths.get(appConfigDir + "\\license.txt"))); + } + catch(Exception e) { + System.err.println("Couldn't read license.txt in location :" + appConfigDir); 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 + "\\" + schemaZipFileEnum.getSourceFolderName() + "\\" + 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(); + + 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 normatives + AnnotationConfigApplicationContext context = initContext(appConfigDir); schemaFilesCassandraDao = (SdcSchemaFilesCassandraDao) context.getBean("sdc-schema-files-cassandra-dao"); - Path path = Paths.get(pathAndFile); - byte[] fileBytes = Files.readAllBytes(path); - + 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.out.println("SdcSchemaFileImport failed cassandra error" + saveSchemaFile); + if(!saveSchemaFile.equals(CassandraOperationStatus.OK)) { + System.err.println("SdcSchemaFileImport failed cassandra error" + saveSchemaFile); System.exit(1); } @@ -86,13 +150,107 @@ public class SdcSchemaFileImport { System.exit(0); } - private static void usageAndExit(){ + 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<String, Object>(); + + 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 + stream.close(); + zos.closeEntry(); + file.delete(); + + } catch (IOException e) { + System.out.println("Error in file creation : " + fileName + ", " + e.getMessage()); + System.exit(1); + } + } + + public static void createAndSaveNodeSchemaFile() throws IOException { + + //Initialize the snake yaml dumper option + DumperOptions options = new DumperOptions(); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + + Map<String, Object> nodeTypeList = new LinkedHashMap<String, Object>(); + + 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"}; + + for (String nodeTypesMainFolder : nodeTypesMainFolders) { + Files.walk(Paths.get(importToscaPath + "\\" + nodeTypesMainFolder)) + .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 = (Map<String, Object>) yaml.load(inputStream); + 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.out.println("Usage: <file dir/filename> <SDC release number> <Schema conformance level> <configuration dir>"); + private static void SdcSchemaFileImportUsage() { + System.err.println("Usage: <file dir/filename> <SDC release number> <Schema conformance level> <configuration dir>"); } private static AnnotationConfigApplicationContext initContext(String appConfigDir) { |