4d92ee87c31d3cd79b6e8f95514d1365f4ddf5e9
[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       } catch (IOException ioe) {
79         LOGGER.error(ioe.getMessage());
80         ErrorCode errorCode = new GeneralErrorBuilder(ioe.getMessage()).build();
81         response.addErrorMessageToMap(errorCode.id(), errorCode.message(),ErrorLevel.ERROR);
82       }
83     } else {
84       if (!uploadFileResponse.getErrors().isEmpty()) {
85         response.addStructureErrors(uploadFileResponse.getErrors());
86       }
87     }
88     return response;
89   }
90
91   private void processCsar(VspDetails vspDetails,
92                            FileContentHandler fileContentHandler,
93                            OrchestrationTemplateCandidateData candidateData,
94                            OrchestrationTemplateActionResponse response) throws IOException {
95     response.setFileNames(new ArrayList<>(fileContentHandler.getFileList()));
96     Map<String, List<ErrorMessage>> errors = validateCsar(fileContentHandler);
97     toscaTreeManager.createTree();
98
99     if (!isValid(errors)) {
100       response.addStructureErrors(errors);
101       toscaTreeManager.addErrors(errors);
102       candidateService.updateValidationData(vspDetails.getId(), vspDetails.getVersion(),
103           new ValidationStructureList(toscaTreeManager.getTree()));
104       return;
105     }
106
107     HeatStructureTree tree = toscaTreeManager.getTree();
108
109     Map<String, String> componentsQuestionnaire = new HashMap<>();
110     Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
111     Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
112     Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
113     Map<String, ProcessEntity> processArtifact = new HashMap<>();
114     OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
115     orchestrationUtil.backupComponentsQuestionnaireBeforeDelete(vspDetails.getId(),
116         vspDetails.getVersion(), componentsQuestionnaire,
117         componentNicsQuestionnaire, componentMibList, processes, processArtifact);
118
119     Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
120         .fetchZipFileByteArrayInputStream(vspDetails.getId(), candidateData, null,
121             OnboardingTypesEnum.CSAR, errors);
122
123     orchestrationUtil.deleteUploadDataAndContent(vspDetails.getId(), vspDetails.getVersion());
124     zipByteArrayInputStream.ifPresent(byteArrayInputStream -> orchestrationUtil
125             .saveUploadData(vspDetails, candidateData, byteArrayInputStream,
126             fileContentHandler, tree));
127
128     ToscaServiceModel toscaServiceModel = new ToscaConverterImpl().convert(fileContentHandler);
129     orchestrationUtil.saveServiceModel(vspDetails.getId(),
130             vspDetails.getVersion(), toscaServiceModel,
131         toscaServiceModel);
132     candidateService
133         .deleteOrchestrationTemplateCandidate(vspDetails.getId(), vspDetails.getVersion());
134   }
135
136   private void addFiles(FileContentHandler fileContentHandler) {
137     for (Map.Entry<String, byte[]> fileEntry : fileContentHandler.getFiles().entrySet()) {
138       toscaTreeManager.addFile(fileEntry.getKey(), fileEntry.getValue());
139     }
140   }
141
142   private Map<String, List<ErrorMessage>> validateCsar(FileContentHandler fileContentHandler) {
143     Map<String, List<ErrorMessage>> errors = new HashMap<>();
144     addFiles(fileContentHandler);
145     toscaTreeManager.createTree();
146     toscaTreeManager.addErrors(errors);
147     //todo - add tosca validation here to the existing validation framework
148     return errors;
149   }
150
151   private boolean isValid(Map<String, List<ErrorMessage>> errors) {
152     return MapUtils.isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, errors));
153   }
154 }