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