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
|
/*-
* ============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.be.model.operations.impl;
import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
import static org.springframework.util.CollectionUtils.isEmpty;
import fj.data.Either;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
import org.openecomp.sdc.be.dao.utils.MapUtil;
import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.model.DataTypeDefinition;
import org.openecomp.sdc.be.model.GroupDefinition;
import org.openecomp.sdc.be.model.GroupProperty;
import org.openecomp.sdc.be.model.GroupTypeDefinition;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.TopologyTemplateOperation;
import org.openecomp.sdc.be.model.operations.StorageException;
import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.resources.data.ArtifactData;
import org.openecomp.sdc.be.resources.data.ComponentInstanceData;
import org.openecomp.sdc.be.resources.data.GroupData;
import org.openecomp.sdc.be.resources.data.GroupTypeData;
import org.openecomp.sdc.be.resources.data.PropertyData;
import org.openecomp.sdc.be.resources.data.PropertyValueData;
import org.openecomp.sdc.be.resources.data.UniqueIdData;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.springframework.stereotype.Component;
@Component
public class GroupOperation extends AbstractOperation implements IGroupOperation {
private static final Logger log = Logger.getLogger(GroupOperation.class.getName());
private final JanusGraphDao janusGraphDao;
private final TopologyTemplateOperation topologyTemplateOperation;
private final PropertyOperation propertyOperation;
private final GroupTypeOperation groupTypeOperation;
private final ApplicationDataTypeCache applicationDataTypeCache;
public GroupOperation(JanusGraphDao janusGraphDao, TopologyTemplateOperation topologyTemplateOperation, PropertyOperation propertyOperation,
GroupTypeOperation groupTypeOperation, ApplicationDataTypeCache applicationDataTypeCache) {
this.janusGraphDao = janusGraphDao;
this.topologyTemplateOperation = topologyTemplateOperation;
this.propertyOperation = propertyOperation;
this.groupTypeOperation = groupTypeOperation;
this.applicationDataTypeCache = applicationDataTypeCache;
}
private GroupDefinition convertGroupDataToGroupDefinition(GroupData groupData) {
return new GroupDefinition(groupData.getGroupDataDefinition());
}
/**
* get members of group
*
* @param groupUniqueId
* @return
*/
private Either<Map<String, String>, JanusGraphOperationStatus> getGroupMembers(String groupUniqueId) {
Either<Map<String, String>, JanusGraphOperationStatus> result = null;
Either<List<ImmutablePair<ComponentInstanceData, GraphEdge>>, JanusGraphOperationStatus> childrenNodes = janusGraphGenericDao
.getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Group), groupUniqueId, GraphEdgeLabels.GROUP_MEMBER,
NodeTypeEnum.ResourceInstance, ComponentInstanceData.class);
if (childrenNodes.isRight()) {
JanusGraphOperationStatus status = childrenNodes.right().value();
if (status == JanusGraphOperationStatus.NOT_FOUND) {
status = JanusGraphOperationStatus.OK;
}
result = Either.right(status);
} else {
Map<String, String> compInstaMap = new HashMap<>();
List<ImmutablePair<ComponentInstanceData, GraphEdge>> list = childrenNodes.left().value();
if (list != null) {
for (ImmutablePair<ComponentInstanceData, GraphEdge> pair : list) {
ComponentInstanceData componentInstanceData = pair.getKey();
String compInstUniqueId = componentInstanceData.getComponentInstDataDefinition().getUniqueId();
String compInstName = componentInstanceData.getName();
compInstaMap.put(compInstName, compInstUniqueId);
}
}
result = Either.left(compInstaMap);
}
return result;
}
private Either<GroupTypeDefinition, StorageOperationStatus> getGroupTypeOfGroup(String groupUniqueId) {
Either<ImmutablePair<GroupTypeData, GraphEdge>, StorageOperationStatus> groupTypeRes = janusGraphGenericDao
.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Group), groupUniqueId, GraphEdgeLabels.TYPE_OF, NodeTypeEnum.GroupType,
GroupTypeData.class).right().map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
if (groupTypeRes.isRight()) {
StorageOperationStatus status = groupTypeRes.right().value();
log.debug("Cannot find group type associated with capability {}. Status is {}", groupUniqueId, status);
BeEcompErrorManager.getInstance()
.logBeFailedFindAssociationError("Fetch Group type", NodeTypeEnum.GroupType.getName(), groupUniqueId, String.valueOf(status));
return Either.right(groupTypeRes.right().value());
}
GroupTypeData groupTypeData = groupTypeRes.left().value().getKey();
return groupTypeOperation.getGroupTypeByUid(groupTypeData.getGroupTypeDataDefinition().getUniqueId());
}
/**
* get all properties of the group.
* <p>
* the propert definition is taken from the group type.
*
* @param groupUid
* @return
*/
private Either<List<GroupProperty>, StorageOperationStatus> getGroupProperties(String groupUid) {
List<GroupProperty> groupPropertiesList = new ArrayList<>();
Either<GroupTypeDefinition, StorageOperationStatus> groupTypeOfGroupRes = getGroupTypeOfGroup(groupUid);
if (groupTypeOfGroupRes.isRight()) {
StorageOperationStatus status = groupTypeOfGroupRes.right().value();
return Either.right(status);
}
GroupTypeDefinition groupTypeDefinition = groupTypeOfGroupRes.left().value();
// Get the properties on the group type of this group
List<PropertyDefinition> groupTypeProperties = groupTypeDefinition.getProperties();
if (isEmpty(groupTypeProperties)) {
return Either.right(StorageOperationStatus.OK);
}
Map<String, PropertyDefinition> uidToPropDefMap = groupTypeProperties.stream()
.collect(Collectors.toMap(PropertyDefinition::getUniqueId, Function.identity()));
// Find all properties values on the group
Either<List<ImmutablePair<PropertyValueData, GraphEdge>>, StorageOperationStatus> propertyImplNodes = janusGraphGenericDao
.getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Group), groupUid, GraphEdgeLabels.PROPERTY_VALUE,
NodeTypeEnum.PropertyValue, PropertyValueData.class).right().map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
if (propertyImplNodes.isRight()) {
StorageOperationStatus status = propertyImplNodes.right().value();
if (status == StorageOperationStatus.NOT_FOUND) {
groupPropertiesList = groupTypeProperties.stream().map(p -> new GroupProperty(p, p.getDefaultValue(), null))
.collect(Collectors.toList());
return Either.left(groupPropertiesList);
} else {
return Either.right(status);
}
}
List<ImmutablePair<PropertyValueData, GraphEdge>> list = propertyImplNodes.left().value();
if (isEmpty(list)) {
return Either.right(StorageOperationStatus.OK);
}
List<String> processedProps = new ArrayList<>();
for (ImmutablePair<PropertyValueData, GraphEdge> propertyValue : list) {
PropertyValueData propertyValueData = propertyValue.getLeft();
String propertyValueUid = propertyValueData.getUniqueId();
String value = propertyValueData.getValue();
Either<ImmutablePair<PropertyData, GraphEdge>, StorageOperationStatus> propertyDefRes = janusGraphGenericDao
.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.PropertyValue), propertyValueUid, GraphEdgeLabels.PROPERTY_IMPL,
NodeTypeEnum.Property, PropertyData.class).right().map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
if (propertyDefRes.isRight()) {
StorageOperationStatus status = propertyDefRes.right().value();
if (status == StorageOperationStatus.NOT_FOUND) {
status = StorageOperationStatus.INVALID_ID;
}
return Either.right(status);
}
ImmutablePair<PropertyData, GraphEdge> propertyDefPair = propertyDefRes.left().value();
PropertyData propertyData = propertyDefPair.left;
String propertyUniqueId = propertyData.getPropertyDataDefinition().getUniqueId();
PropertyDefinition propertyDefinition = uidToPropDefMap.get(propertyUniqueId);
GroupProperty groupProperty = new GroupProperty(propertyDefinition, value, propertyValueUid);
processedProps.add(propertyUniqueId);
groupPropertiesList.add(groupProperty);
}
// Find all properties which does not have property value on the group.
List<GroupProperty> leftProps = groupTypeProperties.stream()
// filter out the group type properties which already processed
.filter(p -> !processedProps.contains(p.getUniqueId())).map(p -> new GroupProperty(p, p.getDefaultValue(), null))
.collect(Collectors.toList());
if (leftProps != null) {
groupPropertiesList.addAll(leftProps);
}
return Either.left(groupPropertiesList);
}
public Either<List<GraphRelation>, StorageOperationStatus> dissociateAllGroupsFromArtifactOnGraph(String componentId,
NodeTypeEnum componentTypeEnum,
String artifactId) {
List<GraphRelation> relations = new ArrayList<>();
Either<List<GraphRelation>, StorageOperationStatus> result = Either.left(relations);
Either<List<GroupDefinition>, StorageOperationStatus> allGroupsFromGraph = getAllGroupsFromGraph(componentId, componentTypeEnum, true, true,
false);
if (allGroupsFromGraph.isRight()) {
StorageOperationStatus status = allGroupsFromGraph.right().value();
return Either.right(status);
}
List<GroupDefinition> allGroups = allGroupsFromGraph.left().value();
if (isEmpty(allGroups)) {
return Either.right(StorageOperationStatus.OK);
}
// Find all groups which contains this artifact id
List<GroupDefinition> associatedGroups = allGroups.stream().filter(p -> p.getArtifacts() != null && p.getArtifacts().contains(artifactId))
.collect(Collectors.toList());
if (isNotEmpty(associatedGroups)) {
log.debug("The groups {} contains the artifact {}",
associatedGroups.stream().map(GroupDataDefinition::getName).collect(Collectors.toList()), artifactId);
UniqueIdData artifactData = new UniqueIdData(NodeTypeEnum.ArtifactRef, artifactId);
for (GroupDefinition groupDefinition : associatedGroups) {
UniqueIdData groupData = new UniqueIdData(NodeTypeEnum.Group, groupDefinition.getUniqueId());
Either<GraphRelation, StorageOperationStatus> deleteRelation = janusGraphGenericDao
.deleteRelation(groupData, artifactData, GraphEdgeLabels.GROUP_ARTIFACT_REF).right()
.map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
if (deleteRelation.isRight()) {
StorageOperationStatus status = deleteRelation.right().value();
if (status == StorageOperationStatus.NOT_FOUND) {
status = StorageOperationStatus.INVALID_ID;
}
return Either.right(status);
}
relations.add(deleteRelation.left().value());
}
return result;
} else {
log.debug("No group under component id {} is associated to artifact {}", componentId, artifactId);
return Either.right(StorageOperationStatus.OK);
}
}
public Either<GroupDefinition, StorageOperationStatus> getGroupFromGraph(String uniqueId, boolean skipProperties, boolean skipMembers,
boolean skipArtifacts) {
Either<GroupDefinition, StorageOperationStatus> result = null;
Either<GroupData, StorageOperationStatus> groupRes = janusGraphGenericDao
.getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Group), uniqueId, GroupData.class).right()
.map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
if (groupRes.isRight()) {
StorageOperationStatus status = groupRes.right().value();
log.debug("Failed to retrieve group {} from graph. Status is {}", uniqueId, status);
BeEcompErrorManager.getInstance().logBeFailedRetrieveNodeError("Fetch Group", uniqueId, String.valueOf(status));
result = Either.right(status);
return result;
}
GroupData groupData = groupRes.left().value();
GroupDefinition groupDefinition = convertGroupDataToGroupDefinition(groupData);
Either<GroupTypeDefinition, StorageOperationStatus> groupTypeOfGroup = getGroupTypeOfGroup(uniqueId);
if (groupTypeOfGroup.isRight()) {
StorageOperationStatus status = groupTypeOfGroup.right().value();
log.debug("Failed to retrieve capability type of capability {}. Status is {}", uniqueId, status);
result = Either.right(status);
return result;
}
GroupTypeDefinition groupTypeDefinition = groupTypeOfGroup.left().value();
groupDefinition.setTypeUid(groupTypeDefinition.getUniqueId());
if (!skipMembers) {
Either<Map<String, String>, StorageOperationStatus> membersRes = getGroupMembers(uniqueId).right()
.map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
if (membersRes.isRight()) {
StorageOperationStatus status = membersRes.right().value();
if (status != StorageOperationStatus.OK) {
result = Either.right(status);
return result;
}
} else {
Map<String, String> members = membersRes.left().value();
groupDefinition.setMembers(members);
}
}
if (!skipProperties) {
Either<List<GroupProperty>, StorageOperationStatus> propertiesRes = getGroupProperties(uniqueId);
if (propertiesRes.isRight()) {
StorageOperationStatus status = propertiesRes.right().value();
if (status != StorageOperationStatus.OK) {
result = Either.right(status);
return result;
}
} else {
List<GroupProperty> properties = propertiesRes.left().value();
groupDefinition.convertFromGroupProperties(properties);
}
}
if (!skipArtifacts) {
Either<List<ImmutablePair<String, String>>, StorageOperationStatus> artifactsRes = getGroupArtifactsPairs(uniqueId).right()
.map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
if (artifactsRes.isRight()) {
StorageOperationStatus status = artifactsRes.right().value();
if (status != StorageOperationStatus.OK) {
result = Either.right(status);
return result;
}
} else {
List<String> artifactsUid = new ArrayList<>();
List<String> artifactsUUID = new ArrayList<>();
List<ImmutablePair<String, String>> list = artifactsRes.left().value();
if (list != null) {
for (ImmutablePair<String, String> pair : list) {
String uid = pair.left;
String UUID = pair.right;
artifactsUid.add(uid);
artifactsUUID.add(UUID);
}
groupDefinition.setArtifacts(artifactsUid);
groupDefinition.setArtifactsUuid(artifactsUUID);
}
}
}
result = Either.left(groupDefinition);
return result;
}
public boolean isGroupExist(String groupName, boolean inTransaction) {
Either<List<GroupData>, JanusGraphOperationStatus> eitherGroup = null;
try {
Map<String, Object> properties = new HashMap<>();
properties.put(GraphPropertiesDictionary.NAME.getProperty(), groupName);
eitherGroup = janusGraphGenericDao.getByCriteria(NodeTypeEnum.Group, properties, GroupData.class);
return eitherGroup.isLeft() && !eitherGroup.left().value().isEmpty();
} finally {
handleTransactionCommitRollback(inTransaction, eitherGroup);
}
}
protected Either<List<GroupDefinition>, StorageOperationStatus> getAllGroupsFromGraph(String componentId, NodeTypeEnum componentTypeEnum,
boolean skipProperties, boolean skipMembers,
boolean skipArtifacts) {
List<GroupDefinition> groups = new ArrayList<>();
Either<List<ImmutablePair<GroupData, GraphEdge>>, StorageOperationStatus> childrenNodes = janusGraphGenericDao
.getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(componentTypeEnum), componentId, GraphEdgeLabels.GROUP, NodeTypeEnum.Group,
GroupData.class).right().map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
if (childrenNodes.isRight()) {
StorageOperationStatus status = childrenNodes.right().value();
return Either.right(status);
}
List<ImmutablePair<GroupData, GraphEdge>> graphGroups = childrenNodes.left().value();
if (isEmpty(graphGroups)) {
return Either.right(StorageOperationStatus.NOT_FOUND);
}
for (ImmutablePair<GroupData, GraphEdge> pair : graphGroups) {
String groupUniqueId = pair.left.getGroupDataDefinition().getUniqueId();
Either<GroupDefinition, StorageOperationStatus> groupRes = this
.getGroupFromGraph(groupUniqueId, skipProperties, skipMembers, skipArtifacts);
if (groupRes.isRight()) {
StorageOperationStatus status = groupRes.right().value();
if (status == StorageOperationStatus.NOT_FOUND) {
status = StorageOperationStatus.INVALID_ID;
}
return Either.right(status);
} else {
groups.add(groupRes.left().value());
}
}
return Either.left(groups);
}
private StorageOperationStatus dissociateAndAssociateGroupsFromArtifactOnGraph(String componentId, NodeTypeEnum componentTypeEnum,
String oldArtifactId, ArtifactData newArtifact) {
Either<List<GroupDefinition>, StorageOperationStatus> allGroupsFromGraph = getAllGroupsFromGraph(componentId, componentTypeEnum, true, true,
false);
if (allGroupsFromGraph.isRight()) {
return allGroupsFromGraph.right().value();
}
List<GroupDefinition> allGroups = allGroupsFromGraph.left().value();
if (isEmpty(allGroups)) {
return StorageOperationStatus.OK;
}
// Find all groups which contains this artifact id
List<GroupDefinition> associatedGroups = allGroups.stream().filter(p -> p.getArtifacts() != null && p.getArtifacts().contains(oldArtifactId))
.collect(Collectors.toList());
if (isNotEmpty(associatedGroups)) {
log.debug("The groups {} contains the artifact {}",
associatedGroups.stream().map(GroupDataDefinition::getName).collect(Collectors.toList()), oldArtifactId);
UniqueIdData oldArtifactData = new UniqueIdData(NodeTypeEnum.ArtifactRef, oldArtifactId);
UniqueIdData newArtifactData = new UniqueIdData(NodeTypeEnum.ArtifactRef, newArtifact.getArtifactDataDefinition().getUniqueId());
Map<String, Object> props = new HashMap<>();
props.put(GraphPropertiesDictionary.NAME.getProperty(), newArtifactData.getLabel());
for (GroupDefinition groupDefinition : associatedGroups) {
UniqueIdData groupData = new UniqueIdData(NodeTypeEnum.Group, groupDefinition.getUniqueId());
Either<GraphRelation, StorageOperationStatus> deleteRelation = janusGraphGenericDao
.deleteRelation(groupData, oldArtifactData, GraphEdgeLabels.GROUP_ARTIFACT_REF).right()
.map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
log.trace("After dissociate group {} from artifact {}", groupDefinition.getName(), oldArtifactId);
if (deleteRelation.isRight()) {
StorageOperationStatus status = deleteRelation.right().value();
if (status == StorageOperationStatus.NOT_FOUND) {
status = StorageOperationStatus.INVALID_ID;
}
return status;
}
Either<GraphRelation, StorageOperationStatus> createRelation = janusGraphGenericDao
.createRelation(groupData, newArtifactData, GraphEdgeLabels.GROUP_ARTIFACT_REF, props).right()
.map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
log.trace("After associate group {} to artifact {}", groupDefinition.getName(), newArtifact.getUniqueIdKey());
if (createRelation.isRight()) {
StorageOperationStatus status = createRelation.right().value();
if (status == StorageOperationStatus.NOT_FOUND) {
status = StorageOperationStatus.INVALID_ID;
}
return status;
}
}
}
return StorageOperationStatus.OK;
}
public StorageOperationStatus dissociateAndAssociateGroupsFromArtifact(String componentId, NodeTypeEnum componentTypeEnum, String oldArtifactId,
ArtifactData newArtifact, boolean inTransaction) {
StorageOperationStatus result = null;
try {
StorageOperationStatus status = this
.dissociateAndAssociateGroupsFromArtifactOnGraph(componentId, componentTypeEnum, oldArtifactId, newArtifact);
if (status != StorageOperationStatus.OK && status != StorageOperationStatus.NOT_FOUND) {
return status;
}
result = StorageOperationStatus.OK;
return result;
} finally {
if (!inTransaction) {
if (result == null || result != StorageOperationStatus.OK) {
log.debug("Going to execute rollback on graph.");
BeEcompErrorManager.getInstance().logBeExecuteRollbackError("Rollback on graph");
janusGraphGenericDao.rollback();
} else {
log.debug("Going to execute commit on graph.");
janusGraphGenericDao.commit();
}
}
}
}
private Either<List<ImmutablePair<String, String>>, JanusGraphOperationStatus> getGroupArtifactsPairs(String groupUniqueId) {
Either<List<ImmutablePair<String, String>>, JanusGraphOperationStatus> result = null;
Either<List<ImmutablePair<ArtifactData, GraphEdge>>, JanusGraphOperationStatus> childrenNodes = janusGraphGenericDao
.getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Group), groupUniqueId, GraphEdgeLabels.GROUP_ARTIFACT_REF,
NodeTypeEnum.ArtifactRef, ArtifactData.class);
if (childrenNodes.isRight()) {
JanusGraphOperationStatus status = childrenNodes.right().value();
if (status == JanusGraphOperationStatus.NOT_FOUND) {
status = JanusGraphOperationStatus.OK;
}
result = Either.right(status);
} else {
List<ImmutablePair<String, String>> artifactsList = new ArrayList<>();
List<ImmutablePair<ArtifactData, GraphEdge>> list = childrenNodes.left().value();
if (list != null) {
for (ImmutablePair<ArtifactData, GraphEdge> pair : list) {
ArtifactData artifactData = pair.getKey();
String uniqueId = artifactData.getArtifactDataDefinition().getUniqueId();
String UUID = artifactData.getArtifactDataDefinition().getArtifactUUID();
ImmutablePair<String, String> artifact = new ImmutablePair<>(uniqueId, UUID);
artifactsList.add(artifact);
}
}
log.debug("The artifacts list related to group {} is {}", groupUniqueId, artifactsList);
result = Either.left(artifactsList);
}
return result;
}
public StorageOperationStatus validateAndUpdatePropertyValue(GroupProperty property) {
StorageOperationStatus result = null;
String innerType =
property.getSchema() == null ? null : property.getSchema().getProperty() == null ? null : property.getSchema().getProperty().getType();
Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = applicationDataTypeCache.getAll(property.getModel());
Either<Object, Boolean> isValid = null;
if (allDataTypes.isRight()) {
JanusGraphOperationStatus status = allDataTypes.right().value();
log.debug("Failed to fetch data types from cache. Status is {}. ", status);
result = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status);
}
if (result == null) {
isValid = propertyOperation.validateAndUpdatePropertyValue(property.getType(), property.getValue(), innerType,
allDataTypes.left().value());
if (isValid.isRight()) {
log.debug("Failed to validate property value {}. Status is {}. ", property.getValue(), StorageOperationStatus.INVALID_PROPERTY);
result = StorageOperationStatus.INVALID_PROPERTY;
}
}
if (result == null) {
String validValue = String.valueOf(isValid.left().value());
property.setValue(validValue);
result = StorageOperationStatus.OK;
}
return result;
}
public StorageOperationStatus updateGroupProperties(org.openecomp.sdc.be.model.Component containerComponent, String groupId,
List<PropertyDataDefinition> propertiesToUpdate) {
log.debug("#updateGroupProperties - updating the properties of group {} in component {}", groupId, containerComponent.getUniqueId());
Optional<GroupDefinition> group = containerComponent.getGroupById(groupId);
if (group.isPresent()) {
return janusGraphDao.getVertexById(containerComponent.getUniqueId(), JsonParseFlagEnum.NoParse)
.either(containerVertex -> updateGroupProperties(containerVertex, group.get(), propertiesToUpdate),
DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
} else {
throw new StorageException(StorageOperationStatus.NOT_FOUND, groupId);
}
}
private StorageOperationStatus updateGroupProperties(GraphVertex container, GroupDefinition group,
List<PropertyDataDefinition> propertiesToUpdate) {
List<PropertyDataDefinition> groupProperties = group.getProperties();
List<PropertyDataDefinition> updatedGroupProperties = updateGroupProperties(groupProperties, propertiesToUpdate);
group.setProperties(updatedGroupProperties);
return topologyTemplateOperation.updateGroupOfToscaElement(container, group);
}
private List<PropertyDataDefinition> updateGroupProperties(List<PropertyDataDefinition> currentGroupProperties,
List<PropertyDataDefinition> toBeUpdatedProperties) {
Map<String, PropertyDataDefinition> currPropsByName = MapUtil.toMap(currentGroupProperties, PropertyDataDefinition::getName);
overrideCurrentPropertiesWithUpdatedProperties(currPropsByName, toBeUpdatedProperties);
return new ArrayList<>(currPropsByName.values());
}
private void overrideCurrentPropertiesWithUpdatedProperties(Map<String, PropertyDataDefinition> currPropsByName,
List<PropertyDataDefinition> toBeUpdatedProperties) {
toBeUpdatedProperties.forEach(prop -> currPropsByName.put(prop.getName(), prop));
}
}
|