855007dfd9c8001b5a1e76789cf9cf836e593b0e
[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.dao.UniqueValueDao;
20 import org.openecomp.core.util.UniqueValueUtil;
21 import org.openecomp.core.utilities.file.FileUtils;
22 import org.openecomp.sdc.common.errors.CoreException;
23 import org.openecomp.sdc.common.errors.ErrorCode;
24 import org.openecomp.sdc.vendorsoftwareproduct.ProcessManager;
25 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants;
26 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
27 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
28 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
29 import org.openecomp.sdc.vendorsoftwareproduct.errors.UploadInvalidErrorBuilder;
30 import org.openecomp.sdc.versioning.VersioningUtil;
31 import org.openecomp.sdc.versioning.dao.types.Version;
32 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
33
34 import java.io.File;
35 import java.io.FileOutputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.nio.ByteBuffer;
39 import java.util.Collection;
40
41 public class ProcessManagerImpl implements ProcessManager {
42
43   private static final String PROCESS_ARTIFACT_NOT_EXIST_MSG =
44       "Process artifact for process with Id %s does not exist for %s with Id %s and version %s";
45   private final ProcessDao processDao;
46   private final UniqueValueUtil uniqueValueUtil;
47
48   public ProcessManagerImpl(ProcessDao processDao, UniqueValueDao uniqueValueDao) {
49     this.processDao = processDao;
50     this.uniqueValueUtil = new UniqueValueUtil(uniqueValueDao);
51   }
52
53   @Override
54   public Collection<ProcessEntity> listProcesses(String vspId, Version version,
55                                                  String componentId) {
56     return processDao.list(new ProcessEntity(vspId, version, componentId, null));
57   }
58
59   @Override
60   public void deleteProcesses(String vspId, Version version, String componentId) {
61     ProcessEntity allProcesses = new ProcessEntity(vspId, version, componentId, null);
62     Collection<ProcessEntity> processes = processDao.list(allProcesses);
63
64     if (!processes.isEmpty()) {
65       for (ProcessEntity process : processes) {
66         deleteUniqueValue(process.getVspId(), process.getVersion(), process.getComponentId(),
67             process.getName());
68       }
69     }
70
71     if (componentId == null) {
72       processDao.deleteVspAll(vspId,version);
73     } else {
74       processDao.deleteAll(allProcesses);
75     }
76   }
77
78   @Override
79   public ProcessEntity createProcess(ProcessEntity process) {
80     validateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
81         process.getName());
82
83     processDao.create(process);
84     createUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
85         process.getName());
86     return process;
87   }
88
89   @Override
90   public ProcessEntity getProcess(String vspId, Version version, String componentId,
91                                   String processId) {
92     ProcessEntity retrieved =
93         processDao.get(new ProcessEntity(vspId, version, componentId, processId));
94     validateProcessExistence(vspId, version, componentId, processId, retrieved);
95     return retrieved;
96   }
97
98   @Override
99   public void updateProcess(ProcessEntity process) {
100     ProcessEntity retrieved = processDao.get(process);
101     validateProcessExistence(process.getVspId(), process.getVersion(), process.getComponentId(),
102         process.getId(), retrieved);
103
104     updateUniqueName(process.getVspId(), process.getVersion(), process.getComponentId(),
105         retrieved.getName(), process.getName());
106     processDao.update(process);
107   }
108
109   @Override
110   public void deleteProcess(String vspId, Version version, String componentId, String processId) {
111     ProcessEntity retrieved = getProcess(vspId, version, componentId, processId);
112
113     processDao.delete(retrieved);
114     deleteUniqueValue(retrieved.getVspId(), retrieved.getVersion(), retrieved.getComponentId(),
115         retrieved.getName());
116   }
117
118
119   @Override
120   public File getProcessArtifact(String vspId, Version version, String componentId,
121                                  String processId) {
122     ProcessEntity retrieved = getValidatedProcessArtifact(vspId, version, componentId, processId);
123
124     File file = new File(String.format("%s_%s_%s", vspId, componentId, processId));
125     try (FileOutputStream fos = new FileOutputStream(file)) {
126       fos.write(retrieved.getArtifact().array());
127     } catch (IOException exception) {
128       throw new CoreException(new UploadInvalidErrorBuilder().build(), exception);
129     }
130     return file;
131   }
132
133   @Override
134   public void deleteProcessArtifact(String vspId, Version version, String componentId,
135                                     String processId) {
136     ProcessEntity retrieved = getValidatedProcessArtifact(vspId, version, componentId, processId);
137
138     processDao.deleteArtifact(retrieved);
139   }
140
141   @Override
142   public void uploadProcessArtifact(InputStream artifactFile, String artifactFileName, String vspId,
143                                     Version version, String componentId, String processId) {
144     ProcessEntity process = getProcess(vspId, version, componentId, processId);
145     process.setArtifactName(artifactFileName);
146     process.setArtifact(readArtifact(artifactFile));
147     processDao.uploadArtifact(process);
148   }
149
150   private ProcessEntity getValidatedProcessArtifact(String vspId, Version version,
151                                                     String componentId, String processId) {
152     ProcessEntity retrieved =
153         processDao.getArtifact(new ProcessEntity(vspId, version, componentId, processId));
154     validateProcessArtifactExistence(vspId, version, componentId, processId, retrieved);
155     return retrieved;
156   }
157
158   private ByteBuffer readArtifact(InputStream artifactInputStream) {
159     if (artifactInputStream == null) {
160       throw new CoreException(new UploadInvalidErrorBuilder().build());
161     }
162     try {
163       return ByteBuffer.wrap(FileUtils.toByteArray(artifactInputStream));
164     } catch (RuntimeException exception) {
165       throw new CoreException(new UploadInvalidErrorBuilder().build(), exception);
166     }
167   }
168
169
170   private void validateProcessExistence(String vspId, Version version, String componentId,
171                                         String processId, ProcessEntity retrieved) {
172     VersioningUtil.validateEntityExistence(retrieved,
173         new ProcessEntity(vspId, version, componentId, processId),
174         VspDetails.ENTITY_TYPE);
175   }
176
177   private void validateProcessArtifactExistence(String vspId, Version version, String componentId,
178                                                 String processId, ProcessEntity retrieved) {
179     ProcessEntity inputProcess = new ProcessEntity(vspId, version, componentId, processId);
180     VersioningUtil.validateEntityExistence(retrieved, inputProcess, VspDetails.ENTITY_TYPE);
181     if (retrieved.getArtifact() == null) {
182       throw new CoreException(new ErrorCode.ErrorCodeBuilder()
183           .withId(VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND)
184           .withMessage(String.format(PROCESS_ARTIFACT_NOT_EXIST_MSG,
185               processId, VspDetails.ENTITY_TYPE, vspId, version)).build());
186     }
187   }
188
189
190   protected void validateUniqueName(String vspId, Version version, String componentId,
191                                     String processName) {
192     uniqueValueUtil.validateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME,
193         vspId, version.getId(), componentId, processName);
194   }
195
196   protected void createUniqueName(String vspId, Version version, String componentId,
197                                   String processName) {
198     uniqueValueUtil
199         .createUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
200             version.getId(), componentId, processName);
201   }
202
203   protected void updateUniqueName(String vspId, Version version, String componentId,
204                                   String oldProcessName, String newProcessName) {
205     uniqueValueUtil
206         .updateUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, oldProcessName,
207             newProcessName, vspId, version.getId(), componentId);
208   }
209
210   protected void deleteUniqueValue(String vspId, Version version, String componentId,
211                                    String processName) {
212     if (componentId == null) {
213       uniqueValueUtil
214           .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
215               version.getId(), processName);
216     }
217     uniqueValueUtil
218         .deleteUniqueValue(VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME, vspId,
219             version.getId(), componentId, processName);
220   }
221 }