41510ecc13a06aa451ca05b8a660df673d880c49
[sdc.git] /
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, String user) 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(), user, uploadedFileData, tree, contentMap,
71                       vspDetails.getVersion());
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) throws IOException {
85     addManifestToFileContentMapIfNotExist(vspDetails, contentMap, analyzedZipHeatFiles);
86     HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(contentMap);
87     heatTreeManager.createTree();
88     return heatTreeManager.getTree();
89   }
90
91   private void addManifestToFileContentMapIfNotExist(VspDetails vspDetails,
92                                                      FileContentHandler fileContentHandler,
93                                                      AnalyzedZipHeatFiles analyzedZipHeatFiles) throws IOException {
94     MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP Id", vspDetails.getId());
95
96     try (InputStream manifest = fileContentHandler.getFileContent(SdcCommon.MANIFEST_NAME)) {
97
98       if (Objects.isNull(manifest)) {
99         Optional<ManifestContent> manifestContentOptional =
100                 candidateService.createManifest(vspDetails, fileContentHandler, analyzedZipHeatFiles);
101         if (!manifestContentOptional.isPresent()) {
102           throw new RuntimeException(Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage());
103         }
104         ManifestContent manifestContent = manifestContentOptional.get();
105         fileContentHandler.addFile(
106                 SdcCommon.MANIFEST_NAME,
107                 String.valueOf(JsonUtil.sbObject2Json(manifestContent)).getBytes());
108       }
109     } finally {
110       MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP Id", vspDetails.getId());
111     }
112   }
113 }