[SDC] Onboarding 1710 rebase.
[sdc.git] / openecomp-be / lib / openecomp-healing-lib / openecomp-sdc-healing-impl / src / main / java / org / openecomp / sdc / healing / healers / CompositionDataHealer.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.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.ComputeDao;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDaoFactory;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
41 import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDaoFactory;
42 import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDao;
43 import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDaoFactory;
44 import org.openecomp.sdc.vendorsoftwareproduct.dao.NetworkDao;
45 import org.openecomp.sdc.vendorsoftwareproduct.dao.NetworkDaoFactory;
46 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
47 import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDaoFactory;
48 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
49 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDaoFactory;
50 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
51 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
52 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.DeploymentFlavorEntity;
53 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity;
54 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
55 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
56 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadDataEntity;
57 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionDataExtractorFactory;
58 import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionEntityDataManagerFactory;
59 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
60 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
61 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Component;
62 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
63 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionData;
64 import org.openecomp.sdc.versioning.dao.types.Version;
65
66 import java.io.IOException;
67 import java.util.Collection;
68 import java.util.Map;
69 import java.util.Objects;
70 import java.util.Optional;
71
72 public class CompositionDataHealer implements Healer {
73   private static final Version VERSION00 = new Version(0, 0);
74   private static final Version VERSION01 = new Version(0, 1);
75   private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
76
77   private static final OrchestrationTemplateDao orchestrationTemplateDataDao =
78       OrchestrationTemplateDaoFactory.getInstance().createInterface();
79
80   private static ComponentDao componentDao = ComponentDaoFactory.getInstance().createInterface();
81   private static NicDao nicDao = NicDaoFactory.getInstance().createInterface();
82   private static NetworkDao networkDao = NetworkDaoFactory.getInstance().createInterface();
83   private static ComputeDao computeDao = ComputeDaoFactory.getInstance().createInterface();
84   private static DeploymentFlavorDao deloymentFlavorDao = DeploymentFlavorDaoFactory.getInstance()
85       .createInterface();
86   private static ImageDao imageDao = ImageDaoFactory.getInstance().createInterface();
87
88   private static final ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao =
89       ServiceModelDaoFactory.getInstance().createInterface();
90   private static CompositionDataExtractor compositionDataExtractor =
91       CompositionDataExtractorFactory.getInstance().createInterface();
92   private static CompositionEntityDataManager compositionEntityDataManager =
93       CompositionEntityDataManagerFactory.getInstance().createInterface();
94
95   public CompositionDataHealer() {
96   }
97
98   @Override
99   public Optional<CompositionData> heal(Map<String, Object> healingParams) throws IOException {
100     mdcDataDebugMessage.debugEntryMessage(null);
101
102     String vspId = (String) healingParams.get(SdcCommon.VSP_ID);
103     Version version = VERSION00.equals(healingParams.get(SdcCommon.VERSION))
104         ? VERSION01
105         : (Version) healingParams.get(SdcCommon.VERSION);
106
107     Collection<ComponentEntity> componentEntities =
108         componentDao.list(new ComponentEntity(vspId, version, null));
109     Collection<NicEntity> nicEntities = nicDao.listByVsp(vspId, version);
110     Collection<NetworkEntity> networkEntities =
111         networkDao.list(new NetworkEntity(vspId, version, null));
112
113     Optional<ToscaServiceModel> serviceModelForHealing = getServiceModelForHealing(vspId, version);
114     CompositionData compositionData = null;
115     if (!doesVspNeedCompositionDataHealing(vspId, version, componentEntities, networkEntities,
116         nicEntities)) {
117       updateComponentsDisplayNames(componentEntities);
118       mdcDataDebugMessage.debugExitMessage(null, null);
119       //return Optional.empty();
120     } else {
121       if (!serviceModelForHealing.isPresent()) {
122         mdcDataDebugMessage.debugExitMessage(null, null);
123         return Optional.empty();
124       }
125       compositionData = healCompositionData(vspId, version, serviceModelForHealing);
126     }
127     compositionData =
128         getCompositionDataForHealing(vspId, version, serviceModelForHealing.get());
129     HealNfodData(vspId, version, compositionData);
130     mdcDataDebugMessage.debugExitMessage(null, null);
131     return Optional.of(compositionData);
132   }
133
134   private boolean doesVspNeedCompositionDataHealing(String vspId, Version version,
135                                                     Collection<ComponentEntity> componentEntities,
136                                                     Collection<NetworkEntity> networkEntities,
137                                                     Collection<NicEntity> nicEntities) {
138
139     return (CollectionUtils.isEmpty(componentEntities) && CollectionUtils.isEmpty(nicEntities) &&
140         CollectionUtils.isEmpty(networkEntities) );
141
142 //    mdcDataDebugMessage.debugEntryMessage(null, null);
143 //
144 ////    ToscaServiceModel toscaServiceModel;
145 //
146 //    ByteBuffer contentData = uploadData.getContentData();
147 //    FileContentHandler fileContentHandler = CommonUtil.validateAndUploadFileContent(uploadData
148 //        .getContentData().array());
149 //
150 //
151 //
152 //    TranslatorOutput translatorOutput =
153 //        HeatToToscaUtil.loadAndTranslateTemplateData(fileContentHandler);
154 //    ToscaServiceModel toscaServiceModel = translatorOutput.getToscaServiceModel();
155 //
156 ////    toscaServiceModel = enrichedServiceModelDao.getServiceModel(vspId, version);
157 //
158 //    mdcDataDebugMessage.debugExitMessage(null, null);
159 //    return toscaServiceModel;
160
161   }
162
163   private void HealNfodData(String vspId, Version version, CompositionData compositionData) {
164     Collection<ComponentEntity> componentEntities;
165     /*componentEntities =
166         vendorSoftwareProductDao.listComponents(vspId, version);*/
167     componentEntities = componentDao.list(new ComponentEntity(vspId, version, null));
168
169     /*Collection<ComputeEntity> computeEntities=vendorSoftwareProductDao.listComputesByVsp(vspId,
170         version);
171     Collection<ImageEntity> imageEntities =vendorSoftwareProductDao.listImagesByVsp(vspId, version);
172     Collection<DeploymentFlavorEntity> deploymentFlavorEntities =vendorSoftwareProductDao
173         .listDeploymentFlavors(vspId, version);*/
174
175     Collection<ComputeEntity> computeEntities = computeDao.listByVsp(vspId, version);
176     Collection<ImageEntity> imageEntities = imageDao.listByVsp(vspId, version);
177     Collection<DeploymentFlavorEntity> deploymentFlavorEntities = deloymentFlavorDao.list(new
178         DeploymentFlavorEntity(vspId, version, null));
179
180     if (CollectionUtils.isEmpty(computeEntities) && CollectionUtils.isEmpty(imageEntities)) {
181       for (Component component : compositionData.getComponents()) {
182         String componentId = null;
183         for (ComponentEntity componentEntity:componentEntities) {
184           if (componentEntity.getComponentCompositionData().getName().equals(component.getData()
185               .getName())) {
186             componentId = componentEntity.getId();
187             break;
188           }
189         }
190         compositionEntityDataManager.saveComputesFlavorByComponent(vspId,version,component,
191             componentId);
192         compositionEntityDataManager.saveImagesByComponent(vspId,version,component,
193             componentId);
194       }
195
196     }
197
198     if (CollectionUtils.isEmpty(deploymentFlavorEntities)) {
199       compositionEntityDataManager.saveDeploymentFlavors(vspId,version,compositionData);
200     }
201   }
202
203   private CompositionData healCompositionData(String vspId, Version version,
204                                               Optional<ToscaServiceModel> serviceModelForHealing) {
205     ToscaServiceModel toscaServiceModel = serviceModelForHealing.get();
206     CompositionData compositionData =
207         getCompositionDataForHealing(vspId, version, toscaServiceModel);
208     compositionEntityDataManager.saveCompositionData(vspId, version, compositionData);
209     return compositionData;
210   }
211
212   private boolean doesVspNeedCompositionDataHealing(Collection<ComponentEntity> componentEntities,
213                                                     Collection<NetworkEntity> networkEntities,
214                                                     Collection<NicEntity> nicEntities) {
215
216     return (CollectionUtils.isEmpty(componentEntities) && CollectionUtils.isEmpty(nicEntities) &&
217         CollectionUtils.isEmpty(networkEntities));
218   }
219
220   private CompositionData getCompositionDataForHealing(String vspId, Version version,
221                                                        ToscaServiceModel toscaServiceModel) {
222     mdcDataDebugMessage.debugEntryMessage(null);
223
224     if (Objects.isNull(toscaServiceModel)) {
225       return null;
226     }
227
228     CompositionData compositionData = new CompositionData();
229     if (Objects.nonNull(toscaServiceModel)) {
230       compositionData = compositionDataExtractor
231           .extractServiceCompositionData(toscaServiceModel);
232       serviceModelDao.storeServiceModel(vspId, version, toscaServiceModel);
233     }
234
235     mdcDataDebugMessage.debugExitMessage(null);
236     return compositionData;
237   }
238
239   private void updateComponentsDisplayNames(Collection<ComponentEntity> componentEntities) {
240     if (CollectionUtils.isEmpty(componentEntities)) {
241       return;
242     }
243
244     for (ComponentEntity component : componentEntities) {
245       updateComponentName(component);
246       componentDao.update(component);
247     }
248   }
249
250   private void updateComponentName(ComponentEntity component) {
251     mdcDataDebugMessage.debugEntryMessage("VSP id, component id", component.getVspId(), component
252         .getId());
253
254     ComponentData componentData =
255         JsonUtil.json2Object(component.getCompositionData(), ComponentData.class);
256     componentData
257         .setDisplayName(compositionDataExtractor.getComponentDisplayName(componentData.getName()));
258     String displayName = componentData.getDisplayName();
259     componentData.setName(componentData.getName().replace("com.att.d2", "org.openecomp"));
260     componentData.setVfcCode(displayName);
261     component.setCompositionData(JsonUtil.object2Json(componentData));
262
263     mdcDataDebugMessage.debugExitMessage("VSP id, component id", component.getVspId(), component
264         .getId());
265
266   }
267
268   private Optional<ToscaServiceModel> getServiceModelForHealing(String vspId, Version version)
269       throws IOException {
270     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
271
272     /*UploadDataEntity uploadData =
273         vendorSoftwareProductDao.getUploadData(new UploadDataEntity(vspId, version));*/
274     UploadDataEntity uploadData =
275         orchestrationTemplateDataDao.getOrchestrationTemplate(vspId, version);
276
277     if (Objects.isNull(uploadData) || Objects.isNull(uploadData.getContentData())) {
278       return Optional.empty();
279     }
280
281     TranslatorOutput translatorOutput = getTranslatorOutputForHealing(uploadData);
282
283     if (Objects.isNull(translatorOutput)) {
284       return Optional.empty();
285     }
286
287     try {
288       serviceModelDao.storeServiceModel(vspId, version,
289           translatorOutput.getToscaServiceModel());
290     } catch (Exception e) {
291       return Optional.empty();
292     }
293
294     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
295     return Optional.of(translatorOutput.getToscaServiceModel());
296   }
297
298   private TranslatorOutput getTranslatorOutputForHealing(UploadDataEntity uploadData) {
299
300     FileContentHandler fileContentHandler;
301     try {
302       fileContentHandler =
303           CommonUtil.validateAndUploadFileContent(uploadData.getContentData().array());
304       return HeatToToscaUtil.loadAndTranslateTemplateData(fileContentHandler);
305     } catch (Exception e) {
306       return null;
307     }
308   }
309 }