13728b0c72754fd3eabd180e7dc7ff2997f74984
[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.context.impl.MdcDataDebugMessage;
34 import org.openecomp.sdc.logging.messages.AuditMessages;
35 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
36 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
37 import org.openecomp.sdc.validation.util.ValidationManagerUtil;
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 static final MdcDataDebugMessage MDC_DATA_DEBUG_MESSAGE = new MdcDataDebugMessage();
63   private final CandidateService candidateService =
64       CandidateServiceFactory.getInstance().createInterface();
65   private static final String VSP_ID = "VSP id";
66
67   @Override
68   public OrchestrationTemplateActionResponse process(VspDetails vspDetails,
69                                                      OrchestrationTemplateCandidateData candidateData) {
70     String vspId = vspDetails.getId();
71     Version version = vspDetails.getVersion();
72     LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_STARTED + vspId);
73     OrchestrationTemplateActionResponse response = new OrchestrationTemplateActionResponse();
74     UploadFileResponse uploadFileResponse = new UploadFileResponse();
75     Optional<FileContentHandler> fileContent = OrchestrationUtil
76         .getFileContentMap(OnboardingTypesEnum.ZIP, uploadFileResponse,
77             candidateData.getContentData().array());
78     if (!fileContent.isPresent()) {
79       response.addStructureErrors(uploadFileResponse.getErrors());
80       MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
81       response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList, vspId,
82           HEAT_VALIDATION_ERROR));
83       return response;
84     }
85
86     Map<String, List<ErrorMessage>> uploadErrors = uploadFileResponse.getErrors();
87     FileContentHandler fileContentMap = fileContent.get();
88     FilesDataStructure structure =
89         JsonUtil.json2Object(candidateData.getFilesDataStructure(), FilesDataStructure.class);
90
91     if (CollectionUtils.isNotEmpty(structure.getUnassigned())) {
92       response.addErrorMessageToMap(SdcCommon.UPLOAD_FILE,
93           Messages.FOUND_UNASSIGNED_FILES.getErrorMessage(), ErrorLevel.ERROR);
94
95       MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
96       response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList, vspId,
97           HEAT_VALIDATION_ERROR));
98       return response;
99     }
100
101
102     String manifest = candidateService.createManifest(vspDetails, structure);
103     fileContentMap.addFile(SdcCommon.MANIFEST_NAME, manifest.getBytes());
104
105     Optional<ByteArrayInputStream> zipByteArrayInputStream = candidateService
106         .fetchZipFileByteArrayInputStream(
107             vspId, candidateData, manifest, OnboardingTypesEnum.ZIP, uploadErrors);
108     if (!zipByteArrayInputStream.isPresent()) {
109       response.getErrors().values()
110           .forEach(errorList -> printAuditForErrors(errorList, vspId, HEAT_VALIDATION_ERROR));
111       return response;
112     }
113
114     HeatStructureTree tree = createAndValidateHeatTree(response, fileContentMap);
115
116     Map<String, String> componentsQuestionnaire = new HashMap<>();
117     Map<String, Map<String, String>> componentNicsQuestionnaire = new HashMap<>();
118     Map<String, Collection<ComponentMonitoringUploadEntity>> componentMibList = new HashMap<>();
119     Map<String, Collection<ProcessEntity>> processes = new HashMap<>();
120     Map<String, ProcessEntity> processArtifact = new HashMap<>();
121
122     OrchestrationUtil orchestrationUtil = new OrchestrationUtil();
123     Map<String, String> vspComponentIdNameInfoBeforeProcess =
124         orchestrationUtil.getVspComponentIdNameInfo(vspId, version);
125     orchestrationUtil
126         .backupComponentsQuestionnaireBeforeDelete(vspId, version, componentsQuestionnaire,
127             componentNicsQuestionnaire, componentMibList, processes, processArtifact);
128
129     orchestrationUtil.deleteUploadDataAndContent(vspId, version);
130     orchestrationUtil
131         .saveUploadData(vspDetails, candidateData, zipByteArrayInputStream.get(), fileContentMap,
132             tree);
133
134     response.getErrors().values().forEach(errorList -> printAuditForErrors(errorList, vspId,
135         HEAT_VALIDATION_ERROR));
136     if (MapUtils
137         .isEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, response.getErrors()))) {
138       LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_VALIDATION_COMPLETED + vspId);
139     }
140
141     LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_TRANSLATION_STARTED + vspId);
142
143     TranslatorOutput translatorOutput =
144         HeatToToscaUtil.loadAndTranslateTemplateData(fileContentMap);
145
146     ToscaServiceModel toscaServiceModel = translatorOutput.getToscaServiceModel();
147     orchestrationUtil
148         .saveServiceModel(vspId, version, translatorOutput.getNonUnifiedToscaServiceModel(),
149             toscaServiceModel);
150     orchestrationUtil.retainComponentQuestionnaireData(vspId, version, componentsQuestionnaire,
151         componentNicsQuestionnaire, componentMibList, processes, processArtifact);
152     orchestrationUtil.updateVspComponentDependencies(vspId, version,
153         vspComponentIdNameInfoBeforeProcess);
154
155     LOGGER.audit(AuditMessages.AUDIT_MSG + AuditMessages.HEAT_TRANSLATION_COMPLETED + vspId);
156     uploadFileResponse.addStructureErrors(uploadErrors);
157
158     MDC_DATA_DEBUG_MESSAGE.debugExitMessage(VSP_ID, vspId);
159     return response;
160   }
161
162   private HeatStructureTree createAndValidateHeatTree(OrchestrationTemplateActionResponse response,
163                                                       FileContentHandler fileContentMap) {
164     VendorSoftwareProductUtils.addFileNamesToUploadFileResponse(fileContentMap, response);
165     Map<String, List<ErrorMessage>> validationErrors =
166         ValidationManagerUtil.initValidationManager(fileContentMap).validate();
167     response.getErrors().putAll(validationErrors);
168
169     return OrchestrationUtil.createHeatTree(fileContentMap, validationErrors);
170   }
171
172   private void printAuditForErrors(List<ErrorMessage> errorList, String vspId, String auditType) {
173
174     errorList.forEach(errorMessage -> {
175       if (errorMessage.getLevel().equals(ErrorLevel.ERROR)) {
176         LOGGER.audit(
177             AuditMessages.AUDIT_MSG + String.format(auditType, errorMessage.getMessage(), vspId));
178       }
179     });
180   }
181 }