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 java.io.ByteArrayInputStream;
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.Optional;
27 import org.apache.commons.collections4.MapUtils;
28 import org.openecomp.core.impl.ToscaConverterImpl;
29 import org.openecomp.core.utilities.file.FileContentHandler;
30 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
31 import org.openecomp.core.validation.util.MessageContainerUtil;
32 import org.openecomp.sdc.common.errors.CoreException;
33 import org.openecomp.sdc.common.errors.ErrorCode;
34 import org.openecomp.sdc.common.errors.GeneralErrorBuilder;
35 import org.openecomp.sdc.datatypes.error.ErrorLevel;
36 import org.openecomp.sdc.datatypes.error.ErrorMessage;
37 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
38 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
39 import org.openecomp.sdc.heat.services.tree.ToscaTreeManager;
40 import org.openecomp.sdc.logging.api.Logger;
41 import org.openecomp.sdc.logging.api.LoggerFactory;
42 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
44 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
45 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
46 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
47 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
48 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
49 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
50 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
51 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
53 public class OrchestrationTemplateProcessCsarHandler
54 implements OrchestrationTemplateProcessHandler {
55 private static final Logger LOGGER = LoggerFactory
56 .getLogger(OrchestrationTemplateProcessCsarHandler.class);
57 private final CandidateService candidateService = CandidateServiceFactory
58 .getInstance().createInterface();
59 private final ToscaTreeManager toscaTreeManager = new ToscaTreeManager();
62 public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
63 OrchestrationTemplateCandidateData candidateData) {
65 UploadFileResponse uploadFileResponse = new UploadFileResponse();
66 Optional<FileContentHandler> fileContent = OrchestrationUtil
67 .getFileContentMap(OnboardingTypesEnum.CSAR, uploadFileResponse,
68 candidateData.getContentData().array());
70 OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
71 if (fileContent.isPresent()) {
73 FileContentHandler fileContentHandler = fileContent.get();
74 processCsar(vspDetails, fileContentHandler, candidateData, response);
75 } catch (CoreException e) {
76 LOGGER.error(e.getMessage());
77 response.addErrorMessageToMap(e.code().id(), e.code().message(),ErrorLevel.ERROR);
80 if (!uploadFileResponse.getErrors().isEmpty()) {
81 response.addStructureErrors(uploadFileResponse.getErrors());
87 private void processCsar(VspDetails vspDetails,
88 FileContentHandler fileContentHandler,
89 OrchestrationTemplateCandidateData candidateData,
90 OrchestrationTemplateActionResponse response) {
91 response.setFileNames(new ArrayList<>(fileContentHandler.getFileList()));
92 Map<String, List<ErrorMessage>> errors = validateCsar(fileContentHandler);
93 toscaTreeManager.createTree();
95 if (!isValid(errors)) {
96 response.addStructureErrors(errors);
97 toscaTreeManager.addErrors(errors);
98 candidateService.updateValidationData(vspDetails.getId(), vspDetails.getVersion(),
99 new ValidationStructureList(toscaTreeManager.getTree()));
103 HeatStructureTree tree = toscaTreeManager.getTree();
105 Map<String, String> componentsQuestionnaire = new HashMap<>();
106 Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
107 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
108 Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
109 Map<String, ProcessEntity> processArtifact = new HashMap<>();
110 OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
111 orchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspDetails.getId(),
112 vspDetails.getVersion(), componentsQuestionnaire,
113 componentNicsQuestionnaire, componentMibList, processes, processArtifact);
115 Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
116 .fetchZipFileByteArrayInputStream(vspDetails.getId(), candidateData, null,
117 OnboardingTypesEnum.CSAR, errors);
119 orchestrationUtil.deleteUploadDataAndContent(vspDetails.getId(), vspDetails.getVersion());
120 zipByteArrayInputStream.ifPresent(byteArrayInputStream -> orchestrationUtil
121 .saveUploadData(vspDetails, candidateData, byteArrayInputStream,
122 fileContentHandler, tree));
124 ToscaServiceModel toscaServiceModel = new ToscaConverterImpl().convert(fileContentHandler);
125 orchestrationUtil.saveServiceModel(vspDetails.getId(),
126 vspDetails.getVersion(), toscaServiceModel,
129 .deleteOrchestrationTemplateCandidate(vspDetails.getId(), vspDetails.getVersion());
132 private void addFiles(FileContentHandler fileContentHandler) {
133 for (Map.Entry<String, byte[]> fileEntry : fileContentHandler.getFiles().entrySet()) {
134 toscaTreeManager.addFile(fileEntry.getKey(), fileEntry.getValue());
138 private Map<String, List<ErrorMessage>> validateCsar(FileContentHandler fileContentHandler) {
139 Map<String, List<ErrorMessage>> errors = new HashMap<>();
140 addFiles(fileContentHandler);
141 toscaTreeManager.createTree();
142 toscaTreeManager.addErrors(errors);
143 //todo - add tosca validation here to the existing validation framework
147 private boolean isValid(Map<String, List<ErrorMessage>> errors) {
148 return MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors));