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.messages.AuditMessages;
34 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
35 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
36 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
42 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
43 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
44 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
48 import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils;
49 import org.openecomp.sdc.versioning.dao.types.Version;
51 import java.io.ByteArrayInputStream;
52 import java.util.Collection;
53 import java.util.HashMap;
54 import java.util.List;
56 import java.util.Optional;
58 import static org.openecomp.sdc.logging.messages.AuditMessages.HEAT_VALIDATION_ERROR;
60 public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemplateProcessHandler {
61 private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateProcessZipHandler.class);
62 private final CandidateService candidateService =
63 CandidateServiceFactory.getInstance().createInterface();
64 private static final String VSP_ID = "VSP id";
67 public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
68 OrchestrationTemplateCandidateData candidateData) {
69 String vspId = vspDetails.getId();
70 Version version = vspDetails.getVersion();
71 LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_STARTED + vspId);
72 OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
73 UploadFileResponse uploadFileResponse = new UploadFileResponse();
74 Optional<FileContentHandler> fileContent = OrchestrationUtil
75 .getFileContentMap(OnboardingTypesEnum.ZIP, uploadFileResponse,
76 candidateData.getContentData().array());
77 if (!fileContent.isPresent()) {
78 response.addStructureErrors(uploadFileResponse.getErrors());
79 response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList, vspId,
80 HEAT_VALIDATION_ERROR));
84 Map<String, List<ErrorMessage>> uploadErrors = uploadFileResponse.getErrors();
85 FileContentHandler fileContentMap = fileContent.get();
86 FilesDataStructure structure =
87 JsonUtil.json2Object(candidateData.getFilesDataStructure(), FilesDataStructure.class);
89 if (CollectionUtils.isNotEmpty(structure.getUnassigned())) {
90 response.addErrorMessageToMap(SdcCommon.UPLOAD_FILE,
91 Messages.FOUND_UNASSIGNED_FILES.getErrorMessage(), ErrorLevel.ERROR);
92 response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList, vspId,
93 HEAT_VALIDATION_ERROR));
98 String manifest = candidateService.createManifest(vspDetails, structure);
99 fileContentMap.addFile(SdcCommon.MANIFEST_NAME, manifest.getBytes());
101 Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
102 .fetchZipFileByteArrayInputStream(
103 vspId, candidateData, manifest, OnboardingTypesEnum.ZIP, uploadErrors);
104 if (!zipByteArrayInputStream.isPresent()) {
105 response.getErrors().values()
106 .forEach(errorList -> printAuditForErrors(errorList, vspId, HEAT_VALIDATION_ERROR));
110 HeatStructureTree tree = createAndValidateHeatTree(response, fileContentMap);
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<>();
118 OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
119 Map<String, String> vspComponentIdNameInfoBeforeProcess =
120 orchestrationUtil.getVspComponentIdNameInfo(vspId, version);
121 Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
122 orchestrationUtil.getComponentDependenciesBeforeDelete(vspId, version);
124 .backupComponentsQuestionnaireBeforeDelete(vspId, version, componentsQuestionnaire,
125 componentNicsQuestionnaire, componentMibList, processes, processArtifact);
127 orchestrationUtil.deleteUploadDataAndContent(vspId, version);
129 .saveUploadData(vspDetails, candidateData, zipByteArrayInputStream.get(), fileContentMap,
132 response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList, vspId,
133 HEAT_VALIDATION_ERROR));
135 .isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, response.getErrors()))) {
136 LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_COMPLETED + vspId);
139 LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_TRANSLATION_STARTED + vspId);
141 TranslatorOutput translatorOutput =
142 HeatToToscaUtil.loadAndTranslateTemplateData(fileContentMap);
144 ToscaServiceModel toscaServiceModel = translatorOutput.getToscaServiceModel();
146 .saveServiceModel(vspId, version, translatorOutput.getNonUnifiedToscaServiceModel(),
148 orchestrationUtil.retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire,
149 componentNicsQuestionnaire, componentMibList, processes, processArtifact);
150 orchestrationUtil.updateVspComponentDependencies(vspId, version,
151 vspComponentIdNameInfoBeforeProcess, componentDependenciesBeforeDelete);
153 LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_TRANSLATION_COMPLETED + vspId);
154 uploadFileResponse.addStructureErrors(uploadErrors);
158 private HeatStructureTree createAndValidateHeatTree(OrchestrationTemplateActionResponse response,
159 FileContentHandler fileContentMap) {
160 VendorSoftwareProductUtils.addFileNamesToUploadFileResponse(fileContentMap, response);
161 Map<String, List<ErrorMessage>> validationErrors =
162 ValidationManagerUtil.initValidationManager(fileContentMap).validate();
163 response.getErrors().putAll(validationErrors);
165 return OrchestrationUtil.createHeatTree(fileContentMap, validationErrors);
168 private void printAuditForErrors(List<ErrorMessage> errorList, String vspId, String auditType) {
170 errorList.forEach(errorMessage -> {
171 if (errorMessage.getLevel().equals(ErrorLevel.ERROR)) {
173 AuditMessages.AUDIT_MSG + String.format(auditType, errorMessage.getMessage(), vspId));