[sdc] update code of sdc
[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.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;
55
56 import java.io.IOException;
57 import java.util.Collection;
58 import java.util.Map;
59 import java.util.Objects;
60 import java.util.Optional;
61
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();
66
67   private static final OrchestrationTemplateDao orchestrationTemplateDataDao =
68       OrchestrationTemplateDaoFactory.getInstance().createInterface();
69
70   private static ComponentDao componentDao = ComponentDaoFactory.getInstance().createInterface();
71   private static NicDao nicDao = NicDaoFactory.getInstance().createInterface();
72   private static NetworkDao networkDao = NetworkDaoFactory.getInstance().createInterface();
73
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();
80
81   public CompositionDataHealer() {
82   }
83
84   @Override
85   public Optional<CompositionData> heal(Map<String, Object> healingParams) throws IOException {
86     mdcDataDebugMessage.debugEntryMessage(null);
87
88     String vspId = (String) healingParams.get(SdcCommon.VSP_ID);
89     Version version = VERSION00.equals(healingParams.get(SdcCommon.VERSION))
90         ? VERSION01
91         : (Version) healingParams.get(SdcCommon.VERSION);
92
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));
98
99     Optional<ToscaServiceModel> serviceModelForHealing = getServiceModelForHealing(vspId, version);
100
101     if (!doesVspNeedCompositionDataHealing(componentEntities, networkEntities,
102         nicEntities)) {
103       updateComponentsDisplayNames(componentEntities);
104       mdcDataDebugMessage.debugExitMessage(null);
105       return Optional.empty();
106     }
107
108
109     if (!serviceModelForHealing.isPresent()) {
110       mdcDataDebugMessage.debugExitMessage(null);
111       return Optional.empty();
112     }
113
114     CompositionData compositionData = healCompositionData(vspId, version, serviceModelForHealing);
115
116     mdcDataDebugMessage.debugExitMessage(null);
117     return Optional.of(compositionData);
118   }
119
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;
127   }
128
129   private boolean doesVspNeedCompositionDataHealing(Collection<ComponentEntity> componentEntities,
130                                                     Collection<NetworkEntity> networkEntities,
131                                                     Collection<NicEntity> nicEntities) {
132
133     return (CollectionUtils.isEmpty(componentEntities) && CollectionUtils.isEmpty(nicEntities) &&
134         CollectionUtils.isEmpty(networkEntities));
135   }
136
137   private CompositionData getCompositionDataForHealing(String vspId, Version version,
138                                                        ToscaServiceModel toscaServiceModel) {
139     mdcDataDebugMessage.debugEntryMessage(null);
140
141     if (Objects.isNull(toscaServiceModel)) {
142       return null;
143     }
144
145     CompositionData compositionData = new CompositionData();
146     if (Objects.nonNull(toscaServiceModel)) {
147       compositionData = compositionDataExtractor
148           .extractServiceCompositionData(toscaServiceModel);
149       serviceModelDao.storeServiceModel(vspId, version, toscaServiceModel);
150     }
151
152     mdcDataDebugMessage.debugExitMessage(null);
153     return compositionData;
154   }
155
156   private void updateComponentsDisplayNames(Collection<ComponentEntity> componentEntities) {
157     if (CollectionUtils.isEmpty(componentEntities)) {
158       return;
159     }
160
161     for (ComponentEntity component : componentEntities) {
162       updateComponentName(component);
163       componentDao.update(component);
164     }
165   }
166
167   private void updateComponentName(ComponentEntity component) {
168     mdcDataDebugMessage.debugEntryMessage("VSP id, component id", component.getVspId(), component
169         .getId());
170
171     ComponentData componentData =
172         JsonUtil.json2Object(component.getCompositionData(), ComponentData.class);
173     componentData
174         .setDisplayName(compositionDataExtractor.getComponentDisplayName(componentData.getName()));
175     String displayName = componentData.getDisplayName();
176     componentData.setName(componentData.getName().replace("com.att.d2", "org.openecomp"));
177     componentData.setVfcCode(displayName);
178     component.setCompositionData(JsonUtil.object2Json(componentData));
179
180     mdcDataDebugMessage.debugExitMessage("VSP id, component id", component.getVspId(), component
181         .getId());
182
183   }
184
185   private Optional<ToscaServiceModel> getServiceModelForHealing(String vspId, Version version)
186       throws IOException {
187     mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
188
189     /*UploadDataEntity uploadData =
190         vendorSoftwareProductDao.getUploadData(new UploadDataEntity(vspId, version));*/
191     UploadDataEntity uploadData =
192         orchestrationTemplateDataDao.getOrchestrationTemplate(vspId, version);
193
194     if (Objects.isNull(uploadData) || Objects.isNull(uploadData.getContentData())) {
195       return Optional.empty();
196     }
197
198     TranslatorOutput translatorOutput = getTranslatorOutputForHealing(uploadData);
199
200     if (Objects.isNull(translatorOutput)) {
201       return Optional.empty();
202     }
203
204     try {
205       serviceModelDao.storeServiceModel(vspId, version,
206           translatorOutput.getToscaServiceModel());
207     }catch (Exception e){
208       return Optional.empty();
209     }
210
211     mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
212     return Optional.of(translatorOutput.getToscaServiceModel());
213   }
214
215   private TranslatorOutput getTranslatorOutputForHealing(UploadDataEntity uploadData){
216
217     FileContentHandler fileContentHandler;
218     try {
219       fileContentHandler =
220           CommonUtil.loadUploadFileContent(uploadData.getContentData().array());
221       return HeatToToscaUtil.loadAndTranslateTemplateData(fileContentHandler);
222     }catch (Exception e){
223       return null;
224     }
225   }
226 }