b65629002a5ba05f868e96c3ea4185017985f083
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.healing.healers;
21
22 import java.io.IOException;
23 import java.util.Objects;
24 import java.util.Optional;
25 import org.apache.commons.collections4.MapUtils;
26 import org.openecomp.core.converter.ToscaConverter;
27 import org.openecomp.core.converter.factory.ToscaConverterFactory;
28 import org.openecomp.core.model.dao.ServiceModelDao;
29 import org.openecomp.core.model.dao.ServiceModelDaoFactory;
30 import org.openecomp.core.model.types.ServiceElement;
31 import org.openecomp.core.translator.datatypes.TranslatorOutput;
32 import org.openecomp.core.utilities.file.FileContentHandler;
33 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
34 import org.openecomp.core.validation.util.MessageContainerUtil;
35 import org.openecomp.sdc.common.utils.CommonUtil;
36 import org.openecomp.sdc.datatypes.error.ErrorLevel;
37 import org.openecomp.sdc.healing.interfaces.Healer;
38 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
39 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDaoFactory;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateEntity;
43 import org.openecomp.sdc.versioning.dao.types.Version;
44
45 public class ToscaServiceModelHealer implements Healer {
46
47     private static final String VALIDATION_FAILURE_MESSAGE =
48         "Product was updated. Please " + "update the uploaded Heat file according to these validation errors: \n";
49     private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao;
50     private OrchestrationTemplateDao orchestrationTemplateDao;
51
52     public ToscaServiceModelHealer() {
53         this.serviceModelDao = ServiceModelDaoFactory.getInstance().createInterface();
54         this.orchestrationTemplateDao = OrchestrationTemplateDaoFactory.getInstance().createInterface();
55     }
56
57     public ToscaServiceModelHealer(ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao,
58                                    OrchestrationTemplateDao orchestrationTemplateDao) {
59         this.serviceModelDao = serviceModelDao;
60         this.orchestrationTemplateDao = orchestrationTemplateDao;
61     }
62
63     @Override
64     public boolean isHealingNeeded(String itemId, Version version) {
65         OrchestrationTemplateEntity orchestrationTemplate = orchestrationTemplateDao.get(itemId, version);
66         OnboardingTypesEnum onboardingTypes = OnboardingTypesEnum.getOnboardingTypesEnum(orchestrationTemplate.getFileSuffix());
67         return Objects.nonNull(onboardingTypes) && Objects.nonNull(orchestrationTemplate.getContentData());
68     }
69
70     @Override
71     public void heal(String itemId, Version version) throws Exception {
72         OrchestrationTemplateEntity orchestrationTemplateEntity = orchestrationTemplateDao.get(itemId, version);
73         OnboardingTypesEnum type = OnboardingTypesEnum.getOnboardingTypesEnum(orchestrationTemplateEntity.getFileSuffix());
74         Optional<ToscaServiceModel> healedServiceModel = healServiceModel(orchestrationTemplateEntity, type);
75         healedServiceModel.ifPresent(serviceModel -> serviceModelDao.overrideServiceModel(itemId, version, serviceModel));
76     }
77
78     private Optional<ToscaServiceModel> healServiceModel(OrchestrationTemplateEntity orchestrationTemplateEntity, OnboardingTypesEnum type)
79         throws IOException {
80         switch (type) {
81             case ZIP:
82                 return Optional.of(healServiceModelFromZip(getFileContentHandlerForHealing(orchestrationTemplateEntity, type)));
83             case CSAR:
84                 return Optional.of(healServiceModelFromCsar(getFileContentHandlerForHealing(orchestrationTemplateEntity, type)));
85             default:
86                 return Optional.empty();
87         }
88     }
89
90     private FileContentHandler getFileContentHandlerForHealing(OrchestrationTemplateEntity orchestrationTemplateEntity, OnboardingTypesEnum type)
91         throws IOException {
92         byte[] uploadedFileContent = orchestrationTemplateEntity.getContentData().array();
93         return CommonUtil.validateAndUploadFileContent(type, uploadedFileContent);
94     }
95
96     private ToscaServiceModel healServiceModelFromZip(FileContentHandler contentMap) {
97         TranslatorOutput translatorOutput = HeatToToscaUtil.loadAndTranslateTemplateData(contentMap);
98         if (areThereValidationErrors(translatorOutput)) {
99             String validationErrorsAsString = MessageContainerUtil
100                 .getErrorMessagesListAsString(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, translatorOutput.getErrorMessages()));
101             throw new RuntimeException(VALIDATION_FAILURE_MESSAGE + validationErrorsAsString);
102         }
103         return translatorOutput.getToscaServiceModel();
104     }
105
106     private boolean areThereValidationErrors(TranslatorOutput translatorOutput) {
107         return MapUtils.isNotEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, translatorOutput.getErrorMessages()));
108     }
109
110     private ToscaServiceModel healServiceModelFromCsar(FileContentHandler contentMap) throws IOException {
111         ToscaConverter toscaConverter = ToscaConverterFactory.getInstance().createInterface();
112         return toscaConverter.convert(contentMap);
113     }
114 }