Create new VSP, onboard from TOSCA file - UI
[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.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;
41
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Optional;
46
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();
53
54   public FileDataStructureHealer() {
55   }
56
57   @Override
58   public Optional<FilesDataStructure> heal(Map<String, Object> healingParams) throws Exception {
59
60
61     mdcDataDebugMessage.debugEntryMessage(null);
62
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);
67
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);
73     }
74
75     Optional<FilesDataStructure> candidateFileDataStructure =
76         candidateService.getOrchestrationTemplateCandidateFileDataStructure(vspId, version);
77
78     if (!candidateFileDataStructure.isPresent()) {
79       healingResult = healFilesDataStructure(vspId, version, user, uploadData);
80     }
81
82     mdcDataDebugMessage.debugExitMessage(null);
83     return healingResult;
84   }
85
86   private Optional<FilesDataStructure> healFilesDataStructure(String vspId, Version version,
87                                                               String user,
88                                                               UploadDataEntity uploadData)
89       throws Exception {
90
91
92     mdcDataDebugMessage.debugEntryMessage(null);
93
94     Optional<FilesDataStructure> healingResult;
95     byte[] byteContentData = uploadData.getContentData().array();
96     FileContentHandler fileContentHandler;
97     try{
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);
104
105       healingResult = getFileDataStructureFromJson(candidateDataEntity.getFilesDataStructure());
106     }catch (Exception e){
107       return Optional.empty();
108     }
109
110     mdcDataDebugMessage.debugExitMessage(null);
111     return healingResult;
112   }
113
114   private Optional<FilesDataStructure> getFileDataStructureFromJson(String fileDataStructureJson) {
115     return Optional.of(JsonUtil.json2Object(fileDataStructureJson, FilesDataStructure.class));
116   }
117 }