2 * Copyright © 2016-2017 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.structure.HeatStructureTree;
31 import org.openecomp.sdc.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
33 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
34 import org.openecomp.sdc.logging.messages.AuditMessages;
35 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
36 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
37 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
43 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
44 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
45 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
48 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
49 import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils;
50 import org.openecomp.sdc.versioning.dao.types.Version;
52 import java.io.ByteArrayInputStream;
53 import java.util.Collection;
54 import java.util.HashMap;
55 import java.util.List;
57 import java.util.Optional;
59 import static org.openecomp.sdc.logging.messages.AuditMessages.HEAT_VALIDATION_ERROR;
61 public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemplateProcessHandler {
62 private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateProcessZipHandler.class);
63 private static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
64 private final CandidateService candidateService =
65 CandidateServiceFactory.getInstance().createInterface();
66 private static final String VSP_ID = "VSP id";
69 public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
70 OrchestrationTemplateCandidateData candidateData) {
71 String vspId = vspDetails.getId();
72 Version version = vspDetails.getVersion();
73 LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_STARTED + vspId);
74 OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
75 UploadFileResponse uploadFileResponse = new UploadFileResponse();
76 Optional<FileContentHandler> fileContent = OrchestrationUtil
77 .getFileContentMap(OnboardingTypesEnum.ZIP, uploadFileResponse,
78 candidateData.getContentData().array());
79 if (!fileContent.isPresent()) {
80 response.addStructureErrors(uploadFileResponse.getErrors());
81 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
82 response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList, vspId,
83 HEAT_VALIDATION_ERROR));
87 Map<String, List<ErrorMessage>> uploadErrors = uploadFileResponse.getErrors();
88 FileContentHandler fileContentMap = fileContent.get();
89 FilesDataStructure structure =
90 JsonUtil.json2Object(candidateData.getFilesDataStructure(), FilesDataStructure.class);
92 if (CollectionUtils.isNotEmpty(structure.getUnassigned())) {
93 response.addErrorMessageToMap(SdcCommon.UPLOAD_FILE,
94 Messages.FOUND_UNASSIGNED_FILES.getErrorMessage(), ErrorLevel.ERROR);
96 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
97 response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList, vspId,
98 HEAT_VALIDATION_ERROR));
103 String manifest = candidateService.createManifest(vspDetails, structure);
104 fileContentMap.addFile(SdcCommon.MANIFEST_NAME, manifest.getBytes());
106 Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
107 .fetchZipFileByteArrayInputStream(
108 vspId, candidateData, manifest, OnboardingTypesEnum.ZIP, uploadErrors);
109 if (!zipByteArrayInputStream.isPresent()) {
110 response.getErrors().values()
111 .forEach(errorList -> printAuditForErrors(errorList, vspId, HEAT_VALIDATION_ERROR));
115 HeatStructureTree tree = createAndValidateHeatTree(response, fileContentMap);
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 response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList, vspId,
138 HEAT_VALIDATION_ERROR));
140 .isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, response.getErrors()))) {
141 LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_COMPLETED + vspId);
144 LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_TRANSLATION_STARTED + vspId);
146 TranslatorOutput translatorOutput =
147 HeatToToscaUtil.loadAndTranslateTemplateData(fileContentMap);
149 ToscaServiceModel toscaServiceModel = translatorOutput.getToscaServiceModel();
151 .saveServiceModel(vspId, version, translatorOutput.getNonUnifiedToscaServiceModel(),
153 orchestrationUtil.retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire,
154 componentNicsQuestionnaire, componentMibList, processes, processArtifact);
155 orchestrationUtil.updateVspComponentDependencies(vspId, version,
156 vspComponentIdNameInfoBeforeProcess, componentDependenciesBeforeDelete);
158 LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_TRANSLATION_COMPLETED + vspId);
159 uploadFileResponse.addStructureErrors(uploadErrors);
161 MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
165 private HeatStructureTree createAndValidateHeatTree(OrchestrationTemplateActionResponse response,
166 FileContentHandler fileContentMap) {
167 VendorSoftwareProductUtils.addFileNamesToUploadFileResponse(fileContentMap, response);
168 Map<String, List<ErrorMessage>> validationErrors =
169 ValidationManagerUtil.initValidationManager(fileContentMap).validate();
170 response.getErrors().putAll(validationErrors);
172 return OrchestrationUtil.createHeatTree(fileContentMap, validationErrors);
175 private void printAuditForErrors(List<ErrorMessage> errorList, String vspId, String auditType) {
177 errorList.forEach(errorMessage -> {
178 if (errorMessage.getLevel().equals(ErrorLevel.ERROR)) {
180 AuditMessages.AUDIT_MSG + String.format(auditType, errorMessage.getMessage(), vspId));