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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
|
/*
* Copyright © 2016-2017 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;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.openecomp.core.translator.api.HeatToToscaTranslator;
import org.openecomp.core.utilities.file.FileUtils;
import org.openecomp.sdc.common.utils.SdcCommon;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;
import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
import org.openecomp.sdc.tosca.datatypes.model.GroupDefinition;
import org.openecomp.sdc.tosca.datatypes.model.NodeTemplate;
import org.openecomp.sdc.tosca.datatypes.model.RequirementAssignment;
import org.openecomp.sdc.tosca.datatypes.model.ServiceTemplate;
import org.openecomp.sdc.tosca.datatypes.model.TopologyTemplate;
import org.openecomp.sdc.tosca.services.DataModelUtil;
import org.openecomp.sdc.tosca.services.ToscaConstants;
import org.openecomp.sdc.tosca.services.ToscaExtensionYamlUtil;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.ComputeConsolidationData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.ComputeTemplateConsolidationData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.ConsolidationData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.EntityConsolidationData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.FileComputeConsolidationData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.FileNestedConsolidationData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.FilePortConsolidationData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.GetAttrFuncData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.NestedConsolidationData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.NestedTemplateConsolidationData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.PortTemplateConsolidationData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.RequirementAssignmentData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.SubInterfaceTemplateConsolidationData;
import org.openecomp.sdc.translator.datatypes.heattotosca.unifiedmodel.consolidation.TypeComputeConsolidationData;
import org.openecomp.sdc.translator.services.heattotosca.globaltypes.GlobalTypesGenerator;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.NotDirectoryException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
public class TestUtils {
private static final String MANIFEST_NAME = SdcCommon.MANIFEST_NAME;
private static String zipFilename = "VSP.zip";
private static String validationFilename = "validationOutput.json";
private static Logger logger = (Logger) LoggerFactory.getLogger(TestUtils.class);
private TestUtils() {
}
public static void addFilesToTranslator(HeatToToscaTranslator heatToToscaTranslator, String path)
throws IOException {
File manifestFile = new File(path);
File[] files = manifestFile.listFiles();
byte[] fileContent;
Assert.assertNotNull("manifest files is empty", files);
for (File file : files) {
try (FileInputStream fis = new FileInputStream(file)) {
fileContent = FileUtils.toByteArray(fis);
if (file.getName().equals(MANIFEST_NAME)) {
heatToToscaTranslator.addManifest(MANIFEST_NAME, fileContent);
} else {
if (!file.getName().equals(zipFilename) && (!file.getName().equals(validationFilename))) {
heatToToscaTranslator.addFile(file.getName(), fileContent);
}
}
}
}
}
/**
* Get tosca service template models for the files in a directory
*
* @param baseDirPath base directory for the tosca file
* @return Map of <ServiceTemplateFilename, ServiceTemplate> for the files in this directory
*/
private static Map<String, ServiceTemplate> getServiceTemplates(String baseDirPath) {
Map<String, ServiceTemplate> serviceTemplateMap = new HashMap<>();
ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
baseDirPath = "." + baseDirPath + "/";
try {
String[] fileList = {};
URL filesDirUrl = TestUtils.class.getClassLoader().getResource(baseDirPath);
if (filesDirUrl != null && filesDirUrl.getProtocol().equals("file")) {
fileList = new File(filesDirUrl.toURI()).list();
} else {
Assert.fail("Invalid expected output files directory");
}
for (String fileName : fileList) {
URL resource = TestUtils.class.getClassLoader().getResource(baseDirPath + fileName);
ServiceTemplate serviceTemplate = FileUtils.readViaInputStream(resource,
stream -> toscaExtensionYamlUtil.yamlToObject(stream, ServiceTemplate.class));
serviceTemplateMap.put(fileName, serviceTemplate);
}
} catch (Exception e) {
logger.debug("", e);
Assert.fail(e.getMessage());
}
return serviceTemplateMap;
}
/**
* Get tosca service template models
*
* @param expectedResultMap Map of filename and payload of the expected result files
* @return Map of <ServiceTemplateFilename, ServiceTemplate> for the files in this directory
*/
public static Map<String, ServiceTemplate> getServiceTemplates(Map<String, byte[]>
expectedResultMap) {
Map<String, ServiceTemplate> serviceTemplateMap = new HashMap<>();
ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
for (String fileName : expectedResultMap.keySet()) {
ServiceTemplate serviceTemplate = toscaExtensionYamlUtil.yamlToObject
(new String(expectedResultMap.get(fileName)), ServiceTemplate.class);
serviceTemplateMap.put(fileName, serviceTemplate);
}
return serviceTemplateMap;
}
public static ToscaServiceModel loadToscaServiceModel(String serviceTemplatesPath,
String globalServiceTemplatesPath,
String entryDefinitionServiceTemplate)
throws IOException {
ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
Map<String, ServiceTemplate> serviceTemplates = new HashMap<>();
if (entryDefinitionServiceTemplate == null) {
entryDefinitionServiceTemplate = "MainServiceTemplate.yaml";
}
serviceTemplates = getServiceTemplates(serviceTemplatesPath);
if (globalServiceTemplatesPath != null) {
serviceTemplates = getServiceTemplates(globalServiceTemplatesPath);
}
return new ToscaServiceModel(null, serviceTemplates, entryDefinitionServiceTemplate);
}
public static ServiceTemplate loadServiceTemplate(String serviceTemplatePath)
throws IOException {
ServiceTemplate serviceTemplateFromYaml = new ServiceTemplate();
ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
URL urlFile = TestUtils.class.getResource(serviceTemplatePath);
if (urlFile != null) {
File pathFile = new File(urlFile.getFile());
File[] files = pathFile.listFiles();
for (File file : files) {
try (InputStream yamlFile = new FileInputStream(file)) {
serviceTemplateFromYaml =
toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
createConcreteRequirementObjectsInServiceTemplate(serviceTemplateFromYaml,
toscaExtensionYamlUtil);
try {
yamlFile.close();
} catch (IOException ignore) {
logger.debug("", ignore);
}
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw e;
}
}
} else {
throw new NotDirectoryException(serviceTemplatePath);
}
return serviceTemplateFromYaml;
}
public static void loadServiceTemplates(String serviceTemplatesPath,
ToscaExtensionYamlUtil toscaExtensionYamlUtil,
Map<String, ServiceTemplate> serviceTemplates)
throws IOException {
URL urlFile = TestUtils.class.getResource(serviceTemplatesPath);
if (urlFile != null) {
File pathFile = new File(urlFile.getFile());
File[] files = pathFile.listFiles();
if (files != null) {
addServiceTemplateFiles(serviceTemplates, files, toscaExtensionYamlUtil);
} else {
throw new NotDirectoryException(serviceTemplatesPath);
}
} else {
throw new NotDirectoryException(serviceTemplatesPath);
}
}
private static void addServiceTemplateFiles(Map<String, ServiceTemplate> serviceTemplates,
File[] files,
ToscaExtensionYamlUtil toscaExtensionYamlUtil)
throws IOException {
for (File file : files) {
try (InputStream yamlFile = new FileInputStream(file)) {
ServiceTemplate serviceTemplateFromYaml =
toscaExtensionYamlUtil.yamlToObject(yamlFile, ServiceTemplate.class);
createConcreteRequirementObjectsInServiceTemplate(serviceTemplateFromYaml,
toscaExtensionYamlUtil);
serviceTemplates.put(file.getName(), serviceTemplateFromYaml);
try {
yamlFile.close();
} catch (IOException ignore) {
logger.debug("", ignore);
}
} catch (FileNotFoundException e) {
throw e;
} catch (IOException e) {
throw e;
}
}
}
private static void createConcreteRequirementObjectsInServiceTemplate(ServiceTemplate
serviceTemplateFromYaml,
ToscaExtensionYamlUtil
toscaExtensionYamlUtil) {
if (serviceTemplateFromYaml == null
|| serviceTemplateFromYaml.getTopology_template() == null
|| serviceTemplateFromYaml.getTopology_template().getNode_templates() == null) {
return;
}
//Creating concrete objects
Map<String, NodeTemplate> nodeTemplates =
serviceTemplateFromYaml.getTopology_template().getNode_templates();
for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) {
NodeTemplate nodeTemplate = entry.getValue();
List<Map<String, RequirementAssignment>> requirements = nodeTemplate.getRequirements();
List<Map<String, RequirementAssignment>> concreteRequirementList = new ArrayList<>();
if (requirements != null) {
ListIterator<Map<String, RequirementAssignment>> reqListIterator = requirements
.listIterator();
while (reqListIterator.hasNext()) {
Map<String, RequirementAssignment> requirement = reqListIterator.next();
Map<String, RequirementAssignment> concreteRequirement = new HashMap<>();
for (Map.Entry<String, RequirementAssignment> reqEntry : requirement.entrySet()) {
RequirementAssignment requirementAssignment = (toscaExtensionYamlUtil
.yamlToObject(toscaExtensionYamlUtil.objectToYaml(reqEntry.getValue()),
RequirementAssignment.class));
concreteRequirement.put(reqEntry.getKey(), requirementAssignment);
concreteRequirementList.add(concreteRequirement);
reqListIterator.remove();
}
}
requirements.clear();
requirements.addAll(concreteRequirementList);
nodeTemplate.setRequirements(requirements);
}
System.out.println();
//toscaExtensionYamlUtil.yamlToObject(nodeTemplate, NodeTemplate.class);
}
}
public static void updateMultiplePortConsolidationDatas(String serviceTemplateName,
List<String> portNodeTemplateIds,
List<List<String>> nodesConnectedInIds,
List<List<String>> nodesConnectedOutIds,
List<List<String>> groupIds,
List<List<String>> getAttrInIds,
List<List<Pair<String, GetAttrFuncData>>> getAttrOutFuncDataList,
ConsolidationData consolidationData) {
for (int i = 0; i < portNodeTemplateIds.size(); i++) {
updatePortConsolidationData(serviceTemplateName, portNodeTemplateIds.get(i),
nodesConnectedInIds.get(i), nodesConnectedOutIds.get(i),
groupIds.get(i), getAttrInIds.get(i), getAttrOutFuncDataList.get(i), consolidationData);
}
}
public static void addPortSubInterface(
SubInterfaceConsolidationDataTestInfo subInterfaceConsolidationDataTestInfo) {
PortTemplateConsolidationData portTemplateConsolidationData =
subInterfaceConsolidationDataTestInfo.getConsolidationData().getPortConsolidationData().getFilePortConsolidationData
(subInterfaceConsolidationDataTestInfo.getServiceTemplateFileName()).getPortTemplateConsolidationData(
subInterfaceConsolidationDataTestInfo.getPortNodeTemplateId());
SubInterfaceTemplateConsolidationData subInterface =
new SubInterfaceTemplateConsolidationData();
subInterface.setNodeTemplateId(subInterfaceConsolidationDataTestInfo.getSubInterfaceId());
subInterface.setNetworkRole(subInterfaceConsolidationDataTestInfo.getNetworkRole());
subInterface.setResourceGroupCount(
subInterfaceConsolidationDataTestInfo.getResourceGroupCount());
updateRelationsForEntityConsolidationData(
subInterfaceConsolidationDataTestInfo.getNodesConnectedIn(),
subInterfaceConsolidationDataTestInfo.getNodesConnectedOut(), null,
subInterfaceConsolidationDataTestInfo.getNodesGetAttrIn(),
subInterfaceConsolidationDataTestInfo.getNodesGetAttrOut(), subInterface);
portTemplateConsolidationData.addSubInterfaceConsolidationData(
subInterfaceConsolidationDataTestInfo.getSubInterfaceType(), subInterface);
}
public static void updatePortConsolidationData(String serviceTemplateFileName,
String portNodeTemplateId,
List<String> nodesConnectedInIds,
List<String> nodesConnectedOutIds,
List<String> groupIds, List<String> getAttrInIds,
List<Pair<String, GetAttrFuncData>> getAttrOutFuncDataList,
ConsolidationData consolidationData) {
PortTemplateConsolidationData portTemplateConsolidationData =
createPortTemplateConsolidationData(portNodeTemplateId);
updateRelationsForEntityConsolidationData(nodesConnectedInIds,
nodesConnectedOutIds, groupIds, getAttrInIds, getAttrOutFuncDataList,
portTemplateConsolidationData);
consolidationData.getPortConsolidationData()
.getFilePortConsolidationData(serviceTemplateFileName)
.setPortTemplateConsolidationData(portNodeTemplateId, portTemplateConsolidationData);
}
public static PortTemplateConsolidationData createPortTemplateConsolidationData(
String portNodeTemplateId) {
PortTemplateConsolidationData portTemplateConsolidationData =
new PortTemplateConsolidationData();
portTemplateConsolidationData.setNodeTemplateId(portNodeTemplateId);
return portTemplateConsolidationData;
}
public static void initPortConsolidationData(String serviceTemplateFileName,
ConsolidationData consolidationData) {
consolidationData.getPortConsolidationData()
.setFilePortConsolidationData(serviceTemplateFileName, new FilePortConsolidationData());
}
public static void updateComputeTemplateConsolidationData(String serviceTemplateFileName,
String computeNodeTypeName,
String computeNodeTemplateId,
List<String> nodeIdsConnectedIn,
List<String> nodeIdsConnectedOut,
List<String> volumeIds,
List<String> groupIds,
List<String> getAttrInIds,
List<Pair<String, GetAttrFuncData>> getAttrOutIds,
List<Pair<String, String>> portTypeToIdList,
ConsolidationData consolidationData) {
initComputeNodeTemplateIdInConsolidationData(serviceTemplateFileName, computeNodeTypeName,
computeNodeTemplateId, consolidationData);
Map<String, List<RequirementAssignmentData>> volumes =
consolidationData.getComputeConsolidationData().getFileComputeConsolidationData
(serviceTemplateFileName).getTypeComputeConsolidationData(computeNodeTypeName)
.getComputeTemplateConsolidationData(computeNodeTemplateId).getVolumes();
ComputeTemplateConsolidationData computeTemplateConsolidationData =
createComputeTemplateConsolidationData(computeNodeTemplateId, portTypeToIdList, volumes);
updateRelationsForEntityConsolidationData(nodeIdsConnectedIn,
nodeIdsConnectedOut, groupIds, getAttrInIds, getAttrOutIds,
computeTemplateConsolidationData);
updateVolumes(computeTemplateConsolidationData, volumeIds);
consolidationData.getComputeConsolidationData()
.getFileComputeConsolidationData(serviceTemplateFileName)
.getTypeComputeConsolidationData(computeNodeTypeName)
.setComputeTemplateConsolidationData(computeNodeTemplateId,
computeTemplateConsolidationData);
}
private static void updateRelationsForEntityConsolidationData(List<String> nodeIdsConnectedIn,
List<String> nodeIdsConnectedOut,
List<String> groupIds,
List<String> getAttrInIds,
List<Pair<String, GetAttrFuncData>> getAttrOutFuncDataList,
EntityConsolidationData entity) {
updateRelationsIn(entity, nodeIdsConnectedIn);
updateRelationsOut(entity, nodeIdsConnectedOut);
updateGetAttrIn(entity, getAttrInIds);
updateGetAttrOut(entity, getAttrOutFuncDataList);
entity.setGroupIds(groupIds);
}
public static void initComputeNodeTemplateIdInConsolidationData(String serviceTemplateFileName,
String computeNodeTypeName,
String computeNodeTemplateId,
ConsolidationData consolidationData) {
if (Objects
.isNull(consolidationData.getComputeConsolidationData().getFileComputeConsolidationData
(serviceTemplateFileName))) {
consolidationData.getComputeConsolidationData().setFileComputeConsolidationData
(serviceTemplateFileName, new FileComputeConsolidationData());
}
TypeComputeConsolidationData typeComputeConsolidationData =
consolidationData.getComputeConsolidationData().getFileComputeConsolidationData
(serviceTemplateFileName).getTypeComputeConsolidationData(computeNodeTypeName);
if (
typeComputeConsolidationData.getComputeTemplateConsolidationData(computeNodeTemplateId) ==
null) {
consolidationData.getComputeConsolidationData()
.getFileComputeConsolidationData(serviceTemplateFileName)
.getTypeComputeConsolidationData(computeNodeTypeName)
.setComputeTemplateConsolidationData(computeNodeTemplateId,
new ComputeTemplateConsolidationData());
}
}
public static void updateNestedConsolidationData(String serviceTemplateName,
List<String> substitutionNodeTemplateIds,
ConsolidationData consolidationData) {
if (Objects.isNull(consolidationData.getNestedConsolidationData())) {
consolidationData.setNestedConsolidationData(new NestedConsolidationData());
}
FileNestedConsolidationData fileNestedConsolidationData = new FileNestedConsolidationData();
for (String substitutionNodeTemplateId : substitutionNodeTemplateIds) {
NestedTemplateConsolidationData nestedTemplateConsolidationData =
new NestedTemplateConsolidationData();
nestedTemplateConsolidationData.setNodeTemplateId(substitutionNodeTemplateId);
fileNestedConsolidationData.setNestedTemplateConsolidationData(substitutionNodeTemplateId,
nestedTemplateConsolidationData);
}
consolidationData.getNestedConsolidationData()
.setFileNestedConsolidationData(serviceTemplateName, fileNestedConsolidationData);
}
public static ComputeTemplateConsolidationData createComputeTemplateConsolidationData(
String computeNodeTemplateId,
List<Pair<String, String>> portTypeToIdList,
Map<String, List<RequirementAssignmentData>> volumes) {
ComputeTemplateConsolidationData compute = new ComputeTemplateConsolidationData();
compute.setNodeTemplateId(computeNodeTemplateId);
if (portTypeToIdList != null) {
for (Pair<String, String> port : portTypeToIdList) {
compute.addPort(port.getLeft(), port.getRight());
}
}
compute.setVolumes(volumes);
return compute;
}
private static void updateRelationsIn(EntityConsolidationData entity,
List<String> idsPontingTome) {
if (CollectionUtils.isEmpty(idsPontingTome)) {
return;
}
for (String pointingId : idsPontingTome) {
entity
.addNodesConnectedIn(pointingId, entity.getNodeTemplateId(), new RequirementAssignment());
}
}
private static void updateRelationsOut(EntityConsolidationData entity,
List<String> idsToUpdate) {
if (CollectionUtils.isEmpty(idsToUpdate)) {
return;
}
for (String id : idsToUpdate) {
entity.addNodesConnectedOut(id, id, new RequirementAssignment());
}
}
private static void updateGetAttrIn(EntityConsolidationData entity,
List<String> idsToUpdate) {
if (CollectionUtils.isEmpty(idsToUpdate)) {
return;
}
for (String id : idsToUpdate) {
entity.addNodesGetAttrIn(id, new GetAttrFuncData());
}
}
private static void updateGetAttrOut(EntityConsolidationData entity,
List<Pair<String, GetAttrFuncData>> getAttrOutIds) {
if (CollectionUtils.isEmpty(getAttrOutIds)) {
return;
}
for (Pair<String, GetAttrFuncData> getAttrOutFunc : getAttrOutIds) {
entity.addNodesGetAttrOut(getAttrOutFunc.getLeft(), getAttrOutFunc.getRight());
}
}
private static void updateVolumes(ComputeTemplateConsolidationData compute,
List<String> volumeIds) {
if (CollectionUtils.isEmpty(volumeIds)) {
return;
}
for (String id : volumeIds) {
RequirementAssignment requirementAssignment = new RequirementAssignment();
requirementAssignment.setNode(id);
compute.addVolume(id, requirementAssignment);
}
}
public static void initComputeNodeTypeInConsolidationData(String serviceTemplateFileName,
String computeNodeTypeName,
ConsolidationData consolidationData) {
ComputeConsolidationData computeConsolidationData =
consolidationData.getComputeConsolidationData();
if (!computeConsolidationData.getAllServiceTemplateFileNames()
.contains(serviceTemplateFileName)) {
computeConsolidationData
.setFileComputeConsolidationData(serviceTemplateFileName,
new FileComputeConsolidationData());
}
computeConsolidationData
.getFileComputeConsolidationData(serviceTemplateFileName).setTypeComputeConsolidationData(
computeNodeTypeName, new TypeComputeConsolidationData());
}
public static Map<String, List<RequirementAssignmentData>> getNodeConnectedOutList(
NodeTemplate nodeTemplate, String requirementKey) {
Map<String, List<RequirementAssignmentData>> requirementAssignmentDataMap = new HashMap<>();
Optional<List<RequirementAssignmentData>> requirementAssignmentDataList =
TestUtils.createRequirementAssignmentDataList(nodeTemplate, requirementKey);
if (requirementAssignmentDataList.isPresent()) {
for (RequirementAssignmentData requirementAssignmentData : requirementAssignmentDataList
.get()) {
String connectedNodeTemplateId = requirementAssignmentData.getRequirementAssignment()
.getNode();
requirementAssignmentDataMap
.computeIfAbsent(connectedNodeTemplateId, k -> new ArrayList<>());
requirementAssignmentDataMap.get(connectedNodeTemplateId).add(requirementAssignmentData);
}
}
return requirementAssignmentDataMap;
}
public static Map<String, List<GetAttrFuncData>> getNodesGetAttrIn(NodeTemplate nodeTemplate,
String nodeTemplateId) {
Map<String, List<GetAttrFuncData>> nodesGetAttrIn = new HashMap<>();
List<GetAttrFuncData> getAttrList = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> nodeTemplateProperties = nodeTemplate.getProperties();
for (Map.Entry<String, Object> propertyEntry : nodeTemplateProperties.entrySet()) {
Map<String, List> propertyValue = mapper.convertValue(propertyEntry.getValue(), Map.class);
for (Map.Entry<String, List> entry : propertyValue.entrySet()) {
if (entry.getKey().equals("get_attribute")) {
GetAttrFuncData data = new GetAttrFuncData();
data.setFieldName(propertyEntry.getKey());
data.setAttributeName(entry.getValue().get(1).toString());
getAttrList.add(data);
}
}
System.out.println();
}
nodesGetAttrIn.put(nodeTemplateId, getAttrList);
return nodesGetAttrIn;
}
public static void updatePortsInComputeTemplateConsolidationData(
List<Pair<String, String>> portIdToTypeList, ComputeTemplateConsolidationData
compute) {
compute.setPorts(new HashMap<>());
for (Pair<String, String> portIdToType : portIdToTypeList) {
compute.getPorts().putIfAbsent(portIdToType.getLeft(), new ArrayList<>());
compute.getPorts().get(portIdToType.getLeft()).add(portIdToType.getRight());
}
}
public static Map<String, List<RequirementAssignmentData>> getNodeConnectedInList(
String sourceNodeTemplateId,
ServiceTemplate serviceTemplate, String requirementKey) {
Optional<List<RequirementAssignmentData>> requirementAssignmentDataList = Optional.empty();
List<RequirementAssignmentData> assignmentDataList = new ArrayList<>();
Map<String, List<RequirementAssignmentData>> requirementAssignmentDataMap = new HashMap<>();
Map<String, NodeTemplate> nodeTemplates = serviceTemplate.getTopology_template()
.getNode_templates();
for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) {
String nodeTemplateId = entry.getKey();
List<Map<String, RequirementAssignment>> requirements = entry.getValue().getRequirements();
if (requirements != null) {
for (Map<String, RequirementAssignment> requirement : requirements) {
if (requirement.get(requirementKey) != null) {
RequirementAssignment requirementAssignment = requirement.get(requirementKey);
if (requirementAssignment != null) {
if (requirementAssignment.getNode().equals(sourceNodeTemplateId)) {
RequirementAssignmentData data = new RequirementAssignmentData(requirementKey,
requirementAssignment);
assignmentDataList.add(data);
}
}
}
}
requirementAssignmentDataList = Optional.ofNullable(assignmentDataList);
if (requirementAssignmentDataList.isPresent()) {
for (RequirementAssignmentData requirementAssignmentData : requirementAssignmentDataList
.get()) {
requirementAssignmentDataMap.computeIfAbsent(nodeTemplateId, k -> new ArrayList<>());
requirementAssignmentDataMap.get(nodeTemplateId).add(requirementAssignmentData);
}
}
requirementAssignmentDataList = Optional.empty();
}
}
return requirementAssignmentDataMap;
}
public static List<String> getGroupsForNode(ServiceTemplate serviceTemplate, String
nodeTemplateId) {
List<String> entityGroups = new ArrayList<>();
Map<String, GroupDefinition> groups = serviceTemplate.getTopology_template().getGroups();
Map<String, NodeTemplate> nodeTemplates =
serviceTemplate.getTopology_template().getNode_templates();
for (Map.Entry<String, GroupDefinition> entry : groups.entrySet()) {
String groupId = entry.getKey();
GroupDefinition groupDefinition = entry.getValue();
if (groupDefinition.getType().contains("HeatStack")) {
continue;
}
List<String> groupMembers = groupDefinition.getMembers();
for (String member : groupMembers) {
if (groups.containsKey(member)) {
continue;
}
if (member.equals(nodeTemplateId)) {
entityGroups.add(groupId);
}
}
}
return entityGroups;
}
private static Optional<List<RequirementAssignmentData>> createRequirementAssignmentDataList(
NodeTemplate nodeTemplate, String requirementKey) {
Optional<List<RequirementAssignment>> requirementAssignmentLink =
DataModelUtil.getRequirementAssignment(nodeTemplate.getRequirements(), requirementKey);
if (!requirementAssignmentLink.isPresent()) {
return Optional.empty();
}
List<RequirementAssignmentData> requirementAssignmentDataList = new ArrayList<>();
for (RequirementAssignment requirementAssignment : requirementAssignmentLink.get()) {
RequirementAssignmentData requirementAssignmentData = new RequirementAssignmentData
(requirementKey, requirementAssignment);
requirementAssignmentDataList.add(requirementAssignmentData);
}
return Optional.ofNullable(requirementAssignmentDataList);
}
public static Optional<List<RequirementAssignmentData>> getRequirementAssignmentDataList(
NodeTemplate nodeTemplate, String requirementKey) {
List<RequirementAssignmentData> returnedReqAssignmentDataList = new ArrayList<>();
Optional<List<RequirementAssignmentData>> requirementAssignmentDataList =
TestUtils.createRequirementAssignmentDataList(nodeTemplate, requirementKey);
if (requirementAssignmentDataList.isPresent()) {
for (RequirementAssignmentData requirementAssignmentData : requirementAssignmentDataList
.get()) {
returnedReqAssignmentDataList.add(requirementAssignmentData);
}
return Optional.of(returnedReqAssignmentDataList);
}
return Optional.empty();
}
public static ServiceTemplate createInitServiceTemplate() {
ServiceTemplate initServiceTemplate = new ServiceTemplate();
Map<String, String> templateMetadata = new HashMap<>();
templateMetadata.put(ToscaConstants.ST_METADATA_TEMPLATE_NAME, "Test");
initServiceTemplate.setTosca_definitions_version(ToscaConstants.TOSCA_DEFINITIONS_VERSION);
initServiceTemplate.setMetadata(templateMetadata);
initServiceTemplate.setTopology_template(new TopologyTemplate());
initServiceTemplate.setImports(GlobalTypesGenerator.getGlobalTypesImportList());
return initServiceTemplate;
}
}
|