2 * Copyright © 2016-2018 European Support Limited
3 * Modifications copyright (c) 2021 Nokia
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process;
19 import java.io.ByteArrayInputStream;
21 import java.io.FileInputStream;
22 import java.io.InputStream;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Objects;
28 import java.util.Optional;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.apache.commons.collections4.MapUtils;
31 import org.openecomp.core.translator.datatypes.TranslatorOutput;
32 import org.openecomp.core.utilities.file.FileContentHandler;
33 import org.openecomp.core.utilities.json.JsonUtil;
34 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
35 import org.openecomp.core.validation.util.MessageContainerUtil;
36 import org.openecomp.sdc.common.errors.Messages;
37 import org.openecomp.sdc.common.utils.SdcCommon;
38 import org.openecomp.sdc.datatypes.error.ErrorLevel;
39 import org.openecomp.sdc.datatypes.error.ErrorMessage;
40 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
41 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
42 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
43 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
44 import org.openecomp.sdc.logging.api.Logger;
45 import org.openecomp.sdc.logging.api.LoggerFactory;
46 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
47 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
48 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
49 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
50 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
51 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
52 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
53 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
54 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
55 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
56 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
57 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
58 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
59 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
60 import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils;
61 import org.openecomp.sdc.versioning.dao.types.Version;
63 public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemplateProcessHandler {
65 private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateProcessZipHandler.class);
66 private final CandidateService candidateService = CandidateServiceFactory.getInstance().createInterface();
69 public OrchestrationTemplateActionResponse process(VspDetails vspDetails, OrchestrationTemplateCandidateData candidateData) {
70 String vspId = vspDetails.getId();
71 Version version = vspDetails.getVersion();
72 OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
73 UploadFileResponse uploadFileResponse = new UploadFileResponse();
74 Optional<FileContentHandler> fileContent = OrchestrationUtil
75 .getFileContentMap(OnboardingTypesEnum.ZIP, uploadFileResponse, candidateData.getContentData().array());
76 if (!fileContent.isPresent()) {
77 response.addStructureErrors(uploadFileResponse.getErrors());
80 Map<String, List<ErrorMessage>> uploadErrors = uploadFileResponse.getErrors();
81 FileContentHandler fileContentMap = fileContent.get();
82 try (InputStream zipFileManifest = fileContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) {
83 addDummyHeatBase(zipFileManifest, fileContentMap);
84 } catch (Exception e) {
85 LOGGER.error("Invalid package content", e);
87 FilesDataStructure structure = JsonUtil.json2Object(candidateData.getFilesDataStructure(), FilesDataStructure.class);
88 if (CollectionUtils.isNotEmpty(structure.getUnassigned())) {
89 response.addErrorMessageToMap(SdcCommon.UPLOAD_FILE, Messages.FOUND_UNASSIGNED_FILES.getErrorMessage(), ErrorLevel.ERROR);
92 ManifestContent zipManifestFile = readManifestFromZip(fileContentMap);
93 String manifest = null;
94 if (zipManifestFile == null) {
95 manifest = candidateService.createManifest(vspDetails, structure);
97 manifest = candidateService.createManifestFromExisting(vspDetails, structure, zipManifestFile);
99 fileContentMap.addFile(SdcCommon.MANIFEST_NAME, manifest.getBytes());
100 Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
101 .fetchZipFileByteArrayInputStream(vspId, candidateData, manifest, OnboardingTypesEnum.ZIP, uploadErrors);
102 if (!zipByteArrayInputStream.isPresent()) {
105 HeatStructureTree tree = createAndValidateHeatTree(response, fileContentMap);
106 Map<String, List<ErrorMessage>> errors = getErrors(response);
107 if (MapUtils.isNotEmpty(errors)) {
108 response.addStructureErrors(errors);
109 candidateService.updateValidationData(vspId, version, new ValidationStructureList(tree));
112 Map<String, String> componentsQuestionnaire = new HashMap<>();
113 Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
114 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
115 Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
116 Map<String, ProcessEntity> processArtifact = new HashMap<>();
117 OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
118 Map<String, String> vspComponentIdNameInfoBeforeProcess = orchestrationUtil.getVspComponentIdNameInfo(vspId, version);
119 Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete = orchestrationUtil
120 .getComponentDependenciesBeforeDelete(vspId, version);
122 .backupComponentsQuestionnaireBeforeDelete(vspId, version, componentsQuestionnaire, componentNicsQuestionnaire, componentMibList,
123 processes, processArtifact);
124 orchestrationUtil.deleteUploadDataAndContent(vspId, version);
125 orchestrationUtil.saveUploadData(vspDetails, candidateData, zipByteArrayInputStream.get(), fileContentMap, tree);
126 TranslatorOutput translatorOutput = HeatToToscaUtil.loadAndTranslateTemplateData(fileContentMap);
127 ToscaServiceModel toscaServiceModel = translatorOutput.getToscaServiceModel();
129 .saveServiceModel(vspId, version, translatorOutput.getNonUnifiedToscaServiceModel(), toscaServiceModel);
131 .retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire, componentNicsQuestionnaire, componentMibList, processes,
133 orchestrationUtil.updateVspComponentDependencies(vspId, version, vspComponentIdNameInfoBeforeProcess, componentDependenciesBeforeDelete);
134 uploadFileResponse.addStructureErrors(uploadErrors);
135 candidateService.deleteOrchestrationTemplateCandidate(vspId, version);
139 private ManifestContent readManifestFromZip(FileContentHandler fileContentMap) {
140 ManifestContent zipManifestFile = null;
141 try (InputStream zipFileManifest = fileContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) {
142 zipManifestFile = JsonUtil.json2Object(zipFileManifest, ManifestContent.class);
143 } catch (Exception e) {
144 LOGGER.error("Invalid package content", e);
146 return zipManifestFile;
149 private FileContentHandler addDummyHeatBase(InputStream zipFileManifest, FileContentHandler fileContentMap) {
150 ManifestContent manifestContent = JsonUtil.json2Object(zipFileManifest, ManifestContent.class);
151 for (FileData fileData : manifestContent.getData()) {
152 if (Objects.nonNull(fileData.getType()) && fileData.getType().equals(FileData.Type.HELM) && fileData.getBase()) {
153 String filePath = new File("").getAbsolutePath() + "/resources";
154 File envFilePath = new File(filePath + "/base_template.env");
155 File baseFilePath = new File(filePath + "/base_template.yaml");
156 try (InputStream envStream = new FileInputStream(envFilePath); InputStream baseStream = new FileInputStream(baseFilePath);) {
157 fileContentMap.addFile("base_template_dummy_ignore.env", envStream);
158 fileContentMap.addFile("base_template_dummy_ignore.yaml", baseStream);
159 } catch (Exception e) {
160 LOGGER.error("File not found error {}", e);
164 return fileContentMap;
167 private Map<String, List<ErrorMessage>> getErrors(OrchestrationTemplateActionResponse orchestrationTemplateActionResponse) {
168 Map<String, List<ErrorMessage>> errors = MessageContainerUtil
169 .getMessageByLevel(ErrorLevel.ERROR, orchestrationTemplateActionResponse.getErrors());
170 return MapUtils.isEmpty(errors) ? null : orchestrationTemplateActionResponse.getErrors();
173 private HeatStructureTree createAndValidateHeatTree(OrchestrationTemplateActionResponse response, FileContentHandler fileContentMap) {
174 VendorSoftwareProductUtils.addFileNamesToUploadFileResponse(fileContentMap, response);
175 Map<String, List<ErrorMessage>> validationErrors = ValidationManagerUtil.initValidationManager(fileContentMap).validate();
176 response.getErrors().putAll(validationErrors);
177 return OrchestrationUtil.createHeatTree(fileContentMap, validationErrors);