2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.healing.healers;
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;
44 import java.util.HashMap;
45 import java.util.List;
47 import java.util.Optional;
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();
56 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
58 public FileDataStructureHealer() {
62 public Optional<FilesDataStructure> heal(Map<String, Object> healingParams) throws Exception {
65 mdcDataDebugMessage.debugEntryMessage(null);
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);
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);
79 Optional<FilesDataStructure> candidateFileDataStructure =
80 candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
82 if (!candidateFileDataStructure.isPresent()) {
83 healingResult = healFilesDataStructure(vspId, version, user, uploadData);
86 mdcDataDebugMessage.debugExitMessage(null);
90 private Optional<FilesDataStructure> healFilesDataStructure(String vspId, Version version,
92 UploadDataEntity uploadData)
96 mdcDataDebugMessage.debugEntryMessage(null);
98 Optional<FilesDataStructure> healingResult;
99 byte[] byteContentData = uploadData.getContentData().array();
100 FileContentHandler fileContentHandler;
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);
109 healingResult = getFileDataStructureFromJson(candidateDataEntity.getFilesDataStructure());
110 }catch (Exception e){
112 return Optional.empty();
115 mdcDataDebugMessage.debugExitMessage(null);
116 return healingResult;
119 private Optional<FilesDataStructure> getFileDataStructureFromJson(String fileDataStructureJson) {
120 return Optional.of(JsonUtil.json2Object(fileDataStructureJson, FilesDataStructure.class));