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.AbstractToscaSolConverter;
29 import org.openecomp.core.impl.ToscaConverterImpl;
30 import org.openecomp.core.impl.ToscaSolConverterVnf;
31 import org.openecomp.core.impl.ToscaSolModelDrivenConverterPnf;
32 import org.openecomp.core.utilities.file.FileContentHandler;
33 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
34 import org.openecomp.core.validation.util.MessageContainerUtil;
35 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
36 import org.openecomp.sdc.common.errors.CoreException;
37 import org.openecomp.sdc.common.utils.SdcCommon;
38 import org.openecomp.sdc.datatypes.error.ErrorLevel;
39 import org.openecomp.sdc.datatypes.error.ErrorMessage;
40 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
41 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
42 import org.openecomp.sdc.heat.services.tree.ToscaTreeManager;
43 import org.openecomp.sdc.logging.api.Logger;
44 import org.openecomp.sdc.logging.api.LoggerFactory;
45 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
46 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
47 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
48 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
49 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
50 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
51 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
52 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
53 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIService;
54 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIServiceImpl;
55 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
56 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
58 public class OrchestrationTemplateProcessCsarHandler implements OrchestrationTemplateProcessHandler {
60 private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateProcessCsarHandler.class);
61 private static final String SDC_ONBOARDED_PACKAGE_DIR = "Deployment/ONBOARDED_PACKAGE/";
62 private static final String EXT_SEPARATOR = ".";
63 private final CandidateService candidateService = CandidateServiceFactory.getInstance().createInterface();
64 private final ToscaTreeManager toscaTreeManager = new ToscaTreeManager();
67 public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
68 OrchestrationTemplateCandidateData candidateData) {
70 UploadFileResponse uploadFileResponse = new UploadFileResponse();
71 Optional<FileContentHandler> fileContent = OrchestrationUtil
72 .getFileContentMap(OnboardingTypesEnum.CSAR, uploadFileResponse,
73 candidateData.getContentData().array());
75 OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
76 if (fileContent.isPresent()) {
78 FileContentHandler fileContentHandler = fileContent.get();
79 processCsar(vspDetails, fileContentHandler, candidateData, response);
80 } catch (CoreException e) {
81 LOGGER.error(e.getMessage(), e);
82 response.addErrorMessageToMap(e.code().id(), e.code().message(),ErrorLevel.ERROR);
83 }catch (IOException e){
84 LOGGER.error(e.getMessage(), e);
85 response.addErrorMessageToMap(SdcCommon.PROCESS_FILE, e.getMessage(), 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 ETSIService etsiService = new ETSIServiceImpl();
133 ToscaServiceModel toscaServiceModel;
134 if (etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)) {
135 if (OnboardingTypesEnum.CSAR.toString().equalsIgnoreCase(candidateData.getFileSuffix())) {
136 fileContentHandler.addFile(SDC_ONBOARDED_PACKAGE_DIR + candidateData.getOriginalFileName() +
137 EXT_SEPARATOR + candidateData.getOriginalFileSuffix(), candidateData.getOriginalFileContentData().array());
139 fileContentHandler.addFile(SDC_ONBOARDED_PACKAGE_DIR + candidateData.getFileName() +
140 EXT_SEPARATOR + candidateData.getFileSuffix(), candidateData.getContentData().array());
142 final ResourceTypeEnum resourceType = etsiService.getResourceType(fileContentHandler);
143 toscaServiceModel = instantiateToscaConverterFor(resourceType).convert(fileContentHandler);
145 toscaServiceModel = new ToscaConverterImpl().convert(fileContentHandler);
147 orchestrationUtil.saveServiceModel(vspDetails.getId(),
148 vspDetails.getVersion(), toscaServiceModel,
151 .deleteOrchestrationTemplateCandidate(vspDetails.getId(), vspDetails.getVersion());
154 private AbstractToscaSolConverter instantiateToscaConverterFor(ResourceTypeEnum resourceType) {
155 if (resourceType == ResourceTypeEnum.PNF) {
156 return new ToscaSolModelDrivenConverterPnf();
159 return new ToscaSolConverterVnf();
162 private void addFiles(FileContentHandler fileContentHandler) {
163 for (Map.Entry<String, byte[]> fileEntry : fileContentHandler.getFiles().entrySet()) {
164 toscaTreeManager.addFile(fileEntry.getKey(), fileEntry.getValue());
168 private Map<String, List<ErrorMessage>> validateCsar(FileContentHandler fileContentHandler) {
169 Map<String, List<ErrorMessage>> errors = new HashMap<>();
170 addFiles(fileContentHandler);
171 toscaTreeManager.createTree();
172 toscaTreeManager.addErrors(errors);
173 //todo - add tosca validation here to the existing validation framework
177 private boolean isValid(Map<String, List<ErrorMessage>> errors) {
178 return MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors));