2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.csar.storage;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.nio.file.StandardCopyOption;
29 import java.util.Optional;
30 import java.util.UUID;
31 import org.openecomp.sdc.be.csar.storage.exception.PersistentVolumeArtifactStorageException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class PersistentVolumeArtifactStorageManager implements ArtifactStorageManager {
37 private static final Logger LOGGER = LoggerFactory.getLogger(PersistentVolumeArtifactStorageManager.class);
39 private final PersistentVolumeArtifactStorageConfig storageConfiguration;
41 public PersistentVolumeArtifactStorageManager(final ArtifactStorageConfig storageConfiguration) {
42 this.storageConfiguration = (PersistentVolumeArtifactStorageConfig) storageConfiguration;
46 public ArtifactInfo persist(final String vspId, final String versionId, final ArtifactInfo uploadedArtifactInfo) {
47 final var temporaryPath = uploadedArtifactInfo.getPath();
48 if (!Files.exists(temporaryPath)) {
49 throw new PersistentVolumeArtifactStorageException(String.format("Given artifact does not exist '%s'", uploadedArtifactInfo.getPath()));
52 final var filePath = buildFilePath(vspId, versionId);
53 final var backupPath = backupPreviousVersion(filePath).orElse(null);
55 moveFile(temporaryPath, filePath);
56 } catch (final Exception e) {
57 rollback(backupPath, filePath);
58 final var errorMsg = String.format("Could not persist artifact for VSP '%s', version '%s'", vspId, versionId);
59 throw new PersistentVolumeArtifactStorageException(errorMsg, e);
62 removePreviousVersion(backupPath);
64 return new PersistentStorageArtifactInfo(filePath);
68 public ArtifactInfo upload(final String vspId, final String versionId, final InputStream artifactInputStream) {
69 final var destinationFolder = buildDestinationFolder(vspId, versionId);
71 Files.createDirectories(destinationFolder);
72 } catch (final IOException e) {
73 throw new PersistentVolumeArtifactStorageException(String.format("Could not create directory '%s'", destinationFolder), e);
76 final var filePath = createTempFilePath(destinationFolder);
78 persist(artifactInputStream, filePath);
79 } catch (final IOException e) {
80 throw new PersistentVolumeArtifactStorageException(String.format("Could not persist artifact '%s'", filePath), e);
83 return new PersistentStorageArtifactInfo(filePath);
86 private Path buildFilePath(final String vspId, final String versionId) {
87 return buildDestinationFolder(vspId, versionId).resolve(versionId);
91 public boolean isEnabled() {
92 return storageConfiguration != null && storageConfiguration.isEnabled();
95 private Optional<Path> backupPreviousVersion(final Path filePath) {
96 if (!Files.exists(filePath)) {
97 return Optional.empty();
100 final var backupPath = Path.of(filePath + UUID.randomUUID().toString());
101 moveFile(filePath, backupPath);
102 return Optional.ofNullable(backupPath);
105 private void rollback(final Path backupPath, final Path filePath) {
107 moveFile(backupPath, filePath);
108 } catch (final Exception ex) {
109 LOGGER.warn("Could not rollback the backup file '{}' to the original '{}'", backupPath, filePath, ex);
113 private void removePreviousVersion(final Path filePath) {
114 if (filePath == null || !Files.exists(filePath)) {
119 Files.delete(filePath);
120 } catch (final IOException e) {
121 throw new PersistentVolumeArtifactStorageException(String.format("Could not delete previous version '%s'", filePath), e);
125 private Path createTempFilePath(final Path destinationFolder) {
126 final var retries = 10;
127 return createTempFilePath(destinationFolder, retries).orElseThrow(() -> {
128 throw new PersistentVolumeArtifactStorageException(String.format("Could not generate upload file path after '%s' retries", retries));
132 private Optional<Path> createTempFilePath(final Path destinationFolder, int retries) {
133 for (var i = 0; i < retries; i++) {
134 final var filePath = destinationFolder.resolve(UUID.randomUUID().toString());
135 if (Files.notExists(filePath)) {
136 return Optional.of(filePath);
139 return Optional.empty();
142 private Path buildDestinationFolder(final String vspId, final String versionId) {
143 return storageConfiguration.getStoragePath().resolve(vspId).resolve(versionId);
146 private void persist(final InputStream artifactInputStream, final Path filePath) throws IOException {
147 try (final var inputStream = artifactInputStream;
148 final var fileOutputStream = new FileOutputStream(filePath.toFile());) {
149 inputStream.transferTo(fileOutputStream);
153 private void moveFile(final Path from, final Path to) {
155 Files.move(from, to, StandardCopyOption.REPLACE_EXISTING);
156 } catch (final IOException e) {
157 throw new PersistentVolumeArtifactStorageException(String.format("Could not move file '%s' to '%s'", from, to), e);