6f4ec173a8e15398a2a8aca28ded1ecd19de6c92
[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.context.impl.MdcDataErrorMessage;
27 import org.openecomp.sdc.logging.types.LoggerConstants;
28 import org.openecomp.sdc.logging.types.LoggerErrorCode;
29 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
30 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
31 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
32 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
33 import org.openecomp.sdc.tosca.exceptions.CsarCreationErrorBuilder;
34 import org.openecomp.sdc.tosca.exceptions.CsarMissingEntryPointErrorBuilder;
35 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
36
37 import java.io.BufferedOutputStream;
38 import java.io.ByteArrayInputStream;
39 import java.io.ByteArrayOutputStream;
40 import java.io.File;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.util.Map;
44 import java.util.zip.ZipEntry;
45 import java.util.zip.ZipOutputStream;
46
47
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 = "TOSCA-Meta-File-Version";
55   private static final String TOSCA_META_FILE_VERSION_VALUE = "1.0";
56   private static final String TOSCA_META_FILE_NAME = "TOSCA.meta";
57   private static final String CSAR_VERSION = "CSAR-Version";
58   private static final String CSAR_VERSION_VALUE = "1.1";
59   private static final String CREATED_BY = "Created-By";
60   private static final String CREATED_BY_VALUE = "ASDC Onboarding portal";
61   private static final String ENTRY_DEFINITIONS = "Entry-Definitions";
62   private static final String META_FILE_DELIMITER = ":";
63   private static final String SPACE = " ";
64   private static final String FILE_SEPARATOR = File.separator;
65   private static final Logger logger = LoggerFactory.getLogger(ToscaFileOutputServiceCsarImpl.class);
66
67
68   @Override
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);
77       }
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());
83       }
84       createAndPackToscaMetaFile(zos, toscaServiceModel.getEntryDefinitionServiceTemplate());
85       if (externalArtifacts != null) {
86         packExternalArtifacts(zos, externalArtifacts);
87       }
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);
93     }
94     return baos.toByteArray();
95   }
96
97
98   @Override
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
105         + FILE_SEPARATOR
106         + entryDefinitionsFileName;
107   }
108
109   @Override
110   public String getArtifactsFolderName() {
111     return ARTIFACTS_FOLDER_NAME;
112   }
113
114   private void createAndPackToscaMetaFile(ZipOutputStream zos, String entryDefinitionsFileName)
115       throws IOException {
116     String metaFile = createMetaFile(entryDefinitionsFileName);
117     zos.putNextEntry(
118         new ZipEntry(TOSCA_META_FOLDER_NAME + FILE_SEPARATOR + TOSCA_META_FILE_NAME));
119     writeBytesToZip(zos, new ByteArrayInputStream(metaFile.getBytes()));
120   }
121
122   private void packDefinitions(ZipOutputStream zos, Map<String, ServiceTemplate> serviceTemplates)
123       throws IOException {
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));
127       writeBytesToZip(zos,
128           FileUtils.convertToInputStream(serviceTemplate.getValue(), FileUtils.FileExtension.YAML));
129     }
130   }
131
132   private void packExternalArtifacts(ZipOutputStream zos, FileContentHandler externalArtifacts) {
133
134     for (String filenameIncludingPath : externalArtifacts.getFileList()) {
135       try {
136         zos.putNextEntry(new ZipEntry(filenameIncludingPath));
137         writeBytesToZip(zos, externalArtifacts.getFileContent(filenameIncludingPath));
138
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);
144       } finally {
145         try {
146           zos.closeEntry();
147         } catch (IOException ignore) {
148             logger.debug(ignore.getMessage(), ignore);
149         }
150       }
151     }
152
153   }
154
155   private void packArtifacts(ZipOutputStream zos, FileContentHandler artifacts) {
156
157     for (String fileName : artifacts.getFileList()) {
158       try {
159         zos.putNextEntry(new ZipEntry(ARTIFACTS_FOLDER_NAME + FILE_SEPARATOR + fileName));
160         writeBytesToZip(zos, artifacts.getFileContent(fileName));
161
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);
167       } finally {
168         try {
169           zos.closeEntry();
170         } catch (IOException ignore) {
171             logger.debug(ignore.getMessage(), ignore);
172         }
173       }
174     }
175
176   }
177
178   private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException {
179     if(is != null){
180       IOUtils.copy(is, zos);
181     }
182   }
183 }