9f29d9316b0a0863db7a813380e3825ba29d8d14
[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.CollectionUtils;
20 import org.apache.commons.collections4.MapUtils;
21 import org.openecomp.core.translator.datatypes.TranslatorOutput;
22 import org.openecomp.core.utilities.file.FileContentHandler;
23 import org.openecomp.core.utilities.json.JsonUtil;
24 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
25 import org.openecomp.core.validation.util.MessageContainerUtil;
26 import org.openecomp.sdc.common.errors.Messages;
27 import org.openecomp.sdc.common.utils.SdcCommon;
28 import org.openecomp.sdc.datatypes.error.ErrorLevel;
29 import org.openecomp.sdc.datatypes.error.ErrorMessage;
30 import org.openecomp.sdc.heat.datatypes.structure.HeatStructureTree;
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.translator.services.heattotosca.HeatToToscaUtil;
36 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
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.types.OrchestrationTemplateActionResponse;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
48 import org.openecomp.sdc.vendorsoftwareproduct.utils.VendorSoftwareProductUtils;
49 import org.openecomp.sdc.versioning.dao.types.Version;
50
51 import java.io.ByteArrayInputStream;
52 import java.util.Collection;
53 import java.util.HashMap;
54 import java.util.List;
55 import java.util.Map;
56 import java.util.Optional;
57
58 import static org.openecomp.sdc.logging.messages.AuditMessages.HEAT_VALIDATION_ERROR;
59
60 public class OrchestrationTemplateProcessZipHandler implements OrchestrationTemplateProcessHandler {
61   private static final Logger LOGGER = LoggerFactory.getLogger(OrchestrationTemplateProcessZipHandler.class);
62   private final CandidateService candidateService =
63       CandidateServiceFactory.getInstance().createInterface();
64   private static final String VSP_ID = "VSP id";
65
66   @Override
67   public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
68                                                      OrchestrationTemplateCandidateData candidateData) {
69     String vspId = vspDetails.getId();
70     Version version = vspDetails.getVersion();
71     LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_STARTED + vspId);
72     OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
73     UploadFileResponse uploadFileResponse = new UploadFileResponse();
74     Optional<FileContentHandler> fileContent = OrchestrationUtil
75         .getFileContentMap(OnboardingTypesEnum.ZIP, uploadFileResponse,
76             candidateData.getContentData().array());
77     if (!fileContent.isPresent()) {
78       response.addStructureErrors(uploadFileResponse.getErrors());
79       response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList, vspId,
80           HEAT_VALIDATION_ERROR));
81       return response;
82     }
83
84     Map<String, List<ErrorMessage>> uploadErrors = uploadFileResponse.getErrors();
85     FileContentHandler fileContentMap = fileContent.get();
86     FilesDataStructure structure =
87         JsonUtil.json2Object(candidateData.getFilesDataStructure(), FilesDataStructure.class);
88
89     if (CollectionUtils.isNotEmpty(structure.getUnassigned())) {
90       response.addErrorMessageToMap(SdcCommon.UPLOAD_FILE,
91           Messages.FOUND_UNASSIGNED_FILES.getErrorMessage(), ErrorLevel.ERROR);
92       response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList, vspId,
93           HEAT_VALIDATION_ERROR));
94       return response;
95     }
96
97
98     String manifest = candidateService.createManifest(vspDetails, structure);
99     fileContentMap.addFile(SdcCommon.MANIFEST_NAME, manifest.getBytes());
100
101     Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
102         .fetchZipFileByteArrayInputStream(
103             vspId, candidateData, manifest, OnboardingTypesEnum.ZIP, uploadErrors);
104     if (!zipByteArrayInputStream.isPresent()) {
105       response.getErrors().values()
106           .forEach(errorList -> printAuditForErrors(errorList, vspId, HEAT_VALIDATION_ERROR));
107       return response;
108     }
109
110     HeatStructureTree tree = createAndValidateHeatTree(response, fileContentMap);
111
112     Map<String, String> componentsQuestionnaire = new HashMap<>();
113     Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
114     Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
115     Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
116     Map<String, ProcessEntity> processArtifact = new HashMap<>();
117
118     OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
119     Map<String, String> vspComponentIdNameInfoBeforeProcess =
120         orchestrationUtil.getVspComponentIdNameInfo(vspId, version);
121     Collection<ComponentDependencyModelEntity> componentDependenciesBeforeDelete =
122         orchestrationUtil.getComponentDependenciesBeforeDelete(vspId, version);
123     orchestrationUtil
124         .backupComponentsQuestionnaireBeforeDelete(vspId, version, componentsQuestionnaire,
125             componentNicsQuestionnaire, componentMibList, processes, processArtifact);
126
127     orchestrationUtil.deleteUploadDataAndContent(vspId, version);
128     orchestrationUtil
129         .saveUploadData(vspDetails, candidateData, zipByteArrayInputStream.get(), fileContentMap,
130             tree);
131
132     response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList, vspId,
133         HEAT_VALIDATION_ERROR));
134     if (MapUtils
135         .isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, response.getErrors()))) {
136       LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_COMPLETED + vspId);
137     }
138
139     LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_TRANSLATION_STARTED + vspId);
140
141     TranslatorOutput translatorOutput =
142         HeatToToscaUtil.loadAndTranslateTemplateData(fileContentMap);
143
144     ToscaServiceModel toscaServiceModel = translatorOutput.getToscaServiceModel();
145     orchestrationUtil
146         .saveServiceModel(vspId, version, translatorOutput.getNonUnifiedToscaServiceModel(),
147             toscaServiceModel);
148     orchestrationUtil.retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire,
149         componentNicsQuestionnaire, componentMibList, processes, processArtifact);
150     orchestrationUtil.updateVspComponentDependencies(vspId, version,
151         vspComponentIdNameInfoBeforeProcess, componentDependenciesBeforeDelete);
152
153     LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_TRANSLATION_COMPLETED + vspId);
154     uploadFileResponse.addStructureErrors(uploadErrors);
155     return response;
156   }
157
158   private HeatStructureTree createAndValidateHeatTree(OrchestrationTemplateActionResponse response,
159                                                       FileContentHandler fileContentMap) {
160     VendorSoftwareProductUtils.addFileNamesToUploadFileResponse(fileContentMap, response);
161     Map<String, List<ErrorMessage>> validationErrors =
162         ValidationManagerUtil.initValidationManager(fileContentMap).validate();
163     response.getErrors().putAll(validationErrors);
164
165     return OrchestrationUtil.createHeatTree(fileContentMap, validationErrors);
166   }
167
168   private void printAuditForErrors(List<ErrorMessage> errorList, String vspId, String auditType) {
169
170     errorList.forEach(errorMessage -> {
171       if (errorMessage.getLevel().equals(ErrorLevel.ERROR)) {
172         LOGGER.audit(
173             AuditMessages.AUDIT_MSG + String.format(auditType, errorMessage.getMessage(), vspId));
174       }
175     });
176   }
177 }