Add collaboration feature
[sdc.git] / openecomp-be / lib / openecomp-sdc-vendor-software-product-lib / openecomp-sdc-vendor-software-product-api / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / services / utils / CandidateEntityBuilder.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.vendorsoftwareproduct.services.utils;
22
23 import org.openecomp.core.utilities.file.FileContentHandler;
24 import org.openecomp.core.utilities.json.JsonUtil;
25 import org.openecomp.sdc.common.errors.Messages;
26 import org.openecomp.sdc.common.utils.SdcCommon;
27 import org.openecomp.sdc.datatypes.error.ErrorMessage;
28 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
29 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
30 import org.openecomp.sdc.heat.services.tree.HeatTreeManager;
31 import org.openecomp.sdc.heat.services.tree.HeatTreeManagerUtil;
32 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
35 import org.openecomp.sdc.vendorsoftwareproduct.services.HeatFileAnalyzer;
36 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
37 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.HeatFileAnalyzerRowDataImpl;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.CandidateDataEntityTo;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.AnalyzedZipHeatFiles;
40
41 import java.io.IOException;
42 import java.io.InputStream;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Objects;
46 import java.util.Optional;
47
48 public class CandidateEntityBuilder {
49
50   private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
51
52   private final CandidateService candidateService;
53
54   public CandidateEntityBuilder(CandidateService candidateService) {
55     this.candidateService = candidateService;
56   }
57
58   public OrchestrationTemplateCandidateData buildCandidateEntityFromZip(
59       VspDetails vspDetails, byte[] uploadedFileData, FileContentHandler contentMap,
60       Map<String, List<ErrorMessage>> uploadErrors) throws Exception {
61     //mdcDataDebugMessage.debugEntryMessage("VSP Id", vspDetails.getId());
62
63     try (InputStream zipFileManifest = contentMap.getFileContent(SdcCommon.MANIFEST_NAME)) {
64       HeatFileAnalyzer heatFileAnalyzer = new HeatFileAnalyzerRowDataImpl();
65       AnalyzedZipHeatFiles analyzedZipHeatFiles =
66           heatFileAnalyzer.analyzeFilesNotEligibleForModulesFromFileAnalyzer(contentMap.getFiles());
67       HeatStructureTree tree = getHeatStructureTree(vspDetails, contentMap, analyzedZipHeatFiles);
68
69       CandidateDataEntityTo candidateDataEntityTo =
70           new CandidateDataEntityTo(vspDetails.getId(), vspDetails.getVersion(), uploadedFileData,
71               tree, contentMap);
72       candidateDataEntityTo.setErrors(uploadErrors);
73       OrchestrationTemplateCandidateData candidateDataEntity =
74           candidateService.createCandidateDataEntity(candidateDataEntityTo, zipFileManifest,
75               analyzedZipHeatFiles);
76
77       MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP Id", vspDetails.getId());
78       return candidateDataEntity;
79     }
80   }
81
82   private HeatStructureTree getHeatStructureTree(VspDetails vspDetails,
83                                                  FileContentHandler contentMap,
84                                                  AnalyzedZipHeatFiles analyzedZipHeatFiles)
85       throws IOException {
86     addManifestToFileContentMapIfNotExist(vspDetails, contentMap, analyzedZipHeatFiles);
87     HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(contentMap);
88     heatTreeManager.createTree();
89     return heatTreeManager.getTree();
90   }
91
92   private void addManifestToFileContentMapIfNotExist(VspDetails vspDetails,
93                                                      FileContentHandler fileContentHandler,
94                                                      AnalyzedZipHeatFiles analyzedZipHeatFiles)
95       throws IOException {
96     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP Id", vspDetails.getId());
97
98     try (InputStream manifest = fileContentHandler.getFileContent(SdcCommon.MANIFEST_NAME)) {
99
100       if (Objects.isNull(manifest)) {
101         Optional<ManifestContent> manifestContentOptional =
102             candidateService.createManifest(vspDetails, fileContentHandler, analyzedZipHeatFiles);
103         if (!manifestContentOptional.isPresent()) {
104           throw new RuntimeException(Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage());
105         }
106         ManifestContent manifestContent = manifestContentOptional.get();
107         fileContentHandler.addFile(
108             SdcCommon.MANIFEST_NAME,
109             String.valueOf(JsonUtil.sbObject2Json(manifestContent)).getBytes());
110       }
111     } finally {
112       MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP Id", vspDetails.getId());
113     }
114   }
115 }