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.MapUtils;
20 import org.openecomp.core.impl.ToscaConverterImpl;
21 import org.openecomp.core.utilities.file.FileContentHandler;
22 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
23 import org.openecomp.core.validation.util.MessageContainerUtil;
24 import org.openecomp.sdc.common.errors.CoreException;
25 import org.openecomp.sdc.common.errors.ErrorCode;
26 import org.openecomp.sdc.common.errors.GeneralErrorBuilder;
27 import org.openecomp.sdc.datatypes.error.ErrorLevel;
28 import org.openecomp.sdc.datatypes.error.ErrorMessage;
29 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
30 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
31 import org.openecomp.sdc.heat.services.tree.ToscaTreeManager;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.openecomp.sdc.logging.messages.AuditMessages;
35 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
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;
46 import java.io.ByteArrayInputStream;
47 import java.io.IOException;
48 import java.util.ArrayList;
49 import java.util.Collection;
50 import java.util.HashMap;
51 import java.util.List;
53 import java.util.Optional;
55 public class OrchestrationTemplateProcessCsarHandler
56 implements OrchestrationTemplateProcessHandler {
57 private static final Logger LOGGER = LoggerFactory
58 .getLogger(OrchestrationTemplateProcessCsarHandler.class);
59 private final CandidateService candidateService = CandidateServiceFactory
60 .getInstance().createInterface();
61 private final ToscaTreeManager toscaTreeManager = new ToscaTreeManager();
64 public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
65 OrchestrationTemplateCandidateData candidateData) {
67 AuditMessages.AUDIT_MSG + AuditMessages.CSAR_VALIDATION_STARTED + vspDetails.getId());
69 UploadFileResponse uploadFileResponse = new UploadFileResponse();
70 Optional<FileContentHandler> fileContent = OrchestrationUtil
71 .getFileContentMap(OnboardingTypesEnum.CSAR, uploadFileResponse,
72 candidateData.getContentData().array());
74 OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
75 if (fileContent.isPresent()) {
77 FileContentHandler fileContentHandler = fileContent.get();
78 processCsar(vspDetails, fileContentHandler, candidateData, response);
79 } catch (CoreException e) {
80 LOGGER.error(e.getMessage());
81 response.addErrorMessageToMap(e.code().id(), e.code().message(),ErrorLevel.ERROR);
82 } catch (IOException ioe) {
83 LOGGER.error(ioe.getMessage());
84 ErrorCode errorCode = new GeneralErrorBuilder(ioe.getMessage()).build();
85 response.addErrorMessageToMap(errorCode.id(), errorCode.message(),ErrorLevel.ERROR);
88 if (!uploadFileResponse.getErrors().isEmpty()) {
89 response.addStructureErrors(uploadFileResponse.getErrors());
95 private void processCsar(VspDetails vspDetails,
96 FileContentHandler fileContentHandler,
97 OrchestrationTemplateCandidateData candidateData,
98 OrchestrationTemplateActionResponse response) throws IOException {
99 response.setFileNames(new ArrayList<>(fileContentHandler.getFileList()));
100 Map<String, List<ErrorMessage>> errors = validateCsar(fileContentHandler);
101 toscaTreeManager.createTree();
103 if (!isValid(errors)) {
104 response.addStructureErrors(errors);
105 toscaTreeManager.addErrors(errors);
106 candidateService.updateValidationData(vspDetails.getId(), vspDetails.getVersion(),
107 new ValidationStructureList(toscaTreeManager.getTree()));
111 HeatStructureTree tree = toscaTreeManager.getTree();
113 Map<String, String> componentsQuestionnaire = new HashMap<>();
114 Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
115 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
116 Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
117 Map<String, ProcessEntity> processArtifact = new HashMap<>();
118 OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
119 orchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspDetails.getId(),
120 vspDetails.getVersion(), componentsQuestionnaire,
121 componentNicsQuestionnaire, componentMibList, processes, processArtifact);
123 Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
124 .fetchZipFileByteArrayInputStream(vspDetails.getId(), candidateData, null,
125 OnboardingTypesEnum.CSAR, errors);
127 orchestrationUtil.deleteUploadDataAndContent(vspDetails.getId(), vspDetails.getVersion());
128 zipByteArrayInputStream.ifPresent(byteArrayInputStream -> orchestrationUtil
129 .saveUploadData(vspDetails, candidateData, byteArrayInputStream,
130 fileContentHandler, tree));
132 ToscaServiceModel toscaServiceModel = new ToscaConverterImpl().convert(fileContentHandler);
133 orchestrationUtil.saveServiceModel(vspDetails.getId(),
134 vspDetails.getVersion(), toscaServiceModel,
137 .deleteOrchestrationTemplateCandidate(vspDetails.getId(), vspDetails.getVersion());
140 private void addFiles(FileContentHandler fileContentHandler) {
141 for (Map.Entry<String, byte[]> fileEntry : fileContentHandler.getFiles().entrySet()) {
142 toscaTreeManager.addFile(fileEntry.getKey(), fileEntry.getValue());
146 private Map<String, List<ErrorMessage>> validateCsar(FileContentHandler fileContentHandler) {
147 Map<String, List<ErrorMessage>> errors = new HashMap<>();
148 addFiles(fileContentHandler);
149 toscaTreeManager.createTree();
150 toscaTreeManager.addErrors(errors);
151 //todo - add tosca validation here to the existing validation framework
155 private boolean isValid(Map<String, List<ErrorMessage>> errors) {
156 return MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors));