2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.healing.healers;
23 import org.apache.commons.collections4.MapUtils;
24 import org.openecomp.core.converter.ToscaConverter;
25 import org.openecomp.core.converter.factory.ToscaConverterFactory;
26 import org.openecomp.core.model.dao.ServiceModelDao;
27 import org.openecomp.core.model.dao.ServiceModelDaoFactory;
28 import org.openecomp.core.model.types.ServiceElement;
29 import org.openecomp.core.translator.datatypes.TranslatorOutput;
30 import org.openecomp.core.utilities.file.FileContentHandler;
31 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
32 import org.openecomp.core.validation.util.MessageContainerUtil;
33 import org.openecomp.sdc.common.utils.CommonUtil;
34 import org.openecomp.sdc.datatypes.error.ErrorLevel;
35 import org.openecomp.sdc.healing.interfaces.Healer;
36 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
37 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDaoFactory;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.OrchestrationTemplateEntity;
41 import org.openecomp.sdc.versioning.dao.types.Version;
43 import java.io.IOException;
44 import java.util.Objects;
45 import java.util.Optional;
47 public class ToscaServiceModelHealer implements Healer {
48 private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao;
49 private OrchestrationTemplateDao orchestrationTemplateDao;
50 private static final String VALIDATION_FAILURE_MESSAGE = "Product was updated. Please " +
51 "update the uploaded Heat file according to these validation errors: \n";
53 public ToscaServiceModelHealer() {
54 this.serviceModelDao = ServiceModelDaoFactory.getInstance().createInterface();
55 this.orchestrationTemplateDao = OrchestrationTemplateDaoFactory.getInstance().createInterface();
58 public ToscaServiceModelHealer(
59 ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao,
60 OrchestrationTemplateDao orchestrationTemplateDao) {
61 this.serviceModelDao = serviceModelDao;
62 this.orchestrationTemplateDao = orchestrationTemplateDao;
66 public boolean isHealingNeeded(String itemId, Version version) {
67 OrchestrationTemplateEntity orchestrationTemplate =
68 orchestrationTemplateDao.get(itemId, version);
69 OnboardingTypesEnum onboardingTypes =
70 OnboardingTypesEnum.getOnboardingTypesEnum(orchestrationTemplate.getFileSuffix());
72 return Objects.nonNull(onboardingTypes) &&
73 Objects.nonNull(orchestrationTemplate.getContentData());
77 public void heal(String itemId, Version version) throws Exception {
78 OrchestrationTemplateEntity orchestrationTemplateEntity =
79 orchestrationTemplateDao.get(itemId, version);
80 OnboardingTypesEnum type =
81 OnboardingTypesEnum.getOnboardingTypesEnum(orchestrationTemplateEntity.getFileSuffix());
83 Optional<ToscaServiceModel> healedServiceModel =
84 healServiceModel(orchestrationTemplateEntity, type);
86 healedServiceModel.ifPresent(serviceModel -> serviceModelDao
87 .overrideServiceModel(itemId, version, serviceModel));
90 private Optional<ToscaServiceModel> healServiceModel(
91 OrchestrationTemplateEntity orchestrationTemplateEntity,
92 OnboardingTypesEnum type) throws IOException {
95 return Optional.of(healServiceModelFromZip(
96 getFileContentHandlerForHealing(orchestrationTemplateEntity, type)));
99 return Optional.of(healServiceModelFromCsar(
100 getFileContentHandlerForHealing(orchestrationTemplateEntity, type)));
103 return Optional.empty();
107 private FileContentHandler getFileContentHandlerForHealing(
108 OrchestrationTemplateEntity orchestrationTemplateEntity, OnboardingTypesEnum type)
110 byte[] uploadedFileContent = orchestrationTemplateEntity.getContentData().array();
111 return CommonUtil.validateAndUploadFileContent(type, uploadedFileContent);
114 private ToscaServiceModel healServiceModelFromZip(FileContentHandler contentMap) {
115 TranslatorOutput translatorOutput =
116 HeatToToscaUtil.loadAndTranslateTemplateData(contentMap);
118 if (areThereValidationErrors(translatorOutput)) {
119 String validationErrorsAsString = MessageContainerUtil.getErrorMessagesListAsString
120 (MessageContainerUtil
121 .getMessageByLevel(ErrorLevel.ERROR, translatorOutput.getErrorMessages()));
122 throw new RuntimeException(VALIDATION_FAILURE_MESSAGE + validationErrorsAsString);
125 return translatorOutput.getToscaServiceModel();
128 private boolean areThereValidationErrors(TranslatorOutput translatorOutput) {
129 return MapUtils.isNotEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR,
130 translatorOutput.getErrorMessages()));
133 private ToscaServiceModel healServiceModelFromCsar(FileContentHandler contentMap)
135 ToscaConverter toscaConverter = ToscaConverterFactory.getInstance().createInterface();
136 return toscaConverter.convert(contentMap);