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.sdc.tosca.services.impl;
23 import org.openecomp.core.utilities.file.FileContentHandler;
24 import org.openecomp.core.utilities.file.FileUtils;
25 import org.openecomp.sdc.common.errors.CoreException;
26 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
27 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
28 import org.openecomp.sdc.tosca.exceptions.CsarCreationErrorBuilder;
29 import org.openecomp.sdc.tosca.exceptions.CsarMissingEntryPointErrorBuilder;
30 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
32 import java.io.BufferedOutputStream;
33 import java.io.ByteArrayInputStream;
34 import java.io.ByteArrayOutputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
39 import java.util.zip.ZipEntry;
40 import java.util.zip.ZipOutputStream;
42 public class ToscaFileOutputServiceCsarImpl implements ToscaFileOutputService {
43 static final String EXTERNAL_ARTIFACTS_FOLDER_NAME = "Artifacts";
44 private static final String DEFINITIONS_FOLDER_NAME = "Definitions";
45 private static final String ARTIFACTS_FOLDER_NAME = "Artifacts";
46 //todo currently duplicated, to be changed when external artifacts are separated from internal
47 private static final String TOSCA_META_FOLDER_NAME = "TOSCA-Metadata";
48 private static final String TOSCA_META_FILE_VERSION = "TOSCA-Meta-File-Version";
49 private static final String TOSCA_META_FILE_VERSION_VALUE = "1.0";
50 private static final String TOSCA_META_FILE_NAME = "TOSCA.meta";
51 private static final String CSAR_VERSION = "CSAR-Version";
52 private static final String CSAR_VERSION_VALUE = "1.1";
53 private static final String CREATED_BY = "Created-By";
54 private static final String CREATED_BY_VALUE = "ASDC Onboarding portal";
55 private static final String ENTRY_DEFINITIONS = "Entry-Definitions";
56 private static final String META_FILE_DELIMITER = ":";
57 private static final String SPACE = " ";
58 private static final String FILE_SEPARATOR = File.separator;
61 public byte[] createOutputFile(ToscaServiceModel toscaServiceModel,
62 FileContentHandler externalArtifacts) {
63 ByteArrayOutputStream baos = new ByteArrayOutputStream();
64 try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos))) {
65 packDefinitions(zos, toscaServiceModel.getServiceTemplates());
66 FileContentHandler artifactFiles = toscaServiceModel.getArtifactFiles();
67 if (artifactFiles != null && !artifactFiles.isEmpty()) {
68 packArtifacts(zos, artifactFiles);
70 if (toscaServiceModel.getEntryDefinitionServiceTemplate() == null) {
71 throw new CoreException(new CsarMissingEntryPointErrorBuilder().build());
73 createAndPackToscaMetaFile(zos, toscaServiceModel.getEntryDefinitionServiceTemplate());
74 if (externalArtifacts != null) {
75 packExternalArtifacts(zos, externalArtifacts);
77 } catch (IOException exc) {
78 throw new CoreException(new CsarCreationErrorBuilder().build(), exc);
80 return baos.toByteArray();
84 public String createMetaFile(String entryDefinitionsFileName) {
85 return TOSCA_META_FILE_VERSION + META_FILE_DELIMITER + SPACE + TOSCA_META_FILE_VERSION_VALUE
86 + System.lineSeparator()
87 + CSAR_VERSION + META_FILE_DELIMITER + SPACE + CSAR_VERSION_VALUE + System.lineSeparator()
88 + CREATED_BY + META_FILE_DELIMITER + SPACE + CREATED_BY_VALUE + System.lineSeparator()
89 + ENTRY_DEFINITIONS + META_FILE_DELIMITER + SPACE + DEFINITIONS_FOLDER_NAME
90 + FILE_SEPARATOR + entryDefinitionsFileName;
94 public String getArtifactsFolderName() {
95 return ARTIFACTS_FOLDER_NAME;
98 private void createAndPackToscaMetaFile(ZipOutputStream zos, String entryDefinitionsFileName)
100 String metaFile = createMetaFile(entryDefinitionsFileName);
102 new ZipEntry((TOSCA_META_FOLDER_NAME + FILE_SEPARATOR + TOSCA_META_FILE_NAME)));
103 writeBytesToZip(zos, new ByteArrayInputStream(metaFile.getBytes()));
106 private void packDefinitions(ZipOutputStream zos, Map<String, ServiceTemplate> serviceTemplates)
108 for (Map.Entry<String, ServiceTemplate> serviceTemplate : serviceTemplates.entrySet()) {
109 String fileName = serviceTemplate.getKey();
110 zos.putNextEntry(new ZipEntry(DEFINITIONS_FOLDER_NAME + FILE_SEPARATOR + fileName));
112 FileUtils.convertToInputStream(serviceTemplate.getValue(), FileUtils.FileExtension.YAML));
116 private void packExternalArtifacts(ZipOutputStream zos, FileContentHandler externalArtifacts) {
118 for (String filenameIncludingPath : externalArtifacts.getFileList()) {
120 zos.putNextEntry(new ZipEntry(filenameIncludingPath));
121 writeBytesToZip(zos, externalArtifacts.getFileContent(filenameIncludingPath));
123 } catch (IOException exc) {
124 throw new RuntimeException(exc);
128 } catch (IOException ignore) {
136 private void packArtifacts(ZipOutputStream zos, FileContentHandler artifacts) {
138 for (String fileName : artifacts.getFileList()) {
140 zos.putNextEntry(new ZipEntry((ARTIFACTS_FOLDER_NAME + FILE_SEPARATOR + fileName)));
141 writeBytesToZip(zos, artifacts.getFileContent(fileName));
143 } catch (IOException exc) {
144 throw new RuntimeException(exc);
148 } catch (IOException ignore) {
155 private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException {
156 FileUtils.copy(is, zos);