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 static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.CREATED_BY_ENTRY;
20 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.CSAR_VERSION_ENTRY;
21 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ENTRY_DEFINITIONS;
22 import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.TOSCA_META_FILE_VERSION_ENTRY;
24 import org.apache.commons.io.IOUtils;
25 import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
26 import org.openecomp.core.utilities.file.FileContentHandler;
27 import org.openecomp.core.utilities.file.FileUtils;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
32 import org.openecomp.sdc.tosca.exceptions.CsarCreationErrorBuilder;
33 import org.openecomp.sdc.tosca.exceptions.CsarMissingEntryPointErrorBuilder;
34 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
36 import java.io.BufferedOutputStream;
37 import java.io.ByteArrayInputStream;
38 import java.io.ByteArrayOutputStream;
40 import java.io.IOException;
41 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_VALUE = "1.0";
55 private static final String TOSCA_META_FILE_NAME = "TOSCA.meta";
56 private static final String CSAR_VERSION_VALUE = "1.1";
57 private static final String CREATED_BY_VALUE = "ASDC Onboarding portal";
58 private static final String META_FILE_DELIMITER = ":";
59 private static final String SPACE = " ";
60 private static final String FILE_SEPARATOR = File.separator;
61 private static final Logger logger = LoggerFactory.getLogger(ToscaFileOutputServiceCsarImpl.class);
65 public byte[] createOutputFile(ToscaServiceModel toscaServiceModel,
66 FileContentHandler externalArtifacts) {
67 ByteArrayOutputStream baos = new ByteArrayOutputStream();
68 try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos))) {
69 packDefinitions(zos, toscaServiceModel.getServiceTemplates());
70 FileContentHandler artifactFiles = toscaServiceModel.getArtifactFiles();
71 if (artifactFiles != null && !artifactFiles.isEmpty()) {
72 packArtifacts(zos, artifactFiles);
74 if (toscaServiceModel.getEntryDefinitionServiceTemplate() == null) {
75 throw new CoreException(new CsarMissingEntryPointErrorBuilder().build());
77 createAndPackToscaMetaFile(zos, toscaServiceModel.getEntryDefinitionServiceTemplate());
78 if (externalArtifacts != null) {
79 packExternalArtifacts(zos, externalArtifacts);
81 } catch (IOException ex) {
82 throw new CoreException(new CsarCreationErrorBuilder().build(), ex);
84 return baos.toByteArray();
89 public String createMetaFile(String entryDefinitionsFileName) {
90 return TOSCA_META_FILE_VERSION_ENTRY.getName() + META_FILE_DELIMITER + SPACE + TOSCA_META_FILE_VERSION_VALUE
91 + System.lineSeparator()
92 + CSAR_VERSION_ENTRY.getName() + META_FILE_DELIMITER + SPACE + CSAR_VERSION_VALUE + System.lineSeparator()
93 + CREATED_BY_ENTRY.getName() + META_FILE_DELIMITER + SPACE + CREATED_BY_VALUE + System.lineSeparator()
94 + ENTRY_DEFINITIONS.getName() + META_FILE_DELIMITER + SPACE + DEFINITIONS_FOLDER_NAME
96 + entryDefinitionsFileName;
100 public String getArtifactsFolderName() {
101 return ARTIFACTS_FOLDER_NAME;
104 private void createAndPackToscaMetaFile(ZipOutputStream zos, String entryDefinitionsFileName)
106 String metaFile = createMetaFile(entryDefinitionsFileName);
108 new ZipEntry(TOSCA_META_FOLDER_NAME + FILE_SEPARATOR + TOSCA_META_FILE_NAME));
109 writeBytesToZip(zos, new ByteArrayInputStream(metaFile.getBytes()));
112 private void packDefinitions(ZipOutputStream zos, Map<String, ServiceTemplate> serviceTemplates)
114 for (Map.Entry<String, ServiceTemplate> serviceTemplate : serviceTemplates.entrySet()) {
115 String fileName = serviceTemplate.getKey();
116 zos.putNextEntry(new ZipEntry(DEFINITIONS_FOLDER_NAME + FILE_SEPARATOR + fileName));
118 FileUtils.convertToInputStream(serviceTemplate.getValue(), FileUtils.FileExtension.YAML));
122 private void packExternalArtifacts(ZipOutputStream zos, FileContentHandler externalArtifacts) {
124 for (String filenameIncludingPath : externalArtifacts.getFileList()) {
126 zos.putNextEntry(new ZipEntry(filenameIncludingPath));
127 writeBytesToZip(zos, externalArtifacts.getFileContentAsStream(filenameIncludingPath));
129 } catch (IOException ex) {
130 throw new RuntimeException(ex);
134 } catch (IOException ignore) {
135 logger.debug(ignore.getMessage(), ignore);
142 private void packArtifacts(ZipOutputStream zos, FileContentHandler artifacts) {
144 for (String fileName : artifacts.getFileList()) {
146 zos.putNextEntry(new ZipEntry(ARTIFACTS_FOLDER_NAME + FILE_SEPARATOR + fileName));
147 writeBytesToZip(zos, artifacts.getFileContentAsStream(fileName));
149 } catch (IOException ex) {
150 throw new RuntimeException(ex);
154 } catch (IOException ignore) {
155 logger.debug(ignore.getMessage(), ignore);
162 private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException {
164 IOUtils.copy(is, zos);