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.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;
41 import java.util.HashMap;
42 import java.util.List;
44 import java.util.Optional;
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();
53 public FileDataStructureHealer() {
57 public Optional<FilesDataStructure> heal(Map<String, Object> healingParams) throws Exception {
60 mdcDataDebugMessage.debugEntryMessage(null);
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);
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);
74 Optional<FilesDataStructure> candidateFileDataStructure =
75 candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
77 if (!candidateFileDataStructure.isPresent()) {
78 healingResult = healFilesDataStructure(vspId, version, user, uploadData);
81 mdcDataDebugMessage.debugExitMessage(null);
85 private Optional<FilesDataStructure> healFilesDataStructure(String vspId, Version version,
87 UploadDataEntity uploadData)
91 mdcDataDebugMessage.debugEntryMessage(null);
93 Optional<FilesDataStructure> healingResult;
94 byte[] byteContentData = uploadData.getContentData().array();
95 FileContentHandler fileContentHandler;
97 fileContentHandler = CommonUtil.validateAndUploadFileContent(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);
104 healingResult = getFileDataStructureFromJson(candidateDataEntity.getFilesDataStructure());
105 }catch (Exception e){
106 return Optional.empty();
109 mdcDataDebugMessage.debugExitMessage(null);
110 return healingResult;
113 private Optional<FilesDataStructure> getFileDataStructureFromJson(String fileDataStructureJson) {
114 return Optional.of(JsonUtil.json2Object(fileDataStructureJson, FilesDataStructure.class));