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.context.impl.MdcDataErrorMessage;
27 import org.openecomp.sdc.logging.types.LoggerConstants;
28 import org.openecomp.sdc.logging.types.LoggerErrorCode;
29 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
30 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
31 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
32 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
33 import org.openecomp.sdc.tosca.exceptions.CsarCreationErrorBuilder;
34 import org.openecomp.sdc.tosca.exceptions.CsarMissingEntryPointErrorBuilder;
35 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
37 import java.io.BufferedOutputStream;
38 import java.io.ByteArrayInputStream;
39 import java.io.ByteArrayOutputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
44 import java.util.zip.ZipEntry;
45 import java.util.zip.ZipOutputStream;
48 public class ToscaFileOutputServiceCsarImpl implements ToscaFileOutputService {
49 static final String EXTERNAL_ARTIFACTS_FOLDER_NAME = "Artifacts";
50 private static final String DEFINITIONS_FOLDER_NAME = "Definitions";
51 private static final String ARTIFACTS_FOLDER_NAME = "Artifacts";
52 //todo currently duplicated, to be changed when external artifacts are separated from internal
53 private static final String TOSCA_META_FOLDER_NAME = "TOSCA-Metadata";
54 private static final String TOSCA_META_FILE_VERSION = "TOSCA-Meta-File-Version";
55 private static final String TOSCA_META_FILE_VERSION_VALUE = "1.0";
56 private static final String TOSCA_META_FILE_NAME = "TOSCA.meta";
57 private static final String CSAR_VERSION = "CSAR-Version";
58 private static final String CSAR_VERSION_VALUE = "1.1";
59 private static final String CREATED_BY = "Created-By";
60 private static final String CREATED_BY_VALUE = "ASDC Onboarding portal";
61 private static final String ENTRY_DEFINITIONS = "Entry-Definitions";
62 private static final String META_FILE_DELIMITER = ":";
63 private static final String SPACE = " ";
64 private static final String FILE_SEPARATOR = File.separator;
65 private static final Logger logger = LoggerFactory.getLogger(ToscaFileOutputServiceCsarImpl.class);
69 public byte[] createOutputFile(ToscaServiceModel toscaServiceModel,
70 FileContentHandler externalArtifacts) {
71 ByteArrayOutputStream baos = new ByteArrayOutputStream();
72 try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos))) {
73 packDefinitions(zos, toscaServiceModel.getServiceTemplates());
74 FileContentHandler artifactFiles = toscaServiceModel.getArtifactFiles();
75 if (artifactFiles != null && !artifactFiles.isEmpty()) {
76 packArtifacts(zos, artifactFiles);
78 if (toscaServiceModel.getEntryDefinitionServiceTemplate() == null) {
79 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
80 LoggerTragetServiceName.CREATE_CSAR, ErrorLevel.ERROR.name(),
81 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.CREATE_CSAR);
82 throw new CoreException(new CsarMissingEntryPointErrorBuilder().build());
84 createAndPackToscaMetaFile(zos, toscaServiceModel.getEntryDefinitionServiceTemplate());
85 if (externalArtifacts != null) {
86 packExternalArtifacts(zos, externalArtifacts);
88 } catch (IOException ex) {
89 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
90 LoggerTragetServiceName.CREATE_CSAR, ErrorLevel.ERROR.name(),
91 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.CREATE_CSAR);
92 throw new CoreException(new CsarCreationErrorBuilder().build(), ex);
94 return baos.toByteArray();
99 public String createMetaFile(String entryDefinitionsFileName) {
100 return TOSCA_META_FILE_VERSION + META_FILE_DELIMITER + SPACE + TOSCA_META_FILE_VERSION_VALUE
101 + System.lineSeparator()
102 + CSAR_VERSION + META_FILE_DELIMITER + SPACE + CSAR_VERSION_VALUE + System.lineSeparator()
103 + CREATED_BY + META_FILE_DELIMITER + SPACE + CREATED_BY_VALUE + System.lineSeparator()
104 + ENTRY_DEFINITIONS + META_FILE_DELIMITER + SPACE + DEFINITIONS_FOLDER_NAME
106 + entryDefinitionsFileName;
110 public String getArtifactsFolderName() {
111 return ARTIFACTS_FOLDER_NAME;
114 private void createAndPackToscaMetaFile(ZipOutputStream zos, String entryDefinitionsFileName)
116 String metaFile = createMetaFile(entryDefinitionsFileName);
118 new ZipEntry(TOSCA_META_FOLDER_NAME + FILE_SEPARATOR + TOSCA_META_FILE_NAME));
119 writeBytesToZip(zos, new ByteArrayInputStream(metaFile.getBytes()));
122 private void packDefinitions(ZipOutputStream zos, Map<String, ServiceTemplate> serviceTemplates)
124 for (Map.Entry<String, ServiceTemplate> serviceTemplate : serviceTemplates.entrySet()) {
125 String fileName = serviceTemplate.getKey();
126 zos.putNextEntry(new ZipEntry(DEFINITIONS_FOLDER_NAME + FILE_SEPARATOR + fileName));
128 FileUtils.convertToInputStream(serviceTemplate.getValue(), FileUtils.FileExtension.YAML));
132 private void packExternalArtifacts(ZipOutputStream zos, FileContentHandler externalArtifacts) {
134 for (String filenameIncludingPath : externalArtifacts.getFileList()) {
136 zos.putNextEntry(new ZipEntry(filenameIncludingPath));
137 writeBytesToZip(zos, externalArtifacts.getFileContent(filenameIncludingPath));
139 } catch (IOException ex) {
140 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
141 LoggerTragetServiceName.PACK_ARTIFACTS, ErrorLevel.ERROR.name(),
142 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.PACK_ARTIFACTS);
143 throw new RuntimeException(ex);
147 } catch (IOException ignore) {
148 logger.debug(ignore.getMessage(), ignore);
155 private void packArtifacts(ZipOutputStream zos, FileContentHandler artifacts) {
157 for (String fileName : artifacts.getFileList()) {
159 zos.putNextEntry(new ZipEntry(ARTIFACTS_FOLDER_NAME + FILE_SEPARATOR + fileName));
160 writeBytesToZip(zos, artifacts.getFileContent(fileName));
162 } catch (IOException ex) {
163 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
164 LoggerTragetServiceName.PACK_ARTIFACTS, ErrorLevel.ERROR.name(),
165 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.PACK_ARTIFACTS);
166 throw new RuntimeException(ex);
170 } catch (IOException ignore) {
171 logger.debug(ignore.getMessage(), ignore);
178 private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException {
180 IOUtils.copy(is, zos);