fae8f8b69762a837f1a88f9b8076b252607fcdcc
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 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     uploadData.setFilesDataStructure(candidateData.getFilesDataStructure());
321     orchestrationTemplateDataDao.update(vspDetails.getId(), vspDetails.getVersion(), uploadData);
322
323     VspMergeDaoFactory.getInstance().createInterface()
324         .updateVspModelId(vspDetails.getId(), vspDetails.getVersion());
325   }
326
327   public void saveServiceModel(String vspId, Version version,
328                                ToscaServiceModel serviceModelToExtract,
329                                ToscaServiceModel serviceModelToStore) {
330     if (serviceModelToExtract != null) {
331       serviceModelDao.storeServiceModel(vspId, version, serviceModelToStore);
332       //Extracting the compostion data from the output service model of the first phase of
333       // translation
334       compositionEntityDataManager.saveCompositionData(vspId, version,
335           compositionDataExtractor.extractServiceCompositionData(serviceModelToExtract));
336     }
337   }
338
339   public static HeatStructureTree createHeatTree(FileContentHandler fileContentMap,
340                                                  Map<String, List<ErrorMessage>> validationErrors) {
341     HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileContentMap);
342     heatTreeManager.createTree();
343     heatTreeManager.addErrors(validationErrors);
344     return heatTreeManager.getTree();
345   }
346
347   public void updateVspComponentDependencies(String vspId, Version version,
348                                              Map<String, String>
349                                                  vspComponentIdNameInfoBeforeProcess,
350                                              Collection<ComponentDependencyModelEntity>
351                                                  componentDependenciesBeforeDelete) {
352     Map<String, String> updatedVspComponentNameIdInfo = getVspComponentNameIdInfo(vspId, version);
353     if (MapUtils.isNotEmpty(updatedVspComponentNameIdInfo)) {
354       Set<String> updatedVspComponentNames = updatedVspComponentNameIdInfo.keySet();
355       if (CollectionUtils.isNotEmpty(componentDependenciesBeforeDelete)) {
356         restoreComponentDependencies(vspId, version, vspComponentIdNameInfoBeforeProcess,
357             componentDependenciesBeforeDelete, updatedVspComponentNames,
358             updatedVspComponentNameIdInfo);
359       }
360     }
361   }
362
363   private void restoreComponentDependencies(String vspId, Version version,
364                                             Map<String, String> vspComponentIdNameInfoBeforeProcess,
365                                             Collection<ComponentDependencyModelEntity>
366                                              componentDependenciesBeforeDelete,
367                                             Set<String> updatedVspComponentNames,
368                                             Map<String, String> updatedVspComponentNameIdInfo) {
369     for (ComponentDependencyModelEntity componentDependency : componentDependenciesBeforeDelete) {
370       String sourceComponentName = vspComponentIdNameInfoBeforeProcess.get(componentDependency
371           .getSourceComponentId());
372       String targetComponentName = vspComponentIdNameInfoBeforeProcess.get(componentDependency
373           .getTargetComponentId());
374       if (updatedVspComponentNames.contains(sourceComponentName)
375           && (updatedVspComponentNames.contains(targetComponentName))) {
376         ComponentDependencyModelEntity restoredDependency =
377             new ComponentDependencyModelEntity(vspId, version, null);
378         String newSourceComponentId = updatedVspComponentNameIdInfo.get(sourceComponentName);
379         restoredDependency.setSourceComponentId(newSourceComponentId);
380         String newTargetComponentId = updatedVspComponentNameIdInfo.get(targetComponentName);
381         restoredDependency.setTargetComponentId(newTargetComponentId);
382         restoredDependency.setRelation(componentDependency.getRelation());
383         componentDependencyModelDao.create(restoredDependency);
384       }
385     }
386   }
387
388   public Map<String, String> getVspComponentIdNameInfo(String vspId, Version version) {
389     Collection<ComponentEntity> updatedVspComponents =
390         componentDao.list(new ComponentEntity(vspId, version, null));
391     Map<String, String> vspComponentIdNameMap = new HashMap<>();
392     if (CollectionUtils.isNotEmpty(updatedVspComponents)) {
393       vspComponentIdNameMap = updatedVspComponents.stream()
394           .filter(componentEntity -> componentEntity.getComponentCompositionData() != null)
395           .collect(Collectors.toMap(ComponentEntity::getId,
396               componentEntity -> componentEntity.getComponentCompositionData().getName()));
397
398     }
399     return vspComponentIdNameMap;
400   }
401
402   public Collection<ComponentDependencyModelEntity> getComponentDependenciesBeforeDelete(String
403                                                                          vspId, Version version) {
404     return componentDependencyModelDao.list(new ComponentDependencyModelEntity(vspId,
405             version, null));
406   }
407
408   private Map<String, String> getVspComponentNameIdInfo(String vspId,
409                                                         Version version) {
410     Collection<ComponentEntity> updatedVspComponents =
411         componentDao.list(new ComponentEntity(vspId, version, null));
412     Map<String, String> vspComponentNameIdMap = new HashMap<>();
413     if (CollectionUtils.isNotEmpty(updatedVspComponents)) {
414       vspComponentNameIdMap = updatedVspComponents.stream()
415           .filter(componentEntity -> componentEntity.getComponentCompositionData() != null)
416           .collect(Collectors
417               .toMap(componentEntity -> componentEntity.getComponentCompositionData().getName(),
418                   ComponentEntity::getId));
419     }
420     return vspComponentNameIdMap;
421   }
422
423 }