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
|
/*
* Copyright © 2016-2018 European Support Limited
*
* 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.
*/
package org.openecomp.sdc.translator.services.heattotosca;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
import org.openecomp.core.utilities.file.FileUtils;
import org.openecomp.sdc.datatypes.configuration.ImplementationConfiguration;
import org.openecomp.sdc.heat.datatypes.model.HeatOrchestrationTemplate;
import org.openecomp.sdc.heat.datatypes.model.HeatResourcesTypes;
import org.openecomp.sdc.heat.datatypes.model.Resource;
import org.openecomp.sdc.tosca.services.ToscaUtil;
import org.openecomp.sdc.translator.datatypes.heattotosca.TranslationContext;
import org.openecomp.sdc.translator.datatypes.heattotosca.to.TranslateTo;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.ComputeConsolidationDataHandler;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.ComputeTemplateConsolidationData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.ConsolidationDataHandler;
import org.openecomp.sdc.translator.services.heattotosca.impl.functiontranslation.FunctionTranslator;
import static org.openecomp.sdc.translator.services.heattotosca.ConfigConstants.TRANS_MAPPING_DELIMITER_CHAR;
/**
* Utility class for consolidation data collection helper methods.
*/
public class ConsolidationDataUtil {
private static final String UNDERSCORE = "_";
private static final String DIGIT_REGEX = "\\d+";
private ConsolidationDataUtil() {
// prevent instantiation of utility class
}
public static boolean isNodeTemplatePointsToServiceTemplateWithoutNodeTemplates(
String nestedNodeTemplateId, String nestedHeatFileName, TranslationContext context) {
return context.isServiceTemplateWithoutNodeTemplatesSection(
FileUtils.getFileWithoutExtention(nestedHeatFileName))
|| context.isNodeTemplateIdPointsToStWithoutNodeTemplates(nestedNodeTemplateId);
}
/**
* Update volume information in consolidation data.
*
* @param translateTo {@link TranslateTo} object
* @param computeType Local type of the compute node
* @param computeNodeTemplateId Node template id of the compute node
* @param requirementAssignment RequirementAssignment object
*/
public static void updateComputeConsolidationDataVolumes(TranslateTo translateTo,
String computeType,
String computeNodeTemplateId,
String requirementId,
RequirementAssignment requirementAssignment) {
ComputeConsolidationDataHandler handler =
translateTo.getContext().getComputeConsolidationDataHandler();
handler.addVolumeToConsolidationData(
translateTo, computeType, computeNodeTemplateId, requirementId, requirementAssignment);
}
/**
* Update port in consolidation data.
*
* @param translateTo the translate to
* @param computeNodeType the compute node type
* @param portResourceId the port resource id
* @param portNodeTemplateId the port node template id
*/
public static void updatePortInConsolidationData(TranslateTo translateTo,
String computeNodeType,
String portResourceId,
String portResourceType,
String portNodeTemplateId) {
TranslationContext translationContext = translateTo.getContext();
String computeNodeTemplateId = translateTo.getTranslatedId();
String portType = getPortType(portNodeTemplateId);
translationContext.getComputeConsolidationDataHandler().addPortToConsolidationData(
translateTo, computeNodeType, computeNodeTemplateId, portType, portNodeTemplateId);
ServiceTemplate serviceTemplate = translateTo.getServiceTemplate();
String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate);
translationContext.getPortConsolidationDataHandler().addConsolidationData(
serviceTemplateFileName, portResourceId, portResourceType, portNodeTemplateId);
}
/**
* Update nodes connected in and out for Depends on and connectivity in consolidation data.
*
* @param translateTo the translate to
* @param targetResourceId the target resource id
* @param nodeTemplateId the source node template id
* @param requirementAssignment the requirement assignment
*/
public static void updateNodesConnectedData(TranslateTo translateTo, String targetResourceId,
Resource targetResource, Resource sourceResource,
String nodeTemplateId, String requirementId,
RequirementAssignment requirementAssignment) {
ConsolidationEntityType consolidationEntityType = ConsolidationEntityType.OTHER;
consolidationEntityType.setEntityType(sourceResource, targetResource, translateTo.getContext());
// Add resource dependency information in nodesConnectedIn if the target node
// is a consolidation entity
if (isConsolidationEntity(consolidationEntityType.getTargetEntityType())) {
ConsolidationDataUtil.updateNodesConnectedIn(translateTo,
nodeTemplateId, consolidationEntityType.getTargetEntityType(), targetResourceId,
requirementId, requirementAssignment);
}
//Add resource dependency information in nodesConnectedOut if the source node
//is a consolidation entity
if (isConsolidationEntity(consolidationEntityType.getSourceEntityType())) {
ConsolidationDataUtil.updateNodesConnectedOut(translateTo,
requirementAssignment.getNode(), consolidationEntityType.getSourceEntityType(),
requirementId, requirementAssignment);
}
}
private static boolean isConsolidationEntity(ConsolidationEntityType consolidationEntityType) {
return ConsolidationEntityType.getSupportedConsolidationEntities().contains(consolidationEntityType);
}
/**
* Update nodes connected from this node in consolidation data.
*
* @param translateTo the translate to
* @param nodeTemplateId the node template id of the target node
* @param consolidationEntityType the entity type (compute or port)
* @param requirementId the requirement id
* @param requirementAssignment the requirement assignment
*/
public static void updateNodesConnectedOut(TranslateTo translateTo,
String nodeTemplateId,
ConsolidationEntityType consolidationEntityType,
String requirementId,
RequirementAssignment requirementAssignment) {
TranslationContext translationContext = translateTo.getContext();
translationContext.updateRequirementAssignmentIdIndex(
ToscaUtil.getServiceTemplateFileName(translateTo.getServiceTemplate()), translateTo.getResourceId(),
requirementId);
Optional<ConsolidationDataHandler> consolidationDataHandler =
translationContext.getConsolidationDataHandler(consolidationEntityType);
consolidationDataHandler.ifPresent(handler -> handler.addNodesConnectedOut(
translateTo, nodeTemplateId, requirementId, requirementAssignment));
}
/**
* Update nodes connected from this node in consolidation data.
*
* @param translateTo the translate to
* @param sourceNodeTemplateId the node template id of the source node
* @param consolidationEntityType Entity type (compute or port)
* @param targetResourceId Target Resource Id
* @param requirementId Requirement Id
* @param requirementAssignment the requirement assignment
*/
public static void updateNodesConnectedIn(TranslateTo translateTo, String sourceNodeTemplateId,
ConsolidationEntityType consolidationEntityType,
String targetResourceId,
String requirementId,
RequirementAssignment requirementAssignment) {
TranslationContext translationContext = translateTo.getContext();
Optional<ConsolidationDataHandler> consolidationDataHandler =
translationContext.getConsolidationDataHandler(consolidationEntityType);
String dependentNodeTemplateId = requirementAssignment.getNode();
consolidationDataHandler.ifPresent(
handler -> handler.addNodesConnectedIn(translateTo, sourceNodeTemplateId, dependentNodeTemplateId,
targetResourceId, requirementId, requirementAssignment));
}
/**
* Checks if the current HEAT resource if of type compute.
*
* @param resource the resource
* @return true if the resource is of compute type and false otherwise
*/
public static boolean isComputeResource(Resource resource) {
String resourceType = resource.getType();
Map<String, ImplementationConfiguration> supportedComputeResources =
TranslationContext.getSupportedConsolidationComputeResources();
if (supportedComputeResources.containsKey(resourceType)) {
return supportedComputeResources.get(resourceType).isEnable();
}
return false;
}
/**
* Checks if the current HEAT resource if of type port.
*
* @param resource the resource
* @return true if the resource is of port type and false otherwise
*/
public static boolean isPortResource(Resource resource) {
String resourceType = resource.getType();
Map<String, ImplementationConfiguration> supportedPortResources =
TranslationContext.getSupportedConsolidationPortResources();
if (supportedPortResources.containsKey(resourceType)) {
return supportedPortResources.get(resourceType).isEnable();
}
return false;
}
/**
* Checks if the current HEAT resource if of type volume.
*
* @param resource the resource
* @return true if the resource is of volume type and false otherwise
*/
public static boolean isVolumeResource(Resource resource) {
String resourceType = resource.getType();
return resourceType.equals(HeatResourcesTypes.CINDER_VOLUME_RESOURCE_TYPE.getHeatResource())
|| resourceType.equals(HeatResourcesTypes.CINDER_VOLUME_ATTACHMENT_RESOURCE_TYPE
.getHeatResource());
}
/**
* Gets port type.
*
* @param portNodeTemplateId the port node template id
* @return the port type
*/
public static String getPortType(String portNodeTemplateId) {
if (StringUtils.isBlank(portNodeTemplateId)) {
return portNodeTemplateId;
}
String formattedName = portNodeTemplateId.replaceAll(UNDERSCORE + DIGIT_REGEX + "$", "");
StringBuilder sb = new StringBuilder();
int count = 0;
for (String token : formattedName.split(UNDERSCORE)) {
if (StringUtils.isNotBlank(token)) {
count++;
}
if (count != 2 || (!StringUtils.isBlank(token) && !token.matches(DIGIT_REGEX))) {
sb.append(token).append(UNDERSCORE);
}
}
return portNodeTemplateId.endsWith(UNDERSCORE) ? sb.toString() : sb.substring(0, sb.length() - 1);
}
/**
* Update node template id for the nested node templates in the consolidation data.
*
* @param translateTo the translate to
*/
public static void updateNestedNodeTemplateId(TranslateTo translateTo) {
TranslationContext context = translateTo.getContext();
ServiceTemplate serviceTemplate = translateTo.getServiceTemplate();
String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate);
// create nested in consolidation data
context.getNestedConsolidationDataHandler()
.addConsolidationData(serviceTemplateFileName, context,
translateTo.getHeatFileName(), translateTo.getTranslatedId());
}
public static void removeSharedResource(ServiceTemplate serviceTemplate,
HeatOrchestrationTemplate heatOrchestrationTemplate,
TranslationContext context,
String paramName,
String contrailSharedResourceId,
String sharedTranslatedResourceId) {
Optional<ConsolidationDataHandler> consolidationDataHandler =
ConsolidationDataUtil.getConsolidationDataHandler(heatOrchestrationTemplate, context,
contrailSharedResourceId);
consolidationDataHandler.ifPresent(
handler -> handler.removeParamNameFromAttrFuncList(serviceTemplate, heatOrchestrationTemplate,
paramName, contrailSharedResourceId, sharedTranslatedResourceId));
}
public static void updateNodeGetAttributeIn(FunctionTranslator functionTranslator, String resourceTranslatedId,
String targetResourceId, String targetResourceTranslatedId, String attributeName) {
if (functionTranslator == null || functionTranslator.getServiceTemplate() == null) {
return;
}
Optional<ConsolidationDataHandler> consolidationDataHandler =
ConsolidationDataUtil.getConsolidationDataHandler(functionTranslator.getHeatOrchestrationTemplate(),
functionTranslator.getContext(), targetResourceId);
consolidationDataHandler.ifPresent(handler -> handler.addNodesGetAttrIn(functionTranslator,
resourceTranslatedId, targetResourceId, targetResourceTranslatedId,
getToscaPropertyName(functionTranslator), attributeName));
}
public static void updateNodeGetAttributeOut(FunctionTranslator functionTranslator,
String targetTranslatedResourceId, String resourceTranslatedId, String attrName) {
if (functionTranslator == null || functionTranslator.getServiceTemplate() == null) {
return;
}
Optional<ConsolidationDataHandler> consolidationDataHandler =
ConsolidationDataUtil.getConsolidationDataHandler(functionTranslator.getHeatOrchestrationTemplate(),
functionTranslator.getContext(), functionTranslator.getResourceId());
consolidationDataHandler.ifPresent(handler -> handler.addNodesGetAttrOut(functionTranslator,
targetTranslatedResourceId, resourceTranslatedId, getToscaPropertyName(functionTranslator), attrName));
}
public static void updateOutputParamGetAttrIn(FunctionTranslator functionTranslator,
String targetResourceId, String targetResourceTranslatedId, String propertyName, String attrName) {
if (functionTranslator == null || functionTranslator.getServiceTemplate() == null) {
return;
}
Optional<ConsolidationDataHandler> consolidationDataHandler =
ConsolidationDataUtil.getConsolidationDataHandler(functionTranslator.getHeatOrchestrationTemplate(),
functionTranslator.getContext(), targetResourceId);
consolidationDataHandler.ifPresent(handler -> handler.addOutputParamGetAttrIn(functionTranslator,
targetResourceId, targetResourceTranslatedId, propertyName, attrName));
}
private static Optional<ConsolidationDataHandler> getConsolidationDataHandler(
HeatOrchestrationTemplate heatOrchestrationTemplate, TranslationContext context,
String contrailSharedResourceId) {
Resource resource = heatOrchestrationTemplate.getResources().get(contrailSharedResourceId);
ConsolidationEntityType consolidationEntityType = ConsolidationEntityType.OTHER;
consolidationEntityType.setEntityType(resource, resource, context);
return context.getConsolidationDataHandler(consolidationEntityType.getSourceEntityType());
}
private static String getToscaPropertyName(FunctionTranslator functionTranslator) {
HeatOrchestrationTemplate heatOrchestrationTemplate = functionTranslator.getHeatOrchestrationTemplate();
Resource resource = heatOrchestrationTemplate.getResources().get(functionTranslator.getResourceId());
String toscaPropertyName = functionTranslator.getPropertyName();
if (!HeatToToscaUtil.isNestedResource(resource)) {
return HeatToToscaUtil.getToscaPropertyName(functionTranslator.getContext(),
resource.getType(), getHeatPropertyName(toscaPropertyName));
}
return toscaPropertyName;
}
private static String getHeatPropertyName(String toscaPropertyName) {
if (toscaPropertyName.contains(TRANS_MAPPING_DELIMITER_CHAR)) {
return toscaPropertyName.substring(0, toscaPropertyName.indexOf(TRANS_MAPPING_DELIMITER_CHAR));
}
return toscaPropertyName;
}
public static boolean isComputeReferenceToPortId(ComputeTemplateConsolidationData compute,
String portId) {
if (MapUtils.isEmpty(compute.getPorts())) {
return false;
}
for (List<String> portIdsPerType : compute.getPorts().values()) {
if (portIdsPerType.contains(portId)) {
return true;
}
}
return false;
}
}
|