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.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;
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 static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
49 private CandidateService candidateService;
51 public CandidateEntityBuilder(CandidateService candidateService) {
52 this.candidateService = candidateService;
55 public OrchestrationTemplateCandidateData buildCandidateEntityFromZip(
56 VspDetails vspDetails, byte[] uploadedFileData, FileContentHandler contentMap,
57 Map<String, List<ErrorMessage>> uploadErrors, String user) throws Exception {
58 //mdcDataDebugMessage.debugEntryMessage("VSP Id", vspDetails.getId());
60 InputStream zipFileManifest = contentMap.getFileContent(SdcCommon.MANIFEST_NAME);
61 HeatFileAnalyzer heatFileAnalyzer = new HeatFileAnalyzerRowDataImpl();
62 AnalyzedZipHeatFiles analyzedZipHeatFiles =
63 heatFileAnalyzer.analyzeFilesNotEligibleForModulesFromFileAnalyzer(contentMap.getFiles());
64 HeatStructureTree tree = getHeatStructureTree(vspDetails, contentMap, analyzedZipHeatFiles);
66 CandidateDataEntityTo candidateDataEntityTo =
67 new CandidateDataEntityTo(vspDetails.getId(), user, uploadedFileData, tree, contentMap,
68 vspDetails.getVersion());
69 candidateDataEntityTo.setErrors(uploadErrors);
70 OrchestrationTemplateCandidateData candidateDataEntity =
71 candidateService.createCandidateDataEntity(candidateDataEntityTo, zipFileManifest,
72 analyzedZipHeatFiles);
74 mdcDataDebugMessage.debugExitMessage("VSP Id", vspDetails.getId());
75 return candidateDataEntity;
78 private HeatStructureTree getHeatStructureTree(VspDetails vspDetails,
79 FileContentHandler contentMap,
80 AnalyzedZipHeatFiles analyzedZipHeatFiles) {
81 addManifestToFileContentMapIfNotExist(vspDetails, contentMap, analyzedZipHeatFiles);
82 HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(contentMap);
83 heatTreeManager.createTree();
84 return heatTreeManager.getTree();
87 private void addManifestToFileContentMapIfNotExist(VspDetails vspDetails,
88 FileContentHandler fileContentHandler,
89 AnalyzedZipHeatFiles analyzedZipHeatFiles) {
90 mdcDataDebugMessage.debugEntryMessage("VSP Id", vspDetails.getId());
92 InputStream manifest = fileContentHandler.getFileContent(SdcCommon.MANIFEST_NAME);
93 if (Objects.isNull(manifest)) {
94 Optional<ManifestContent> manifestContentOptional =
95 candidateService.createManifest(vspDetails, fileContentHandler, analyzedZipHeatFiles);
96 if (!manifestContentOptional.isPresent()) {
97 throw new RuntimeException(Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage());
99 ManifestContent manifestContent = manifestContentOptional.get();
100 fileContentHandler.addFile(
101 SdcCommon.MANIFEST_NAME,
102 String.valueOf(JsonUtil.sbObject2Json(manifestContent)).getBytes());
105 mdcDataDebugMessage.debugExitMessage("VSP Id", vspDetails.getId());