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.ErrorCategory;
23 import org.openecomp.sdc.common.errors.ErrorCode;
24 import org.openecomp.sdc.datatypes.error.ErrorLevel;
25 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
26 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
27 import org.openecomp.sdc.logging.types.LoggerConstants;
28 import org.openecomp.sdc.logging.types.LoggerErrorCode;
29 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
30 import org.openecomp.sdc.vendorsoftwareproduct.ProcessManager;
31 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
35 import org.openecomp.sdc.vendorsoftwareproduct.errors.UploadInvalidErrorBuilder;
36 import org.openecomp.sdc.versioning.VersioningUtil;
37 import org.openecomp.sdc.versioning.dao.types.Version;
38 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
41 import java.io.FileOutputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.nio.ByteBuffer;
45 import java.util.Collection;
47 public class ProcessManagerImpl implements ProcessManager {
48 private static final String PROCESS_ARTIFACT_NOT_EXIST_MSG =
49 "Process artifact for process with Id %s does not exist for %s with Id %s and version %s";
51 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
53 private final ProcessDao processDao;
54 private static final String VSP_ID_COMPONENT_ID = "VSP id, component id";
56 public ProcessManagerImpl(ProcessDao processDao) {
57 this.processDao = processDao;
61 public Collection<ProcessEntity> listProcesses(String vspId, Version version,
63 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
64 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
66 return processDao.list(new ProcessEntity(vspId, version, componentId, null));
70 public void deleteProcesses(String vspId, Version version, String componentId) {
71 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
73 ProcessEntity allProcesses = new ProcessEntity(vspId, version, componentId, null);
74 Collection<ProcessEntity> processes = processDao.list(allProcesses);
76 if (!processes.isEmpty()) {
77 for (ProcessEntity process : processes) {
78 deleteUniqueValue(process.getVspId(), process.getVersion(), process.getComponentId(),
83 if (componentId == null) {
84 processDao.deleteVspAll(vspId,version);
86 processDao.deleteAll(allProcesses);
89 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
93 public ProcessEntity createProcess(ProcessEntity process) {
94 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, process.getId(),
95 process.getComponentId());
96 validateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
99 processDao.create(process);
100 createUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
103 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, process.getId(),
104 process.getComponentId());
110 public ProcessEntity getProcess(String vspId, Version version, String componentId,
112 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
114 ProcessEntity retrieved =
115 processDao.get(new ProcessEntity(vspId, version, componentId, processId));
116 validateProcessExistence(vspId, version, componentId, processId, retrieved);
118 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
124 public void updateProcess(ProcessEntity process) {
125 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, process.getId(),
126 process.getComponentId());
128 ProcessEntity retrieved = processDao.get(process);
129 validateProcessExistence(process.getVspId(), process.getVersion(), process.getComponentId(),
130 process.getId(), retrieved);
132 updateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
133 retrieved.getName(), process.getName());
134 processDao.update(process);
136 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, process.getId(),
137 process.getComponentId());
141 public void deleteProcess(String vspId, Version version, String componentId, String processId) {
142 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
144 ProcessEntity retrieved = getProcess(vspId, version, componentId, processId);
146 processDao.delete(retrieved);
147 deleteUniqueValue(retrieved.getVspId(), retrieved.getVersion(), retrieved.getComponentId(),
148 retrieved.getName());
150 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
155 public File getProcessArtifact(String vspId, Version version, String componentId,
157 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
159 ProcessEntity retrieved = getValidatedProcessArtifact(vspId, version, componentId, processId);
161 File file = new File(String.format("%s_%s_%s", vspId, componentId, processId));
162 try (FileOutputStream fos = new FileOutputStream(file)) {
163 fos.write(retrieved.getArtifact().array());
164 } catch (IOException exception) {
165 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
166 LoggerTragetServiceName.GET_PROCESS_ARTIFACT, ErrorLevel.ERROR.name(),
167 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't get process artifact");
168 throw new CoreException(new UploadInvalidErrorBuilder().build(), exception);
171 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
177 public void deleteProcessArtifact(String vspId, Version version, String componentId,
179 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
181 ProcessEntity retrieved = getValidatedProcessArtifact(vspId, version, componentId, processId);
183 processDao.deleteArtifact(retrieved);
185 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
189 public void uploadProcessArtifact(InputStream artifactFile, String artifactFileName, String vspId,
190 Version version, String componentId, String processId) {
191 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
193 ProcessEntity process = getProcess(vspId, version, componentId, processId);
194 process.setArtifactName(artifactFileName);
195 process.setArtifact(readArtifact(artifactFile));
196 processDao.uploadArtifact(process);
198 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
201 private ProcessEntity getValidatedProcessArtifact(String vspId, Version version,
202 String componentId, String processId) {
203 ProcessEntity retrieved =
204 processDao.getArtifact(new ProcessEntity(vspId, version, componentId, processId));
205 validateProcessArtifactExistence(vspId, version, componentId, processId, retrieved);
209 private ByteBuffer readArtifact(InputStream artifactInputStream) {
210 if (artifactInputStream == null) {
211 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
212 LoggerTragetServiceName.UPLOAD_PROCESS_ARTIFACT, ErrorLevel.ERROR.name(),
213 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't upload process artifact");
214 throw new CoreException(new UploadInvalidErrorBuilder().build());
217 return ByteBuffer.wrap(FileUtils.toByteArray(artifactInputStream));
218 } catch (RuntimeException exception) {
219 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
220 LoggerTragetServiceName.UPLOAD_PROCESS_ARTIFACT, ErrorLevel.ERROR.name(),
221 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't upload process artifact");
222 throw new CoreException(new UploadInvalidErrorBuilder().build(), exception);
227 private void validateProcessExistence(String vspId, Version version, String componentId,
228 String processId, ProcessEntity retrieved) {
229 VersioningUtil.validateEntityExistence(retrieved,
230 new ProcessEntity(vspId, version, componentId, processId),
231 VspDetails.ENTITY_TYPE);
234 private void validateProcessArtifactExistence(String vspId, Version version, String componentId,
235 String processId, ProcessEntity retrieved) {
236 ProcessEntity inputProcess = new ProcessEntity(vspId, version, componentId, processId);
237 VersioningUtil.validateEntityExistence(retrieved, inputProcess, VspDetails.ENTITY_TYPE);
238 if (retrieved.getArtifact() == null) {
239 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
240 .withCategory(ErrorCategory.APPLICATION)
241 .withId(VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND)
242 .withMessage(String.format(PROCESS_ARTIFACT_NOT_EXIST_MSG,
243 processId, VspDetails.ENTITY_TYPE, vspId, version)).build());
248 protected void validateUniqueName(String vspId, Version version, String componentId,
249 String processName) {
250 UniqueValueUtil.validateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME,
251 vspId, version.getId(), componentId, processName);
254 protected void createUniqueName(String vspId, Version version, String componentId,
255 String processName) {
257 .createUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
258 version.getId(), componentId, processName);
261 protected void updateUniqueName(String vspId, Version version, String componentId,
262 String oldProcessName, String newProcessName) {
264 .updateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, oldProcessName,
265 newProcessName, vspId, version.getId(), componentId);
268 protected void deleteUniqueValue(String vspId, Version version, String componentId,
269 String processName) {
270 if (componentId == null) {
272 .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
273 version.getId(), processName);
276 .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
277 version.getId(), componentId, processName);