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.context.impl.MdcDataErrorMessage;
29 import org.openecomp.sdc.logging.types.LoggerConstants;
30 import org.openecomp.sdc.logging.types.LoggerErrorCode;
31 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
32 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
33 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
34 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
35 import org.openecomp.sdc.tosca.exceptions.CsarCreationErrorBuilder;
36 import org.openecomp.sdc.tosca.exceptions.CsarMissingEntryPointErrorBuilder;
37 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
39 import java.io.BufferedOutputStream;
40 import java.io.ByteArrayInputStream;
41 import java.io.ByteArrayOutputStream;
43 import java.io.IOException;
44 import java.io.InputStream;
46 import java.util.zip.ZipEntry;
47 import java.util.zip.ZipOutputStream;
50 public class ToscaFileOutputServiceCsarImpl implements ToscaFileOutputService {
51 static final String EXTERNAL_ARTIFACTS_FOLDER_NAME = "Artifacts";
52 private static final String DEFINITIONS_FOLDER_NAME = "Definitions";
53 private static final String ARTIFACTS_FOLDER_NAME = "Artifacts";
54 //todo currently duplicated, to be changed when external artifacts are separated from internal
55 private static final String TOSCA_META_FOLDER_NAME = "TOSCA-Metadata";
56 private static final String TOSCA_META_FILE_VERSION = "TOSCA-Meta-File-Version";
57 private static final String TOSCA_META_FILE_VERSION_VALUE = "1.0";
58 private static final String TOSCA_META_FILE_NAME = "TOSCA.meta";
59 private static final String CSAR_VERSION = "CSAR-Version";
60 private static final String CSAR_VERSION_VALUE = "1.1";
61 private static final String CREATED_BY = "Created-By";
62 private static final String CREATED_BY_VALUE = "ASDC Onboarding portal";
63 private static final String ENTRY_DEFINITIONS = "Entry-Definitions";
64 private static final String META_FILE_DELIMITER = ":";
65 private static final String SPACE = " ";
66 private static final String FILE_SEPARATOR = File.separator;
70 public byte[] createOutputFile(ToscaServiceModel toscaServiceModel,
71 FileContentHandler externalArtifacts) {
72 ByteArrayOutputStream baos = new ByteArrayOutputStream();
73 try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos))) {
74 packDefinitions(zos, toscaServiceModel.getServiceTemplates());
75 FileContentHandler artifactFiles = toscaServiceModel.getArtifactFiles();
76 if (artifactFiles != null && !artifactFiles.isEmpty()) {
77 packArtifacts(zos, artifactFiles);
79 if (toscaServiceModel.getEntryDefinitionServiceTemplate() == null) {
80 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
81 LoggerTragetServiceName.CREATE_CSAR, ErrorLevel.ERROR.name(),
82 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.CREATE_CSAR);
83 throw new CoreException(new CsarMissingEntryPointErrorBuilder().build());
85 createAndPackToscaMetaFile(zos, toscaServiceModel.getEntryDefinitionServiceTemplate());
86 if (externalArtifacts != null) {
87 packExternalArtifacts(zos, externalArtifacts);
89 } catch (IOException ex) {
90 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
91 LoggerTragetServiceName.CREATE_CSAR, ErrorLevel.ERROR.name(),
92 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.CREATE_CSAR);
93 throw new CoreException(new CsarCreationErrorBuilder().build(), ex);
95 return baos.toByteArray();
100 public String createMetaFile(String entryDefinitionsFileName) {
101 return TOSCA_META_FILE_VERSION + META_FILE_DELIMITER + SPACE + TOSCA_META_FILE_VERSION_VALUE
102 + System.lineSeparator()
103 + CSAR_VERSION + META_FILE_DELIMITER + SPACE + CSAR_VERSION_VALUE + System.lineSeparator()
104 + CREATED_BY + META_FILE_DELIMITER + SPACE + CREATED_BY_VALUE + System.lineSeparator()
105 + ENTRY_DEFINITIONS + META_FILE_DELIMITER + SPACE + DEFINITIONS_FOLDER_NAME
107 + entryDefinitionsFileName;
111 public String getArtifactsFolderName() {
112 return ARTIFACTS_FOLDER_NAME;
115 private void createAndPackToscaMetaFile(ZipOutputStream zos, String entryDefinitionsFileName)
117 String metaFile = createMetaFile(entryDefinitionsFileName);
119 new ZipEntry((TOSCA_META_FOLDER_NAME + FILE_SEPARATOR + TOSCA_META_FILE_NAME)));
120 writeBytesToZip(zos, new ByteArrayInputStream(metaFile.getBytes()));
123 private void packDefinitions(ZipOutputStream zos, Map<String, ServiceTemplate> serviceTemplates)
125 for (Map.Entry<String, ServiceTemplate> serviceTemplate : serviceTemplates.entrySet()) {
126 String fileName = serviceTemplate.getKey();
127 zos.putNextEntry(new ZipEntry(DEFINITIONS_FOLDER_NAME + FILE_SEPARATOR + fileName));
129 FileUtils.convertToInputStream(serviceTemplate.getValue(), FileUtils.FileExtension.YAML));
133 private void packExternalArtifacts(ZipOutputStream zos, FileContentHandler externalArtifacts) {
135 for (String filenameIncludingPath : externalArtifacts.getFileList()) {
137 zos.putNextEntry(new ZipEntry(filenameIncludingPath));
138 writeBytesToZip(zos, externalArtifacts.getFileContent(filenameIncludingPath));
140 } catch (IOException ex) {
141 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
142 LoggerTragetServiceName.PACK_ARTIFACTS, ErrorLevel.ERROR.name(),
143 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.PACK_ARTIFACTS);
144 throw new RuntimeException(ex);
148 } catch (IOException ignore) {
156 private void packArtifacts(ZipOutputStream zos, FileContentHandler artifacts) {
158 for (String fileName : artifacts.getFileList()) {
160 zos.putNextEntry(new ZipEntry((ARTIFACTS_FOLDER_NAME + FILE_SEPARATOR + fileName)));
161 writeBytesToZip(zos, artifacts.getFileContent(fileName));
163 } catch (IOException ex) {
164 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
165 LoggerTragetServiceName.PACK_ARTIFACTS, ErrorLevel.ERROR.name(),
166 LoggerErrorCode.DATA_ERROR.getErrorCode(), LoggerErrorDescription.PACK_ARTIFACTS);
167 throw new RuntimeException(ex);
171 } catch (IOException ignore) {
179 private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException {
181 IOUtils.copy(is, zos);