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.context.impl.MdcDataDebugMessage;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDaoFactory;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadDataEntity;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
36 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
37 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
38 import org.openecomp.sdc.vendorsoftwareproduct.services.utils.CandidateEntityBuilder;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
40 import org.openecomp.sdc.versioning.dao.types.Version;
42 import java.util.HashMap;
43 import java.util.List;
45 import java.util.Optional;
47 public class FileDataStructureHealer implements Healer {
48 private static final OrchestrationTemplateDao orchestrationTemplateDataDao =
49 OrchestrationTemplateDaoFactory.getInstance().createInterface();
50 private static CandidateService candidateService =
51 CandidateServiceFactory.getInstance().createInterface();
52 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
54 public FileDataStructureHealer() {
58 public Optional<FilesDataStructure> heal(Map<String, Object> healingParams) throws Exception {
61 mdcDataDebugMessage.debugEntryMessage(null);
63 Optional<FilesDataStructure> healingResult = Optional.empty();
64 String vspId = (String) healingParams.get(SdcCommon.VSP_ID);
65 Version version = (Version) healingParams.get(SdcCommon.VERSION);
66 String user = (String) healingParams.get(SdcCommon.USER);
68 UploadDataEntity uploadData =
69 orchestrationTemplateDataDao.getOrchestrationTemplate(vspId,version);
70 if (uploadData == null || uploadData.getContentData() == null) {
71 FilesDataStructure emptyFilesDataStructure = new FilesDataStructure();
72 return Optional.of(emptyFilesDataStructure);
75 Optional<FilesDataStructure> candidateFileDataStructure =
76 candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
78 if (!candidateFileDataStructure.isPresent()) {
79 healingResult = healFilesDataStructure(vspId, version, user, uploadData);
82 mdcDataDebugMessage.debugExitMessage(null);
86 private Optional<FilesDataStructure> healFilesDataStructure(String vspId, Version version,
88 UploadDataEntity uploadData)
92 mdcDataDebugMessage.debugEntryMessage(null);
94 Optional<FilesDataStructure> healingResult;
95 byte[] byteContentData = uploadData.getContentData().array();
96 FileContentHandler fileContentHandler;
98 fileContentHandler = CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, byteContentData);
99 Map<String, List<ErrorMessage>> errors = new HashMap<>();
100 OrchestrationTemplateCandidateData candidateDataEntity =
101 new CandidateEntityBuilder(candidateService)
102 .buildCandidateEntityFromZip(new VspDetails(vspId, version), byteContentData,
103 fileContentHandler, errors, user);
105 healingResult = getFileDataStructureFromJson(candidateDataEntity.getFilesDataStructure());
106 }catch (Exception e){
107 return Optional.empty();
110 mdcDataDebugMessage.debugExitMessage(null);
111 return healingResult;
114 private Optional<FilesDataStructure> getFileDataStructureFromJson(String fileDataStructureJson) {
115 return Optional.of(JsonUtil.json2Object(fileDataStructureJson, FilesDataStructure.class));