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