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.datatypes.error.ErrorLevel;
27 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
28 import org.openecomp.sdc.logging.types.LoggerConstants;
29 import org.openecomp.sdc.logging.types.LoggerErrorCode;
30 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
31 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
32 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
33 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
34 import org.openecomp.sdc.tosca.exceptions.CsarCreationErrorBuilder;
35 import org.openecomp.sdc.tosca.exceptions.CsarMissingEntryPointErrorBuilder;
36 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
38 import java.io.BufferedOutputStream;
39 import java.io.ByteArrayInputStream;
40 import java.io.ByteArrayOutputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
45 import java.util.zip.ZipEntry;
46 import java.util.zip.ZipOutputStream;
49 public class ToscaFileOutputServiceCsarImpl implements ToscaFileOutputService {
50 static final String EXTERNAL_ARTIFACTS_FOLDER_NAME = "Artifacts";
51 private static final String DEFINITIONS_FOLDER_NAME = "Definitions";
52 private static final String ARTIFACTS_FOLDER_NAME = "Artifacts";
53 //todo currently duplicated, to be changed when external artifacts are separated from internal
54 private static final String TOSCA_META_FOLDER_NAME = "TOSCA-Metadata";
55 private static final String TOSCA_META_FILE_VERSION = "TOSCA-Meta-File-Version";
56 private static final String TOSCA_META_FILE_VERSION_VALUE = "1.0";
57 private static final String TOSCA_META_FILE_NAME = "TOSCA.meta";
58 private static final String CSAR_VERSION = "CSAR-Version";
59 private static final String CSAR_VERSION_VALUE = "1.1";
60 private static final String CREATED_BY = "Created-By";
61 private static final String CREATED_BY_VALUE = "ASDC Onboarding portal";
62 private static final String ENTRY_DEFINITIONS = "Entry-Definitions";
63 private static final String META_FILE_DELIMITER = ":";
64 private static final String SPACE = " ";
65 private static final String FILE_SEPARATOR = File.separator;
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) {
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) {
178 private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException {
179 FileUtils.copy(is, zos);