a3d02860196a57591ba2f83a2768bed950b6c19f
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
22
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;
45
46 import java.io.File;
47 import java.io.FileOutputStream;
48 import java.io.IOException;
49 import java.io.InputStream;
50 import java.util.Collection;
51
52 public class ProcessManagerImpl implements ProcessManager {
53   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
54   private final ActivityLogManager activityLogManager;
55
56   private final VendorSoftwareProductDao vendorSoftwareProductDao;
57
58   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
59
60   public ProcessManagerImpl(VendorSoftwareProductDao vendorSoftwareProductDao, ActivityLogManager activityLogManager) {
61     this.vendorSoftwareProductDao = vendorSoftwareProductDao;
62     this.activityLogManager = activityLogManager;
63   }
64
65   @Override
66   public Collection<ProcessEntity> listProcesses(String vspId, Version version,
67                                                  String componentId,
68                                                  String user) {
69     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id", vspId, componentId);
70     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", vspId, componentId);
71
72     return vendorSoftwareProductDao.listProcesses(vspId, version, componentId);
73   }
74
75   @Override
76   public void deleteProcesses(String vspId, Version version, String componentId, String user) {
77     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id", vspId, componentId);
78
79     Collection<ProcessEntity> processes =
80         vendorSoftwareProductDao.listProcesses(vspId, version, componentId);
81
82     if (!processes.isEmpty()) {
83       for (ProcessEntity process : processes) {
84         deleteUniqueValue(process.getVspId(), process.getVersion(), process.getComponentId(),
85             process.getName());
86       }
87
88       vendorSoftwareProductDao.deleteProcesses(vspId, version, componentId);
89     }
90     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", vspId, componentId);
91   }
92
93   @Override
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(),
98         process.getName());
99     //process.setId(CommonMethods.nextUuId());
100
101     vendorSoftwareProductDao.createProcess(process);
102     createUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
103         process.getName());
104
105     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", process.getId(),
106         process.getComponentId());
107
108     return process;
109   }
110
111
112   @Override
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);
116
117     ProcessEntity retrieved =
118         vendorSoftwareProductDao.getProcess(vspId, version, componentId, processId);
119     validateProcessExistence(vspId, version, componentId, processId, retrieved);
120
121     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", vspId, componentId);
122
123     return retrieved;
124   }
125
126   @Override
127   public void updateProcess(ProcessEntity process, String user) {
128     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id", process.getId(),
129         process.getComponentId());
130
131     ProcessEntity retrieved = vendorSoftwareProductDao
132         .getProcess(process.getVspId(), process.getVersion(), process.getComponentId(),
133             process.getId());
134     validateProcessExistence(process.getVspId(), process.getVersion(), process.getComponentId(),
135         process.getId(), retrieved);
136
137     updateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
138         retrieved.getName(), process.getName());
139     vendorSoftwareProductDao.updateProcess(process);
140
141     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", process.getId(),
142         process.getComponentId());
143   }
144
145   @Override
146   public void deleteProcess(String vspId, Version version, String componentId, String processId,
147                             String user) {
148     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id", vspId, componentId);
149
150     ProcessEntity retrieved =
151         vendorSoftwareProductDao.getProcess(vspId, version, componentId, processId);
152     validateProcessExistence(vspId, version, componentId, processId, retrieved);
153
154     vendorSoftwareProductDao.deleteProcess(vspId, version, componentId, processId);
155     deleteUniqueValue(retrieved.getVspId(), retrieved.getVersion(), retrieved.getComponentId(),
156         retrieved.getName());
157
158     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", vspId, componentId);
159   }
160
161
162   @Override
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);
166
167     ProcessEntity retrieved =
168         vendorSoftwareProductDao.getProcessArtifact(vspId, version, componentId, processId);
169     validateProcessArtifactExistence(vspId, version, componentId, processId, retrieved);
170
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);
179     }
180
181     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", vspId, componentId);
182
183     return file;
184   }
185
186   @Override
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);
190
191     ProcessEntity retrieved =
192         vendorSoftwareProductDao.getProcessArtifact(vspId, version, componentId, processId);
193     validateProcessArtifactExistence(vspId, version, componentId, processId, retrieved);
194
195     vendorSoftwareProductDao.deleteProcessArtifact(vspId, version, componentId, processId);
196
197     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", vspId, componentId);
198   }
199
200   @Override
201   public void uploadProcessArtifact(InputStream artifactFile, String artifactFileName, String vspId,
202                                     Version version, String componentId, String processId,
203                                     String user) {
204     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP id, component id", vspId, componentId);
205
206     ProcessEntity retrieved =
207         vendorSoftwareProductDao.getProcess(vspId, version, componentId, processId);
208     validateProcessExistence(vspId, version, componentId, processId, retrieved);
209
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());
215     }
216
217     byte[] artifact;
218     try {
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);
225     }
226
227     vendorSoftwareProductDao.uploadProcessArtifact(vspId, version, componentId, processId, artifact,
228             artifactFileName);
229     ActivityLogEntity activityLogEntity = new ActivityLogEntity(vspId, String.valueOf(version.getMajor()+1),
230         ActivityType.UPLOAD_MONITORING_FILE.toString(), user, true, "", "");
231     activityLogManager.addActionLog(activityLogEntity, user);
232
233     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id", vspId, componentId);
234   }
235
236
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,
240         processId);
241
242     if (retrieved != null) {
243       return;
244     }
245     VersioningUtil.validateEntityExistence(retrieved,
246         new ProcessEntity(vspId, version, componentId, processId),
247         VspDetails.ENTITY_TYPE);//todo retrieved is always null ??
248
249     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id, process id", vspId, componentId,
250         processId);
251   }
252
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,
256         processId);
257
258     if (retrieved != null) {
259       VersioningUtil.validateEntityExistence(retrieved.getArtifact(),
260           new ProcessEntity(vspId, version, componentId, processId),
261           VspDetails.ENTITY_TYPE);
262     } else {
263       VersioningUtil.validateEntityExistence(retrieved,
264           new ProcessEntity(vspId, version, componentId, processId),
265           VspDetails.ENTITY_TYPE); //todo retrieved is always null ??
266     }
267
268     MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP id, component id, process id", vspId, componentId,
269         processId);
270   }
271
272
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);
277   }
278
279   protected void createUniqueName(String vspId, Version version, String componentId,
280                                   String processName) {
281     UniqueValueUtil
282         .createUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
283             version.toString(), componentId, processName);
284   }
285
286   protected void updateUniqueName(String vspId, Version version, String componentId,
287                                   String oldProcessName, String newProcessName) {
288     UniqueValueUtil
289         .updateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, oldProcessName,
290             newProcessName, vspId, version.toString(), componentId);
291   }
292
293   protected void deleteUniqueValue(String vspId, Version version, String componentId,
294                                    String processName) {
295     UniqueValueUtil
296         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
297             version.toString(), componentId, processName);
298   }
299 }