73c2a425dd1d214f78b898363cef78af3392186f
[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 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;
25 import java.util.Map;
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;
52
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();
60
61   @Override
62   public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
63                                   OrchestrationTemplateCandidateData candidateData) {
64
65     UploadFileResponse uploadFileResponse = new UploadFileResponse();
66     Optional<FileContentHandler> fileContent = OrchestrationUtil
67         .getFileContentMap(OnboardingTypesEnum.CSAR, uploadFileResponse,
68             candidateData.getContentData().array());
69
70     OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
71     if (fileContent.isPresent()) {
72       try {
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       }
79     } else {
80       if (!uploadFileResponse.getErrors().isEmpty()) {
81         response.addStructureErrors(uploadFileResponse.getErrors());
82       }
83     }
84     return response;
85   }
86
87   private void processCsar(VspDetails vspDetails,
88                            FileContentHandler fileContentHandler,
89                            OrchestrationTemplateCandidateData candidateData,
90                            OrchestrationTemplateActionResponse response) {
91     response.setFileNames(new ArrayList<>(fileContentHandler.getFileList()));
92     Map<String, List<ErrorMessage>> errors = validateCsar(fileContentHandler);
93     toscaTreeManager.createTree();
94
95     if (!isValid(errors)) {
96       response.addStructureErrors(errors);
97       toscaTreeManager.addErrors(errors);
98       candidateService.updateValidationData(vspDetails.getId(), vspDetails.getVersion(),
99           new ValidationStructureList(toscaTreeManager.getTree()));
100       return;
101     }
102
103     HeatStructureTree tree = toscaTreeManager.getTree();
104
105     Map<String, String> componentsQuestionnaire = new HashMap<>();
106     Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
107     Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
108     Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
109     Map<String, ProcessEntity> processArtifact = new HashMap<>();
110     OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
111     orchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspDetails.getId(),
112         vspDetails.getVersion(), componentsQuestionnaire,
113         componentNicsQuestionnaire, componentMibList, processes, processArtifact);
114
115     Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
116         .fetchZipFileByteArrayInputStream(vspDetails.getId(), candidateData, null,
117             OnboardingTypesEnum.CSAR, errors);
118
119     orchestrationUtil.deleteUploadDataAndContent(vspDetails.getId(), vspDetails.getVersion());
120     zipByteArrayInputStream.ifPresent(byteArrayInputStream -> orchestrationUtil
121             .saveUploadData(vspDetails, candidateData, byteArrayInputStream,
122             fileContentHandler, tree));
123
124     ToscaServiceModel toscaServiceModel = new ToscaConverterImpl().convert(fileContentHandler);
125     orchestrationUtil.saveServiceModel(vspDetails.getId(),
126             vspDetails.getVersion(), toscaServiceModel,
127         toscaServiceModel);
128     candidateService
129         .deleteOrchestrationTemplateCandidate(vspDetails.getId(), vspDetails.getVersion());
130   }
131
132   private void addFiles(FileContentHandler fileContentHandler) {
133     for (Map.Entry<String, byte[]> fileEntry : fileContentHandler.getFiles().entrySet()) {
134       toscaTreeManager.addFile(fileEntry.getKey(), fileEntry.getValue());
135     }
136   }
137
138   private Map<String, List<ErrorMessage>> validateCsar(FileContentHandler fileContentHandler) {
139     Map<String, List<ErrorMessage>> errors = new HashMap<>();
140     addFiles(fileContentHandler);
141     toscaTreeManager.createTree();
142     toscaTreeManager.addErrors(errors);
143     //todo - add tosca validation here to the existing validation framework
144     return errors;
145   }
146
147   private boolean isValid(Map<String, List<ErrorMessage>> errors) {
148     return MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors));
149   }
150 }