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.
18 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process;
20 import org.apache.commons.collections4.CollectionUtils;
21 import org.apache.commons.collections4.MapUtils;
22 import org.openecomp.core.translator.datatypes.TranslatorOutput;
23 import org.openecomp.core.utilities.file.FileContentHandler;
24 import org.openecomp.core.utilities.json.JsonUtil;
25 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
26 import org.openecomp.core.validation.util.MessageContainerUtil;
27 import org.openecomp.sdc.common.errors.Messages;
28 import org.openecomp.sdc.common.utils.SdcCommon;
29 import org.openecomp.sdc.datatypes.error.ErrorLevel;
30 import org.openecomp.sdc.datatypes.error.ErrorMessage;
31 import org.openecomp.sdc.heat.datatypes.manifest.FileData;
32 import org.openecomp.sdc.heat.datatypes.manifest.ManifestContent;
33 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
34 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
35 import org.openecomp.sdc.logging.api.Logger;
36 import org.openecomp.sdc.logging.api.LoggerFactory;
37 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
38 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
39 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.*;
41 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
42 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
43 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
47 import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils;
48 import org.openecomp.sdc.versioning.dao.types.Version;
50 import java.io.ByteArrayInputStream;
52 import java.io.FileInputStream;
53 import java.io.InputStream;
56 public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemplateProcessHandler {
57 private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateProcessZipHandler.class);
59 private final CandidateService candidateService =
60 CandidateServiceFactory.getInstance().createInterface();
63 public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
64 OrchestrationTemplateCandidateData candidateData) {
65 String vspId = vspDetails.getId();
66 Version version = vspDetails.getVersion();
67 OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
68 UploadFileResponse uploadFileResponse = new UploadFileResponse();
69 Optional<FileContentHandler> fileContent = OrchestrationUtil
70 .getFileContentMap(OnboardingTypesEnum.ZIP, uploadFileResponse,
71 candidateData.getContentData().array());
72 if (!fileContent.isPresent()) {
73 response.addStructureErrors(uploadFileResponse.getErrors());
77 Map<String, List<ErrorMessage>> uploadErrors = uploadFileResponse.getErrors();
78 FileContentHandler fileContentMap = fileContent.get();
79 try (InputStream zipFileManifest = fileContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) {
80 addDummyHeatBase(zipFileManifest ,fileContentMap);
81 } catch (Exception e) {
82 LOGGER.error("Invalid package content", e);
84 FilesDataStructure structure =
85 JsonUtil.json2Object(candidateData.getFilesDataStructure(), FilesDataStructure.class);
87 if (CollectionUtils.isNotEmpty(structure.getUnassigned())) {
88 response.addErrorMessageToMap(SdcCommon.UPLOAD_FILE,
89 Messages.FOUND_UNASSIGNED_FILES.getErrorMessage(), ErrorLevel.ERROR);
94 ManifestContent zipManifestFile = readManifestFromZip(fileContentMap);
95 String manifest = null;
96 if (zipManifestFile == null) {
97 manifest = candidateService.createManifest(vspDetails, structure);
99 manifest = candidateService.createManifestFromExisting(vspDetails, structure, zipManifestFile);
101 fileContentMap.addFile(SdcCommon.MANIFEST_NAME, manifest.getBytes());
103 Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
104 .fetchZipFileByteArrayInputStream(
105 vspId, candidateData, manifest, OnboardingTypesEnum.ZIP, uploadErrors);
106 if (!zipByteArrayInputStream.isPresent()) {
110 HeatStructureTree tree = createAndValidateHeatTree(response, fileContentMap);
111 Map<String, List<ErrorMessage>> errors = getErrors(response);
112 if (MapUtils.isNotEmpty(errors)) {
113 response.addStructureErrors(errors);
114 candidateService.updateValidationData(vspId, version, new ValidationStructureList(tree));
117 Map<String, String> componentsQuestionnaire = new HashMap<>();
118 Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
119 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
120 Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
121 Map<String, ProcessEntity> processArtifact = new HashMap<>();
123 OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
124 Map<String, String> vspComponentIdNameInfoBeforeProcess =
125 orchestrationUtil.getVspComponentIdNameInfo(vspId, version);
126 Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
127 orchestrationUtil.getComponentDependenciesBeforeDelete(vspId, version);
129 .backupComponentsQuestionnaireBeforeDelete(vspId, version, componentsQuestionnaire,
130 componentNicsQuestionnaire, componentMibList, processes, processArtifact);
132 orchestrationUtil.deleteUploadDataAndContent(vspId, version);
134 .saveUploadData(vspDetails, candidateData, zipByteArrayInputStream.get(), fileContentMap,
137 TranslatorOutput translatorOutput =
138 HeatToToscaUtil.loadAndTranslateTemplateData(fileContentMap);
140 ToscaServiceModel toscaServiceModel = translatorOutput.getToscaServiceModel();
142 .saveServiceModel(vspId, version, translatorOutput.getNonUnifiedToscaServiceModel(),
144 orchestrationUtil.retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire,
145 componentNicsQuestionnaire, componentMibList, processes, processArtifact);
146 orchestrationUtil.updateVspComponentDependencies(vspId, version,
147 vspComponentIdNameInfoBeforeProcess, componentDependenciesBeforeDelete);
149 uploadFileResponse.addStructureErrors(uploadErrors);
150 candidateService.deleteOrchestrationTemplateCandidate(vspId, version);
154 private ManifestContent readManifestFromZip(FileContentHandler fileContentMap) {
155 ManifestContent zipManifestFile = null;
156 try (InputStream zipFileManifest = fileContentMap.getFileContentAsStream(SdcCommon.MANIFEST_NAME)) {
157 zipManifestFile = JsonUtil.json2Object(zipFileManifest, ManifestContent.class);
158 } catch (Exception e) {
159 LOGGER.error("Invalid package content", e);
161 return zipManifestFile;
164 private FileContentHandler addDummyHeatBase(InputStream zipFileManifest, FileContentHandler fileContentMap) {
165 ManifestContent manifestContent =
166 JsonUtil.json2Object(zipFileManifest, ManifestContent.class);
167 for (FileData fileData : manifestContent.getData()) {
168 if (Objects.nonNull(fileData.getType()) &&
169 fileData.getType().equals(FileData.Type.HELM) && fileData.getBase()) {
170 String filePath = new File("").getAbsolutePath() + "/resources";
171 File envFilePath = new File(filePath + "/base_template.env");
172 File baseFilePath = new File(filePath + "/base_template.yaml");
174 InputStream envStream = new FileInputStream(envFilePath);
175 InputStream baseStream = new FileInputStream(baseFilePath);) {
176 fileContentMap.addFile("base_template_dummy_ignore.env", envStream);
177 fileContentMap.addFile("base_template_dummy_ignore.yaml", baseStream);
178 } catch (Exception e) {
179 LOGGER.error("File not found error {}", e);
183 return fileContentMap;
186 private Map<String, List<ErrorMessage>> getErrors(OrchestrationTemplateActionResponse
187 orchestrationTemplateActionResponse) {
188 Map<String, List<ErrorMessage>> errors =
189 MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR,
190 orchestrationTemplateActionResponse.getErrors());
191 return MapUtils.isEmpty(errors) ? null : orchestrationTemplateActionResponse.getErrors();
194 private HeatStructureTree createAndValidateHeatTree(OrchestrationTemplateActionResponse response,
195 FileContentHandler fileContentMap) {
196 VendorSoftwareProductUtils.addFileNamesToUploadFileResponse(fileContentMap, response);
197 Map<String, List<ErrorMessage>> validationErrors =
198 ValidationManagerUtil.initValidationManager(fileContentMap).validate();
199 response.getErrors().putAll(validationErrors);
201 return OrchestrationUtil.createHeatTree(fileContentMap, validationErrors);