9a3ed8e73f8a4c4b3c7613488d074ca57fe4cf03
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.tosca.services.impl;
22
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;
38
39 import java.io.BufferedOutputStream;
40 import java.io.ByteArrayInputStream;
41 import java.io.ByteArrayOutputStream;
42 import java.io.File;
43 import java.io.IOException;
44 import java.io.InputStream;
45 import java.util.Map;
46 import java.util.zip.ZipEntry;
47 import java.util.zip.ZipOutputStream;
48
49
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;
67
68
69   @Override
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);
78       }
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());
84       }
85       createAndPackToscaMetaFile(zos, toscaServiceModel.getEntryDefinitionServiceTemplate());
86       if (externalArtifacts != null) {
87         packExternalArtifacts(zos, externalArtifacts);
88       }
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);
94     }
95     return baos.toByteArray();
96   }
97
98
99   @Override
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
106         + FILE_SEPARATOR
107         + entryDefinitionsFileName;
108   }
109
110   @Override
111   public String getArtifactsFolderName() {
112     return ARTIFACTS_FOLDER_NAME;
113   }
114
115   private void createAndPackToscaMetaFile(ZipOutputStream zos, String entryDefinitionsFileName)
116       throws IOException {
117     String metaFile = createMetaFile(entryDefinitionsFileName);
118     zos.putNextEntry(
119         new ZipEntry((TOSCA_META_FOLDER_NAME + FILE_SEPARATOR + TOSCA_META_FILE_NAME)));
120     writeBytesToZip(zos, new ByteArrayInputStream(metaFile.getBytes()));
121   }
122
123   private void packDefinitions(ZipOutputStream zos, Map<String, ServiceTemplate> serviceTemplates)
124       throws IOException {
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));
128       writeBytesToZip(zos,
129           FileUtils.convertToInputStream(serviceTemplate.getValue(), FileUtils.FileExtension.YAML));
130     }
131   }
132
133   private void packExternalArtifacts(ZipOutputStream zos, FileContentHandler externalArtifacts) {
134
135     for (String filenameIncludingPath : externalArtifacts.getFileList()) {
136       try {
137         zos.putNextEntry(new ZipEntry(filenameIncludingPath));
138         writeBytesToZip(zos, externalArtifacts.getFileContent(filenameIncludingPath));
139
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);
145       } finally {
146         try {
147           zos.closeEntry();
148         } catch (IOException ignore) {
149           //do nothing
150         }
151       }
152     }
153
154   }
155
156   private void packArtifacts(ZipOutputStream zos, FileContentHandler artifacts) {
157
158     for (String fileName : artifacts.getFileList()) {
159       try {
160         zos.putNextEntry(new ZipEntry((ARTIFACTS_FOLDER_NAME + FILE_SEPARATOR + fileName)));
161         writeBytesToZip(zos, artifacts.getFileContent(fileName));
162
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);
168       } finally {
169         try {
170           zos.closeEntry();
171         } catch (IOException ignore) {
172           //do nothing
173         }
174       }
175     }
176
177   }
178
179   private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException {
180     if(is != null){
181       IOUtils.copy(is, zos);
182     }
183   }
184 }