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