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.dao.UniqueValueDao;
20 import org.openecomp.core.util.UniqueValueUtil;
21 import org.openecomp.core.utilities.file.FileUtils;
22 import org.openecomp.sdc.common.errors.CoreException;
23 import org.openecomp.sdc.common.errors.ErrorCode;
24 import org.openecomp.sdc.vendorsoftwareproduct.ProcessManager;
25 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
26 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
27 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
28 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
29 import org.openecomp.sdc.vendorsoftwareproduct.errors.UploadInvalidErrorBuilder;
30 import org.openecomp.sdc.versioning.VersioningUtil;
31 import org.openecomp.sdc.versioning.dao.types.Version;
32 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.nio.ByteBuffer;
39 import java.util.Collection;
41 public class ProcessManagerImpl implements ProcessManager {
43 private static final String PROCESS_ARTIFACT_NOT_EXIST_MSG =
44 "Process artifact for process with Id %s does not exist for %s with Id %s and version %s";
45 private final ProcessDao processDao;
46 private final UniqueValueUtil uniqueValueUtil;
48 public ProcessManagerImpl(ProcessDao processDao, UniqueValueDao uniqueValueDao) {
49 this.processDao = processDao;
50 this.uniqueValueUtil = new UniqueValueUtil(uniqueValueDao);
54 public Collection<ProcessEntity> listProcesses(String vspId, Version version,
56 return processDao.list(new ProcessEntity(vspId, version, componentId, null));
60 public void deleteProcesses(String vspId, Version version, String componentId) {
61 ProcessEntity allProcesses = new ProcessEntity(vspId, version, componentId, null);
62 Collection<ProcessEntity> processes = processDao.list(allProcesses);
64 if (!processes.isEmpty()) {
65 for (ProcessEntity process : processes) {
66 deleteUniqueValue(process.getVspId(), process.getVersion(), process.getComponentId(),
71 if (componentId == null) {
72 processDao.deleteVspAll(vspId,version);
74 processDao.deleteAll(allProcesses);
79 public ProcessEntity createProcess(ProcessEntity process) {
80 validateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
83 processDao.create(process);
84 createUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
90 public ProcessEntity getProcess(String vspId, Version version, String componentId,
92 ProcessEntity retrieved =
93 processDao.get(new ProcessEntity(vspId, version, componentId, processId));
94 validateProcessExistence(vspId, version, componentId, processId, retrieved);
99 public void updateProcess(ProcessEntity process) {
100 ProcessEntity retrieved = processDao.get(process);
101 validateProcessExistence(process.getVspId(), process.getVersion(), process.getComponentId(),
102 process.getId(), retrieved);
104 updateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
105 retrieved.getName(), process.getName());
106 processDao.update(process);
110 public void deleteProcess(String vspId, Version version, String componentId, String processId) {
111 ProcessEntity retrieved = getProcess(vspId, version, componentId, processId);
113 processDao.delete(retrieved);
114 deleteUniqueValue(retrieved.getVspId(), retrieved.getVersion(), retrieved.getComponentId(),
115 retrieved.getName());
120 public File getProcessArtifact(String vspId, Version version, String componentId,
122 ProcessEntity retrieved = getValidatedProcessArtifact(vspId, version, componentId, processId);
124 File file = new File(String.format("%s_%s_%s", vspId, componentId, processId));
125 try (FileOutputStream fos = new FileOutputStream(file)) {
126 fos.write(retrieved.getArtifact().array());
127 } catch (IOException exception) {
128 throw new CoreException(new UploadInvalidErrorBuilder().build(), exception);
134 public void deleteProcessArtifact(String vspId, Version version, String componentId,
136 ProcessEntity retrieved = getValidatedProcessArtifact(vspId, version, componentId, processId);
138 processDao.deleteArtifact(retrieved);
142 public void uploadProcessArtifact(InputStream artifactFile, String artifactFileName, String vspId,
143 Version version, String componentId, String processId) {
144 ProcessEntity process = getProcess(vspId, version, componentId, processId);
145 process.setArtifactName(artifactFileName);
146 process.setArtifact(readArtifact(artifactFile));
147 processDao.uploadArtifact(process);
150 private ProcessEntity getValidatedProcessArtifact(String vspId, Version version,
151 String componentId, String processId) {
152 ProcessEntity retrieved =
153 processDao.getArtifact(new ProcessEntity(vspId, version, componentId, processId));
154 validateProcessArtifactExistence(vspId, version, componentId, processId, retrieved);
158 private ByteBuffer readArtifact(InputStream artifactInputStream) {
159 if (artifactInputStream == null) {
160 throw new CoreException(new UploadInvalidErrorBuilder().build());
163 return ByteBuffer.wrap(FileUtils.toByteArray(artifactInputStream));
164 } catch (RuntimeException exception) {
165 throw new CoreException(new UploadInvalidErrorBuilder().build(), exception);
170 private void validateProcessExistence(String vspId, Version version, String componentId,
171 String processId, ProcessEntity retrieved) {
172 VersioningUtil.validateEntityExistence(retrieved,
173 new ProcessEntity(vspId, version, componentId, processId),
174 VspDetails.ENTITY_TYPE);
177 private void validateProcessArtifactExistence(String vspId, Version version, String componentId,
178 String processId, ProcessEntity retrieved) {
179 ProcessEntity inputProcess = new ProcessEntity(vspId, version, componentId, processId);
180 VersioningUtil.validateEntityExistence(retrieved, inputProcess, VspDetails.ENTITY_TYPE);
181 if (retrieved.getArtifact() == null) {
182 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
183 .withId(VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND)
184 .withMessage(String.format(PROCESS_ARTIFACT_NOT_EXIST_MSG,
185 processId, VspDetails.ENTITY_TYPE, vspId, version)).build());
190 protected void validateUniqueName(String vspId, Version version, String componentId,
191 String processName) {
192 uniqueValueUtil.validateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME,
193 vspId, version.getId(), componentId, processName);
196 protected void createUniqueName(String vspId, Version version, String componentId,
197 String processName) {
199 .createUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
200 version.getId(), componentId, processName);
203 protected void updateUniqueName(String vspId, Version version, String componentId,
204 String oldProcessName, String newProcessName) {
206 .updateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, oldProcessName,
207 newProcessName, vspId, version.getId(), componentId);
210 protected void deleteUniqueValue(String vspId, Version version, String componentId,
211 String processName) {
212 if (componentId == null) {
214 .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
215 version.getId(), processName);
218 .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
219 version.getId(), componentId, processName);