96b91091ff0e05a6affd72fec8dea581ee446c3a
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.healing.healers;
18
19 import org.openecomp.core.utilities.file.FileContentHandler;
20 import org.openecomp.core.utilities.json.JsonUtil;
21 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
22 import org.openecomp.sdc.common.utils.CommonUtil;
23 import org.openecomp.sdc.healing.interfaces.Healer;
24 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
25 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
26 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDaoFactory;
27 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
28 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
29 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
30 import org.openecomp.sdc.vendorsoftwareproduct.services.utils.CandidateEntityBuilder;
31 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
32 import org.openecomp.sdc.versioning.dao.types.Version;
33
34 import java.util.HashMap;
35 import java.util.Optional;
36
37 public class FileDataStructureHealer implements Healer {
38   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
39
40   @Override
41   public Optional<FilesDataStructure> heal(String vspId,
42                                            Version version) throws Exception {
43     mdcDataDebugMessage.debugEntryMessage(null);
44
45     OrchestrationTemplateCandidateDao candidateDao =
46         OrchestrationTemplateCandidateDaoFactory.getInstance().createInterface();
47
48     OrchestrationTemplateCandidateData candidateData = candidateDao.get(vspId, version);
49
50     if (candidateData == null || candidateData.getContentData() == null ||
51         candidateData.getFilesDataStructure() != null) {
52       return Optional.of(new FilesDataStructure());
53     }
54
55     Optional<FilesDataStructure> filesDataStructure =
56         healFilesDataStructure(vspId, version, candidateData);
57
58     filesDataStructure
59         .ifPresent(structure -> candidateDao.updateStructure(vspId, version, structure));
60
61     mdcDataDebugMessage.debugExitMessage(null);
62     return filesDataStructure;
63   }
64
65   private Optional<FilesDataStructure> healFilesDataStructure(
66       String vspId, Version version, OrchestrationTemplateCandidateData candidateData)
67       throws Exception {
68     mdcDataDebugMessage.debugEntryMessage(null);
69
70     Optional<FilesDataStructure> healingResult;
71     byte[] byteContentData = candidateData.getContentData().array();
72     FileContentHandler fileContentHandler;
73     try {
74       fileContentHandler =
75           CommonUtil.validateAndUploadFileContent(OnboardingTypesEnum.ZIP, byteContentData);
76
77       String filesDataStructure =
78           new CandidateEntityBuilder(CandidateServiceFactory.getInstance().createInterface())
79               .buildCandidateEntityFromZip(new VspDetails(vspId, version), byteContentData,
80                   fileContentHandler, new HashMap<>()).getFilesDataStructure();
81
82       healingResult =
83           Optional.of(JsonUtil.json2Object(filesDataStructure, FilesDataStructure.class));
84     } catch (Exception e) {
85       healingResult = Optional.empty();
86     }
87
88     mdcDataDebugMessage.debugExitMessage(null);
89     return healingResult;
90   }
91 }