Added oparent to sdc main
[sdc.git] / openecomp-be / lib / openecomp-healing-lib / openecomp-sdc-healing-impl / src / main / java / org / openecomp / sdc / healing / healers / ToscaServiceModelHealer.java
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
21 package org.openecomp.sdc.healing.healers;
22
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;
42
43 import java.io.IOException;
44 import java.util.Objects;
45 import java.util.Optional;
46
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";
52
53   public ToscaServiceModelHealer() {
54     this.serviceModelDao = ServiceModelDaoFactory.getInstance().createInterface();
55     this.orchestrationTemplateDao = OrchestrationTemplateDaoFactory.getInstance().createInterface();
56   }
57
58   public ToscaServiceModelHealer(
59       ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao,
60       OrchestrationTemplateDao orchestrationTemplateDao) {
61     this.serviceModelDao = serviceModelDao;
62     this.orchestrationTemplateDao = orchestrationTemplateDao;
63   }
64
65   @Override
66   public boolean isHealingNeeded(String itemId, Version version) {
67     OrchestrationTemplateEntity orchestrationTemplate =
68         orchestrationTemplateDao.get(itemId, version);
69     OnboardingTypesEnum onboardingTypes =
70         OnboardingTypesEnum.getOnboardingTypesEnum(orchestrationTemplate.getFileSuffix());
71
72     return Objects.nonNull(onboardingTypes) &&
73         Objects.nonNull(orchestrationTemplate.getContentData());
74   }
75
76   @Override
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());
82
83     Optional<ToscaServiceModel> healedServiceModel =
84         healServiceModel(orchestrationTemplateEntity, type);
85
86     healedServiceModel.ifPresent(serviceModel -> serviceModelDao
87         .overrideServiceModel(itemId, version, serviceModel));
88   }
89
90   private Optional<ToscaServiceModel> healServiceModel(
91       OrchestrationTemplateEntity orchestrationTemplateEntity,
92       OnboardingTypesEnum type) throws IOException {
93     switch (type) {
94       case ZIP:
95         return Optional.of(healServiceModelFromZip(
96             getFileContentHandlerForHealing(orchestrationTemplateEntity, type)));
97
98       case CSAR:
99         return Optional.of(healServiceModelFromCsar(
100             getFileContentHandlerForHealing(orchestrationTemplateEntity, type)));
101
102       default:
103         return Optional.empty();
104     }
105   }
106
107   private FileContentHandler getFileContentHandlerForHealing(
108       OrchestrationTemplateEntity orchestrationTemplateEntity, OnboardingTypesEnum type)
109       throws IOException {
110     byte[] uploadedFileContent = orchestrationTemplateEntity.getContentData().array();
111     return CommonUtil.validateAndUploadFileContent(type, uploadedFileContent);
112   }
113
114   private ToscaServiceModel healServiceModelFromZip(FileContentHandler contentMap) {
115     TranslatorOutput translatorOutput =
116         HeatToToscaUtil.loadAndTranslateTemplateData(contentMap);
117
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);
123     }
124
125     return translatorOutput.getToscaServiceModel();
126   }
127
128   private boolean areThereValidationErrors(TranslatorOutput translatorOutput) {
129     return MapUtils.isNotEmpty(MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR,
130         translatorOutput.getErrorMessages()));
131   }
132
133   private ToscaServiceModel healServiceModelFromCsar(FileContentHandler contentMap)
134       throws IOException {
135     ToscaConverter toscaConverter = ToscaConverterFactory.getInstance().createInterface();
136     return toscaConverter.convert(contentMap);
137   }
138 }