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.AbstractToscaSolConverter;
21 import org.openecomp.core.impl.ToscaConverterImpl;
22 import org.openecomp.core.impl.ToscaSolConverterPnf;
23 import org.openecomp.core.impl.ToscaSolConverterVnf;
24 import org.openecomp.core.utilities.file.FileContentHandler;
25 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
26 import org.openecomp.core.validation.util.MessageContainerUtil;
27 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.common.utils.SdcCommon;
30 import org.openecomp.sdc.datatypes.error.ErrorLevel;
31 import org.openecomp.sdc.datatypes.error.ErrorMessage;
32 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
33 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
34 import org.openecomp.sdc.heat.services.tree.ToscaTreeManager;
35 import org.openecomp.sdc.logging.api.Logger;
36 import org.openecomp.sdc.logging.api.LoggerFactory;
37 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
42 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
43 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
44 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
45 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIService;
46 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIServiceImpl;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
48 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
50 import java.io.ByteArrayInputStream;
51 import java.io.IOException;
52 import java.util.ArrayList;
53 import java.util.Collection;
54 import java.util.HashMap;
55 import java.util.List;
57 import java.util.Optional;
59 public class OrchestrationTemplateProcessCsarHandler implements OrchestrationTemplateProcessHandler {
61 private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateProcessCsarHandler.class);
62 private static final String SDC_ONBOARDED_PACKAGE_DIR = "Deployment/ONBOARDED_PACKAGE/";
63 private static final String EXT_SEPARATOR = ".";
64 private final CandidateService candidateService = CandidateServiceFactory.getInstance().createInterface();
65 private final ToscaTreeManager toscaTreeManager = new ToscaTreeManager();
68 public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
69 OrchestrationTemplateCandidateData candidateData) {
71 UploadFileResponse uploadFileResponse = new UploadFileResponse();
72 Optional<FileContentHandler> fileContent = OrchestrationUtil
73 .getFileContentMap(OnboardingTypesEnum.CSAR, uploadFileResponse,
74 candidateData.getContentData().array());
76 OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
77 if (fileContent.isPresent()) {
79 FileContentHandler fileContentHandler = fileContent.get();
80 processCsar(vspDetails, fileContentHandler, candidateData, response);
81 } catch (CoreException e) {
82 LOGGER.error(e.getMessage(), e);
83 response.addErrorMessageToMap(e.code().id(), e.code().message(),ErrorLevel.ERROR);
84 }catch (IOException e){
85 LOGGER.error(e.getMessage(), e);
86 response.addErrorMessageToMap(SdcCommon.PROCESS_FILE, e.getMessage(), ErrorLevel.ERROR);
89 if (!uploadFileResponse.getErrors().isEmpty()) {
90 response.addStructureErrors(uploadFileResponse.getErrors());
96 private void processCsar(VspDetails vspDetails,
97 FileContentHandler fileContentHandler,
98 OrchestrationTemplateCandidateData candidateData,
99 OrchestrationTemplateActionResponse response) throws IOException{
100 response.setFileNames(new ArrayList<>(fileContentHandler.getFileList()));
101 Map<String, List<ErrorMessage>> errors = validateCsar(fileContentHandler);
102 toscaTreeManager.createTree();
104 if (!isValid(errors)) {
105 response.addStructureErrors(errors);
106 toscaTreeManager.addErrors(errors);
107 candidateService.updateValidationData(vspDetails.getId(), vspDetails.getVersion(),
108 new ValidationStructureList(toscaTreeManager.getTree()));
112 HeatStructureTree tree = toscaTreeManager.getTree();
114 Map<String, String> componentsQuestionnaire = new HashMap<>();
115 Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
116 Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
117 Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
118 Map<String, ProcessEntity> processArtifact = new HashMap<>();
119 OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
120 orchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspDetails.getId(),
121 vspDetails.getVersion(), componentsQuestionnaire,
122 componentNicsQuestionnaire, componentMibList, processes, processArtifact);
124 Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
125 .fetchZipFileByteArrayInputStream(vspDetails.getId(), candidateData, null,
126 OnboardingTypesEnum.CSAR, errors);
128 orchestrationUtil.deleteUploadDataAndContent(vspDetails.getId(), vspDetails.getVersion());
129 zipByteArrayInputStream.ifPresent(byteArrayInputStream -> orchestrationUtil
130 .saveUploadData(vspDetails, candidateData, byteArrayInputStream,
131 fileContentHandler, tree));
133 ETSIService etsiService = new ETSIServiceImpl();
134 ToscaServiceModel toscaServiceModel;
135 if (etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)) {
136 fileContentHandler.addFile(SDC_ONBOARDED_PACKAGE_DIR + candidateData.getFileName() +
137 EXT_SEPARATOR + candidateData.getFileSuffix(), candidateData.getContentData().array());
138 final ResourceTypeEnum resourceType = etsiService.getResourceType(fileContentHandler);
139 toscaServiceModel = instantiateToscaConverterFor(resourceType).convert(fileContentHandler);
141 toscaServiceModel = new ToscaConverterImpl().convert(fileContentHandler);
143 orchestrationUtil.saveServiceModel(vspDetails.getId(),
144 vspDetails.getVersion(), toscaServiceModel,
147 .deleteOrchestrationTemplateCandidate(vspDetails.getId(), vspDetails.getVersion());
150 private AbstractToscaSolConverter instantiateToscaConverterFor(ResourceTypeEnum resourceType) {
151 if (resourceType == ResourceTypeEnum.PNF) {
152 return new ToscaSolConverterPnf();
155 return new ToscaSolConverterVnf();
158 private void addFiles(FileContentHandler fileContentHandler) {
159 for (Map.Entry<String, byte[]> fileEntry : fileContentHandler.getFiles().entrySet()) {
160 toscaTreeManager.addFile(fileEntry.getKey(), fileEntry.getValue());
164 private Map<String, List<ErrorMessage>> validateCsar(FileContentHandler fileContentHandler) {
165 Map<String, List<ErrorMessage>> errors = new HashMap<>();
166 addFiles(fileContentHandler);
167 toscaTreeManager.createTree();
168 toscaTreeManager.addErrors(errors);
169 //todo - add tosca validation here to the existing validation framework
173 private boolean isValid(Map<String, List<ErrorMessage>> errors) {
174 return MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors));