1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*-
* ============LICENSE_START=======================================================
* SDC
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.openecomp.sdc.healing.healers;
import org.apache.commons.collections4.CollectionUtils;
import org.openecomp.core.model.dao.ServiceModelDao;
import org.openecomp.core.model.dao.ServiceModelDaoFactory;
import org.openecomp.core.model.types.ServiceElement;
import org.openecomp.core.translator.datatypes.TranslatorOutput;
import org.openecomp.core.utilities.file.FileContentHandler;
import org.openecomp.core.utilities.json.JsonUtil;
import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
import org.openecomp.sdc.common.utils.CommonUtil;
import org.openecomp.sdc.common.utils.SdcCommon;
import org.openecomp.sdc.healing.interfaces.Healer;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
import org.openecomp.sdc.translator.services.heattotosca.HeatToToscaUtil;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComponentDaoFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ComputeDaoFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.DeploymentFlavorDaoFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.ImageDaoFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.NetworkDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.NetworkDaoFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.NicDaoFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDao;
import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateDaoFactory;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComputeEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ImageEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
import org.openecomp.sdc.vendorsoftwareproduct.dao.type.UploadDataEntity;
import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionDataExtractorFactory;
import org.openecomp.sdc.vendorsoftwareproduct.factory.CompositionEntityDataManagerFactory;
import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Component;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionData;
import org.openecomp.sdc.versioning.dao.types.Version;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
public class CompositionDataHealer implements Healer {
private static final Version VERSION00 = new Version(0, 0);
private static final Version VERSION01 = new Version(0, 1);
private static MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
private static final OrchestrationTemplateDao orchestrationTemplateDataDao =
OrchestrationTemplateDaoFactory.getInstance().createInterface();
private static ComponentDao componentDao = ComponentDaoFactory.getInstance().createInterface();
private static NicDao nicDao = NicDaoFactory.getInstance().createInterface();
private static NetworkDao networkDao = NetworkDaoFactory.getInstance().createInterface();
private static ComputeDao computeDao = ComputeDaoFactory.getInstance().createInterface();
private static DeploymentFlavorDao deloymentFlavorDao = DeploymentFlavorDaoFactory.getInstance()
.createInterface();
private static ImageDao imageDao = ImageDaoFactory.getInstance().createInterface();
private static final ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDao =
ServiceModelDaoFactory.getInstance().createInterface();
private static CompositionDataExtractor compositionDataExtractor =
CompositionDataExtractorFactory.getInstance().createInterface();
private static CompositionEntityDataManager compositionEntityDataManager =
CompositionEntityDataManagerFactory.getInstance().createInterface();
private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
public CompositionDataHealer() {
}
@Override
public Optional<CompositionData> heal(Map<String, Object> healingParams) throws IOException {
mdcDataDebugMessage.debugEntryMessage(null);
String vspId = (String) healingParams.get(SdcCommon.VSP_ID);
Version version = VERSION00.equals(healingParams.get(SdcCommon.VERSION))
? VERSION01
: (Version) healingParams.get(SdcCommon.VERSION);
Collection<ComponentEntity> componentEntities =
componentDao.list(new ComponentEntity(vspId, version, null));
Collection<NicEntity> nicEntities = nicDao.listByVsp(vspId, version);
Collection<NetworkEntity> networkEntities =
networkDao.list(new NetworkEntity(vspId, version, null));
Optional<ToscaServiceModel> serviceModelForHealing = getServiceModelForHealing(vspId, version);
CompositionData compositionData = null;
if (!doesVspNeedCompositionDataHealing(vspId, version, componentEntities, networkEntities,
nicEntities)) {
updateComponentsDisplayNames(componentEntities);
mdcDataDebugMessage.debugExitMessage(null, null);
//return Optional.empty();
} else {
if (!serviceModelForHealing.isPresent()) {
mdcDataDebugMessage.debugExitMessage(null, null);
return Optional.empty();
}
compositionData = healCompositionData(vspId, version, serviceModelForHealing);
}
compositionData =
getCompositionDataForHealing(vspId, version, serviceModelForHealing.get());
HealNfodData(vspId, version, compositionData);
mdcDataDebugMessage.debugExitMessage(null, null);
return Optional.of(compositionData);
}
private boolean doesVspNeedCompositionDataHealing(String vspId, Version version,
Collection<ComponentEntity> componentEntities,
Collection<NetworkEntity> networkEntities,
Collection<NicEntity> nicEntities) {
return (CollectionUtils.isEmpty(componentEntities) && CollectionUtils.isEmpty(nicEntities) &&
CollectionUtils.isEmpty(networkEntities) );
// mdcDataDebugMessage.debugEntryMessage(null, null);
//
//// ToscaServiceModel toscaServiceModel;
//
// ByteBuffer contentData = uploadData.getContentData();
// FileContentHandler fileContentHandler = CommonUtil.validateAndUploadFileContent(uploadData
// .getContentData().array());
//
//
//
// TranslatorOutput translatorOutput =
// HeatToToscaUtil.loadAndTranslateTemplateData(fileContentHandler);
// ToscaServiceModel toscaServiceModel = translatorOutput.getToscaServiceModel();
//
//// toscaServiceModel = enrichedServiceModelDao.getServiceModel(vspId, version);
//
// mdcDataDebugMessage.debugExitMessage(null, null);
// return toscaServiceModel;
}
private void HealNfodData(String vspId, Version version, CompositionData compositionData) {
Collection<ComponentEntity> componentEntities;
/*componentEntities =
vendorSoftwareProductDao.listComponents(vspId, version);*/
componentEntities = componentDao.list(new ComponentEntity(vspId, version, null));
/*Collection<ComputeEntity> computeEntities=vendorSoftwareProductDao.listComputesByVsp(vspId,
version);
Collection<ImageEntity> imageEntities =vendorSoftwareProductDao.listImagesByVsp(vspId, version);
Collection<DeploymentFlavorEntity> deploymentFlavorEntities =vendorSoftwareProductDao
.listDeploymentFlavors(vspId, version);*/
Collection<ComputeEntity> computeEntities = computeDao.listByVsp(vspId, version);
Collection<ImageEntity> imageEntities = imageDao.listByVsp(vspId, version);
//Collection<DeploymentFlavorEntity> deploymentFlavorEntities = deloymentFlavorDao.list(new
//DeploymentFlavorEntity(vspId, version, null));
if (CollectionUtils.isEmpty(computeEntities) && CollectionUtils.isEmpty(imageEntities)) {
for (Component component : compositionData.getComponents()) {
String componentId = null;
for (ComponentEntity componentEntity:componentEntities) {
if (componentEntity.getComponentCompositionData().getName().equals(component.getData()
.getName())) {
componentId = componentEntity.getId();
break;
}
}
compositionEntityDataManager.saveComputesFlavorByComponent(vspId,version,component,
componentId);
compositionEntityDataManager.saveImagesByComponent(vspId,version,component,
componentId);
}
}
/*if (CollectionUtils.isEmpty(deploymentFlavorEntities)) {
compositionEntityDataManager.saveDeploymentFlavors(vspId,version,compositionData);
}*/
}
private CompositionData healCompositionData(String vspId, Version version,
Optional<ToscaServiceModel> serviceModelForHealing) {
ToscaServiceModel toscaServiceModel = serviceModelForHealing.get();
CompositionData compositionData =
getCompositionDataForHealing(vspId, version, toscaServiceModel);
compositionEntityDataManager.saveCompositionData(vspId, version, compositionData);
return compositionData;
}
private boolean doesVspNeedCompositionDataHealing(Collection<ComponentEntity> componentEntities,
Collection<NetworkEntity> networkEntities,
Collection<NicEntity> nicEntities) {
return (CollectionUtils.isEmpty(componentEntities) && CollectionUtils.isEmpty(nicEntities) &&
CollectionUtils.isEmpty(networkEntities));
}
private CompositionData getCompositionDataForHealing(String vspId, Version version,
ToscaServiceModel toscaServiceModel) {
mdcDataDebugMessage.debugEntryMessage(null);
if (Objects.isNull(toscaServiceModel)) {
return null;
}
CompositionData compositionData = new CompositionData();
if (Objects.nonNull(toscaServiceModel)) {
compositionData = compositionDataExtractor
.extractServiceCompositionData(toscaServiceModel);
serviceModelDao.storeServiceModel(vspId, version, toscaServiceModel);
}
mdcDataDebugMessage.debugExitMessage(null);
return compositionData;
}
private void updateComponentsDisplayNames(Collection<ComponentEntity> componentEntities) {
if (CollectionUtils.isEmpty(componentEntities)) {
return;
}
for (ComponentEntity component : componentEntities) {
updateComponentName(component);
componentDao.update(component);
}
}
private void updateComponentName(ComponentEntity component) {
mdcDataDebugMessage.debugEntryMessage("VSP id, component id", component.getVspId(), component
.getId());
ComponentData componentData =
JsonUtil.json2Object(component.getCompositionData(), ComponentData.class);
componentData
.setDisplayName(compositionDataExtractor.getComponentDisplayName(componentData.getName()));
String displayName = componentData.getDisplayName();
componentData.setName(componentData.getName().replace("com.att.d2", "org.openecomp"));
componentData.setVfcCode(displayName);
component.setCompositionData(JsonUtil.object2Json(componentData));
mdcDataDebugMessage.debugExitMessage("VSP id, component id", component.getVspId(), component
.getId());
}
private Optional<ToscaServiceModel> getServiceModelForHealing(String vspId, Version version)
throws IOException {
mdcDataDebugMessage.debugEntryMessage("VSP id", vspId);
/*UploadDataEntity uploadData =
vendorSoftwareProductDao.getUploadData(new UploadDataEntity(vspId, version));*/
UploadDataEntity uploadData =
orchestrationTemplateDataDao.getOrchestrationTemplate(vspId, version);
if (Objects.isNull(uploadData) || Objects.isNull(uploadData.getContentData())) {
return Optional.empty();
}
TranslatorOutput translatorOutput = getTranslatorOutputForHealing(uploadData);
if (Objects.isNull(translatorOutput)) {
return Optional.empty();
}
try {
serviceModelDao.storeServiceModel(vspId, version,
translatorOutput.getToscaServiceModel());
} catch (Exception e) {
log.debug("", e);
return Optional.empty();
}
mdcDataDebugMessage.debugExitMessage("VSP id", vspId);
return Optional.of(translatorOutput.getToscaServiceModel());
}
private TranslatorOutput getTranslatorOutputForHealing(UploadDataEntity uploadData) {
FileContentHandler fileContentHandler;
try {
fileContentHandler =
CommonUtil.validateAndUploadFileContent(
OnboardingTypesEnum.ZIP, uploadData.getContentData().array());
return HeatToToscaUtil.loadAndTranslateTemplateData(fileContentHandler);
} catch (Exception e) {
log.debug("", e);
return null;
}
}
}
|