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.apache.commons.io.IOUtils;
24 import org.openecomp.core.utilities.file.FileContentHandler;
25 import org.openecomp.core.utilities.file.FileUtils;
26 import org.openecomp.sdc.common.errors.CoreException;
27 import org.openecomp.sdc.datatypes.error.ErrorLevel;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
31 import org.openecomp.sdc.logging.types.LoggerConstants;
32 import org.openecomp.sdc.logging.types.LoggerErrorCode;
33 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
34 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
35 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
36 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
37 import org.openecomp.sdc.tosca.exceptions.CsarCreationErrorBuilder;
38 import org.openecomp.sdc.tosca.exceptions.CsarMissingEntryPointErrorBuilder;
39 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
41 import java.io.BufferedOutputStream;
42 import java.io.ByteArrayInputStream;
43 import java.io.ByteArrayOutputStream;
45 import java.io.IOException;
46 import java.io.InputStream;
48 import java.util.zip.ZipEntry;
49 import java.util.zip.ZipOutputStream;
52 public class ToscaFileOutputServiceCsarImpl implements ToscaFileOutputService {
53 static final String EXTERNAL_ARTIFACTS_FOLDER_NAME = "Artifacts";
54 private static final String DEFINITIONS_FOLDER_NAME = "Definitions";
55 private static final String ARTIFACTS_FOLDER_NAME = "Artifacts";
56 //todo currently duplicated, to be changed when external artifacts are separated from internal
57 private static final String TOSCA_META_FOLDER_NAME = "TOSCA-Metadata";
58 private static final String TOSCA_META_FILE_VERSION = "TOSCA-Meta-File-Version";
59 private static final String TOSCA_META_FILE_VERSION_VALUE = "1.0";
60 private static final String TOSCA_META_FILE_NAME = "TOSCA.meta";
61 private static final String CSAR_VERSION = "CSAR-Version";
62 private static final String CSAR_VERSION_VALUE = "1.1";
63 private static final String CREATED_BY = "Created-By";
64 private static final String CREATED_BY_VALUE = "ASDC Onboarding portal";
65 private static final String ENTRY_DEFINITIONS = "Entry-Definitions";
66 private static final String META_FILE_DELIMITER = ":";
67 private static final String SPACE = " ";
68 private static final String FILE_SEPARATOR = File.separator;
69 private static final Logger logger = LoggerFactory.getLogger(ToscaFileOutputServiceCsarImpl.class);
73 public byte[] createOutputFile(ToscaServiceModel toscaServiceModel,
74 FileContentHandler externalArtifacts) {
75 ByteArrayOutputStream baos = new ByteArrayOutputStream();
76 try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos))) {
77 packDefinitions(zos, toscaServiceModel.getServiceTemplates());
78 FileContentHandler artifactFiles = toscaServiceModel.getArtifactFiles();
79 if (artifactFiles != null && !artifactFiles.isEmpty()) {
80 packArtifacts(zos, artifactFiles);
82 if (toscaServiceModel.getEntryDefinitionServiceTemplate() == null) {
83 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
84 LoggerTragetServiceName.CREATE_CSAR, ErrorLevel.ERROR.name(),
85 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.CREATE_CSAR);
86 throw new CoreException(new CsarMissingEntryPointErrorBuilder().build());
88 createAndPackToscaMetaFile(zos, toscaServiceModel.getEntryDefinitionServiceTemplate());
89 if (externalArtifacts != null) {
90 packExternalArtifacts(zos, externalArtifacts);
92 } catch (IOException ex) {
93 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
94 LoggerTragetServiceName.CREATE_CSAR, ErrorLevel.ERROR.name(),
95 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.CREATE_CSAR);
96 throw new CoreException(new CsarCreationErrorBuilder().build(), ex);
98 return baos.toByteArray();
103 public String createMetaFile(String entryDefinitionsFileName) {
104 return TOSCA_META_FILE_VERSION + META_FILE_DELIMITER + SPACE + TOSCA_META_FILE_VERSION_VALUE
105 + System.lineSeparator()
106 + CSAR_VERSION + META_FILE_DELIMITER + SPACE + CSAR_VERSION_VALUE + System.lineSeparator()
107 + CREATED_BY + META_FILE_DELIMITER + SPACE + CREATED_BY_VALUE + System.lineSeparator()
108 + ENTRY_DEFINITIONS + META_FILE_DELIMITER + SPACE + DEFINITIONS_FOLDER_NAME
110 + entryDefinitionsFileName;
114 public String getArtifactsFolderName() {
115 return ARTIFACTS_FOLDER_NAME;
118 private void createAndPackToscaMetaFile(ZipOutputStream zos, String entryDefinitionsFileName)
120 String metaFile = createMetaFile(entryDefinitionsFileName);
122 new ZipEntry((TOSCA_META_FOLDER_NAME + FILE_SEPARATOR + TOSCA_META_FILE_NAME)));
123 writeBytesToZip(zos, new ByteArrayInputStream(metaFile.getBytes()));
126 private void packDefinitions(ZipOutputStream zos, Map<String, ServiceTemplate> serviceTemplates)
128 for (Map.Entry<String, ServiceTemplate> serviceTemplate : serviceTemplates.entrySet()) {
129 String fileName = serviceTemplate.getKey();
130 zos.putNextEntry(new ZipEntry(DEFINITIONS_FOLDER_NAME + FILE_SEPARATOR + fileName));
132 FileUtils.convertToInputStream(serviceTemplate.getValue(), FileUtils.FileExtension.YAML));
136 private void packExternalArtifacts(ZipOutputStream zos, FileContentHandler externalArtifacts) {
138 for (String filenameIncludingPath : externalArtifacts.getFileList()) {
140 zos.putNextEntry(new ZipEntry(filenameIncludingPath));
141 writeBytesToZip(zos, externalArtifacts.getFileContent(filenameIncludingPath));
143 } catch (IOException ex) {
144 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
145 LoggerTragetServiceName.PACK_ARTIFACTS, ErrorLevel.ERROR.name(),
146 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.PACK_ARTIFACTS);
147 throw new RuntimeException(ex);
151 } catch (IOException ignore) {
152 logger.debug(ignore.getMessage(), ignore);
159 private void packArtifacts(ZipOutputStream zos, FileContentHandler artifacts) {
161 for (String fileName : artifacts.getFileList()) {
163 zos.putNextEntry(new ZipEntry((ARTIFACTS_FOLDER_NAME + FILE_SEPARATOR + fileName)));
164 writeBytesToZip(zos, artifacts.getFileContent(fileName));
166 } catch (IOException ex) {
167 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
168 LoggerTragetServiceName.PACK_ARTIFACTS, ErrorLevel.ERROR.name(),
169 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.PACK_ARTIFACTS);
170 throw new RuntimeException(ex);
174 } catch (IOException ignore) {
175 logger.debug(ignore.getMessage(), ignore);
182 private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException {
184 IOUtils.copy(is, zos);