665c63d64c0f190c4a918c25045b7bb21efd1a2d
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.process;
18
19 import org.apache.commons.collections4.MapUtils;
20 import org.openecomp.core.impl.ToscaConverterImpl;
21 import org.openecomp.core.impl.ToscaSolConverterImpl;
22 import org.openecomp.core.utilities.file.FileContentHandler;
23 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
24 import org.openecomp.core.validation.util.MessageContainerUtil;
25 import org.openecomp.sdc.common.errors.CoreException;
26 import org.openecomp.sdc.common.utils.SdcCommon;
27 import org.openecomp.sdc.datatypes.error.ErrorLevel;
28 import org.openecomp.sdc.datatypes.error.ErrorMessage;
29 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
30 import org.openecomp.sdc.heat.datatypes.structure.ValidationStructureList;
31 import org.openecomp.sdc.heat.services.tree.ToscaTreeManager;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
35 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentMonitoringUploadEntity;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateCandidateData;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
39 import org.openecomp.sdc.vendorsoftwareproduct.factory.CandidateServiceFactory;
40 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.OrchestrationUtil;
41 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
42 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIService;
43 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.etsi.ETSIServiceImpl;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
46
47 import java.io.ByteArrayInputStream;
48 import java.io.IOException;
49 import java.util.*;
50
51 public class OrchestrationTemplateProcessCsarHandler implements OrchestrationTemplateProcessHandler {
52
53   private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateProcessCsarHandler.class);
54   private static final String SDC_ONBOARDED_PACKAGE_DIR = "Deployment/ONBOARDED_PACKAGE/";
55   private static final String EXT_SEPARATOR = ".";
56   private final CandidateService candidateService = CandidateServiceFactory.getInstance().createInterface();
57   private final ToscaTreeManager toscaTreeManager = new ToscaTreeManager();
58
59   @Override
60   public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
61                                   OrchestrationTemplateCandidateData candidateData) {
62
63     UploadFileResponse uploadFileResponse = new UploadFileResponse();
64     Optional<FileContentHandler> fileContent = OrchestrationUtil
65         .getFileContentMap(OnboardingTypesEnum.CSAR, uploadFileResponse,
66             candidateData.getContentData().array());
67
68     OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
69     if (fileContent.isPresent()) {
70       try {
71         FileContentHandler fileContentHandler = fileContent.get();
72         processCsar(vspDetails, fileContentHandler, candidateData, response);
73       } catch (CoreException e) {
74         LOGGER.error(e.getMessage(), e);
75         response.addErrorMessageToMap(e.code().id(), e.code().message(),ErrorLevel.ERROR);
76       }catch (IOException e){
77         LOGGER.error(e.getMessage(), e);
78         response.addErrorMessageToMap(SdcCommon.PROCESS_FILE, e.getMessage(), ErrorLevel.ERROR);
79       }
80     } else {
81       if (!uploadFileResponse.getErrors().isEmpty()) {
82         response.addStructureErrors(uploadFileResponse.getErrors());
83       }
84     }
85     return response;
86   }
87
88   private void processCsar(VspDetails vspDetails,
89                            FileContentHandler fileContentHandler,
90                            OrchestrationTemplateCandidateData candidateData,
91                            OrchestrationTemplateActionResponse response) throws IOException{
92     response.setFileNames(new ArrayList<>(fileContentHandler.getFileList()));
93     Map<String, List<ErrorMessage>> errors = validateCsar(fileContentHandler);
94     toscaTreeManager.createTree();
95
96     if (!isValid(errors)) {
97       response.addStructureErrors(errors);
98       toscaTreeManager.addErrors(errors);
99       candidateService.updateValidationData(vspDetails.getId(), vspDetails.getVersion(),
100           new ValidationStructureList(toscaTreeManager.getTree()));
101       return;
102     }
103
104     HeatStructureTree tree = toscaTreeManager.getTree();
105
106     Map<String, String> componentsQuestionnaire = new HashMap<>();
107     Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
108     Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
109     Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
110     Map<String, ProcessEntity> processArtifact = new HashMap<>();
111     OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
112     orchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspDetails.getId(),
113         vspDetails.getVersion(), componentsQuestionnaire,
114         componentNicsQuestionnaire, componentMibList, processes, processArtifact);
115
116     Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
117         .fetchZipFileByteArrayInputStream(vspDetails.getId(), candidateData, null,
118             OnboardingTypesEnum.CSAR, errors);
119
120     orchestrationUtil.deleteUploadDataAndContent(vspDetails.getId(), vspDetails.getVersion());
121     zipByteArrayInputStream.ifPresent(byteArrayInputStream -> orchestrationUtil
122             .saveUploadData(vspDetails, candidateData, byteArrayInputStream,
123             fileContentHandler, tree));
124
125     ETSIService etsiService = new ETSIServiceImpl();
126     ToscaServiceModel toscaServiceModel;
127     if(etsiService.isSol004WithToscaMetaDirectory(fileContentHandler)){
128       fileContentHandler.addFile(SDC_ONBOARDED_PACKAGE_DIR + candidateData.getFileName() +
129                       EXT_SEPARATOR + candidateData.getFileSuffix(), candidateData.getContentData().array());
130       toscaServiceModel = new ToscaSolConverterImpl().convert(fileContentHandler);
131     }else{
132       toscaServiceModel = new ToscaConverterImpl().convert(fileContentHandler);
133     }
134     orchestrationUtil.saveServiceModel(vspDetails.getId(),
135             vspDetails.getVersion(), toscaServiceModel,
136         toscaServiceModel);
137     candidateService
138         .deleteOrchestrationTemplateCandidate(vspDetails.getId(), vspDetails.getVersion());
139   }
140
141   private void addFiles(FileContentHandler fileContentHandler) {
142     for (Map.Entry<String, byte[]> fileEntry : fileContentHandler.getFiles().entrySet()) {
143       toscaTreeManager.addFile(fileEntry.getKey(), fileEntry.getValue());
144     }
145   }
146
147   private Map<String, List<ErrorMessage>> validateCsar(FileContentHandler fileContentHandler) {
148     Map<String, List<ErrorMessage>> errors = new HashMap<>();
149     addFiles(fileContentHandler);
150     toscaTreeManager.createTree();
151     toscaTreeManager.addErrors(errors);
152     //todo - add tosca validation here to the existing validation framework
153     return errors;
154   }
155
156   private boolean isValid(Map<String, List<ErrorMessage>> errors) {
157     return MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors));
158   }
159 }