f0a12e6c10d62a487eec6ce83006ca5727f6b8eb
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.tosca.services.impl;
18
19 import org.apache.commons.io.IOUtils;
20 import org.openecomp.core.utilities.file.FileContentHandler;
21 import org.openecomp.core.utilities.file.FileUtils;
22 import org.openecomp.sdc.common.errors.CoreException;
23 import org.openecomp.sdc.datatypes.error.ErrorLevel;
24 import org.openecomp.sdc.logging.api.Logger;
25 import org.openecomp.sdc.logging.api.LoggerFactory;
26 import org.openecomp.sdc.logging.types.LoggerConstants;
27 import org.openecomp.sdc.logging.types.LoggerErrorCode;
28 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
29 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
30 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
31 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
32 import org.openecomp.sdc.tosca.exceptions.CsarCreationErrorBuilder;
33 import org.openecomp.sdc.tosca.exceptions.CsarMissingEntryPointErrorBuilder;
34 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
35
36 import java.io.BufferedOutputStream;
37 import java.io.ByteArrayInputStream;
38 import java.io.ByteArrayOutputStream;
39 import java.io.File;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.util.Map;
43 import java.util.zip.ZipEntry;
44 import java.util.zip.ZipOutputStream;
45
46
47 public class ToscaFileOutputServiceCsarImpl implements ToscaFileOutputService {
48   static final String EXTERNAL_ARTIFACTS_FOLDER_NAME = "Artifacts";
49   private static final String DEFINITIONS_FOLDER_NAME = "Definitions";
50   private static final String ARTIFACTS_FOLDER_NAME = "Artifacts";
51   //todo currently duplicated, to be changed when external artifacts are separated from internal
52   private static final String TOSCA_META_FOLDER_NAME = "TOSCA-Metadata";
53   private static final String TOSCA_META_FILE_VERSION = "TOSCA-Meta-File-Version";
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 = "CSAR-Version";
57   private static final String CSAR_VERSION_VALUE = "1.1";
58   private static final String CREATED_BY = "Created-By";
59   private static final String CREATED_BY_VALUE = "ASDC Onboarding portal";
60   private static final String ENTRY_DEFINITIONS = "Entry-Definitions";
61   private static final String META_FILE_DELIMITER = ":";
62   private static final String SPACE = " ";
63   private static final String FILE_SEPARATOR = File.separator;
64   private static final Logger logger = LoggerFactory.getLogger(ToscaFileOutputServiceCsarImpl.class);
65
66
67   @Override
68   public byte[] createOutputFile(ToscaServiceModel toscaServiceModel,
69                                  FileContentHandler externalArtifacts) {
70     ByteArrayOutputStream baos = new ByteArrayOutputStream();
71     try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos))) {
72       packDefinitions(zos, toscaServiceModel.getServiceTemplates());
73       FileContentHandler artifactFiles = toscaServiceModel.getArtifactFiles();
74       if (artifactFiles != null && !artifactFiles.isEmpty()) {
75         packArtifacts(zos, artifactFiles);
76       }
77       if (toscaServiceModel.getEntryDefinitionServiceTemplate() == null) {
78         throw new CoreException(new CsarMissingEntryPointErrorBuilder().build());
79       }
80       createAndPackToscaMetaFile(zos, toscaServiceModel.getEntryDefinitionServiceTemplate());
81       if (externalArtifacts != null) {
82         packExternalArtifacts(zos, externalArtifacts);
83       }
84     } catch (IOException ex) {
85       throw new CoreException(new CsarCreationErrorBuilder().build(), ex);
86     }
87     return baos.toByteArray();
88   }
89
90
91   @Override
92   public String createMetaFile(String entryDefinitionsFileName) {
93     return TOSCA_META_FILE_VERSION + META_FILE_DELIMITER + SPACE + TOSCA_META_FILE_VERSION_VALUE
94         + System.lineSeparator()
95         + CSAR_VERSION + META_FILE_DELIMITER + SPACE + CSAR_VERSION_VALUE + System.lineSeparator()
96         + CREATED_BY + META_FILE_DELIMITER + SPACE + CREATED_BY_VALUE + System.lineSeparator()
97         + ENTRY_DEFINITIONS + META_FILE_DELIMITER + SPACE + DEFINITIONS_FOLDER_NAME
98         + FILE_SEPARATOR
99         + entryDefinitionsFileName;
100   }
101
102   @Override
103   public String getArtifactsFolderName() {
104     return ARTIFACTS_FOLDER_NAME;
105   }
106
107   private void createAndPackToscaMetaFile(ZipOutputStream zos, String entryDefinitionsFileName)
108       throws IOException {
109     String metaFile = createMetaFile(entryDefinitionsFileName);
110     zos.putNextEntry(
111         new ZipEntry(TOSCA_META_FOLDER_NAME + FILE_SEPARATOR + TOSCA_META_FILE_NAME));
112     writeBytesToZip(zos, new ByteArrayInputStream(metaFile.getBytes()));
113   }
114
115   private void packDefinitions(ZipOutputStream zos, Map<String, ServiceTemplate> serviceTemplates)
116       throws IOException {
117     for (Map.Entry<String, ServiceTemplate> serviceTemplate : serviceTemplates.entrySet()) {
118       String fileName = serviceTemplate.getKey();
119       zos.putNextEntry(new ZipEntry(DEFINITIONS_FOLDER_NAME + FILE_SEPARATOR + fileName));
120       writeBytesToZip(zos,
121           FileUtils.convertToInputStream(serviceTemplate.getValue(), FileUtils.FileExtension.YAML));
122     }
123   }
124
125   private void packExternalArtifacts(ZipOutputStream zos, FileContentHandler externalArtifacts) {
126
127     for (String filenameIncludingPath : externalArtifacts.getFileList()) {
128       try {
129         zos.putNextEntry(new ZipEntry(filenameIncludingPath));
130         writeBytesToZip(zos, externalArtifacts.getFileContent(filenameIncludingPath));
131
132       } catch (IOException ex) {
133         throw new RuntimeException(ex);
134       } finally {
135         try {
136           zos.closeEntry();
137         } catch (IOException ignore) {
138             logger.debug(ignore.getMessage(), ignore);
139         }
140       }
141     }
142
143   }
144
145   private void packArtifacts(ZipOutputStream zos, FileContentHandler artifacts) {
146
147     for (String fileName : artifacts.getFileList()) {
148       try {
149         zos.putNextEntry(new ZipEntry(ARTIFACTS_FOLDER_NAME + FILE_SEPARATOR + fileName));
150         writeBytesToZip(zos, artifacts.getFileContent(fileName));
151
152       } catch (IOException ex) {
153         throw new RuntimeException(ex);
154       } finally {
155         try {
156           zos.closeEntry();
157         } catch (IOException ignore) {
158             logger.debug(ignore.getMessage(), ignore);
159         }
160       }
161     }
162
163   }
164
165   private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException {
166     if(is != null){
167       IOUtils.copy(is, zos);
168     }
169   }
170 }