2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.CollectionUtils;
24 import org.openecomp.core.model.dao.ServiceModelDao;
25 import org.openecomp.core.model.dao.ServiceModelDaoFactory;
26 import org.openecomp.core.model.types.ServiceElement;
27 import org.openecomp.core.translator.datatypes.TranslatorOutput;
28 import org.openecomp.core.utilities.file.FileContentHandler;
29 import org.openecomp.core.utilities.json.JsonUtil;
30 import org.openecomp.sdc.common.utils.CommonUtil;
31 import org.openecomp.sdc.common.utils.SdcCommon;
32 import org.openecomp.sdc.healing.interfaces.Healer;
33 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
34 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
35 import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.NetworkDao;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.NetworkDaoFactory;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDaoFactory;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDaoFactory;
44 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
45 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
46 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
47 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadDataEntity;
48 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionDataExtractorFactory;
49 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionEntityDataManagerFactory;
50 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
51 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
52 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
53 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionData;
54 import org.openecomp.sdc.versioning.dao.types.Version;
56 import java.io.IOException;
57 import java.util.Collection;
59 import java.util.Objects;
60 import java.util.Optional;
62 public class CompositionDataHealer implements Healer {
63 private static final Version VERSION00 = new Version(0, 0);
64 private static final Version VERSION01 = new Version(0, 1);
65 private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
67 private static final OrchestrationTemplateDao orchestrationTemplateDataDao =
68 OrchestrationTemplateDaoFactory.getInstance().createInterface();
70 private static ComponentDao componentDao = ComponentDaoFactory.getInstance().createInterface();
71 private static NicDao nicDao = NicDaoFactory.getInstance().createInterface();
72 private static NetworkDao networkDao = NetworkDaoFactory.getInstance().createInterface();
74 private static final ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao =
75 ServiceModelDaoFactory.getInstance().createInterface();
76 private static CompositionDataExtractor compositionDataExtractor =
77 CompositionDataExtractorFactory.getInstance().createInterface();
78 private static CompositionEntityDataManager compositionEntityDataManager =
79 CompositionEntityDataManagerFactory.getInstance().createInterface();
81 public CompositionDataHealer() {
85 public Optional<CompositionData> heal(Map<String, Object> healingParams) throws IOException {
86 mdcDataDebugMessage.debugEntryMessage(null);
88 String vspId = (String) healingParams.get(SdcCommon.VSP_ID);
89 Version version = VERSION00.equals(healingParams.get(SdcCommon.VERSION))
91 : (Version) healingParams.get(SdcCommon.VERSION);
93 Collection<ComponentEntity> componentEntities =
94 componentDao.list(new ComponentEntity(vspId, version, null));
95 Collection<NicEntity> nicEntities = nicDao.listByVsp(vspId, version);
96 Collection<NetworkEntity> networkEntities =
97 networkDao.list(new NetworkEntity(vspId, version, null));
99 Optional<ToscaServiceModel> serviceModelForHealing = getServiceModelForHealing(vspId, version);
101 if (!doesVspNeedCompositionDataHealing(componentEntities, networkEntities,
103 updateComponentsDisplayNames(componentEntities);
104 mdcDataDebugMessage.debugExitMessage(null);
105 return Optional.empty();
109 if (!serviceModelForHealing.isPresent()) {
110 mdcDataDebugMessage.debugExitMessage(null);
111 return Optional.empty();
114 CompositionData compositionData = healCompositionData(vspId, version, serviceModelForHealing);
116 mdcDataDebugMessage.debugExitMessage(null);
117 return Optional.of(compositionData);
120 private CompositionData healCompositionData(String vspId, Version version,
121 Optional<ToscaServiceModel> serviceModelForHealing) {
122 ToscaServiceModel toscaServiceModel = serviceModelForHealing.get();
123 CompositionData compositionData =
124 getCompositionDataForHealing(vspId, version, toscaServiceModel);
125 compositionEntityDataManager.saveCompositionData(vspId, version, compositionData);
126 return compositionData;
129 private boolean doesVspNeedCompositionDataHealing(Collection<ComponentEntity> componentEntities,
130 Collection<NetworkEntity> networkEntities,
131 Collection<NicEntity> nicEntities) {
133 return (CollectionUtils.isEmpty(componentEntities) && CollectionUtils.isEmpty(nicEntities) &&
134 CollectionUtils.isEmpty(networkEntities));
137 private CompositionData getCompositionDataForHealing(String vspId, Version version,
138 ToscaServiceModel toscaServiceModel) {
139 mdcDataDebugMessage.debugEntryMessage(null);
141 if (Objects.isNull(toscaServiceModel)) {
145 CompositionData compositionData = new CompositionData();
146 if (Objects.nonNull(toscaServiceModel)) {
147 compositionData = compositionDataExtractor
148 .extractServiceCompositionData(toscaServiceModel);
149 serviceModelDao.storeServiceModel(vspId, version, toscaServiceModel);
152 mdcDataDebugMessage.debugExitMessage(null);
153 return compositionData;
156 private void updateComponentsDisplayNames(Collection<ComponentEntity> componentEntities) {
157 if (CollectionUtils.isEmpty(componentEntities)) {
161 for (ComponentEntity component : componentEntities) {
162 updateComponentName(component);
163 componentDao.update(component);
167 private void updateComponentName(ComponentEntity component) {
168 mdcDataDebugMessage.debugEntryMessage("VSP id, component id", component.getVspId(), component
171 ComponentData componentData =
172 JsonUtil.json2Object(component.getCompositionData(), ComponentData.class);
174 .setDisplayName(compositionDataExtractor.getComponentDisplayName(componentData.getName()));
175 String displayName = componentData.getDisplayName();
176 componentData.setName(displayName);
177 componentData.setVfcCode(displayName);
178 component.setCompositionData(JsonUtil.object2Json(componentData));
180 mdcDataDebugMessage.debugExitMessage("VSP id, component id", component.getVspId(), component
185 private Optional<ToscaServiceModel> getServiceModelForHealing(String vspId, Version version)
187 mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
189 /*UploadDataEntity uploadData =
190 vendorSoftwareProductDao.getUploadData(new UploadDataEntity(vspId, version));*/
191 UploadDataEntity uploadData =
192 orchestrationTemplateDataDao.getOrchestrationTemplate(vspId, version);
194 if (Objects.isNull(uploadData) || Objects.isNull(uploadData.getContentData())) {
195 return Optional.empty();
198 TranslatorOutput translatorOutput = getTranslatorOutputForHealing(uploadData);
200 if (Objects.isNull(translatorOutput)) {
201 return Optional.empty();
205 serviceModelDao.storeServiceModel(vspId, version,
206 translatorOutput.getToscaServiceModel());
207 }catch (Exception e){
208 return Optional.empty();
211 mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
212 return Optional.of(translatorOutput.getToscaServiceModel());
215 private TranslatorOutput getTranslatorOutputForHealing(UploadDataEntity uploadData){
217 FileContentHandler fileContentHandler;
220 CommonUtil.loadUploadFileContent(uploadData.getContentData().array());
221 return HeatToToscaUtil.loadAndTranslateTemplateData(fileContentHandler);
222 }catch (Exception e){