1db8875a05385384d347e23283ab043c465bd700
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 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.utilities.file.FileContentHandler;
22 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
23 import org.openecomp.core.validation.util.MessageContainerUtil;
24 import org.openecomp.sdc.common.errors.CoreException;
25 import org.openecomp.sdc.common.errors.ErrorCode;
26 import org.openecomp.sdc.common.errors.GeneralErrorBuilder;
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.services.tree.ToscaTreeManager;
31 import org.openecomp.sdc.logging.api.Logger;
32 import org.openecomp.sdc.logging.api.LoggerFactory;
33 import org.openecomp.sdc.logging.messages.AuditMessages;
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.types.OrchestrationTemplateActionResponse;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
44
45 import java.io.ByteArrayInputStream;
46 import java.io.IOException;
47 import java.util.ArrayList;
48 import java.util.Collection;
49 import java.util.HashMap;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.Optional;
53
54 public class OrchestrationTemplateProcessCsarHandler
55     implements OrchestrationTemplateProcessHandler {
56   private static final Logger LOGGER = LoggerFactory
57           .getLogger(OrchestrationTemplateProcessCsarHandler.class);
58   private final CandidateService candidateService = CandidateServiceFactory
59           .getInstance().createInterface();
60   private final ToscaTreeManager toscaTreeManager = new ToscaTreeManager();
61
62   @Override
63   public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
64                                   OrchestrationTemplateCandidateData candidateData) {
65     LOGGER.audit(
66         AuditMessages.AUDIT_MSG + AuditMessages.CSAR_VALIDATION_STARTED + vspDetails.getId());
67
68     UploadFileResponse uploadFileResponse = new UploadFileResponse();
69     Optional<FileContentHandler> fileContent = OrchestrationUtil
70         .getFileContentMap(OnboardingTypesEnum.CSAR, uploadFileResponse,
71             candidateData.getContentData().array());
72
73     OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
74     if (fileContent.isPresent()) {
75       try {
76         FileContentHandler fileContentHandler = fileContent.get();
77         processCsar(vspDetails, fileContentHandler, candidateData, response);
78       } catch (CoreException e) {
79         LOGGER.error(e.getMessage());
80         response.addErrorMessageToMap(e.code().id(), e.code().message(),ErrorLevel.ERROR);
81       } catch (IOException ioe) {
82         LOGGER.error(ioe.getMessage());
83         ErrorCode errorCode = new GeneralErrorBuilder(ioe.getMessage()).build();
84         response.addErrorMessageToMap(errorCode.id(), errorCode.message(),ErrorLevel.ERROR);
85       }
86     } else {
87       if (!uploadFileResponse.getErrors().isEmpty()) {
88         response.addStructureErrors(uploadFileResponse.getErrors());
89       }
90     }
91     return response;
92   }
93
94   private void processCsar(VspDetails vspDetails,
95                            FileContentHandler fileContentHandler,
96                            OrchestrationTemplateCandidateData candidateData,
97                            OrchestrationTemplateActionResponse response) throws IOException {
98     response.setFileNames(new ArrayList<>(fileContentHandler.getFileList()));
99     Map<String, List<ErrorMessage>> errors = validateCsar(fileContentHandler);
100     if (!isValid(errors)) {
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     ToscaServiceModel toscaServiceModel = new ToscaConverterImpl().convert(fileContentHandler);
126     orchestrationUtil.saveServiceModel(vspDetails.getId(),
127             vspDetails.getVersion(), toscaServiceModel,
128         toscaServiceModel);
129
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 }