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.MdcDataErrorMessage;
26 import org.openecomp.sdc.logging.types.LoggerConstants;
27 import org.openecomp.sdc.logging.types.LoggerErrorCode;
28 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
29 import org.openecomp.sdc.vendorsoftwareproduct.ProcessManager;
30 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
34 import org.openecomp.sdc.vendorsoftwareproduct.errors.UploadInvalidErrorBuilder;
35 import org.openecomp.sdc.versioning.VersioningUtil;
36 import org.openecomp.sdc.versioning.dao.types.Version;
37 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.nio.ByteBuffer;
44 import java.util.Collection;
46 public class ProcessManagerImpl implements ProcessManager {
47 private static final String PROCESS_ARTIFACT_NOT_EXIST_MSG =
48 "Process artifact for process with Id %s does not exist for %s with Id %s and version %s";
49 private final ProcessDao processDao;
50 private static final String VSP_ID_COMPONENT_ID = "VSP id, component id";
52 public ProcessManagerImpl(ProcessDao processDao) {
53 this.processDao = processDao;
57 public Collection<ProcessEntity> listProcesses(String vspId, Version version,
59 return processDao.list(new ProcessEntity(vspId, version, componentId, null));
63 public void deleteProcesses(String vspId, Version version, String componentId) {
64 ProcessEntity allProcesses = new ProcessEntity(vspId, version, componentId, null);
65 Collection<ProcessEntity> processes = processDao.list(allProcesses);
67 if (!processes.isEmpty()) {
68 for (ProcessEntity process : processes) {
69 deleteUniqueValue(process.getVspId(), process.getVersion(), process.getComponentId(),
74 if (componentId == null) {
75 processDao.deleteVspAll(vspId,version);
77 processDao.deleteAll(allProcesses);
82 public ProcessEntity createProcess(ProcessEntity process) {
83 validateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
86 processDao.create(process);
87 createUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
93 public ProcessEntity getProcess(String vspId, Version version, String componentId,
95 ProcessEntity retrieved =
96 processDao.get(new ProcessEntity(vspId, version, componentId, processId));
97 validateProcessExistence(vspId, version, componentId, processId, retrieved);
102 public void updateProcess(ProcessEntity process) {
103 ProcessEntity retrieved = processDao.get(process);
104 validateProcessExistence(process.getVspId(), process.getVersion(), process.getComponentId(),
105 process.getId(), retrieved);
107 updateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
108 retrieved.getName(), process.getName());
109 processDao.update(process);
113 public void deleteProcess(String vspId, Version version, String componentId, String processId) {
114 ProcessEntity retrieved = getProcess(vspId, version, componentId, processId);
116 processDao.delete(retrieved);
117 deleteUniqueValue(retrieved.getVspId(), retrieved.getVersion(), retrieved.getComponentId(),
118 retrieved.getName());
123 public File getProcessArtifact(String vspId, Version version, String componentId,
125 ProcessEntity retrieved = getValidatedProcessArtifact(vspId, version, componentId, processId);
127 File file = new File(String.format("%s_%s_%s", vspId, componentId, processId));
128 try (FileOutputStream fos = new FileOutputStream(file)) {
129 fos.write(retrieved.getArtifact().array());
130 } catch (IOException exception) {
131 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
132 LoggerTragetServiceName.GET_PROCESS_ARTIFACT, ErrorLevel.ERROR.name(),
133 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't get process artifact");
134 throw new CoreException(new UploadInvalidErrorBuilder().build(), exception);
140 public void deleteProcessArtifact(String vspId, Version version, String componentId,
142 ProcessEntity retrieved = getValidatedProcessArtifact(vspId, version, componentId, processId);
144 processDao.deleteArtifact(retrieved);
148 public void uploadProcessArtifact(InputStream artifactFile, String artifactFileName, String vspId,
149 Version version, String componentId, String processId) {
150 ProcessEntity process = getProcess(vspId, version, componentId, processId);
151 process.setArtifactName(artifactFileName);
152 process.setArtifact(readArtifact(artifactFile));
153 processDao.uploadArtifact(process);
156 private ProcessEntity getValidatedProcessArtifact(String vspId, Version version,
157 String componentId, String processId) {
158 ProcessEntity retrieved =
159 processDao.getArtifact(new ProcessEntity(vspId, version, componentId, processId));
160 validateProcessArtifactExistence(vspId, version, componentId, processId, retrieved);
164 private ByteBuffer readArtifact(InputStream artifactInputStream) {
165 if (artifactInputStream == null) {
166 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
167 LoggerTragetServiceName.UPLOAD_PROCESS_ARTIFACT, ErrorLevel.ERROR.name(),
168 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't upload process artifact");
169 throw new CoreException(new UploadInvalidErrorBuilder().build());
172 return ByteBuffer.wrap(FileUtils.toByteArray(artifactInputStream));
173 } catch (RuntimeException exception) {
174 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
175 LoggerTragetServiceName.UPLOAD_PROCESS_ARTIFACT, ErrorLevel.ERROR.name(),
176 LoggerErrorCode.DATA_ERROR.getErrorCode(), "Can't upload process artifact");
177 throw new CoreException(new UploadInvalidErrorBuilder().build(), exception);
182 private void validateProcessExistence(String vspId, Version version, String componentId,
183 String processId, ProcessEntity retrieved) {
184 VersioningUtil.validateEntityExistence(retrieved,
185 new ProcessEntity(vspId, version, componentId, processId),
186 VspDetails.ENTITY_TYPE);
189 private void validateProcessArtifactExistence(String vspId, Version version, String componentId,
190 String processId, ProcessEntity retrieved) {
191 ProcessEntity inputProcess = new ProcessEntity(vspId, version, componentId, processId);
192 VersioningUtil.validateEntityExistence(retrieved, inputProcess, VspDetails.ENTITY_TYPE);
193 if (retrieved.getArtifact() == null) {
194 throw new CoreException(new ErrorCode.ErrorCodeBuilder()
195 .withCategory(ErrorCategory.APPLICATION)
196 .withId(VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND)
197 .withMessage(String.format(PROCESS_ARTIFACT_NOT_EXIST_MSG,
198 processId, VspDetails.ENTITY_TYPE, vspId, version)).build());
203 protected void validateUniqueName(String vspId, Version version, String componentId,
204 String processName) {
205 UniqueValueUtil.validateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME,
206 vspId, version.getId(), componentId, processName);
209 protected void createUniqueName(String vspId, Version version, String componentId,
210 String processName) {
212 .createUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
213 version.getId(), componentId, processName);
216 protected void updateUniqueName(String vspId, Version version, String componentId,
217 String oldProcessName, String newProcessName) {
219 .updateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, oldProcessName,
220 newProcessName, vspId, version.getId(), componentId);
223 protected void deleteUniqueValue(String vspId, Version version, String componentId,
224 String processName) {
225 if (componentId == null) {
227 .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
228 version.getId(), processName);
231 .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
232 version.getId(), componentId, processName);