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.
16 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process;
18 import org.apache.commons.collections4.CollectionUtils;
19 import org.apache.commons.collections4.MapUtils;
20 import org.openecomp.core.impl.AbstractToscaSolConverter;
21 import org.openecomp.core.impl.ToscaConverterImpl;
22 import org.openecomp.core.impl.ToscaModelConverter;
23 import org.openecomp.core.impl.ToscaSolConverterVnf;
24 import org.openecomp.core.impl.ToscaSolModelDrivenConverterPnf;
25 import org.openecomp.core.utilities.file.FileContentHandler;
26 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
27 import org.openecomp.core.validation.util.MessageContainerUtil;
28 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
29 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
30 import org.openecomp.sdc.common.errors.CoreException;
31 import org.openecomp.sdc.common.utils.SdcCommon;
32 import org.openecomp.sdc.datatypes.error.ErrorLevel;
33 import org.openecomp.sdc.datatypes.error.ErrorMessage;
34 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
35 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
36 import org.openecomp.sdc.heat.services.tree.ToscaTreeManager;
37 import org.openecomp.sdc.logging.api.Logger;
38 import org.openecomp.sdc.logging.api.LoggerFactory;
39 import org.openecomp.sdc.tosca.csar.AsdPackageHelper;
40 import org.openecomp.sdc.tosca.csar.ManifestUtils;
41 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
44 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
45 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
46 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
47 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIService;
48 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIServiceImpl;
49 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
50 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
52 import java.io.ByteArrayInputStream;
53 import java.io.IOException;
54 import java.util.ArrayList;
55 import java.util.HashMap;
56 import java.util.List;
58 import java.util.Optional;
60 public class OrchestrationTemplateProcessCsarHandler implements OrchestrationTemplateProcessHandler {
62 private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateProcessCsarHandler.class);
63 private static final String SDC_ONBOARDED_PACKAGE_DIR = "Deployment/" + ArtifactTypeEnum.ETSI_PACKAGE.getType() + "/";
64 private static final String EXT_SEPARATOR = ".";
65 private final CandidateService candidateService = CandidateServiceFactory.getInstance().createInterface();
66 private final ToscaTreeManager toscaTreeManager = new ToscaTreeManager();
67 private final ETSIService etsiService;
68 private final AsdPackageHelper asdPackageHelper;
70 public OrchestrationTemplateProcessCsarHandler() {
71 etsiService = new ETSIServiceImpl();
72 this.asdPackageHelper = new AsdPackageHelper(new ManifestUtils());
76 public OrchestrationTemplateActionResponse process(VspDetails vspDetails, OrchestrationTemplateCandidateData candidateData) {
77 UploadFileResponse uploadFileResponse = new UploadFileResponse();
78 Optional<FileContentHandler> fileContent = OrchestrationUtil
79 .getFileContentMap(OnboardingTypesEnum.CSAR, uploadFileResponse, candidateData.getContentData().array());
80 OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
81 if (fileContent.isPresent()) {
83 FileContentHandler fileContentHandler = fileContent.get();
84 processCsar(vspDetails, fileContentHandler, candidateData, response);
85 } catch (CoreException e) {
86 LOGGER.error(e.getMessage(), e);
87 response.addErrorMessageToMap(e.code().id(), e.code().message(), ErrorLevel.ERROR);
88 } catch (IOException e) {
89 LOGGER.error(e.getMessage(), e);
90 response.addErrorMessageToMap(SdcCommon.PROCESS_FILE, e.getMessage(), ErrorLevel.ERROR);
93 if (!uploadFileResponse.getErrors().isEmpty()) {
94 response.addStructureErrors(uploadFileResponse.getErrors());
100 private void processCsar(VspDetails vspDetails, FileContentHandler fileContentHandler, OrchestrationTemplateCandidateData candidateData,
101 OrchestrationTemplateActionResponse response) throws IOException {
102 response.setFileNames(new ArrayList<>(fileContentHandler.getFileList()));
103 Map<String, List<ErrorMessage>> errors = validateCsar(fileContentHandler);
104 toscaTreeManager.createTree();
105 if (!isValid(errors)) {
106 response.addStructureErrors(errors);
107 toscaTreeManager.addErrors(errors);
109 .updateValidationData(vspDetails.getId(), vspDetails.getVersion(), new ValidationStructureList(toscaTreeManager.getTree()));
112 HeatStructureTree tree = toscaTreeManager.getTree();
113 final var orchestrationUtil = new OrchestrationUtil();
114 orchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspDetails.getId(), vspDetails.getVersion(),
115 new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>(), new HashMap<>());
116 Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
117 .fetchZipFileByteArrayInputStream(vspDetails.getId(), candidateData, null, OnboardingTypesEnum.CSAR, errors);
118 orchestrationUtil.deleteUploadDataAndContent(vspDetails.getId(), vspDetails.getVersion());
119 zipByteArrayInputStream.ifPresent(
120 byteArrayInputStream -> orchestrationUtil.saveUploadData(vspDetails, candidateData, byteArrayInputStream, fileContentHandler, tree));
121 final var toscaServiceModel = convertToToscaServiceModel(vspDetails.getModelIdList(), fileContentHandler, candidateData);
123 .saveServiceModel(vspDetails.getId(), vspDetails.getVersion(), toscaServiceModel, toscaServiceModel);
124 candidateService.deleteOrchestrationTemplateCandidate(vspDetails.getId(), vspDetails.getVersion());
127 private ToscaServiceModel convertToToscaServiceModel(final List<String> modelList, final FileContentHandler fileContentHandler,
128 final OrchestrationTemplateCandidateData candidateData) throws IOException {
129 if (CollectionUtils.isNotEmpty(modelList)) {
130 return handleToscaModelConversion(modelList, fileContentHandler, candidateData);
132 if (etsiService.isEtsiPackage(fileContentHandler) || asdPackageHelper.isAsdPackage(fileContentHandler)) {
133 return getToscaServiceModelSol004(fileContentHandler, candidateData);
135 return new ToscaConverterImpl().convert(fileContentHandler);
138 private ToscaServiceModel handleToscaModelConversion(final List<String> modelList, final FileContentHandler fileContentHandler,
139 final OrchestrationTemplateCandidateData candidateData) throws IOException {
140 addOriginalOnboardedPackage(fileContentHandler, candidateData);
141 final var toscaServiceModel = new ToscaModelConverter().convert(fileContentHandler);
142 toscaServiceModel.setModelList(modelList);
143 return toscaServiceModel;
146 private ToscaServiceModel getToscaServiceModelSol004(final FileContentHandler fileContentHandler,
147 final OrchestrationTemplateCandidateData candidateData) throws IOException {
148 addOriginalOnboardedPackage(fileContentHandler, candidateData);
149 final ResourceTypeEnum resourceType = etsiService.getResourceType(fileContentHandler);
150 return instantiateToscaConverterFor(resourceType).convert(fileContentHandler);
153 private void addOriginalOnboardedPackage(final FileContentHandler fileContentHandler, final OrchestrationTemplateCandidateData candidateData) {
154 if (OnboardingTypesEnum.CSAR.getType().equalsIgnoreCase(candidateData.getFileSuffix())) {
156 .addFile(SDC_ONBOARDED_PACKAGE_DIR + candidateData.getOriginalFileName() + EXT_SEPARATOR + candidateData.getOriginalFileSuffix(),
157 candidateData.getOriginalFileContentData().array());
159 fileContentHandler.addFile(SDC_ONBOARDED_PACKAGE_DIR + candidateData.getFileName() + EXT_SEPARATOR + candidateData.getFileSuffix(),
160 candidateData.getContentData().array());
164 private AbstractToscaSolConverter instantiateToscaConverterFor(ResourceTypeEnum resourceType) {
165 if (resourceType == ResourceTypeEnum.PNF) {
166 return new ToscaSolModelDrivenConverterPnf();
169 return new ToscaSolConverterVnf();
172 private void addFiles(FileContentHandler fileContentHandler) {
173 for (Map.Entry<String, byte[]> fileEntry : fileContentHandler.getFiles().entrySet()) {
174 toscaTreeManager.addFile(fileEntry.getKey(), fileEntry.getValue());
178 private Map<String, List<ErrorMessage>> validateCsar(FileContentHandler fileContentHandler) {
179 Map<String, List<ErrorMessage>> errors = new HashMap<>();
180 addFiles(fileContentHandler);
181 toscaTreeManager.createTree();
182 toscaTreeManager.addErrors(errors);
183 //todo - add tosca validation here to the existing validation framework
187 private boolean isValid(Map<String, List<ErrorMessage>> errors) {
188 return MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors));