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.IOException;
42 import java.io.InputStream;
43 import java.util.List;
45 import java.util.Objects;
46 import java.util.Optional;
48 public class CandidateEntityBuilder {
50 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
52 private final CandidateService candidateService;
54 public CandidateEntityBuilder(CandidateService candidateService) {
55 this.candidateService = candidateService;
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());
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);
69 CandidateDataEntityTo candidateDataEntityTo =
70 new CandidateDataEntityTo(vspDetails.getId(), vspDetails.getVersion(), uploadedFileData,
72 candidateDataEntityTo.setErrors(uploadErrors);
73 OrchestrationTemplateCandidateData candidateDataEntity =
74 candidateService.createCandidateDataEntity(candidateDataEntityTo, zipFileManifest,
75 analyzedZipHeatFiles);
77 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP Id", vspDetails.getId());
78 return candidateDataEntity;
82 private HeatStructureTree getHeatStructureTree(VspDetails vspDetails,
83 FileContentHandler contentMap,
84 AnalyzedZipHeatFiles analyzedZipHeatFiles)
86 addManifestToFileContentMapIfNotExist(vspDetails, contentMap, analyzedZipHeatFiles);
87 HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(contentMap);
88 heatTreeManager.createTree();
89 return heatTreeManager.getTree();
92 private void addManifestToFileContentMapIfNotExist(VspDetails vspDetails,
93 FileContentHandler fileContentHandler,
94 AnalyzedZipHeatFiles analyzedZipHeatFiles)
96 MDC_DATA_DEBUG_MESSAGE.debugEntryMessage("VSP Id", vspDetails.getId());
98 try (InputStream manifest = fileContentHandler.getFileContent(SdcCommon.MANIFEST_NAME)) {
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());
106 ManifestContent manifestContent = manifestContentOptional.get();
107 fileContentHandler.addFile(
108 SdcCommon.MANIFEST_NAME,
109 String.valueOf(JsonUtil.sbObject2Json(manifestContent)).getBytes());
112 MDC_DATA_DEBUG_MESSAGE.debugExitMessage("VSP Id", vspDetails.getId());