10629b3edb4149233557b860d09cba75929d67b1
[sdc.git] /
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.csar.storage;
22
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;
34
35 public class PersistentVolumeArtifactStorageManager implements ArtifactStorageManager {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(PersistentVolumeArtifactStorageManager.class);
38
39     private final PersistentVolumeArtifactStorageConfig storageConfiguration;
40
41     public PersistentVolumeArtifactStorageManager(final ArtifactStorageConfig storageConfiguration) {
42         this.storageConfiguration = (PersistentVolumeArtifactStorageConfig) storageConfiguration;
43     }
44
45     @Override
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()));
50         }
51
52         final var filePath = buildFilePath(vspId, versionId);
53         final var backupPath = backupPreviousVersion(filePath).orElse(null);
54         try {
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);
60         }
61
62         removePreviousVersion(backupPath);
63
64         return new PersistentStorageArtifactInfo(filePath);
65     }
66
67     @Override
68     public ArtifactInfo upload(final String vspId, final String versionId, final InputStream artifactInputStream) {
69         final var destinationFolder = buildDestinationFolder(vspId, versionId);
70         try {
71             Files.createDirectories(destinationFolder);
72         } catch (final IOException e) {
73             throw new PersistentVolumeArtifactStorageException(String.format("Could not create directory '%s'", destinationFolder), e);
74         }
75
76         final var filePath = createTempFilePath(destinationFolder);
77         try {
78             persist(artifactInputStream, filePath);
79         } catch (final IOException e) {
80             throw new PersistentVolumeArtifactStorageException(String.format("Could not persist artifact '%s'", filePath), e);
81         }
82
83         return new PersistentStorageArtifactInfo(filePath);
84     }
85
86     private Path buildFilePath(final String vspId, final String versionId) {
87         return buildDestinationFolder(vspId, versionId).resolve(versionId);
88     }
89
90     @Override
91     public boolean isEnabled() {
92         return storageConfiguration != null && storageConfiguration.isEnabled();
93     }
94
95     private Optional<Path> backupPreviousVersion(final Path filePath) {
96         if (!Files.exists(filePath)) {
97             return Optional.empty();
98         }
99
100         final var backupPath = Path.of(filePath + UUID.randomUUID().toString());
101         moveFile(filePath, backupPath);
102         return Optional.ofNullable(backupPath);
103     }
104
105     private void rollback(final Path backupPath, final Path filePath) {
106         try {
107             moveFile(backupPath, filePath);
108         } catch (final Exception ex) {
109             LOGGER.warn("Could not rollback the backup file '{}' to the original '{}'", backupPath, filePath, ex);
110         }
111     }
112
113     private void removePreviousVersion(final Path filePath) {
114         if (filePath == null || !Files.exists(filePath)) {
115             return;
116         }
117
118         try {
119             Files.delete(filePath);
120         } catch (final IOException e) {
121             throw new PersistentVolumeArtifactStorageException(String.format("Could not delete previous version '%s'", filePath), e);
122         }
123     }
124
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));
129         });
130     }
131
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);
137             }
138         }
139         return Optional.empty();
140     }
141
142     private Path buildDestinationFolder(final String vspId, final String versionId) {
143         return storageConfiguration.getStoragePath().resolve(vspId).resolve(versionId);
144     }
145
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);
150         }
151     }
152
153     private void moveFile(final Path from, final Path to) {
154         try {
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);
158         }
159     }
160
161 }