Add collaboration feature
[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.healing.interfaces.Healer;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDaoFactory;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
35 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
36 import org.openecomp.sdc.vendorsoftwareproduct.services.utils.CandidateEntityBuilder;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
38 import org.openecomp.sdc.versioning.dao.types.Version;
39
40 import java.util.HashMap;
41 import java.util.Optional;
42
43 public class FileDataStructureHealer implements Healer {
44   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
45
46   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
47
48   public FileDataStructureHealer() {
49   }
50
51   @Override
52   public Optional<FilesDataStructure> heal(String vspId,
53                                            Version version) throws Exception {
54     mdcDataDebugMessage.debugEntryMessage(null);
55
56     OrchestrationTemplateCandidateDao candidateDao =
57         OrchestrationTemplateCandidateDaoFactory.getInstance().createInterface();
58
59     OrchestrationTemplateCandidateData candidateData = candidateDao.get(vspId, version);
60
61     if (candidateData == null || candidateData.getContentData() == null ||
62         candidateData.getFilesDataStructure() != null) {
63       return Optional.of(new FilesDataStructure());
64     }
65
66     Optional<FilesDataStructure> filesDataStructure =
67         healFilesDataStructure(vspId, version, candidateData);
68
69     filesDataStructure
70         .ifPresent(structure -> candidateDao.updateStructure(vspId, version, structure));
71
72     mdcDataDebugMessage.debugExitMessage(null);
73     return filesDataStructure;
74   }
75
76   private Optional<FilesDataStructure> healFilesDataStructure(
77       String vspId, Version version, OrchestrationTemplateCandidateData candidateData)
78       throws Exception {
79     mdcDataDebugMessage.debugEntryMessage(null);
80
81     Optional<FilesDataStructure> healingResult;
82     byte[] byteContentData = candidateData.getContentData().array();
83     FileContentHandler fileContentHandler;
84     try {
85       fileContentHandler =
86           CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, byteContentData);
87
88       String filesDataStructure =
89           new CandidateEntityBuilder(CandidateServiceFactory.getInstance().createInterface())
90               .buildCandidateEntityFromZip(new VspDetails(vspId, version), byteContentData,
91                   fileContentHandler, new HashMap<>()).getFilesDataStructure();
92
93       healingResult =
94           Optional.of(JsonUtil.json2Object(filesDataStructure, FilesDataStructure.class));
95     } catch (Exception e) {
96       log.debug("", e);
97       healingResult = Optional.empty();
98     }
99
100     mdcDataDebugMessage.debugExitMessage(null);
101     return healingResult;
102   }
103 }