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