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