summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImpl.java')
-rw-r--r--openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImpl.java213
1 files changed, 97 insertions, 116 deletions
diff --git a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImpl.java b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImpl.java
index 7c0d1271fb..90047aa6df 100644
--- a/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImpl.java
+++ b/openecomp-be/lib/openecomp-tosca-lib/src/main/java/org/openecomp/sdc/tosca/services/impl/ToscaFileOutputServiceCsarImpl.java
@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.openecomp.sdc.tosca.services.impl;
import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.CREATED_BY_ENTRY;
@@ -21,6 +20,15 @@ import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.CSAR_VERSION_ENTRY;
import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.ENTRY_DEFINITIONS;
import static org.openecomp.sdc.tosca.csar.ToscaMetaEntry.TOSCA_META_FILE_VERSION_ENTRY;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
import org.apache.commons.io.IOUtils;
import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
import org.openecomp.core.utilities.file.FileContentHandler;
@@ -33,135 +41,108 @@ import org.openecomp.sdc.tosca.exceptions.CsarCreationErrorBuilder;
import org.openecomp.sdc.tosca.exceptions.CsarMissingEntryPointErrorBuilder;
import org.openecomp.sdc.tosca.services.ToscaFileOutputService;
-import java.io.BufferedOutputStream;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-
-import java.util.Map;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipOutputStream;
-
-
public class ToscaFileOutputServiceCsarImpl implements ToscaFileOutputService {
- static final String EXTERNAL_ARTIFACTS_FOLDER_NAME = "Artifacts";
- private static final String DEFINITIONS_FOLDER_NAME = "Definitions";
- private static final String ARTIFACTS_FOLDER_NAME = "Artifacts";
- //todo currently duplicated, to be changed when external artifacts are separated from internal
- private static final String TOSCA_META_FOLDER_NAME = "TOSCA-Metadata";
- private static final String TOSCA_META_FILE_VERSION_VALUE = "1.0";
- private static final String TOSCA_META_FILE_NAME = "TOSCA.meta";
- private static final String CSAR_VERSION_VALUE = "1.1";
- private static final String CREATED_BY_VALUE = "ASDC Onboarding portal";
- private static final String META_FILE_DELIMITER = ":";
- private static final String SPACE = " ";
- private static final String FILE_SEPARATOR = File.separator;
- private static final Logger logger = LoggerFactory.getLogger(ToscaFileOutputServiceCsarImpl.class);
-
- @Override
- public byte[] createOutputFile(ToscaServiceModel toscaServiceModel,
- FileContentHandler externalArtifacts) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos))) {
- packDefinitions(zos, toscaServiceModel.getServiceTemplates());
- FileContentHandler artifactFiles = toscaServiceModel.getArtifactFiles();
- if (artifactFiles != null && !artifactFiles.isEmpty()) {
- packArtifacts(zos, artifactFiles);
- }
- if (toscaServiceModel.getEntryDefinitionServiceTemplate() == null) {
- throw new CoreException(new CsarMissingEntryPointErrorBuilder().build());
- }
- createAndPackToscaMetaFile(zos, toscaServiceModel.getEntryDefinitionServiceTemplate());
- if (externalArtifacts != null) {
- packExternalArtifacts(zos, externalArtifacts);
- }
- } catch (IOException ex) {
- throw new CoreException(new CsarCreationErrorBuilder().build(), ex);
+ static final String EXTERNAL_ARTIFACTS_FOLDER_NAME = "Artifacts";
+ private static final String DEFINITIONS_FOLDER_NAME = "Definitions";
+ private static final String ARTIFACTS_FOLDER_NAME = "Artifacts";
+ //todo currently duplicated, to be changed when external artifacts are separated from internal
+ private static final String TOSCA_META_FOLDER_NAME = "TOSCA-Metadata";
+ private static final String TOSCA_META_FILE_VERSION_VALUE = "1.0";
+ private static final String TOSCA_META_FILE_NAME = "TOSCA.meta";
+ private static final String CSAR_VERSION_VALUE = "1.1";
+ private static final String CREATED_BY_VALUE = "ASDC Onboarding portal";
+ private static final String META_FILE_DELIMITER = ":";
+ private static final String SPACE = " ";
+ private static final String FILE_SEPARATOR = File.separator;
+ private static final Logger logger = LoggerFactory.getLogger(ToscaFileOutputServiceCsarImpl.class);
+
+ @Override
+ public byte[] createOutputFile(ToscaServiceModel toscaServiceModel, FileContentHandler externalArtifacts) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try (ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(baos))) {
+ packDefinitions(zos, toscaServiceModel.getServiceTemplates());
+ FileContentHandler artifactFiles = toscaServiceModel.getArtifactFiles();
+ if (artifactFiles != null && !artifactFiles.isEmpty()) {
+ packArtifacts(zos, artifactFiles);
+ }
+ if (toscaServiceModel.getEntryDefinitionServiceTemplate() == null) {
+ throw new CoreException(new CsarMissingEntryPointErrorBuilder().build());
+ }
+ createAndPackToscaMetaFile(zos, toscaServiceModel.getEntryDefinitionServiceTemplate());
+ if (externalArtifacts != null) {
+ packExternalArtifacts(zos, externalArtifacts);
+ }
+ } catch (IOException ex) {
+ throw new CoreException(new CsarCreationErrorBuilder().build(), ex);
+ }
+ return baos.toByteArray();
}
- return baos.toByteArray();
- }
-
-
- @Override
- public String createMetaFile(String entryDefinitionsFileName) {
- return TOSCA_META_FILE_VERSION_ENTRY.getName() + META_FILE_DELIMITER + SPACE + TOSCA_META_FILE_VERSION_VALUE
- + System.lineSeparator()
- + CSAR_VERSION_ENTRY.getName() + META_FILE_DELIMITER + SPACE + CSAR_VERSION_VALUE + System.lineSeparator()
- + CREATED_BY_ENTRY.getName() + META_FILE_DELIMITER + SPACE + CREATED_BY_VALUE + System.lineSeparator()
- + ENTRY_DEFINITIONS.getName() + META_FILE_DELIMITER + SPACE + DEFINITIONS_FOLDER_NAME
- + FILE_SEPARATOR
- + entryDefinitionsFileName;
- }
-
- @Override
- public String getArtifactsFolderName() {
- return ARTIFACTS_FOLDER_NAME;
- }
- private void createAndPackToscaMetaFile(ZipOutputStream zos, String entryDefinitionsFileName)
- throws IOException {
- String metaFile = createMetaFile(entryDefinitionsFileName);
- zos.putNextEntry(
- new ZipEntry(TOSCA_META_FOLDER_NAME + FILE_SEPARATOR + TOSCA_META_FILE_NAME));
- writeBytesToZip(zos, new ByteArrayInputStream(metaFile.getBytes()));
- }
-
- private void packDefinitions(ZipOutputStream zos, Map<String, ServiceTemplate> serviceTemplates)
- throws IOException {
- for (Map.Entry<String, ServiceTemplate> serviceTemplate : serviceTemplates.entrySet()) {
- String fileName = serviceTemplate.getKey();
- zos.putNextEntry(new ZipEntry(DEFINITIONS_FOLDER_NAME + FILE_SEPARATOR + fileName));
- writeBytesToZip(zos,
- FileUtils.convertToInputStream(serviceTemplate.getValue(), FileUtils.FileExtension.YAML));
+ @Override
+ public String createMetaFile(String entryDefinitionsFileName) {
+ return TOSCA_META_FILE_VERSION_ENTRY.getName() + META_FILE_DELIMITER + SPACE + TOSCA_META_FILE_VERSION_VALUE + System.lineSeparator()
+ + CSAR_VERSION_ENTRY.getName() + META_FILE_DELIMITER + SPACE + CSAR_VERSION_VALUE + System.lineSeparator() + CREATED_BY_ENTRY.getName()
+ + META_FILE_DELIMITER + SPACE + CREATED_BY_VALUE + System.lineSeparator() + ENTRY_DEFINITIONS.getName() + META_FILE_DELIMITER + SPACE
+ + DEFINITIONS_FOLDER_NAME + FILE_SEPARATOR + entryDefinitionsFileName;
}
- }
- private void packExternalArtifacts(ZipOutputStream zos, FileContentHandler externalArtifacts) {
+ @Override
+ public String getArtifactsFolderName() {
+ return ARTIFACTS_FOLDER_NAME;
+ }
- for (String filenameIncludingPath : externalArtifacts.getFileList()) {
- try {
- zos.putNextEntry(new ZipEntry(filenameIncludingPath));
- writeBytesToZip(zos, externalArtifacts.getFileContentAsStream(filenameIncludingPath));
+ private void createAndPackToscaMetaFile(ZipOutputStream zos, String entryDefinitionsFileName) throws IOException {
+ String metaFile = createMetaFile(entryDefinitionsFileName);
+ zos.putNextEntry(new ZipEntry(TOSCA_META_FOLDER_NAME + FILE_SEPARATOR + TOSCA_META_FILE_NAME));
+ writeBytesToZip(zos, new ByteArrayInputStream(metaFile.getBytes()));
+ }
- } catch (IOException ex) {
- throw new RuntimeException(ex);
- } finally {
- try {
- zos.closeEntry();
- } catch (IOException ignore) {
- logger.debug(ignore.getMessage(), ignore);
+ private void packDefinitions(ZipOutputStream zos, Map<String, ServiceTemplate> serviceTemplates) throws IOException {
+ for (Map.Entry<String, ServiceTemplate> serviceTemplate : serviceTemplates.entrySet()) {
+ String fileName = serviceTemplate.getKey();
+ zos.putNextEntry(new ZipEntry(DEFINITIONS_FOLDER_NAME + FILE_SEPARATOR + fileName));
+ writeBytesToZip(zos, FileUtils.convertToInputStream(serviceTemplate.getValue(), FileUtils.FileExtension.YAML));
}
- }
}
- }
-
- private void packArtifacts(ZipOutputStream zos, FileContentHandler artifacts) {
-
- for (String fileName : artifacts.getFileList()) {
- try {
- zos.putNextEntry(new ZipEntry(ARTIFACTS_FOLDER_NAME + FILE_SEPARATOR + fileName));
- writeBytesToZip(zos, artifacts.getFileContentAsStream(fileName));
-
- } catch (IOException ex) {
- throw new RuntimeException(ex);
- } finally {
- try {
- zos.closeEntry();
- } catch (IOException ignore) {
- logger.debug(ignore.getMessage(), ignore);
+ private void packExternalArtifacts(ZipOutputStream zos, FileContentHandler externalArtifacts) {
+ for (String filenameIncludingPath : externalArtifacts.getFileList()) {
+ try {
+ zos.putNextEntry(new ZipEntry(filenameIncludingPath));
+ writeBytesToZip(zos, externalArtifacts.getFileContentAsStream(filenameIncludingPath));
+ } catch (IOException ex) {
+ throw new RuntimeException(ex);
+ } finally {
+ try {
+ zos.closeEntry();
+ } catch (IOException ignore) {
+ logger.debug(ignore.getMessage(), ignore);
+ }
+ }
}
- }
}
- }
+ private void packArtifacts(ZipOutputStream zos, FileContentHandler artifacts) {
+ for (String fileName : artifacts.getFileList()) {
+ try {
+ zos.putNextEntry(new ZipEntry(ARTIFACTS_FOLDER_NAME + FILE_SEPARATOR + fileName));
+ writeBytesToZip(zos, artifacts.getFileContentAsStream(fileName));
+ } catch (IOException ex) {
+ throw new RuntimeException(ex);
+ } finally {
+ try {
+ zos.closeEntry();
+ } catch (IOException ignore) {
+ logger.debug(ignore.getMessage(), ignore);
+ }
+ }
+ }
+ }
- private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException {
- if(is != null){
- IOUtils.copy(is, zos);
+ private void writeBytesToZip(ZipOutputStream zos, InputStream is) throws IOException {
+ if (is != null) {
+ IOUtils.copy(is, zos);
+ }
}
- }
}