14fe885d53359ed2a6bed9c9816deb898bb15df2
[sdc.git] / openecomp-be / lib / openecomp-healing-lib / openecomp-sdc-healing-impl / src / main / java / org / openecomp / sdc / healing / healers / FileDataStructureHealer.java
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.healing.healers;
22
23 import org.openecomp.core.utilities.file.FileContentHandler;
24 import org.openecomp.core.utilities.json.JsonUtil;
25 import org.openecomp.sdc.common.utils.CommonUtil;
26 import org.openecomp.sdc.common.utils.SdcCommon;
27 import org.openecomp.sdc.datatypes.error.ErrorMessage;
28 import org.openecomp.sdc.healing.interfaces.Healer;
29 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
30 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDaoFactory;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadDataEntity;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
35 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
36 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
37 import org.openecomp.sdc.vendorsoftwareproduct.services.utils.CandidateEntityBuilder;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
39 import org.openecomp.sdc.versioning.dao.types.Version;
40
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Optional;
45
46 public class FileDataStructureHealer implements Healer {
47   private static final OrchestrationTemplateDao orchestrationTemplateDataDao =
48       OrchestrationTemplateDaoFactory.getInstance().createInterface();
49   private static CandidateService candidateService =
50       CandidateServiceFactory.getInstance().createInterface();
51   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
52
53   public FileDataStructureHealer() {
54   }
55
56   @Override
57   public Optional<FilesDataStructure> heal(Map<String, Object> healingParams) throws Exception {
58
59
60     mdcDataDebugMessage.debugEntryMessage(null);
61
62     Optional<FilesDataStructure> healingResult = Optional.empty();
63     String vspId = (String) healingParams.get(SdcCommon.VSP_ID);
64     Version version = (Version) healingParams.get(SdcCommon.VERSION);
65     String user = (String) healingParams.get(SdcCommon.USER);
66
67     UploadDataEntity uploadData =
68         orchestrationTemplateDataDao.getOrchestrationTemplate(vspId,version);
69     if (uploadData == null || uploadData.getContentData() == null) {
70       FilesDataStructure emptyFilesDataStructure = new FilesDataStructure();
71       return Optional.of(emptyFilesDataStructure);
72     }
73
74     Optional<FilesDataStructure> candidateFileDataStructure =
75         candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
76
77     if (!candidateFileDataStructure.isPresent()) {
78       healingResult = healFilesDataStructure(vspId, version, user, uploadData);
79     }
80
81     mdcDataDebugMessage.debugExitMessage(null);
82     return healingResult;
83   }
84
85   private Optional<FilesDataStructure> healFilesDataStructure(String vspId, Version version,
86                                                               String user,
87                                                               UploadDataEntity uploadData)
88       throws Exception {
89
90
91     mdcDataDebugMessage.debugEntryMessage(null);
92
93     Optional<FilesDataStructure> healingResult;
94     byte[] byteContentData = uploadData.getContentData().array();
95     FileContentHandler fileContentHandler;
96     try{
97       fileContentHandler = CommonUtil.loadUploadFileContent(byteContentData);
98       Map<String, List<ErrorMessage>> errors = new HashMap<>();
99       OrchestrationTemplateCandidateData candidateDataEntity =
100         new CandidateEntityBuilder(candidateService)
101             .buildCandidateEntityFromZip(new VspDetails(vspId, version), byteContentData,
102                 fileContentHandler, errors, user);
103
104       healingResult = getFileDataStructureFromJson(candidateDataEntity.getFilesDataStructure());
105     }catch (Exception e){
106       return Optional.empty();
107     }
108
109     mdcDataDebugMessage.debugExitMessage(null);
110     return healingResult;
111   }
112
113   private Optional<FilesDataStructure> getFileDataStructureFromJson(String fileDataStructureJson) {
114     return Optional.of(JsonUtil.json2Object(fileDataStructureJson, FilesDataStructure.class));
115   }
116 }