3ec8c7ae2f84a281f2c6141a298a642b751c6799
[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.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;
38
39 import java.io.File;
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;
45
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";
51
52   public ProcessManagerImpl(ProcessDao processDao) {
53     this.processDao = processDao;
54   }
55
56   @Override
57   public Collection<ProcessEntity> listProcesses(String vspId, Version version,
58                                                  String componentId) {
59     return processDao.list(new ProcessEntity(vspId, version, componentId, null));
60   }
61
62   @Override
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);
66
67     if (!processes.isEmpty()) {
68       for (ProcessEntity process : processes) {
69         deleteUniqueValue(process.getVspId(), process.getVersion(), process.getComponentId(),
70             process.getName());
71       }
72     }
73
74     if (componentId == null) {
75       processDao.deleteVspAll(vspId,version);
76     } else {
77       processDao.deleteAll(allProcesses);
78     }
79   }
80
81   @Override
82   public ProcessEntity createProcess(ProcessEntity process) {
83     validateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
84         process.getName());
85
86     processDao.create(process);
87     createUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
88         process.getName());
89     return process;
90   }
91
92   @Override
93   public ProcessEntity getProcess(String vspId, Version version, String componentId,
94                                   String processId) {
95     ProcessEntity retrieved =
96         processDao.get(new ProcessEntity(vspId, version, componentId, processId));
97     validateProcessExistence(vspId, version, componentId, processId, retrieved);
98     return retrieved;
99   }
100
101   @Override
102   public void updateProcess(ProcessEntity process) {
103     ProcessEntity retrieved = processDao.get(process);
104     validateProcessExistence(process.getVspId(), process.getVersion(), process.getComponentId(),
105         process.getId(), retrieved);
106
107     updateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
108         retrieved.getName(), process.getName());
109     processDao.update(process);
110   }
111
112   @Override
113   public void deleteProcess(String vspId, Version version, String componentId, String processId) {
114     ProcessEntity retrieved = getProcess(vspId, version, componentId, processId);
115
116     processDao.delete(retrieved);
117     deleteUniqueValue(retrieved.getVspId(), retrieved.getVersion(), retrieved.getComponentId(),
118         retrieved.getName());
119   }
120
121
122   @Override
123   public File getProcessArtifact(String vspId, Version version, String componentId,
124                                  String processId) {
125     ProcessEntity retrieved = getValidatedProcessArtifact(vspId, version, componentId, processId);
126
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);
135     }
136     return file;
137   }
138
139   @Override
140   public void deleteProcessArtifact(String vspId, Version version, String componentId,
141                                     String processId) {
142     ProcessEntity retrieved = getValidatedProcessArtifact(vspId, version, componentId, processId);
143
144     processDao.deleteArtifact(retrieved);
145   }
146
147   @Override
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);
154   }
155
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);
161     return retrieved;
162   }
163
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());
170     }
171     try {
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);
178     }
179   }
180
181
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);
187   }
188
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());
199     }
200   }
201
202
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);
207   }
208
209   protected void createUniqueName(String vspId, Version version, String componentId,
210                                   String processName) {
211     UniqueValueUtil
212         .createUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
213             version.getId(), componentId, processName);
214   }
215
216   protected void updateUniqueName(String vspId, Version version, String componentId,
217                                   String oldProcessName, String newProcessName) {
218     UniqueValueUtil
219         .updateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, oldProcessName,
220             newProcessName, vspId, version.getId(), componentId);
221   }
222
223   protected void deleteUniqueValue(String vspId, Version version, String componentId,
224                                    String processName) {
225     if (componentId == null) {
226       UniqueValueUtil
227           .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
228               version.getId(), processName);
229     }
230     UniqueValueUtil
231         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
232             version.getId(), componentId, processName);
233   }
234 }