2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.tosca.services.impl;
19 import org.apache.commons.io.IOUtils;
20 import org.openecomp.core.utilities.file.FileContentHandler;
21 import org.openecomp.core.utilities.file.FileUtils;
22 import org.openecomp.sdc.common.errors.CoreException;
23 import org.openecomp.sdc.datatypes.error.ErrorLevel;
24 import org.openecomp.sdc.logging.api.Logger;
25 import org.openecomp.sdc.logging.api.LoggerFactory;
26 import org.openecomp.sdc.logging.types.LoggerConstants;
27 import org.openecomp.sdc.logging.types.LoggerErrorCode;
28 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
29 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
30 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
31 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
32 import org.openecomp.sdc.tosca.exceptions.CsarCreationErrorBuilder;
33 import org.openecomp.sdc.tosca.exceptions.CsarMissingEntryPointErrorBuilder;
34 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
36 import java.io.BufferedOutputStream;
37 import java.io.ByteArrayInputStream;
38 import java.io.ByteArrayOutputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
43 import java.util.zip.ZipEntry;
44 import java.util.zip.ZipOutputStream;
47 public class ToscaFileOutputServiceCsarImpl implements ToscaFileOutputService {
48 static final String EXTERNAL_ARTIFACTS_FOLDER_NAME = "Artifacts";
49 private static final String DEFINITIONS_FOLDER_NAME = "Definitions";
50 private static final String ARTIFACTS_FOLDER_NAME = "Artifacts";
51 //todo currently duplicated, to be changed when external artifacts are separated from internal
52 private static final String TOSCA_META_FOLDER_NAME = "TOSCA-Metadata";
53 private static final String TOSCA_META_FILE_VERSION = "TOSCA-Meta-File-Version";
54 private static final String TOSCA_META_FILE_VERSION_VALUE = "1.0";
55 private static final String TOSCA_META_FILE_NAME = "TOSCA.meta";
56 private static final String CSAR_VERSION = "CSAR-Version";
57 private static final String CSAR_VERSION_VALUE = "1.1";
58 private static final String CREATED_BY = "Created-By";
59 private static final String CREATED_BY_VALUE = "ASDC Onboarding portal";
60 private static final String ENTRY_DEFINITIONS = "Entry-Definitions";
61 private static final String META_FILE_DELIMITER = ":";
62 private static final String SPACE = " ";
63 private static final String FILE_SEPARATOR = File.separator;
64 private static final Logger logger = LoggerFactory.getLogger(ToscaFileOutputServiceCsarImpl.class);
68 public byte[] createOutputFile(ToscaServiceModel toscaServiceModel,
69 FileContentHandler externalArtifacts) {
70 ByteArrayOutputStream baos = new ByteArrayOutputStream();
71 try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos))) {
72 packDefinitions(zos, toscaServiceModel.getServiceTemplates());
73 FileContentHandler artifactFiles = toscaServiceModel.getArtifactFiles();
74 if (artifactFiles != null && !artifactFiles.isEmpty()) {
75 packArtifacts(zos, artifactFiles);
77 if (toscaServiceModel.getEntryDefinitionServiceTemplate() == null) {
78 throw new CoreException(new CsarMissingEntryPointErrorBuilder().build());
80 createAndPackToscaMetaFile(zos, toscaServiceModel.getEntryDefinitionServiceTemplate());
81 if (externalArtifacts != null) {
82 packExternalArtifacts(zos, externalArtifacts);
84 } catch (IOException ex) {
85 throw new CoreException(new CsarCreationErrorBuilder().build(), ex);
87 return baos.toByteArray();
92 public String createMetaFile(String entryDefinitionsFileName) {
93 return TOSCA_META_FILE_VERSION + META_FILE_DELIMITER + SPACE + TOSCA_META_FILE_VERSION_VALUE
94 + System.lineSeparator()
95 + CSAR_VERSION + META_FILE_DELIMITER + SPACE + CSAR_VERSION_VALUE + System.lineSeparator()
96 + CREATED_BY + META_FILE_DELIMITER + SPACE + CREATED_BY_VALUE + System.lineSeparator()
97 + ENTRY_DEFINITIONS + META_FILE_DELIMITER + SPACE + DEFINITIONS_FOLDER_NAME
99 + entryDefinitionsFileName;
103 public String getArtifactsFolderName() {
104 return ARTIFACTS_FOLDER_NAME;
107 private void createAndPackToscaMetaFile(ZipOutputStream zos, String entryDefinitionsFileName)
109 String metaFile = createMetaFile(entryDefinitionsFileName);
111 new ZipEntry(TOSCA_META_FOLDER_NAME + FILE_SEPARATOR + TOSCA_META_FILE_NAME));
112 writeBytesToZip(zos, new ByteArrayInputStream(metaFile.getBytes()));
115 private void packDefinitions(ZipOutputStream zos, Map<String, ServiceTemplate> serviceTemplates)
117 for (Map.Entry<String, ServiceTemplate> serviceTemplate : serviceTemplates.entrySet()) {
118 String fileName = serviceTemplate.getKey();
119 zos.putNextEntry(new ZipEntry(DEFINITIONS_FOLDER_NAME + FILE_SEPARATOR + fileName));
121 FileUtils.convertToInputStream(serviceTemplate.getValue(), FileUtils.FileExtension.YAML));
125 private void packExternalArtifacts(ZipOutputStream zos, FileContentHandler externalArtifacts) {
127 for (String filenameIncludingPath : externalArtifacts.getFileList()) {
129 zos.putNextEntry(new ZipEntry(filenameIncludingPath));
130 writeBytesToZip(zos, externalArtifacts.getFileContent(filenameIncludingPath));
132 } catch (IOException ex) {
133 throw new RuntimeException(ex);
137 } catch (IOException ignore) {
138 logger.debug(ignore.getMessage(), ignore);
145 private void packArtifacts(ZipOutputStream zos, FileContentHandler artifacts) {
147 for (String fileName : artifacts.getFileList()) {
149 zos.putNextEntry(new ZipEntry(ARTIFACTS_FOLDER_NAME + FILE_SEPARATOR + fileName));
150 writeBytesToZip(zos, artifacts.getFileContent(fileName));
152 } catch (IOException ex) {
153 throw new RuntimeException(ex);
157 } catch (IOException ignore) {
158 logger.debug(ignore.getMessage(), ignore);
165 private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException {
167 IOUtils.copy(is, zos);