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