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);
78 } catch (IOException ioe) {
79 LOGGER.error(ioe.getMessage());
80 ErrorCode errorCode = new GeneralErrorBuilder(ioe.getMessage()).build();
81 response.addErrorMessageToMap(errorCode.id(), errorCode.message(),ErrorLevel.ERROR);
84 if (!uploadFileResponse.getErrors().isEmpty()) {
85 response.addStructureErrors(uploadFileResponse.getErrors());
91 private void processCsar(VspDetails vspDetails,
92 FileContentHandler fileContentHandler,
93 OrchestrationTemplateCandidateData candidateData,
94 OrchestrationTemplateActionResponse response) throws IOException {
95 response.setFileNames(new ArrayList<>(fileContentHandler.getFileList()));
96 Map<String, List<ErrorMessage>> errors = validateCsar(fileContentHandler);
97 toscaTreeManager.createTree();
99 if (!isValid(errors)) {
100 response.addStructureErrors(errors);
101 toscaTreeManager.addErrors(errors);
102 candidateService.updateValidationData(vspDetails.getId(), vspDetails.getVersion(),
103 new ValidationStructureList(toscaTreeManager.getTree()));
107 HeatStructureTree tree = toscaTreeManager.getTree();
109 Map<String, String> componentsQuestionnaire = new HashMap<>();
110 Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
111 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
112 Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
113 Map<String, ProcessEntity> processArtifact = new HashMap<>();
114 OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
115 orchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspDetails.getId(),
116 vspDetails.getVersion(), componentsQuestionnaire,
117 componentNicsQuestionnaire, componentMibList, processes, processArtifact);
119 Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
120 .fetchZipFileByteArrayInputStream(vspDetails.getId(), candidateData, null,
121 OnboardingTypesEnum.CSAR, errors);
123 orchestrationUtil.deleteUploadDataAndContent(vspDetails.getId(), vspDetails.getVersion());
124 zipByteArrayInputStream.ifPresent(byteArrayInputStream -> orchestrationUtil
125 .saveUploadData(vspDetails, candidateData, byteArrayInputStream,
126 fileContentHandler, tree));
128 ToscaServiceModel toscaServiceModel = new ToscaConverterImpl().convert(fileContentHandler);
129 orchestrationUtil.saveServiceModel(vspDetails.getId(),
130 vspDetails.getVersion(), toscaServiceModel,
133 .deleteOrchestrationTemplateCandidate(vspDetails.getId(), vspDetails.getVersion());
136 private void addFiles(FileContentHandler fileContentHandler) {
137 for (Map.Entry<String, byte[]> fileEntry : fileContentHandler.getFiles().entrySet()) {
138 toscaTreeManager.addFile(fileEntry.getKey(), fileEntry.getValue());
142 private Map<String, List<ErrorMessage>> validateCsar(FileContentHandler fileContentHandler) {
143 Map<String, List<ErrorMessage>> errors = new HashMap<>();
144 addFiles(fileContentHandler);
145 toscaTreeManager.createTree();
146 toscaTreeManager.addErrors(errors);
147 //todo - add tosca validation here to the existing validation framework
151 private boolean isValid(Map<String, List<ErrorMessage>> errors) {
152 return MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors));