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