2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
19 import org.openecomp.core.util.UniqueValueUtil;
20 import org.openecomp.core.utilities.file.FileUtils;
21 import org.openecomp.sdc.common.errors.CoreException;
22 import org.openecomp.sdc.common.errors.ErrorCode;
23 import org.openecomp.sdc.vendorsoftwareproduct.ProcessManager;
24 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
25 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
26 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
27 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
28 import org.openecomp.sdc.vendorsoftwareproduct.errors.UploadInvalidErrorBuilder;
29 import org.openecomp.sdc.versioning.VersioningUtil;
30 import org.openecomp.sdc.versioning.dao.types.Version;
31 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.nio.ByteBuffer;
38 import java.util.Collection;
40 public class ProcessManagerImpl implements ProcessManager {
42 private static final String PROCESS_ARTIFACT_NOT_EXIST_MSG =
43 "Process artifact for process with Id %s does not exist for %s with Id %s and version %s";
44 private final ProcessDao processDao;
46 public ProcessManagerImpl(ProcessDao processDao) {
47 this.processDao = processDao;
51 public Collection<ProcessEntity> listProcesses(String vspId, Version version,
53 return processDao.list(new ProcessEntity(vspId, version, componentId, null));
57 public void deleteProcesses(String vspId, Version version, String componentId) {
58 ProcessEntity allProcesses = new ProcessEntity(vspId, version, componentId, null);
59 Collection<ProcessEntity> processes = processDao.list(allProcesses);
61 if (!processes.isEmpty()) {
62 for (ProcessEntity process : processes) {
63 deleteUniqueValue(process.getVspId(), process.getVersion(), process.getComponentId(),
68 if (componentId == null) {
69 processDao.deleteVspAll(vspId,version);
71 processDao.deleteAll(allProcesses);
76 public ProcessEntity createProcess(ProcessEntity process) {
77 validateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
80 processDao.create(process);
81 createUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
87 public ProcessEntity getProcess(String vspId, Version version, String componentId,
89 ProcessEntity retrieved =
90 processDao.get(new ProcessEntity(vspId, version, componentId, processId));
91 validateProcessExistence(vspId, version, componentId, processId, retrieved);
96 public void updateProcess(ProcessEntity process) {
97 ProcessEntity retrieved = processDao.get(process);
98 validateProcessExistence(process.getVspId(), process.getVersion(), process.getComponentId(),
99 process.getId(), retrieved);
101 updateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
102 retrieved.getName(), process.getName());
103 processDao.update(process);
107 public void deleteProcess(String vspId, Version version, String componentId, String processId) {
108 ProcessEntity retrieved = getProcess(vspId, version, componentId, processId);
110 processDao.delete(retrieved);
111 deleteUniqueValue(retrieved.getVspId(), retrieved.getVersion(), retrieved.getComponentId(),
112 retrieved.getName());
117 public File getProcessArtifact(String vspId, Version version, String componentId,
119 ProcessEntity retrieved = getValidatedProcessArtifact(vspId, version, componentId, processId);
121 File file = new File(String.format("%s_%s_%s", vspId, componentId, processId));
122 try (FileOutputStream fos = new FileOutputStream(file)) {
123 fos.write(retrieved.getArtifact().array());
124 } catch (IOException exception) {
125 throw new CoreException(new UploadInvalidErrorBuilder().build(), exception);
131 public void deleteProcessArtifact(String vspId, Version version, String componentId,
133 ProcessEntity retrieved = getValidatedProcessArtifact(vspId, version, componentId, processId);
135 processDao.deleteArtifact(retrieved);
139 public void uploadProcessArtifact(InputStream artifactFile, String artifactFileName, String vspId,
140 Version version, String componentId, String processId) {
141 ProcessEntity process = getProcess(vspId, version, componentId, processId);
142 process.setArtifactName(artifactFileName);
143 process.setArtifact(readArtifact(artifactFile));
144 processDao.uploadArtifact(process);
147 private ProcessEntity getValidatedProcessArtifact(String vspId, Version version,
148 String componentId, String processId) {
149 ProcessEntity retrieved =
150 processDao.getArtifact(new ProcessEntity(vspId, version, componentId, processId));
151 validateProcessArtifactExistence(vspId, version, componentId, processId, retrieved);
155 private ByteBuffer readArtifact(InputStream artifactInputStream) {
156 if (artifactInputStream == null) {
157 throw new CoreException(new UploadInvalidErrorBuilder().build());
160 return ByteBuffer.wrap(FileUtils.toByteArray(artifactInputStream));
161 } catch (RuntimeException exception) {
162 throw new CoreException(new UploadInvalidErrorBuilder().build(), exception);
167 private void validateProcessExistence(String vspId, Version version, String componentId,
168 String processId, ProcessEntity retrieved) {
169 VersioningUtil.validateEntityExistence(retrieved,
170 new ProcessEntity(vspId, version, componentId, processId),
171 VspDetails.ENTITY_TYPE);
174 private void validateProcessArtifactExistence(String vspId, Version version, String componentId,
175 String processId, ProcessEntity retrieved) {
176 ProcessEntity inputProcess = new ProcessEntity(vspId, version, componentId, processId);
177 VersioningUtil.validateEntityExistence(retrieved, inputProcess, VspDetails.ENTITY_TYPE);
178 if (retrieved.getArtifact() == null) {
179 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
180 .withId(VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND)
181 .withMessage(String.format(PROCESS_ARTIFACT_NOT_EXIST_MSG,
182 processId, VspDetails.ENTITY_TYPE, vspId, version)).build());
187 protected void validateUniqueName(String vspId, Version version, String componentId,
188 String processName) {
189 UniqueValueUtil.validateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME,
190 vspId, version.getId(), componentId, processName);
193 protected void createUniqueName(String vspId, Version version, String componentId,
194 String processName) {
196 .createUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
197 version.getId(), componentId, processName);
200 protected void updateUniqueName(String vspId, Version version, String componentId,
201 String oldProcessName, String newProcessName) {
203 .updateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, oldProcessName,
204 newProcessName, vspId, version.getId(), componentId);
207 protected void deleteUniqueValue(String vspId, Version version, String componentId,
208 String processName) {
209 if (componentId == null) {
211 .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
212 version.getId(), processName);
215 .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
216 version.getId(), componentId, processName);