Add collaboration feature
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / orchestration / OrchestrationUtil.java
1 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration;
2
3 import org.apache.commons.collections4.CollectionUtils;
4 import org.apache.commons.collections4.MapUtils;
5 import org.openecomp.core.model.dao.ServiceModelDao;
6 import org.openecomp.core.model.dao.ServiceModelDaoFactory;
7 import org.openecomp.core.util.UniqueValueUtil;
8 import org.openecomp.core.utilities.file.FileContentHandler;
9 import org.openecomp.core.utilities.file.FileUtils;
10 import org.openecomp.core.utilities.json.JsonUtil;
11 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
12 import org.openecomp.sdc.common.errors.CoreException;
13 import org.openecomp.sdc.common.errors.Messages;
14 import org.openecomp.sdc.common.utils.CommonUtil;
15 import org.openecomp.sdc.common.utils.SdcCommon;
16 import org.openecomp.sdc.datatypes.error.ErrorLevel;
17 import org.openecomp.sdc.datatypes.error.ErrorMessage;
18 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
19 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
20 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
21 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
22 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
23 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
24 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
25 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
26 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDao;
27 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDependencyModelDaoFactory;
28 import org.openecomp.sdc.vendorsoftwareproduct.dao.MonitoringUploadDaoFactory;
29 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
30 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDaoFactory;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDaoFactory;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDaoFactory;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDaoFactory;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.VspMergeDaoFactory;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateEntity;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
44 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
45 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionDataExtractorFactory;
46 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionEntityDataManagerFactory;
47 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
48 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
49 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
50 import org.openecomp.sdc.versioning.dao.types.Version;
51
52 import java.io.IOException;
53 import java.io.InputStream;
54 import java.nio.ByteBuffer;
55 import java.util.Collection;
56 import java.util.HashMap;
57 import java.util.List;
58 import java.util.Map;
59 import java.util.Objects;
60 import java.util.Optional;
61 import java.util.Set;
62 import java.util.stream.Collectors;
63
64 import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME;
65
66 public class OrchestrationUtil {
67
68   public static final String ORCHESTRATION_CONFIG_NAMESPACE = "orchestration";
69   public static final String ORCHESTRATION_IMPL_KEY = "orchestration_impl";
70
71   private NicDao nicDao;
72   private ComponentArtifactDao componentArtifactDao;
73   private ProcessDao processDao;
74   private OrchestrationTemplateDao orchestrationTemplateDataDao;
75   private ComponentDao componentDao;
76   private ServiceModelDao serviceModelDao;
77   private ComponentDependencyModelDao componentDependencyModelDao;
78   private CompositionEntityDataManager compositionEntityDataManager;
79   private CompositionDataExtractor compositionDataExtractor;
80
81   public OrchestrationUtil() {
82     this(NicDaoFactory.getInstance().createInterface(),
83         MonitoringUploadDaoFactory.getInstance().createInterface(),
84         ProcessDaoFactory.getInstance().createInterface(),
85         OrchestrationTemplateDaoFactory.getInstance().createInterface(),
86         ComponentDaoFactory.getInstance().createInterface(),
87         ServiceModelDaoFactory.getInstance().createInterface(),
88         ComponentDependencyModelDaoFactory.getInstance().createInterface(),
89         CompositionEntityDataManagerFactory.getInstance().createInterface(),
90         CompositionDataExtractorFactory.getInstance().createInterface());
91   }
92
93   public OrchestrationUtil(
94       NicDao nicDao,
95       ComponentArtifactDao componentArtifactDao,
96       ProcessDao processDao,
97       OrchestrationTemplateDao orchestrationTemplateDataDao,
98       ComponentDao componentDao,
99       ServiceModelDao serviceModelDao,
100       ComponentDependencyModelDao componentDependencyModelDao,
101       CompositionEntityDataManager compositionEntityDataManager,
102       CompositionDataExtractor compositionDataExtractor) {
103     this.nicDao = nicDao;
104     this.componentArtifactDao = componentArtifactDao;
105     this.processDao = processDao;
106     this.orchestrationTemplateDataDao = orchestrationTemplateDataDao;
107     this.componentDao = componentDao;
108     this.serviceModelDao = serviceModelDao;
109     this.componentDependencyModelDao = componentDependencyModelDao;
110     this.compositionEntityDataManager = compositionEntityDataManager;
111     this.compositionDataExtractor = compositionDataExtractor;
112   }
113
114   public static Optional<FileContentHandler> getFileContentMap(OnboardingTypesEnum type,
115                                                                UploadFileResponse uploadFileResponse,
116                                                                byte[] uploadedFileData) {
117     FileContentHandler contentMap = null;
118     try {
119       contentMap = CommonUtil.validateAndUploadFileContent(type, uploadedFileData);
120     } catch (IOException exception) {
121       uploadFileResponse.addStructureError(
122           SdcCommon.UPLOAD_FILE,
123           new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
124     } catch (CoreException coreException) {
125       uploadFileResponse.addStructureError(
126           SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
127     }
128     return Optional.ofNullable(contentMap);
129   }
130
131   public void backupComponentsQuestionnaireBeforeDelete(String vspId, Version version,
132                                                         Map<String, String> componentsQustanniare,
133                                                         Map<String, Map<String, String>>
134                                                             componentNicsQustanniare,
135                                                         Map<String, Collection<ComponentMonitoringUploadEntity>>
136                                                             componentMibList,
137                                                         Map<String, Collection<ProcessEntity>>
138                                                             componentProcesses,
139                                                         Map<String, ProcessEntity> processArtifact) {
140
141     Collection<ComponentEntity> componentsCompositionAndQuestionnaire =
142         componentDao.listCompositionAndQuestionnaire(vspId, version);
143     componentsCompositionAndQuestionnaire.forEach(componentEntity ->
144         backupComponentQuestionnaire(vspId, version, componentEntity, componentsQustanniare,
145             componentNicsQustanniare, componentMibList, componentProcesses, processArtifact));
146   }
147
148   private void backupComponentQuestionnaire(
149       String vspId, Version version,
150       ComponentEntity componentEntity,
151       Map<String, String> componentsQustanniare,
152       Map<String, Map<String, String>> componentNicsQustanniare,
153       Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList,
154       Map<String, Collection<ProcessEntity>> componentProcesses,
155       Map<String, ProcessEntity> processArtifact) {
156     String componentName = componentEntity.getComponentCompositionData().getName();
157     componentsQustanniare.put(componentName, componentEntity.getQuestionnaireData());
158     backupMibData(vspId, version, componentEntity, componentName, componentMibList);
159     backupProcess(vspId, version, componentEntity.getId(), componentName, componentProcesses,
160         processArtifact);
161     backupNicsQuestionnaire(vspId, version, componentEntity, componentName,
162         componentNicsQustanniare);
163   }
164
165   private void backupMibData(String vspId, Version version, ComponentEntity componentEntity,
166                              String componentName,
167                              Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList) {
168     Collection<ComponentMonitoringUploadEntity> componentMib =
169         componentArtifactDao.listArtifacts(new
170             ComponentMonitoringUploadEntity(vspId, version, componentEntity.getId(),
171             null));
172     if (CollectionUtils.isNotEmpty(componentMib)) {
173       componentMibList.put(componentName, componentMib);
174     }
175   }
176
177   private void backupProcess(String vspId, Version version, String componentId,
178                              String componentName, Map<String,
179       Collection<ProcessEntity>> processes,
180                              Map<String, ProcessEntity> processArtifact) {
181     Collection<ProcessEntity> processList =
182         processDao.list(new ProcessEntity(vspId, version, componentId, null));
183     if (!processList.isEmpty()) {
184       processes.put(componentName, processList);
185       processList.forEach(process -> {
186         ProcessEntity artifact = processDao
187             .getArtifact(new ProcessEntity(vspId, version, componentId, process.getId()));
188         if (artifact.getArtifact() != null) {
189           processArtifact.put(process.getId(), artifact);
190         }
191       });
192     }
193   }
194
195   private void backupNicsQuestionnaire(String vspId, Version version,
196                                        ComponentEntity componentEntity,
197                                        String componentName,
198                                        Map<String, Map<String, String>> componentNicsQustanniare) {
199     Collection<NicEntity>
200         nics = nicDao.list(new NicEntity(vspId, version, componentEntity.getId(), null));
201     if (CollectionUtils.isNotEmpty(nics)) {
202       Map<String, String> nicsQuestionnaire = new HashMap<>();
203       nics.forEach(nicEntity -> {
204         NicEntity nicQuestionnaire = nicDao.getQuestionnaireData(vspId, version,
205             componentEntity.getId(), nicEntity.getId());
206
207         nicsQuestionnaire.put(nicEntity.getNicCompositionData().getName(),
208             nicQuestionnaire.getQuestionnaireData());
209       });
210       componentNicsQustanniare.put(componentName, nicsQuestionnaire);
211     }
212   }
213
214   public void retainComponentQuestionnaireData(String vspId, Version version,
215                                                Map<String, String> componentsQustanniare,
216                                                Map<String, Map<String, String>>
217                                                    componentNicsQustanniare,
218                                                Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList,
219                                                Map<String, Collection<ProcessEntity>> processes,
220                                                Map<String, ProcessEntity> processArtifact) {
221     //VSP processes
222     restoreProcess(vspId, version, null, null, processes, processArtifact);
223     Collection<ComponentEntity> components =
224         componentDao.list(new ComponentEntity(vspId, version, null));
225     components.forEach(componentEntity -> {
226       String componentName = componentEntity.getComponentCompositionData().getName();
227       if (componentsQustanniare.containsKey(componentName)) {
228         componentDao.updateQuestionnaireData(vspId, version,
229             componentEntity.getId(),
230             componentsQustanniare.get(componentEntity.getComponentCompositionData()
231                 .getName()));
232         if (componentNicsQustanniare.containsKey(componentName)) {
233           restoreComponentNicQuestionnaire(vspId, version, componentName, componentEntity,
234               componentNicsQustanniare);
235         }
236         //MIB //todo add for VES_EVENTS
237         if (componentMibList.containsKey(componentName)) {
238           restoreComponentMibData(componentName, componentEntity, componentMibList);
239         }
240         //VFC processes
241         restoreProcess(vspId, version, componentEntity.getId(), componentName, processes,
242             processArtifact);
243       }
244     });
245   }
246
247   private void restoreComponentNicQuestionnaire(String vspId, Version version,
248                                                 String componentName,
249                                                 ComponentEntity componentEntity,
250                                                 Map<String, Map<String, String>> componentNicsQustanniare) {
251     Map<String, String> nicsQustanniare = componentNicsQustanniare.get(componentName);
252     Collection<NicEntity> nics =
253         nicDao.list(new NicEntity(vspId, version, componentEntity.getId(), null));
254     nics.forEach(nicEntity -> {
255       if (nicsQustanniare.containsKey(nicEntity.getNicCompositionData().getName())) {
256         nicDao.updateQuestionnaireData(vspId, version,
257             componentEntity.getId(), nicEntity.getId(),
258             nicsQustanniare.get(nicEntity.getNicCompositionData().getName()));
259       }
260     });
261   }
262
263   private void restoreComponentMibData(String componentName, ComponentEntity componentEntity,
264                                        Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList) {
265     Collection<ComponentMonitoringUploadEntity> mibList = componentMibList.get(componentName);
266     mibList.forEach(mib -> {
267       mib.setComponentId(componentEntity.getId());
268       componentArtifactDao.create(mib);
269     });
270   }
271
272   private void restoreProcess(String vspId, Version version, String componentId,
273                               String componentName,
274                               Map<String, Collection<ProcessEntity>> processes,
275                               Map<String, ProcessEntity> processArtifact) {
276     if (processes.containsKey(componentName)) {
277       Collection<ProcessEntity> processList = processes.get(componentName);
278       processList.forEach(process -> {
279         process.setComponentId(componentId);
280         UniqueValueUtil.createUniqueValue(PROCESS_NAME, vspId, version.getId(), componentId,
281             process.getName());
282         processDao.create(process);
283         if (processArtifact.containsKey(process.getId())) {
284           ProcessEntity artifact = processArtifact.get(process.getId());
285           processDao.uploadArtifact(artifact);
286         }
287       });
288     }
289   }
290
291   public void deleteUploadDataAndContent(String vspId, Version version) {
292     VendorSoftwareProductInfoDaoFactory.getInstance().createInterface()
293         .delete(new VspDetails(vspId, version));
294   }
295
296   public void saveUploadData(VspDetails vspDetails,
297                              OrchestrationTemplateCandidateData candidateData,
298                              InputStream uploadedFileData,
299                              FileContentHandler fileContentMap, HeatStructureTree tree) {
300     Map<String, Object> manifestAsMap =
301         fileContentMap.containsFile(SdcCommon.MANIFEST_NAME)
302             ? (Map<String, Object>) JsonUtil.json2Object(fileContentMap.getFileContent(
303             SdcCommon.MANIFEST_NAME), Map.class)
304             : new HashMap<>();
305
306     OrchestrationTemplateEntity uploadData = new OrchestrationTemplateEntity();
307     uploadData.setFileSuffix(candidateData.getFileSuffix());
308     uploadData.setFileName(candidateData.getFileName());
309     uploadData.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(uploadedFileData)));
310     uploadData.setValidationDataStructure(new ValidationStructureList(tree));
311     uploadData.setPackageName(Objects.isNull(manifestAsMap.get("name")) ? null :
312         (String) manifestAsMap.get("name"));
313     uploadData.setPackageVersion(Objects.isNull(manifestAsMap.get("version")) ? null :
314         (String) manifestAsMap.get("version"));
315     orchestrationTemplateDataDao.update(vspDetails.getId(), vspDetails.getVersion(), uploadData);
316
317     VspMergeDaoFactory.getInstance().createInterface()
318         .updateVspModelId(vspDetails.getId(), vspDetails.getVersion());
319   }
320
321   public void saveServiceModel(String vspId, Version version,
322                                ToscaServiceModel serviceModelToExtract,
323                                ToscaServiceModel serviceModelToStore) {
324     if (serviceModelToExtract != null) {
325       serviceModelDao.storeServiceModel(vspId, version, serviceModelToStore);
326       //Extracting the compostion data from the output service model of the first phase of
327       // translation
328       compositionEntityDataManager.saveCompositionData(vspId, version,
329           compositionDataExtractor.extractServiceCompositionData(serviceModelToExtract));
330     }
331   }
332
333   public static HeatStructureTree createHeatTree(FileContentHandler fileContentMap,
334                                                  Map<String, List<ErrorMessage>> validationErrors) {
335     HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileContentMap);
336     heatTreeManager.createTree();
337     heatTreeManager.addErrors(validationErrors);
338     return heatTreeManager.getTree();
339   }
340
341   public void updateVspComponentDependencies(String vspId, Version version,
342                                              Map<String, String> vspComponentIdNameInfoBeforeProcess) {
343     Map<String, String> updatedVspComponentNameIdInfo = getVspComponentNameIdInfo(vspId, version);
344     if (MapUtils.isNotEmpty(updatedVspComponentNameIdInfo)) {
345       Set<String> updatedVspComponentNames = updatedVspComponentNameIdInfo.keySet();
346       Collection<ComponentDependencyModelEntity> componentDependencies =
347           componentDependencyModelDao.list(new ComponentDependencyModelEntity(vspId,
348               version, null));
349       if (CollectionUtils.isNotEmpty(componentDependencies)) {
350         updateComponentDependency(vspComponentIdNameInfoBeforeProcess, componentDependencies,
351             updatedVspComponentNames, updatedVspComponentNameIdInfo);
352       }
353     }
354   }
355
356   private void updateComponentDependency(Map<String, String> vspComponentIdNameInfoBeforeProcess,
357                                          Collection<ComponentDependencyModelEntity>
358                                              componentDependencies,
359                                          Set<String> updatedVspComponentNames,
360                                          Map<String, String> updatedVspComponentNameIdInfo) {
361     for (ComponentDependencyModelEntity componentDependency : componentDependencies) {
362       String sourceComponentName = vspComponentIdNameInfoBeforeProcess.get(componentDependency
363           .getSourceComponentId());
364       String targetComponentName = vspComponentIdNameInfoBeforeProcess.get(componentDependency
365           .getTargetComponentId());
366       if (updatedVspComponentNames.contains(sourceComponentName)
367           && (updatedVspComponentNames.contains(targetComponentName))) {
368         String newSourceComponentId = updatedVspComponentNameIdInfo.get(sourceComponentName);
369         componentDependency.setSourceComponentId(newSourceComponentId);
370         String newTargetComponentId = updatedVspComponentNameIdInfo.get(targetComponentName);
371         componentDependency.setTargetComponentId(newTargetComponentId);
372         componentDependencyModelDao.update(componentDependency);
373       } else {
374         componentDependencyModelDao.delete(componentDependency);
375       }
376     }
377   }
378
379   public Map<String, String> getVspComponentIdNameInfo(String vspId, Version version) {
380     Collection<ComponentEntity> updatedVspComponents =
381         componentDao.list(new ComponentEntity(vspId, version, null));
382     Map<String, String> vspComponentIdNameMap = new HashMap<>();
383     if (CollectionUtils.isNotEmpty(updatedVspComponents)) {
384       vspComponentIdNameMap = updatedVspComponents.stream()
385           .filter(componentEntity -> componentEntity.getComponentCompositionData() != null)
386           .collect(Collectors.toMap(ComponentEntity::getId,
387               componentEntity -> componentEntity.getComponentCompositionData().getName()));
388
389     }
390     return vspComponentIdNameMap;
391   }
392
393   private Map<String, String> getVspComponentNameIdInfo(String vspId,
394                                                         Version version) {
395     Collection<ComponentEntity> updatedVspComponents =
396         componentDao.list(new ComponentEntity(vspId, version, null));
397     Map<String, String> vspComponentNameIdMap = new HashMap<>();
398     if (CollectionUtils.isNotEmpty(updatedVspComponents)) {
399       vspComponentNameIdMap = updatedVspComponents.stream()
400           .filter(componentEntity -> componentEntity.getComponentCompositionData() != null)
401           .collect(Collectors
402               .toMap(componentEntity -> componentEntity.getComponentCompositionData().getName(),
403                   ComponentEntity::getId));
404     }
405     return vspComponentNameIdMap;
406   }
407
408 }