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.
16 package org.openecomp.sdc.tosca.services.impl;
18 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.CREATED_BY_ENTRY;
19 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.CSAR_VERSION_ENTRY;
20 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ENTRY_DEFINITIONS;
21 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.TOSCA_META_FILE_VERSION_ENTRY;
23 import java.io.BufferedOutputStream;
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
30 import java.util.zip.ZipEntry;
31 import java.util.zip.ZipOutputStream;
32 import org.apache.commons.io.IOUtils;
33 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
34 import org.openecomp.core.utilities.file.FileContentHandler;
35 import org.openecomp.core.utilities.file.FileUtils;
36 import org.openecomp.sdc.common.errors.CoreException;
37 import org.openecomp.sdc.logging.api.Logger;
38 import org.openecomp.sdc.logging.api.LoggerFactory;
39 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
40 import org.openecomp.sdc.tosca.exceptions.CsarCreationErrorBuilder;
41 import org.openecomp.sdc.tosca.exceptions.CsarMissingEntryPointErrorBuilder;
42 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
44 public class ToscaFileOutputServiceCsarImpl implements ToscaFileOutputService {
46 static final String EXTERNAL_ARTIFACTS_FOLDER_NAME = "Artifacts";
47 private static final String DEFINITIONS_FOLDER_NAME = "Definitions";
48 private static final String ARTIFACTS_FOLDER_NAME = "Artifacts";
49 //todo currently duplicated, to be changed when external artifacts are separated from internal
50 private static final String TOSCA_META_FOLDER_NAME = "TOSCA-Metadata";
51 private static final String TOSCA_META_FILE_VERSION_VALUE = "1.0";
52 private static final String TOSCA_META_FILE_NAME = "TOSCA.meta";
53 private static final String CSAR_VERSION_VALUE = "1.1";
54 private static final String CREATED_BY_VALUE = "ASDC Onboarding portal";
55 private static final String META_FILE_DELIMITER = ":";
56 private static final String SPACE = " ";
57 private static final String FILE_SEPARATOR = File.separator;
58 private static final Logger logger = LoggerFactory.getLogger(ToscaFileOutputServiceCsarImpl.class);
61 public byte[] createOutputFile(ToscaServiceModel toscaServiceModel, FileContentHandler externalArtifacts) {
62 ByteArrayOutputStream baos = new ByteArrayOutputStream();
63 try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos))) {
64 packDefinitions(zos, toscaServiceModel.getServiceTemplates());
65 FileContentHandler artifactFiles = toscaServiceModel.getArtifactFiles();
66 if (artifactFiles != null && !artifactFiles.isEmpty()) {
67 packArtifacts(zos, artifactFiles);
69 if (toscaServiceModel.getEntryDefinitionServiceTemplate() == null) {
70 throw new CoreException(new CsarMissingEntryPointErrorBuilder().build());
72 createAndPackToscaMetaFile(zos, toscaServiceModel.getEntryDefinitionServiceTemplate());
73 if (externalArtifacts != null) {
74 packExternalArtifacts(zos, externalArtifacts);
76 } catch (IOException ex) {
77 throw new CoreException(new CsarCreationErrorBuilder().build(), ex);
79 return baos.toByteArray();
83 public String createMetaFile(String entryDefinitionsFileName) {
84 return TOSCA_META_FILE_VERSION_ENTRY.getName() + META_FILE_DELIMITER + SPACE + TOSCA_META_FILE_VERSION_VALUE + System.lineSeparator()
85 + CSAR_VERSION_ENTRY.getName() + META_FILE_DELIMITER + SPACE + CSAR_VERSION_VALUE + System.lineSeparator() + CREATED_BY_ENTRY.getName()
86 + META_FILE_DELIMITER + SPACE + CREATED_BY_VALUE + System.lineSeparator() + ENTRY_DEFINITIONS.getName() + META_FILE_DELIMITER + SPACE
87 + DEFINITIONS_FOLDER_NAME + FILE_SEPARATOR + entryDefinitionsFileName;
91 public String getArtifactsFolderName() {
92 return ARTIFACTS_FOLDER_NAME;
95 private void createAndPackToscaMetaFile(ZipOutputStream zos, String entryDefinitionsFileName) throws IOException {
96 String metaFile = createMetaFile(entryDefinitionsFileName);
97 zos.putNextEntry(new ZipEntry(TOSCA_META_FOLDER_NAME + FILE_SEPARATOR + TOSCA_META_FILE_NAME));
98 writeBytesToZip(zos, new ByteArrayInputStream(metaFile.getBytes()));
101 private void packDefinitions(ZipOutputStream zos, Map<String, ServiceTemplate> serviceTemplates) throws IOException {
102 for (Map.Entry<String, ServiceTemplate> serviceTemplate : serviceTemplates.entrySet()) {
103 String fileName = serviceTemplate.getKey();
104 zos.putNextEntry(new ZipEntry(DEFINITIONS_FOLDER_NAME + FILE_SEPARATOR + fileName));
105 writeBytesToZip(zos, FileUtils.convertToInputStream(serviceTemplate.getValue(), FileUtils.FileExtension.YAML));
109 private void packExternalArtifacts(ZipOutputStream zos, FileContentHandler externalArtifacts) {
110 for (String filenameIncludingPath : externalArtifacts.getFileList()) {
112 zos.putNextEntry(new ZipEntry(filenameIncludingPath));
113 writeBytesToZip(zos, externalArtifacts.getFileContentAsStream(filenameIncludingPath));
114 } catch (IOException ex) {
115 throw new RuntimeException(ex);
119 } catch (IOException ignore) {
120 logger.debug(ignore.getMessage(), ignore);
126 private void packArtifacts(ZipOutputStream zos, FileContentHandler artifacts) {
127 for (String fileName : artifacts.getFileList()) {
129 zos.putNextEntry(new ZipEntry(ARTIFACTS_FOLDER_NAME + FILE_SEPARATOR + fileName));
130 writeBytesToZip(zos, artifacts.getFileContentAsStream(fileName));
131 } catch (IOException ex) {
132 throw new RuntimeException(ex);
136 } catch (IOException ignore) {
137 logger.debug(ignore.getMessage(), ignore);
143 private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException {
145 IOUtils.copy(is, zos);