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