[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-tosca-lib / src / main / java / org / openecomp / sdc / tosca / services / impl / ToscaFileOutputServiceCsarImpl.java
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.openecomp.core.utilities.file.FileContentHandler;
24 import org.openecomp.core.utilities.file.FileUtils;
25 import org.openecomp.sdc.common.errors.CoreException;
26 import org.openecomp.sdc.datatypes.error.ErrorLevel;
27 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
28 import org.openecomp.sdc.logging.types.LoggerConstants;
29 import org.openecomp.sdc.logging.types.LoggerErrorCode;
30 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
31 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
32 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
33 import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
34 import org.openecomp.sdc.tosca.exceptions.CsarCreationErrorBuilder;
35 import org.openecomp.sdc.tosca.exceptions.CsarMissingEntryPointErrorBuilder;
36 import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
37
38 import java.io.BufferedOutputStream;
39 import java.io.ByteArrayInputStream;
40 import java.io.ByteArrayOutputStream;
41 import java.io.File;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.util.Map;
45 import java.util.zip.ZipEntry;
46 import java.util.zip.ZipOutputStream;
47
48
49 public class ToscaFileOutputServiceCsarImpl implements ToscaFileOutputService {
50   static final String EXTERNAL_ARTIFACTS_FOLDER_NAME = "Artifacts";
51   private static final String DEFINITIONS_FOLDER_NAME = "Definitions";
52   private static final String ARTIFACTS_FOLDER_NAME = "Artifacts";
53   //todo currently duplicated, to be changed when external artifacts are separated from internal
54   private static final String TOSCA_META_FOLDER_NAME = "TOSCA-Metadata";
55   private static final String TOSCA_META_FILE_VERSION = "TOSCA-Meta-File-Version";
56   private static final String TOSCA_META_FILE_VERSION_VALUE = "1.0";
57   private static final String TOSCA_META_FILE_NAME = "TOSCA.meta";
58   private static final String CSAR_VERSION = "CSAR-Version";
59   private static final String CSAR_VERSION_VALUE = "1.1";
60   private static final String CREATED_BY = "Created-By";
61   private static final String CREATED_BY_VALUE = "ASDC Onboarding portal";
62   private static final String ENTRY_DEFINITIONS = "Entry-Definitions";
63   private static final String META_FILE_DELIMITER = ":";
64   private static final String SPACE = " ";
65   private static final String FILE_SEPARATOR = File.separator;
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           //do nothing
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           //do nothing
172         }
173       }
174     }
175
176   }
177
178   private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException {
179     FileUtils.copy(is, zos);
180   }
181 }