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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
/*-
* ============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.vendorsoftwareproduct.services.impl.composition;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.tosca.datatypes.ToscaCapabilityType;
import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
import org.openecomp.sdc.tosca.datatypes.ToscaNodeType;
import org.openecomp.sdc.tosca.datatypes.ToscaRelationshipType;
import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
import org.onap.sdc.tosca.datatypes.model.ParameterDefinition;
import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
import org.openecomp.sdc.tosca.errors.ToscaInvalidEntryNotFoundErrorBuilder;
import org.openecomp.sdc.tosca.errors.ToscaInvalidSubstituteNodeTemplateErrorBuilder;
import org.openecomp.sdc.tosca.errors.ToscaMissingSubstitutionMappingForReqCapErrorBuilder;
import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
import org.openecomp.sdc.tosca.services.ToscaConstants;
import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;
import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
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.vendorsoftwareproduct.types.composition.ComputeData;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ExtractCompositionDataContext;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Image;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
public class CompositionDataExtractorImpl implements CompositionDataExtractor {
protected static Logger logger;
private static ToscaAnalyzerService toscaAnalyzerService;
static {
logger = LoggerFactory.getLogger(CompositionDataExtractorImpl.class);
toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
}
/**
* Extract service composition data composition data.
*
* @param toscaServiceModel the tosca service model
* @return the composition data
*/
public CompositionData extractServiceCompositionData(ToscaServiceModel toscaServiceModel) {
ExtractCompositionDataContext context = new ExtractCompositionDataContext();
String entryDefinitionServiceTemplateFileName =
toscaServiceModel.getEntryDefinitionServiceTemplate();
ServiceTemplate entryDefinitionServiceTemplate =
toscaServiceModel.getServiceTemplates().get(entryDefinitionServiceTemplateFileName);
extractServiceCompositionData(entryDefinitionServiceTemplateFileName,
entryDefinitionServiceTemplate, toscaServiceModel, context);
CompositionData compositionData = new CompositionData();
compositionData.setNetworks(context.getNetworks());
compositionData.setComponents(context.getComponents());
return compositionData;
}
private void extractServiceCompositionData(String serviceTemplateFileName,
ServiceTemplate serviceTemplate,
ToscaServiceModel toscaServiceModel,
ExtractCompositionDataContext context) {
if (context.getHandledServiceTemplates().contains(serviceTemplateFileName)) {
return;
}
context.addNetworks(extractNetworks(serviceTemplate, toscaServiceModel));
extractComponents(serviceTemplate, toscaServiceModel, context);
handleSubstitution(serviceTemplate, toscaServiceModel, context);
context.addHandledServiceTemplates(serviceTemplateFileName);
}
private void handleSubstitution(ServiceTemplate serviceTemplate,
ToscaServiceModel toscaServiceModel,
ExtractCompositionDataContext context) {
Map<String, NodeTemplate> substitutableNodeTemplates =
toscaAnalyzerService.getSubstitutableNodeTemplates(serviceTemplate);
if (substitutableNodeTemplates != null) {
for (String substitutableNodeTemplateId : substitutableNodeTemplates.keySet()) {
handleSubstitutableNodeTemplate(serviceTemplate, toscaServiceModel,
substitutableNodeTemplateId,
substitutableNodeTemplates.get(substitutableNodeTemplateId), context);
}
}
}
private void handleSubstitutableNodeTemplate(ServiceTemplate serviceTemplate,
ToscaServiceModel toscaServiceModel,
String substitutableNodeTemplateId,
NodeTemplate substitutableNodeTemplate,
ExtractCompositionDataContext context) {
ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
Optional<String> substituteServiceTemplateFileName = toscaAnalyzerService
.getSubstituteServiceTemplateName(substitutableNodeTemplateId, substitutableNodeTemplate);
if (!substituteServiceTemplateFileName.isPresent()) {
throw new CoreException(
new ToscaInvalidSubstituteNodeTemplateErrorBuilder(substitutableNodeTemplateId).build());
}
if (context.getHandledServiceTemplates().contains(substituteServiceTemplateFileName.get())) {
//each substitution is should be handled once, and will get the connection to the upper
// service level according to the first one which was processed
return;
}
ServiceTemplate substituteServiceTemplate =
toscaServiceModel.getServiceTemplates().get(substituteServiceTemplateFileName.get());
extractServiceCompositionData(substituteServiceTemplateFileName.get(),
substituteServiceTemplate, toscaServiceModel, context);
List<Map<String, RequirementAssignment>> substitutableRequirements =
substitutableNodeTemplate.getRequirements();
if (CollectionUtils.isEmpty(substitutableRequirements)) {
return;
}
for (Map<String, RequirementAssignment> substitutableReq : substitutableRequirements) {
substitutableReq.keySet().stream().filter(reqId -> {
RequirementAssignment reqAssignment = toscaExtensionYamlUtil
.yamlToObject(toscaExtensionYamlUtil.objectToYaml(substitutableReq.get(reqId)),
RequirementAssignment.class);
return isLinkToNetworkRequirementAssignment(reqAssignment);
}).forEach(reqId -> {
RequirementAssignment linkToNetworkRequirement = toscaExtensionYamlUtil
.yamlToObject(toscaExtensionYamlUtil.objectToYaml(substitutableReq.get(reqId)),
RequirementAssignment.class);
String connectedNodeId = linkToNetworkRequirement.getNode();
Optional<NodeTemplate> connectedNodeTemplate =
toscaAnalyzerService.getNodeTemplateById(serviceTemplate, connectedNodeId);
if (connectedNodeTemplate.isPresent() && toscaAnalyzerService
.isTypeOf(connectedNodeTemplate.get(), ToscaNodeType.NATIVE_NETWORK,
serviceTemplate, toscaServiceModel)) {
Optional<Map.Entry<String, NodeTemplate>> mappedNodeTemplate = toscaAnalyzerService
.getSubstitutionMappedNodeTemplateByExposedReq(
substituteServiceTemplateFileName.get(), substituteServiceTemplate, reqId);
if (!mappedNodeTemplate.isPresent()) {
throw new CoreException(new ToscaMissingSubstitutionMappingForReqCapErrorBuilder(
ToscaMissingSubstitutionMappingForReqCapErrorBuilder.MappingExposedEntry
.REQUIREMENT, connectedNodeId).build());
}
if (toscaAnalyzerService.isTypeOf(mappedNodeTemplate.get().getValue(),
ToscaNodeType.NATIVE_NETWORK_PORT, serviceTemplate,
toscaServiceModel)) {
Nic port = context.getNics().get(mappedNodeTemplate.get().getKey());
if (port != null) {
port.setNetworkName(connectedNodeId);
} else {
logger.warn(
"Different ports define for the same component which is used in different "
+ "substitution service templates.");
}
}
} else if (!connectedNodeTemplate.isPresent()) {
throw new CoreException(
new ToscaInvalidEntryNotFoundErrorBuilder("Node Template", connectedNodeId).build());
}
});
}
}
private boolean isLinkToNetworkRequirementAssignment(RequirementAssignment requirement) {
return toscaAnalyzerService.isDesiredRequirementAssignment(requirement,
ToscaCapabilityType.NATIVE_NETWORK_LINKABLE, null,
ToscaRelationshipType.NATIVE_NETWORK_LINK_TO);
}
private void connectPortToNetwork(Nic port, NodeTemplate portNodeTemplate) {
List<RequirementAssignment> linkRequirementsToNetwork =
toscaAnalyzerService.getRequirements(portNodeTemplate, ToscaConstants.LINK_REQUIREMENT_ID);
//port is connected to one network
for (RequirementAssignment linkRequirementToNetwork : linkRequirementsToNetwork) {
port.setNetworkName(linkRequirementToNetwork.getNode());
}
}
/*
return Map with key - compute node template id, value - list of connected port node template id
*/
private Map<String, List<String>> getComputeToPortsConnection(
Map<String, NodeTemplate> portNodeTemplates) {
Map<String, List<String>> computeToPortConnection = new HashMap<>();
if (MapUtils.isEmpty(portNodeTemplates)) {
return computeToPortConnection;
}
for (String portId : portNodeTemplates.keySet()) {
List<RequirementAssignment> bindingRequirementsToCompute = toscaAnalyzerService
.getRequirements(portNodeTemplates.get(portId), ToscaConstants.BINDING_REQUIREMENT_ID);
for (RequirementAssignment bindingRequirementToCompute : bindingRequirementsToCompute) {
computeToPortConnection
.putIfAbsent(bindingRequirementToCompute.getNode(), new ArrayList<>());
computeToPortConnection.get(bindingRequirementToCompute.getNode()).add(portId);
}
}
return computeToPortConnection;
}
private void extractComponents(ServiceTemplate serviceTemplate,
ToscaServiceModel toscaServiceModel,
ExtractCompositionDataContext context) {
Map<String, NodeTemplate> computeNodeTemplates = toscaAnalyzerService
.getNodeTemplatesByType(serviceTemplate, ToscaNodeType.NATIVE_COMPUTE,
toscaServiceModel);
if (MapUtils.isEmpty(computeNodeTemplates)) {
return;
}
Map<String, List<String>> imageNodeTemplates = getComponentImages(computeNodeTemplates,
toscaServiceModel);
Map<String, List<String>> computeFlavorNodeTemplates =
getComponentComputeFlavor(computeNodeTemplates, toscaServiceModel);
Map<String, NodeTemplate> portNodeTemplates = toscaAnalyzerService
.getNodeTemplatesByType(serviceTemplate, ToscaNodeType.NATIVE_NETWORK_PORT,
toscaServiceModel);
Map<String, List<String>> computeToPortsConnection =
getComputeToPortsConnection(portNodeTemplates);
Map<String, List<String>> computesGroupedByType =
getNodeTemplatesGroupedByType(computeNodeTemplates);
computesGroupedByType.keySet()
.stream()
.filter(nodeType ->
!context.getCreatedComponents().contains(nodeType))
.forEach(nodeType -> extractComponent(serviceTemplate, computeToPortsConnection,
computesGroupedByType, imageNodeTemplates, computeFlavorNodeTemplates, nodeType,
context));
}
private Map<String,List<String>> getComponentImages(Map<String, NodeTemplate>
computeNodeTemplates,
ToscaServiceModel toscaServiceModel) {
return getComponentProperty(ToscaConstants.COMPUTE_IMAGE, computeNodeTemplates, toscaServiceModel);
}
private Map<String,List<String>> getComponentComputeFlavor(Map<String, NodeTemplate>
computeNodeTemplates,
ToscaServiceModel toscaServiceModel) {
return getComponentProperty(ToscaConstants.COMPUTE_FLAVOR, computeNodeTemplates, toscaServiceModel);
}
private Map<String, List<String>> getComponentProperty(String propertyName,
Map<String, NodeTemplate> computeNodeTemplates,
ToscaServiceModel toscaServiceModel) {
Map<String,List<String>> componentPropertyValues = new HashMap<>();
for (String component : computeNodeTemplates.keySet()) {
List<String> computes = new ArrayList<>();
Map<String,Object> properties = computeNodeTemplates.get(component).getProperties();
if(MapUtils.isEmpty(properties)){
continue;
}
List<Object> computesList = properties.entrySet()
.stream()
.filter(map -> map.getKey().equals(propertyName))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
for (Object obj : computesList) {
if (obj instanceof String) {
computes.add((String) obj);
} else {
Map<String, String> objMap = new ObjectMapper().convertValue(obj, Map.class);
computes.add(getInputs(toscaServiceModel, objMap.get("get_input")));
}
}
componentPropertyValues.put(component,computes);
}
return componentPropertyValues;
}
private String getInputs(ToscaServiceModel toscaServiceModel, String inputValue) {
String mainTemplate = toscaServiceModel.getEntryDefinitionServiceTemplate();
List<ServiceTemplate> toscaServiceTemplates = toscaServiceModel.getServiceTemplates().entrySet()
.stream()
.filter(map -> map.getKey().equals(mainTemplate))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
ServiceTemplate serviceTemplate = toscaServiceTemplates.get(0);
if (Objects.nonNull(serviceTemplate.getTopology_template())
&& MapUtils.isNotEmpty(serviceTemplate.getTopology_template().getInputs())) {
for (Map.Entry<String, ParameterDefinition> inputEntry : serviceTemplate
.getTopology_template().getInputs().entrySet()) {
if (inputEntry.getKey().equals(inputValue)) {
String value;
try {
value= (String) inputEntry.getValue().get_default();
} catch (Exception e) {
logger.debug(e.getMessage(), e);
value = inputEntry.getValue().get_default().toString();
}
return value;
}
}
}
return inputValue;
}
private void extractComponent(ServiceTemplate serviceTemplate,
Map<String, List<String>> computeToPortsConnection,
Map<String, List<String>> computesGroupedByType,
Map<String, List<String>> imageList,
Map<String, List<String>> computeFlavorNodeTemplates,
String computeNodeType,
ExtractCompositionDataContext context) {
ComponentData component = new ComponentData();
component.setName(computeNodeType);
component.setDisplayName(getComponentDisplayName(component.getName()));
component.setVfcCode(component.getDisplayName());
Component componentModel = new Component();
componentModel.setData(component);
String computeId = computesGroupedByType.get(computeNodeType).get(0);
List<String> connectedPortIds = computeToPortsConnection.get(computeId);
List<String> images = imageList.get(computeId);
List<String> computeFlavors = computeFlavorNodeTemplates.get(computeId);
if (connectedPortIds != null) {
componentModel.setNics(new ArrayList<>());
componentModel.setImages(new ArrayList<>());
componentModel.setCompute(new ArrayList<>());
for (String portId : connectedPortIds) {
Nic port = extractPort(serviceTemplate, portId);
componentModel.getNics().add(port);
context.addNic(portId, port);
}
for (String image : images) {
Image img = new Image(image);
componentModel.getImages().add(img);
context.addImage(image, img);
}
for (String flavor : computeFlavors) {
ComputeData computeFlavor = new ComputeData(flavor);
componentModel.getCompute().add(computeFlavor);
context.addCompute(flavor,computeFlavor);
}
}
context.addComponent(componentModel);
context.getCreatedComponents().add(computeNodeType);
}
private Nic extractPort(ServiceTemplate serviceTemplate, String portNodeTemplateId) {
Optional<NodeTemplate> portNodeTemplate =
toscaAnalyzerService.getNodeTemplateById(serviceTemplate, portNodeTemplateId);
if (portNodeTemplate.isPresent()) {
Nic port = new Nic();
port.setName(portNodeTemplateId);
connectPortToNetwork(port, portNodeTemplate.get());
return port;
} else {
throw new CoreException(
new ToscaInvalidEntryNotFoundErrorBuilder("Node Template", portNodeTemplateId).build());
}
}
private Map<String, List<String>> getNodeTemplatesGroupedByType(
Map<String, NodeTemplate> nodeTemplates) {
Map<String, List<String>> nodeTemplatesGrouped =
new HashMap<>(); //key - node type, value - list of node ids with this type
for (String nodeId : nodeTemplates.keySet()) {
String nodeType = nodeTemplates.get(nodeId).getType();
nodeTemplatesGrouped.putIfAbsent(nodeType, new ArrayList<>());
nodeTemplatesGrouped.get(nodeType).add(nodeId);
}
return nodeTemplatesGrouped;
}
private List<Network> extractNetworks(ServiceTemplate serviceTemplate,
ToscaServiceModel toscaServiceModel) {
List<Network> networks = new ArrayList<>();
Map<String, NodeTemplate> networkNodeTemplates = toscaAnalyzerService
.getNodeTemplatesByType(serviceTemplate, ToscaNodeType.NATIVE_NETWORK,
toscaServiceModel);
if (MapUtils.isEmpty(networkNodeTemplates)) {
return networks;
}
for (String networkId : networkNodeTemplates.keySet()) {
Network network = new Network();
network.setName(networkId);
Optional<Boolean> networkDhcpValue =
getNetworkDhcpValue(serviceTemplate, networkNodeTemplates.get(networkId));
network.setDhcp(networkDhcpValue.orElse(true));
networks.add(network);
}
return networks;
}
//dhcp default value is true
private Optional<Boolean> getNetworkDhcpValue(ServiceTemplate serviceTemplate,
NodeTemplate networkNodeTemplate) {
if (networkNodeTemplate == null) {
return Optional.empty();
}
if (networkNodeTemplate.getProperties() == null
|| networkNodeTemplate.getProperties().get(ToscaConstants.DHCP_ENABLED_PROPERTY_NAME)
== null) {
return Optional.of(true);
}
Object dhcp =
networkNodeTemplate.getProperties().get(ToscaConstants.DHCP_ENABLED_PROPERTY_NAME);
if (dhcp instanceof String) {
return Optional.of(Boolean.valueOf((String) dhcp));
} else if (dhcp instanceof Boolean) {
return Optional.of((Boolean) dhcp);
} else if (dhcp instanceof Map) {
String inputParameterName =
(String) ((Map) dhcp).get(ToscaFunctions.GET_INPUT.getDisplayName());
if (inputParameterName != null) {
ParameterDefinition inputParameterDefinition =
serviceTemplate.getTopology_template().getInputs().get(inputParameterName);
if (inputParameterDefinition != null) {
if (inputParameterDefinition.get_default() != null) {
return Optional.of(Boolean.valueOf(inputParameterDefinition.get_default().toString()));
}
} else {
throw new CoreException(
new ToscaInvalidEntryNotFoundErrorBuilder("Input Parameter", inputParameterName)
.build());
}
}
}
return Optional.of(true);
}
@Override
public String getComponentDisplayName(String componentName) {
if (componentName == null) {
return null;
}
String delimiterChar = ".";
if (componentName.contains(delimiterChar)) {
return componentName.substring(componentName.lastIndexOf(delimiterChar) + 1);
}
return componentName;
}
}
|