2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.services.utils;
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.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
34 import org.openecomp.sdc.vendorsoftwareproduct.services.HeatFileAnalyzer;
35 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
36 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.HeatFileAnalyzerRowDataImpl;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.CandidateDataEntityTo;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.AnalyzedZipHeatFiles;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.util.List;
44 import java.util.Objects;
45 import java.util.Optional;
47 public class CandidateEntityBuilder {
48 private final CandidateService candidateService;
50 public CandidateEntityBuilder(CandidateService candidateService) {
51 this.candidateService = candidateService;
54 public OrchestrationTemplateCandidateData buildCandidateEntityFromZip(
55 VspDetails vspDetails, byte[] uploadedFileData, FileContentHandler contentMap,
56 Map<String, List<ErrorMessage>> uploadErrors) throws Exception {
57 //mdcDataDebugMessage.debugEntryMessage("VSP Id", vspDetails.getId());
59 try (InputStream zipFileManifest = contentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) {
60 HeatFileAnalyzer heatFileAnalyzer = new HeatFileAnalyzerRowDataImpl();
61 AnalyzedZipHeatFiles analyzedZipHeatFiles =
62 heatFileAnalyzer.analyzeFilesNotEligibleForModulesFromFileAnalyzer(contentMap.getFiles());
63 HeatStructureTree tree = getHeatStructureTree(vspDetails, contentMap, analyzedZipHeatFiles);
65 CandidateDataEntityTo candidateDataEntityTo =
66 new CandidateDataEntityTo(vspDetails.getId(), vspDetails.getVersion(), uploadedFileData,
68 candidateDataEntityTo.setErrors(uploadErrors);
69 OrchestrationTemplateCandidateData candidateDataEntity =
70 candidateService.createCandidateDataEntity(candidateDataEntityTo, zipFileManifest,
71 analyzedZipHeatFiles);
72 return candidateDataEntity;
76 private HeatStructureTree getHeatStructureTree(VspDetails vspDetails,
77 FileContentHandler contentMap,
78 AnalyzedZipHeatFiles analyzedZipHeatFiles)
80 addManifestToFileContentMapIfNotExist(vspDetails, contentMap, analyzedZipHeatFiles);
81 HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(contentMap);
82 heatTreeManager.createTree();
83 return heatTreeManager.getTree();
86 private void addManifestToFileContentMapIfNotExist(VspDetails vspDetails,
87 FileContentHandler fileContentHandler,
88 AnalyzedZipHeatFiles analyzedZipHeatFiles)
90 try (InputStream manifest = fileContentHandler.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) {
92 if (Objects.isNull(manifest)) {
93 Optional<ManifestContent> manifestContentOptional =
94 candidateService.createManifest(vspDetails, fileContentHandler, analyzedZipHeatFiles);
95 if (!manifestContentOptional.isPresent()) {
96 throw new RuntimeException(Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage());
98 ManifestContent manifestContent = manifestContentOptional.get();
99 fileContentHandler.addFile(
100 SdcCommon.MANIFEST_NAME,
101 String.valueOf(JsonUtil.sbObject2Json(manifestContent)).getBytes());