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