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.tosca.datatypes.ToscaServiceModel;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
39 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
40 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
41 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
42 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
45 import java.io.ByteArrayInputStream;
46 import java.io.IOException;
49 public class OrchestrationTemplateProcessCsarHandler
50 implements OrchestrationTemplateProcessHandler {
51 private static final Logger LOGGER = LoggerFactory
52 .getLogger(OrchestrationTemplateProcessCsarHandler.class);
53 private final CandidateService candidateService = CandidateServiceFactory
54 .getInstance().createInterface();
55 private final ToscaTreeManager toscaTreeManager = new ToscaTreeManager();
58 public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
59 OrchestrationTemplateCandidateData candidateData) {
61 UploadFileResponse uploadFileResponse = new UploadFileResponse();
62 Optional<FileContentHandler> fileContent = OrchestrationUtil
63 .getFileContentMap(OnboardingTypesEnum.CSAR, uploadFileResponse,
64 candidateData.getContentData().array());
66 OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
67 if (fileContent.isPresent()) {
69 FileContentHandler fileContentHandler = fileContent.get();
70 processCsar(vspDetails, fileContentHandler, candidateData, response);
71 } catch (CoreException e) {
72 LOGGER.error(e.getMessage());
73 response.addErrorMessageToMap(e.code().id(), e.code().message(),ErrorLevel.ERROR);
76 if (!uploadFileResponse.getErrors().isEmpty()) {
77 response.addStructureErrors(uploadFileResponse.getErrors());
83 private void processCsar(VspDetails vspDetails,
84 FileContentHandler fileContentHandler,
85 OrchestrationTemplateCandidateData candidateData,
86 OrchestrationTemplateActionResponse response) {
87 response.setFileNames(new ArrayList<>(fileContentHandler.getFileList()));
88 Map<String, List<ErrorMessage>> errors = validateCsar(fileContentHandler);
89 toscaTreeManager.createTree();
91 if (!isValid(errors)) {
92 response.addStructureErrors(errors);
93 toscaTreeManager.addErrors(errors);
94 candidateService.updateValidationData(vspDetails.getId(), vspDetails.getVersion(),
95 new ValidationStructureList(toscaTreeManager.getTree()));
99 HeatStructureTree tree = toscaTreeManager.getTree();
101 Map<String, String> componentsQuestionnaire = new HashMap<>();
102 Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
103 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
104 Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
105 Map<String, ProcessEntity> processArtifact = new HashMap<>();
106 OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
107 orchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspDetails.getId(),
108 vspDetails.getVersion(), componentsQuestionnaire,
109 componentNicsQuestionnaire, componentMibList, processes, processArtifact);
111 Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
112 .fetchZipFileByteArrayInputStream(vspDetails.getId(), candidateData, null,
113 OnboardingTypesEnum.CSAR, errors);
115 orchestrationUtil.deleteUploadDataAndContent(vspDetails.getId(), vspDetails.getVersion());
116 zipByteArrayInputStream.ifPresent(byteArrayInputStream -> orchestrationUtil
117 .saveUploadData(vspDetails, candidateData, byteArrayInputStream,
118 fileContentHandler, tree));
120 ToscaServiceModel toscaServiceModel = new ToscaConverterImpl().convert(fileContentHandler);
121 orchestrationUtil.saveServiceModel(vspDetails.getId(),
122 vspDetails.getVersion(), toscaServiceModel,
125 .deleteOrchestrationTemplateCandidate(vspDetails.getId(), vspDetails.getVersion());
128 private void addFiles(FileContentHandler fileContentHandler) {
129 for (Map.Entry<String, byte[]> fileEntry : fileContentHandler.getFiles().entrySet()) {
130 toscaTreeManager.addFile(fileEntry.getKey(), fileEntry.getValue());
134 private Map<String, List<ErrorMessage>> validateCsar(FileContentHandler fileContentHandler) {
135 Map<String, List<ErrorMessage>> errors = new HashMap<>();
136 addFiles(fileContentHandler);
137 toscaTreeManager.createTree();
138 toscaTreeManager.addErrors(errors);
139 //todo - add tosca validation here to the existing validation framework
143 private boolean isValid(Map<String, List<ErrorMessage>> errors) {
144 return MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors));