2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
23 import org.openecomp.core.util.UniqueValueUtil;
24 import org.openecomp.core.utilities.file.FileUtils;
25 import org.openecomp.sdc.activityLog.ActivityLogManager;
26 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
27 import org.openecomp.sdc.common.errors.CoreException;
28 import org.openecomp.sdc.datatypes.error.ErrorLevel;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
32 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
33 import org.openecomp.sdc.logging.types.LoggerConstants;
34 import org.openecomp.sdc.logging.types.LoggerErrorCode;
35 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
36 import org.openecomp.sdc.vendorsoftwareproduct.ProcessManager;
37 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
41 import org.openecomp.sdc.vendorsoftwareproduct.errors.UploadInvalidErrorBuilder;
42 import org.openecomp.sdc.versioning.VersioningUtil;
43 import org.openecomp.sdc.versioning.dao.types.Version;
44 import org.openecomp.sdcrests.activitylog.types.ActivityType;
47 import java.io.FileOutputStream;
48 import java.io.IOException;
49 import java.io.InputStream;
50 import java.util.Collection;
52 public class ProcessManagerImpl implements ProcessManager {
53 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
54 private final ActivityLogManager activityLogManager;
56 private final VendorSoftwareProductDao vendorSoftwareProductDao;
58 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
60 public ProcessManagerImpl(VendorSoftwareProductDao vendorSoftwareProductDao, ActivityLogManager activityLogManager) {
61 this.vendorSoftwareProductDao = vendorSoftwareProductDao;
62 this.activityLogManager = activityLogManager;
66 public Collection<ProcessEntity> listProcesses(String vspId, Version version,
69 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id", vspId, componentId);
70 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", vspId, componentId);
72 return vendorSoftwareProductDao.listProcesses(vspId, version, componentId);
76 public void deleteProcesses(String vspId, Version version, String componentId, String user) {
77 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id", vspId, componentId);
79 Collection<ProcessEntity> processes =
80 vendorSoftwareProductDao.listProcesses(vspId, version, componentId);
82 if (!processes.isEmpty()) {
83 for (ProcessEntity process : processes) {
84 deleteUniqueValue(process.getVspId(), process.getVersion(), process.getComponentId(),
88 vendorSoftwareProductDao.deleteProcesses(vspId, version, componentId);
90 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", vspId, componentId);
94 public ProcessEntity createProcess(ProcessEntity process, String user) {
95 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id", process.getId(),
96 process.getComponentId());
97 validateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
99 //process.setId(CommonMethods.nextUuId());
101 vendorSoftwareProductDao.createProcess(process);
102 createUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
105 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", process.getId(),
106 process.getComponentId());
113 public ProcessEntity getProcess(String vspId, Version version, String componentId,
114 String processId, String user) {
115 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id", vspId, componentId);
117 ProcessEntity retrieved =
118 vendorSoftwareProductDao.getProcess(vspId, version, componentId, processId);
119 validateProcessExistence(vspId, version, componentId, processId, retrieved);
121 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", vspId, componentId);
127 public void updateProcess(ProcessEntity process, String user) {
128 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id", process.getId(),
129 process.getComponentId());
131 ProcessEntity retrieved = vendorSoftwareProductDao
132 .getProcess(process.getVspId(), process.getVersion(), process.getComponentId(),
134 validateProcessExistence(process.getVspId(), process.getVersion(), process.getComponentId(),
135 process.getId(), retrieved);
137 updateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
138 retrieved.getName(), process.getName());
139 vendorSoftwareProductDao.updateProcess(process);
141 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", process.getId(),
142 process.getComponentId());
146 public void deleteProcess(String vspId, Version version, String componentId, String processId,
148 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id", vspId, componentId);
150 ProcessEntity retrieved =
151 vendorSoftwareProductDao.getProcess(vspId, version, componentId, processId);
152 validateProcessExistence(vspId, version, componentId, processId, retrieved);
154 vendorSoftwareProductDao.deleteProcess(vspId, version, componentId, processId);
155 deleteUniqueValue(retrieved.getVspId(), retrieved.getVersion(), retrieved.getComponentId(),
156 retrieved.getName());
158 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", vspId, componentId);
163 public File getProcessArtifact(String vspId, Version version, String componentId,
164 String processId, String user) {
165 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id", vspId, componentId);
167 ProcessEntity retrieved =
168 vendorSoftwareProductDao.getProcessArtifact(vspId, version, componentId, processId);
169 validateProcessArtifactExistence(vspId, version, componentId, processId, retrieved);
171 File file = new File(String.format("%s_%s_%s", vspId, componentId, processId));
172 try (FileOutputStream fos = new FileOutputStream(file)) {
173 fos.write(retrieved.getArtifact().array());
174 } catch (IOException exception) {
175 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
176 LoggerTragetServiceName.GET_PROCESS_ARTIFACT, ErrorLevel.ERROR.name(),
177 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't get process artifact");
178 throw new CoreException(new UploadInvalidErrorBuilder().build(), exception);
181 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", vspId, componentId);
187 public void deleteProcessArtifact(String vspId, Version version, String componentId,
188 String processId, String user) {
189 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id", vspId, componentId);
191 ProcessEntity retrieved =
192 vendorSoftwareProductDao.getProcessArtifact(vspId, version, componentId, processId);
193 validateProcessArtifactExistence(vspId, version, componentId, processId, retrieved);
195 vendorSoftwareProductDao.deleteProcessArtifact(vspId, version, componentId, processId);
197 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", vspId, componentId);
201 public void uploadProcessArtifact(InputStream artifactFile, String artifactFileName, String vspId,
202 Version version, String componentId, String processId,
204 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id", vspId, componentId);
206 ProcessEntity retrieved =
207 vendorSoftwareProductDao.getProcess(vspId, version, componentId, processId);
208 validateProcessExistence(vspId, version, componentId, processId, retrieved);
210 if (artifactFile == 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());
219 artifact = FileUtils.toByteArray(artifactFile);
220 } catch (RuntimeException exception) {
221 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
222 LoggerTragetServiceName.UPLOAD_PROCESS_ARTIFACT, ErrorLevel.ERROR.name(),
223 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't upload process artifact");
224 throw new CoreException(new UploadInvalidErrorBuilder().build(), exception);
227 vendorSoftwareProductDao.uploadProcessArtifact(vspId, version, componentId, processId, artifact,
229 ActivityLogEntity activityLogEntity = new ActivityLogEntity(vspId, String.valueOf(version.getMajor()+1),
230 ActivityType.UPLOAD_MONITORING_FILE.toString(), user, true, "", "");
231 activityLogManager.addActionLog(activityLogEntity, user);
233 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", vspId, componentId);
237 private void validateProcessExistence(String vspId, Version version, String componentId,
238 String processId, ProcessEntity retrieved) {
239 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id, process id", vspId, componentId,
242 if (retrieved != null) {
245 VersioningUtil.validateEntityExistence(retrieved,
246 new ProcessEntity(vspId, version, componentId, processId),
247 VspDetails.ENTITY_TYPE);//todo retrieved is always null ??
249 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id, process id", vspId, componentId,
253 private void validateProcessArtifactExistence(String vspId, Version version, String componentId,
254 String processId, ProcessEntity retrieved) {
255 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id, process id", vspId, componentId,
258 if (retrieved != null) {
259 VersioningUtil.validateEntityExistence(retrieved.getArtifact(),
260 new ProcessEntity(vspId, version, componentId, processId),
261 VspDetails.ENTITY_TYPE);
263 VersioningUtil.validateEntityExistence(retrieved,
264 new ProcessEntity(vspId, version, componentId, processId),
265 VspDetails.ENTITY_TYPE); //todo retrieved is always null ??
268 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id, process id", vspId, componentId,
273 protected void validateUniqueName(String vspId, Version version, String componentId,
274 String processName) {
275 UniqueValueUtil.validateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME,
276 vspId, version.toString(), componentId, processName);
279 protected void createUniqueName(String vspId, Version version, String componentId,
280 String processName) {
282 .createUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
283 version.toString(), componentId, processName);
286 protected void updateUniqueName(String vspId, Version version, String componentId,
287 String oldProcessName, String newProcessName) {
289 .updateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, oldProcessName,
290 newProcessName, vspId, version.toString(), componentId);
293 protected void deleteUniqueValue(String vspId, Version version, String componentId,
294 String processName) {
296 .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
297 version.toString(), componentId, processName);