Create new VSP, onboard from TOSCA file - UI
[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.InputStream;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Objects;
45 import java.util.Optional;
46
47 public class CandidateEntityBuilder {
48   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
49   private CandidateService candidateService;
50
51   public CandidateEntityBuilder(CandidateService candidateService) {
52     this.candidateService = candidateService;
53   }
54
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());
59
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);
65
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);
73
74     mdcDataDebugMessage.debugExitMessage("VSP Id", vspDetails.getId());
75     return candidateDataEntity;
76   }
77
78 //  public OrchestrationTemplateCandidateData buildOrchestrationTemplateFromCsar(VspDetails vspDetails,
79 //                                                                               byte[] uploadedFileData,
80 //                                                                               FileContentHandler contentMap,
81 //                                                                               Map<String, List<ErrorMessage>> uploadErrors,
82 //                                                                               String user){
83 //
84 //  }
85
86   private HeatStructureTree getHeatStructureTree(VspDetails vspDetails,
87                                                  FileContentHandler contentMap,
88                                                  AnalyzedZipHeatFiles analyzedZipHeatFiles) {
89     addManifestToFileContentMapIfNotExist(vspDetails, contentMap, analyzedZipHeatFiles);
90     HeatTreeManager heatTreeManager = HeatTreeManagerUtil.initHeatTreeManager(contentMap);
91     heatTreeManager.createTree();
92     return heatTreeManager.getTree();
93   }
94
95   private void addManifestToFileContentMapIfNotExist(VspDetails vspDetails,
96                                                      FileContentHandler fileContentHandler,
97                                                      AnalyzedZipHeatFiles analyzedZipHeatFiles) {
98     mdcDataDebugMessage.debugEntryMessage("VSP Id", vspDetails.getId());
99
100     InputStream manifest = fileContentHandler.getFileContent(SdcCommon.MANIFEST_NAME);
101     if (Objects.isNull(manifest)) {
102       Optional<ManifestContent> manifestContentOptional =
103           candidateService.createManifest(vspDetails, fileContentHandler, analyzedZipHeatFiles);
104       if (!manifestContentOptional.isPresent()) {
105         throw new RuntimeException(Messages.CREATE_MANIFEST_FROM_ZIP.getErrorMessage());
106       }
107       ManifestContent manifestContent = manifestContentOptional.get();
108       fileContentHandler.addFile(
109           SdcCommon.MANIFEST_NAME,
110           String.valueOf(JsonUtil.sbObject2Json(manifestContent)).getBytes());
111     }
112
113     mdcDataDebugMessage.debugExitMessage("VSP Id", vspDetails.getId());
114   }
115 }