07e84803aa2254def511777ce4a4b00c60c7ec7d
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.apache.commons.collections4.MapUtils;
25 import org.openecomp.core.model.dao.ServiceModelDao;
26 import org.openecomp.core.model.types.ServiceElement;
27 import org.openecomp.core.translator.datatypes.TranslatorOutput;
28 import org.openecomp.core.util.UniqueValueUtil;
29 import org.openecomp.core.utilities.file.FileContentHandler;
30 import org.openecomp.core.utilities.file.FileUtils;
31 import org.openecomp.core.utilities.json.JsonUtil;
32 import org.openecomp.core.validation.util.MessageContainerUtil;
33 import org.openecomp.sdc.activityLog.ActivityLogManager;
34 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
35 import org.openecomp.sdc.common.errors.CoreException;
36 import org.openecomp.sdc.common.errors.Messages;
37 import org.openecomp.sdc.common.utils.CommonUtil;
38 import org.openecomp.sdc.common.utils.SdcCommon;
39 import org.openecomp.sdc.datatypes.error.ErrorLevel;
40 import org.openecomp.sdc.datatypes.error.ErrorMessage;
41 import org.openecomp.sdc.healing.api.HealingManager;
42 import org.openecomp.sdc.healing.types.HealCode;
43 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
44 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
45 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
46 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
47 import org.openecomp.sdc.logging.api.Logger;
48 import org.openecomp.sdc.logging.api.LoggerFactory;
49 import org.openecomp.sdc.logging.api.annotations.Metrics;
50 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
51 import org.openecomp.sdc.logging.messages.AuditMessages;
52 import org.openecomp.sdc.logging.types.LoggerServiceName;
53 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
54 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
55 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
56 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
57 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
58 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentArtifactDao;
59 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
60 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
61 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
62 import org.openecomp.sdc.vendorsoftwareproduct.dao.ProcessDao;
63 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductDao;
64 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
65 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
66 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
67 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
68 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
69 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
70 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadData;
71 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
72 import org.openecomp.sdc.vendorsoftwareproduct.errors.OrchestrationTemplateNotFoundErrorBuilder;
73 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
74 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
75 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
76 import org.openecomp.sdc.vendorsoftwareproduct.services.utils.CandidateEntityBuilder;
77 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
78 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
79 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
80 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
81 import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils;
82 import org.openecomp.sdc.versioning.dao.types.Version;
83 import org.openecomp.sdcrests.activitylog.types.ActivityType;
84
85 import java.io.ByteArrayInputStream;
86 import java.io.IOException;
87 import java.io.InputStream;
88 import java.nio.ByteBuffer;
89 import java.util.Collection;
90 import java.util.HashMap;
91 import java.util.List;
92 import java.util.Map;
93 import java.util.Optional;
94
95 import static org.openecomp.sdc.logging.messages.AuditMessages.HEAT_VALIDATION_ERROR;
96 import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.GENERAL_COMPONENT_ID;
97 import static org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductConstants.UniqueValues.PROCESS_NAME;
98
99 public class OrchestrationTemplateCandidateManagerImpl
100     implements OrchestrationTemplateCandidateManager {
101   private static final Logger logger =
102       LoggerFactory.getLogger(OrchestrationTemplateCandidateManagerImpl.class);
103   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
104   private VendorSoftwareProductDao vendorSoftwareProductDao;
105   private VendorSoftwareProductInfoDao vspInfoDao;
106   private OrchestrationTemplateDao orchestrationTemplateDataDao;
107   private CandidateService candidateService;
108   private HealingManager healingManager;
109   private CompositionDataExtractor compositionDataExtractor;
110   private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao;
111   private CompositionEntityDataManager compositionEntityDataManager;
112   private NicDao nicDao;
113   private ComponentDao componentDao;
114   private ComponentArtifactDao componentArtifactDao;
115   private ActivityLogManager activityLogManager;
116   private ProcessDao processDao;
117
118   /**
119    * Instantiates a new Orchestration template candidate manager.
120    *
121    * @param vendorSoftwareProductDao     the vendor software product dao
122    * @param vspInfoDao                   the vsp info dao
123    * @param orchestrationTemplateDataDao the orchestration template data dao
124    * @param candidateService             the candidate service
125    * @param healingManager               the healing manager
126    * @param compositionDataExtractor     the composition data extractor
127    * @param serviceModelDao              the service model dao
128    * @param compositionEntityDataManager the composition entity data manager
129    * @param nicDao                       the nic dao
130    * @param componentDao                 the component dao
131    * @param componentArtifactDao                       the mib dao
132    * @param processDao                   the process dao
133    * @param activityLogManager           the activity log manager
134    */
135   public OrchestrationTemplateCandidateManagerImpl(
136       VendorSoftwareProductDao vendorSoftwareProductDao, VendorSoftwareProductInfoDao
137       vspInfoDao,
138       OrchestrationTemplateDao orchestrationTemplateDataDao,
139       CandidateService candidateService, HealingManager healingManager,
140       CompositionDataExtractor compositionDataExtractor,
141       ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao,
142       CompositionEntityDataManager compositionEntityDataManager,
143       NicDao nicDao,
144       ComponentDao componentDao,
145       ComponentArtifactDao componentArtifactDao,
146       ProcessDao processDao,
147       ActivityLogManager activityLogManager) {
148     this.vendorSoftwareProductDao = vendorSoftwareProductDao;
149     this.vspInfoDao = vspInfoDao;
150     this.orchestrationTemplateDataDao = orchestrationTemplateDataDao;
151     this.candidateService = candidateService;
152     this.healingManager = healingManager;
153     this.compositionDataExtractor = compositionDataExtractor;
154     this.serviceModelDao = serviceModelDao;
155     this.compositionEntityDataManager = compositionEntityDataManager;
156     this.nicDao = nicDao;
157     this.componentDao = componentDao;
158     this.componentArtifactDao = componentArtifactDao;
159     this.processDao = processDao;
160     this.activityLogManager = activityLogManager;
161   }
162
163   @Override
164   @Metrics
165   public UploadFileResponse upload(String vspId, Version version, InputStream heatFileToUpload,
166                                    String user) {
167     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
168
169     VspDetails vspDetails = getVspDetails(vspId, version);
170
171     UploadFileResponse uploadFileResponse = new UploadFileResponse();
172     if (isNotEmptyFileToUpload(heatFileToUpload, uploadFileResponse)) {
173       return uploadFileResponse;
174     }
175
176     byte[] uploadedFileData = FileUtils.toByteArray(heatFileToUpload);
177     if (isInvalidRawZipData(uploadFileResponse, uploadedFileData)) {
178       return uploadFileResponse;
179     }
180
181     Optional<FileContentHandler> optionalContentMap =
182         getZipContentMap(uploadFileResponse, uploadedFileData);
183     if (!optionalContentMap.isPresent()) {
184       logger.error(Messages.ZIP_CONTENT_MAP.getErrorMessage());
185       uploadFileResponse
186           .addStructureError(
187               SdcCommon.UPLOAD_FILE,
188               new ErrorMessage(ErrorLevel.ERROR, Messages.ZIP_CONTENT_MAP.getErrorMessage()));
189
190       mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
191       return uploadFileResponse;
192     }
193
194     if (!MapUtils.isEmpty(uploadFileResponse.getErrors())) {
195
196       mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
197       return uploadFileResponse;
198     }
199     try {
200       OrchestrationTemplateCandidateData candidateData =
201           new CandidateEntityBuilder(candidateService)
202               .buildCandidateEntityFromZip(vspDetails, uploadedFileData, optionalContentMap.get(),
203                   uploadFileResponse.getErrors(), user);
204       candidateService.updateCandidateUploadData(candidateData, vspDetails.getId());
205     } catch (Exception exception) {
206       logger.error(Messages.ZIP_CONTENT_MAP.getErrorMessage());
207       uploadFileResponse
208           .addStructureError(
209               SdcCommon.UPLOAD_FILE,
210               new ErrorMessage(ErrorLevel.ERROR, exception.getMessage()));
211
212       mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
213       return uploadFileResponse;
214     }
215
216     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
217     return uploadFileResponse;
218   }
219
220   @Override
221   public OrchestrationTemplateActionResponse process(String vspId,
222                                                      Version version, String user) {
223     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
224
225     Optional<OrchestrationTemplateCandidateData> candidate =
226         fetchCandidateDataEntity(vspId, version);
227     if (!candidate.isPresent()) {
228       throw new CoreException(new OrchestrationTemplateNotFoundErrorBuilder(vspId).build());
229     }
230
231     logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_STARTED + vspId);
232     OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
233     UploadFileResponse uploadFileResponse = new UploadFileResponse();
234     OrchestrationTemplateCandidateData candidateDataEntity = candidate.get();
235     Optional<FileContentHandler> fileContent =
236         getZipContentMap(uploadFileResponse, candidateDataEntity.getContentData().array());
237     if (!fileContent.isPresent()) {
238       response.addStructureErrors(uploadFileResponse.getErrors());
239       mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
240       response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList,vspId,
241           HEAT_VALIDATION_ERROR));
242       return response;
243     }
244
245     Map<String, List<ErrorMessage>> uploadErrors = uploadFileResponse.getErrors();
246     FileContentHandler fileContentMap = fileContent.get();
247     FilesDataStructure structure =
248         JsonUtil.json2Object(candidateDataEntity.getFilesDataStructure(), FilesDataStructure.class);
249
250     if (CollectionUtils.isNotEmpty(structure.getUnassigned())) {
251       response.addErrorMessageToMap(SdcCommon.UPLOAD_FILE,
252           Messages.FOUND_UNASSIGNED_FILES.getErrorMessage(), ErrorLevel.ERROR);
253
254       mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
255       response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList,vspId,
256           HEAT_VALIDATION_ERROR));
257       return response;
258     }
259
260     VspDetails vspDetails =
261         getVspDetails(vspId, version);
262
263     String manifest = candidateService.createManifest(vspDetails, structure);
264     fileContentMap.addFile(SdcCommon.MANIFEST_NAME, manifest.getBytes());
265
266     Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
267         .fetchZipFileByteArrayInputStream(vspId, candidateDataEntity, manifest, uploadErrors);
268     if (!zipByteArrayInputStream.isPresent()) {
269       response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList,vspId,
270           HEAT_VALIDATION_ERROR));
271       return response;
272     }
273
274     HeatStructureTree tree = createAndValidateHeatTree(response, fileContentMap);
275
276     Map<String, String> componentsQuestionnaire = new HashMap<>();
277     Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
278     Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
279     Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
280     Map<String, ProcessEntity> processArtifact = new HashMap<>();
281
282     backupComponentsQuestionnaireBeforeDelete(vspId, version, componentsQuestionnaire,
283         componentNicsQuestionnaire, componentMibList, processes, processArtifact);
284
285     deleteUploadDataAndContent(vspId, version);
286     saveHotData(vspId, version, zipByteArrayInputStream.get(), fileContentMap, tree);
287
288     response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList,vspId,
289         HEAT_VALIDATION_ERROR));
290     if ( MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, response.getErrors
291         ()))) {
292       logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_COMPLETED + vspId);
293     }
294
295     logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_TRANSLATION_STARTED + vspId);
296
297     TranslatorOutput translatorOutput =
298         HeatToToscaUtil.loadAndTranslateTemplateData(fileContentMap);
299
300     ToscaServiceModel toscaServiceModel = translatorOutput.getToscaServiceModel();
301     if (toscaServiceModel != null) {
302       serviceModelDao.storeServiceModel(vspId, version, toscaServiceModel);
303       //Extracting the compostion data from the output service model of the first phase of
304       // translation
305       compositionEntityDataManager.saveCompositionData(vspId, version,
306           compositionDataExtractor.extractServiceCompositionData(translatorOutput
307               .getNonUnifiedToscaServiceModel()));
308       retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire,
309           componentNicsQuestionnaire, componentMibList, processes, processArtifact);
310
311       logger.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_TRANSLATION_COMPLETED + vspId);
312     }
313
314     uploadFileResponse.addStructureErrors(uploadErrors);
315
316     ActivityLogEntity activityLogEntity =
317         new ActivityLogEntity(vspId, String.valueOf(version.getMajor() + 1),
318             ActivityType.UPLOAD_HEAT.toString(), user, true, "", "");
319     activityLogManager.addActionLog(activityLogEntity, user);
320
321     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
322     return response;
323   }
324
325   @Override
326   public Optional<FilesDataStructure> getFilesDataStructure(
327       String vspId, Version version, String user) {
328     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
329
330     Optional<FilesDataStructure> candidateFileDataStructure =
331         candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
332     if (candidateFileDataStructure.isPresent()) {
333       return candidateFileDataStructure;
334     } else {
335       Map<String, Object> healingParams = getHealingParamsAsMap(vspId, version, user);
336
337       mdcDataDebugMessage
338           .debugExitMessage("VSP id", vspId);
339       return (Optional<FilesDataStructure>) healingManager
340           .heal(HealCode.FILE_DATA_STRUCTURE_HEALER, healingParams);
341     }
342   }
343
344   @Override
345
346   public ValidationResponse updateFilesDataStructure(String vspId,
347                                                      Version version, String user,
348                                                      FilesDataStructure fileDataStructure) {
349     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
350
351     ValidationResponse response = new ValidationResponse();
352     Optional<List<ErrorMessage>> validateErrors =
353         candidateService.validateFileDataStructure(fileDataStructure);
354     if (validateErrors.isPresent()) {
355       List<ErrorMessage> errorMessages = validateErrors.get();
356       if (CollectionUtils.isNotEmpty(errorMessages)) {
357         Map<String, List<ErrorMessage>> errorsMap = new HashMap<>();
358         errorsMap.put(SdcCommon.UPLOAD_FILE, errorMessages);
359         response.setUploadDataErrors(errorsMap, LoggerServiceName.Update_Manifest,
360             LoggerTragetServiceName.VALIDATE_FILE_DATA_STRUCTURE);
361
362         mdcDataDebugMessage
363             .debugExitMessage("VSP id", vspId);
364         return response;
365       }
366     }
367     candidateService.updateOrchestrationTemplateCandidateFileDataStructure(vspId, version,
368         fileDataStructure);
369
370     mdcDataDebugMessage
371         .debugExitMessage("VSP id", vspId);
372     return response;
373   }
374
375   @Override
376
377   public Optional<byte[]> get(String vspId, Version version, String user)
378       throws IOException {
379     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
380
381     VspDetails vspDetails =
382         getVspDetails(vspId, version);
383
384     Optional<OrchestrationTemplateCandidateData> candidateDataEntity =
385         fetchCandidateDataEntity(vspId, version);
386
387     if (!candidateDataEntity.isPresent()) {
388       ErrorMessage errorMessage = new ErrorMessage(ErrorLevel.ERROR,
389           Messages.NO_ZIP_FILE_WAS_UPLOADED_OR_ZIP_NOT_EXIST.getErrorMessage());
390       logger.error(errorMessage.getMessage());
391
392       mdcDataDebugMessage
393           .debugExitMessage("VSP id", vspId);
394       return Optional.empty();
395     }
396
397     FilesDataStructure structure = JsonUtil
398         .json2Object(candidateDataEntity.get().getFilesDataStructure(), FilesDataStructure.class);
399     String manifest = candidateService.createManifest(vspDetails, structure);
400
401     mdcDataDebugMessage
402         .debugExitMessage("VSP id", vspId);
403     return Optional.ofNullable(candidateService
404         .replaceManifestInZip(candidateDataEntity.get().getContentData(), manifest, vspId));
405   }
406
407   private Optional<OrchestrationTemplateCandidateData> fetchCandidateDataEntity(
408       String vspId, Version version) {
409     return Optional
410         .ofNullable(candidateService.getOrchestrationTemplateCandidate(vspId, version));
411   }
412
413   private void retainComponentQuestionnaireData(String vspId, Version activeVersion,
414                                                 Map<String, String> componentsQustanniare,
415                                                 Map<String, Map<String, String>>
416                                                     componentNicsQustanniare,
417                                                 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList,
418                                                 Map<String, Collection<ProcessEntity>> processes,
419                                                 Map<String, ProcessEntity> processArtifact) {
420     //VSP processes
421     restoreProcess(vspId, activeVersion, GENERAL_COMPONENT_ID, GENERAL_COMPONENT_ID, processes,
422         processArtifact);
423     Collection<ComponentEntity>
424         components = vendorSoftwareProductDao.listComponents(vspId, activeVersion);
425     components.forEach(componentEntity -> {
426       String componentName = componentEntity.getComponentCompositionData().getName();
427       if (componentsQustanniare.containsKey(componentName)) {
428         componentDao.updateQuestionnaireData(vspId, activeVersion,
429             componentEntity.getId(),
430             componentsQustanniare.get(componentEntity.getComponentCompositionData()
431                 .getName()));
432         if (componentNicsQustanniare.containsKey(componentName)) {
433           Map<String, String> nicsQustanniare = componentNicsQustanniare.get(componentName);
434           Collection<NicEntity>
435               nics =
436               nicDao.list(new NicEntity(vspId, activeVersion, componentEntity.getId(), null));
437           nics.forEach(nicEntity -> {
438             if (nicsQustanniare.containsKey(nicEntity.getNicCompositionData().getName())) {
439               nicDao.updateQuestionnaireData(vspId, activeVersion,
440                   componentEntity.getId(), nicEntity.getId(),
441                   nicsQustanniare.get(nicEntity.getNicCompositionData().getName()));
442             }
443           });
444         }
445         //MIB //todo add for VES_EVENTS
446         if (componentMibList.containsKey(componentName)) {
447           Collection<ComponentMonitoringUploadEntity> mibList =
448               componentMibList.get(componentName);
449           mibList.forEach(mib -> {
450             mib.setComponentId(componentEntity.getId());
451             componentArtifactDao.create(mib);
452           });
453         }
454         //VFC processes
455         restoreProcess(vspId, activeVersion, componentEntity.getId(), componentName, processes,
456             processArtifact);
457       }
458     });
459   }
460
461   private void backupComponentsQuestionnaireBeforeDelete(String vspId, Version activeVersion,
462                                                          Map<String, String> componentsQustanniare,
463                                                          Map<String, Map<String, String>>
464                                                            componentNicsQustanniare,
465                                                          Map<String, Collection<ComponentMonitoringUploadEntity>>
466                                                            componentMibList,
467                                                          Map<String, Collection<ProcessEntity>>
468                                                              componentProcesses,
469                                                          Map<String, ProcessEntity> processArtifact) {
470     //backup VSP processes
471     backupProcess(vspId, activeVersion, GENERAL_COMPONENT_ID, GENERAL_COMPONENT_ID,
472         componentProcesses, processArtifact);
473     Collection<ComponentEntity> componentsCompositionAndQuestionnaire = vendorSoftwareProductDao
474         .listComponentsCompositionAndQuestionnaire(vspId,
475             activeVersion);
476     componentsCompositionAndQuestionnaire.forEach(componentEntity -> {
477       String componentName = componentEntity.getComponentCompositionData().getName();
478       componentsQustanniare.put(componentName, componentEntity
479           .getQuestionnaireData());
480       Collection<NicEntity>
481           nics = nicDao.list(new NicEntity(vspId, activeVersion, componentEntity.getId(), null));
482       //backup mib
483       Collection<ComponentMonitoringUploadEntity> componentMib =
484           componentArtifactDao.listArtifacts(new
485               ComponentMonitoringUploadEntity(vspId, activeVersion, componentEntity.getId(),
486               null));
487       if (CollectionUtils.isNotEmpty(componentMib)) {
488         componentMibList.put(componentName,componentMib);
489       }
490
491       //backup component processes
492       backupProcess(vspId, activeVersion, componentEntity.getId(), componentName,
493           componentProcesses, processArtifact);
494       if (CollectionUtils.isNotEmpty(nics)) {
495         Map<String, String> nicsQustanniare = new HashMap<>();
496         nics.forEach(nicEntity -> {
497           NicEntity nic = nicDao.get(new NicEntity(vspId, activeVersion, componentEntity.getId(),
498               nicEntity.getId()));
499           NicEntity nicQuestionnaire = nicDao.getQuestionnaireData(vspId, activeVersion,
500               componentEntity.getId(), nicEntity.getId());
501
502           nicsQustanniare
503               .put(nicEntity.getNicCompositionData().getName(),
504                   nicQuestionnaire.getQuestionnaireData());
505         });
506         componentNicsQustanniare.put(componentName, nicsQustanniare);
507       }
508     });
509   }
510
511   private void backupProcess(String vspId, Version activeVersion, String componentId,
512                              String componentName, Map<String,
513       Collection<ProcessEntity>> processes,
514                              Map<String, ProcessEntity> processArtifact) {
515     Collection<ProcessEntity> processList = vendorSoftwareProductDao.listProcesses(vspId,
516         activeVersion, componentId);
517     if (!processList.isEmpty()) {
518       processes.put(componentName, processList);
519       processList.forEach(process -> {
520         //ProcessArtifactEntity artifact = vendorSoftwareProductDao.getProcessArtifact(vspId,
521         //    activeVersion, componentId, process.getId());
522         ProcessEntity artifact =
523             processDao.get(new ProcessEntity(vspId, activeVersion, componentId, process.getId()));
524         if (artifact.getArtifact() != null) {
525           processArtifact.put(process.getId(), artifact);
526         }
527       });
528     }
529   }
530
531   private void restoreProcess(String vspId, Version activeVersion, String componentId,
532                               String componentName,
533                               Map<String, Collection<ProcessEntity>> processes,
534                               Map<String, ProcessEntity> processArtifact) {
535     if (processes.containsKey(componentName)) {
536       Collection<ProcessEntity> processList = processes.get(componentName);
537       processList.forEach(process -> {
538         //Reatin VFC process
539         if (!GENERAL_COMPONENT_ID.equals(componentId) && processArtifact.containsKey(process.getId
540             ())) {
541           ProcessEntity artifact = processArtifact.get(process.getId());
542           artifact.setComponentId(componentId);
543           UniqueValueUtil.createUniqueValue(PROCESS_NAME, vspId, activeVersion.toString(),
544               componentId, process.getName());
545           vendorSoftwareProductDao.createProcess(artifact);
546         }
547       });
548     }
549   }
550
551   private HeatStructureTree createAndValidateHeatTree(OrchestrationTemplateActionResponse response,
552                                                       FileContentHandler fileContentMap) {
553     VendorSoftwareProductUtils.addFileNamesToUploadFileResponse(fileContentMap, response);
554     Map<String, List<ErrorMessage>> validationErrors =
555         ValidationManagerUtil.initValidationManager(fileContentMap).validate();
556     response.getErrors().putAll(validationErrors);
557
558     HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(fileContentMap);
559     heatTreeManager.createTree();
560     heatTreeManager.addErrors(validationErrors);
561     return heatTreeManager.getTree();
562   }
563
564   private void saveHotData(String vspId, Version activeVersion, InputStream uploadedFileData,
565                            FileContentHandler fileContentMap, HeatStructureTree tree) {
566     Map<String, Object> manifestAsMap =
567         (Map<String, Object>) JsonUtil.json2Object(fileContentMap.getFileContent(
568             SdcCommon.MANIFEST_NAME), Map.class);
569
570     UploadData uploadData = new UploadData();
571     uploadData.setPackageName((String) manifestAsMap.get("name"));
572     uploadData.setPackageVersion((String) manifestAsMap.get("version"));
573     uploadData.setContentData(ByteBuffer.wrap(FileUtils.toByteArray(uploadedFileData)));
574     uploadData.setValidationDataStructure(new ValidationStructureList(tree));
575     orchestrationTemplateDataDao.updateOrchestrationTemplateData(vspId, uploadData);
576   }
577
578   private void deleteUploadDataAndContent(String vspId, Version version) {
579     //fixme change this when more tables are zusammenized
580     vendorSoftwareProductDao.deleteUploadData(vspId, version);
581   }
582
583   private boolean isInvalidRawZipData(UploadFileResponse uploadFileResponse,
584                                       byte[] uploadedFileData) {
585     Optional<ErrorMessage> errorMessage;
586     errorMessage = candidateService.validateRawZipData(uploadedFileData);
587     if (errorMessage.isPresent()) {
588       uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage.get());
589       return true;
590     }
591     return false;
592   }
593
594   private boolean isNotEmptyFileToUpload(InputStream heatFileToUpload,
595                                          UploadFileResponse uploadFileResponse) {
596     Optional<ErrorMessage> errorMessage =
597         candidateService.validateNonEmptyFileToUpload(heatFileToUpload);
598     if (errorMessage.isPresent()) {
599       uploadFileResponse.addStructureError(SdcCommon.UPLOAD_FILE, errorMessage.get());
600       return true;
601     }
602     return false;
603   }
604
605   private Optional<FileContentHandler> getZipContentMap(UploadFileResponse uploadFileResponse,
606                                                         byte[] uploadedFileData) {
607     FileContentHandler contentMap = null;
608     try {
609       contentMap = CommonUtil.validateAndUploadFileContent(uploadedFileData);
610     } catch (IOException exception) {
611       uploadFileResponse.addStructureError(
612           SdcCommon.UPLOAD_FILE,
613           new ErrorMessage(ErrorLevel.ERROR, Messages.INVALID_ZIP_FILE.getErrorMessage()));
614     } catch (CoreException coreException) {
615       uploadFileResponse.addStructureError(
616           SdcCommon.UPLOAD_FILE, new ErrorMessage(ErrorLevel.ERROR, coreException.getMessage()));
617     }
618     return Optional.ofNullable(contentMap);
619   }
620
621   // todo *************************** move to reusable place! *************************
622
623   private Map<String, Object> getHealingParamsAsMap(String vspId, Version version, String user) {
624     Map<String, Object> healingParams = new HashMap<>();
625
626     healingParams.put(SdcCommon.VSP_ID, vspId);
627     healingParams.put(SdcCommon.VERSION, version);
628     healingParams.put(SdcCommon.USER, user);
629
630     return healingParams;
631   }
632
633   private VspDetails getVspDetails(String vspId, Version version) {
634     VspDetails vspDetails = vspInfoDao.get(new VspDetails(vspId, version));
635     vspDetails.setValidationData(orchestrationTemplateDataDao.getValidationData(vspId, version));
636     return vspDetails;
637   }
638
639   private void printAuditForErrors(List<ErrorMessage> errorList, String vspId, String auditType) {
640
641     errorList.forEach(errorMessage -> {
642       if (errorMessage.getLevel().equals(ErrorLevel.ERROR)) {
643         logger.audit(AuditMessages.AUDIT_MSG + String.format(auditType, errorMessage.getMessage(),
644             vspId));
645       }
646     });
647   }
648
649  }