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