49c30af2dde7fea523201ce59cfc4312cc776e0b
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.impl;
18
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;
39
40 import java.io.File;
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;
46
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";
50
51   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
52
53   private final ProcessDao processDao;
54   private static final String VSP_ID_COMPONENT_ID = "VSP id, component id";
55
56   public ProcessManagerImpl(ProcessDao processDao) {
57     this.processDao = processDao;
58   }
59
60   @Override
61   public Collection<ProcessEntity> listProcesses(String vspId, Version version,
62                                                  String componentId) {
63     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
64     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
65
66     return processDao.list(new ProcessEntity(vspId, version, componentId, null));
67   }
68
69   @Override
70   public void deleteProcesses(String vspId, Version version, String componentId) {
71     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
72
73     ProcessEntity allProcesses = new ProcessEntity(vspId, version, componentId, null);
74     Collection<ProcessEntity> processes = processDao.list(allProcesses);
75
76     if (!processes.isEmpty()) {
77       for (ProcessEntity process : processes) {
78         deleteUniqueValue(process.getVspId(), process.getVersion(), process.getComponentId(),
79             process.getName());
80       }
81     }
82
83     if (componentId == null) {
84       processDao.deleteVspAll(vspId,version);
85     } else {
86       processDao.deleteAll(allProcesses);
87     }
88
89     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
90   }
91
92   @Override
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(),
97         process.getName());
98
99     processDao.create(process);
100     createUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
101         process.getName());
102
103     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, process.getId(),
104         process.getComponentId());
105
106     return process;
107   }
108
109   @Override
110   public ProcessEntity getProcess(String vspId, Version version, String componentId,
111                                   String processId) {
112     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
113
114     ProcessEntity retrieved =
115         processDao.get(new ProcessEntity(vspId, version, componentId, processId));
116     validateProcessExistence(vspId, version, componentId, processId, retrieved);
117
118     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
119
120     return retrieved;
121   }
122
123   @Override
124   public void updateProcess(ProcessEntity process) {
125     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, process.getId(),
126         process.getComponentId());
127
128     ProcessEntity retrieved = processDao.get(process);
129     validateProcessExistence(process.getVspId(), process.getVersion(), process.getComponentId(),
130         process.getId(), retrieved);
131
132     updateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
133         retrieved.getName(), process.getName());
134     processDao.update(process);
135
136     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, process.getId(),
137         process.getComponentId());
138   }
139
140   @Override
141   public void deleteProcess(String vspId, Version version, String componentId, String processId) {
142     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
143
144     ProcessEntity retrieved = getProcess(vspId, version, componentId, processId);
145
146     processDao.delete(retrieved);
147     deleteUniqueValue(retrieved.getVspId(), retrieved.getVersion(), retrieved.getComponentId(),
148         retrieved.getName());
149
150     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
151   }
152
153
154   @Override
155   public File getProcessArtifact(String vspId, Version version, String componentId,
156                                  String processId) {
157     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
158
159     ProcessEntity retrieved = getValidatedProcessArtifact(vspId, version, componentId, processId);
160
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);
169     }
170
171     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
172
173     return file;
174   }
175
176   @Override
177   public void deleteProcessArtifact(String vspId, Version version, String componentId,
178                                     String processId) {
179     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
180
181     ProcessEntity retrieved = getValidatedProcessArtifact(vspId, version, componentId, processId);
182
183     processDao.deleteArtifact(retrieved);
184
185     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
186   }
187
188   @Override
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);
192
193     ProcessEntity process = getProcess(vspId, version, componentId, processId);
194     process.setArtifactName(artifactFileName);
195     process.setArtifact(readArtifact(artifactFile));
196     processDao.uploadArtifact(process);
197
198     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID_COMPONENT_ID, vspId, componentId);
199   }
200
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);
206     return retrieved;
207   }
208
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());
215     }
216     try {
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);
223     }
224   }
225
226
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);
232   }
233
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());
244     }
245   }
246
247
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);
252   }
253
254   protected void createUniqueName(String vspId, Version version, String componentId,
255                                   String processName) {
256     UniqueValueUtil
257         .createUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
258             version.getId(), componentId, processName);
259   }
260
261   protected void updateUniqueName(String vspId, Version version, String componentId,
262                                   String oldProcessName, String newProcessName) {
263     UniqueValueUtil
264         .updateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, oldProcessName,
265             newProcessName, vspId, version.getId(), componentId);
266   }
267
268   protected void deleteUniqueValue(String vspId, Version version, String componentId,
269                                    String processName) {
270     if (componentId == null) {
271       UniqueValueUtil
272           .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
273               version.getId(), processName);
274     }
275     UniqueValueUtil
276         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
277             version.getId(), componentId, processName);
278   }
279 }