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
|
/*
* 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.be.components.impl;
import fj.data.Either;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
import org.openecomp.sdc.be.components.validation.RequirementValidation;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.model.ComponentParametersView;
import org.openecomp.sdc.be.model.RequirementDefinition;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.RequirementOperation;
import org.openecomp.sdc.be.model.operations.api.IElementOperation;
import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
import org.openecomp.sdc.exception.ResponseFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("requirementBusinessLogic")
public class RequirementBusinessLogic extends BaseBusinessLogic {
private static final Logger LOGGER = LoggerFactory.getLogger(RequirementBusinessLogic.class);
private static final String FAILED_TO_LOCK_COMPONENT_RESPONSE_IS = "Failed to lock component {}. Response is {}";
private static final String DELETE_REQUIREMENTS = "deleteRequirement";
private static final String GET_REQUIREMENTS = "getRequirements";
private static final String EXCEPTION_OCCURRED_DURING_REQUIREMENTS = "Exception occurred during {}. Response is {}";
private RequirementOperation requirementOperation;
private RequirementValidation requirementValidation;
@Autowired
public RequirementBusinessLogic(IElementOperation elementDao, IGroupOperation groupOperation, IGroupInstanceOperation groupInstanceOperation,
IGroupTypeOperation groupTypeOperation, InterfaceOperation interfaceOperation,
InterfaceLifecycleOperation interfaceLifecycleTypeOperation, ArtifactsOperations artifactToscaOperation) {
super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation,
artifactToscaOperation);
}
@Autowired
public void setRequirementOperation(RequirementOperation requirementOperation) {
this.requirementOperation = requirementOperation;
}
@Autowired
public void setRequirementValidation(RequirementValidation requirementValidation) {
this.requirementValidation = requirementValidation;
}
public Either<List<RequirementDefinition>, ResponseFormat> createRequirements(String componentId,
List<RequirementDefinition> requirementDefinitions, User user,
String errorContext, boolean lock) {
validateUserExists(user.getUserId());
Either<org.openecomp.sdc.be.model.Component, ResponseFormat> componentEither = getComponentDetails(componentId);
if (componentEither.isRight()) {
return Either.right(componentEither.right().value());
}
org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value();
Either<Boolean, ResponseFormat> requirementsValidationEither = requirementValidation
.validateRequirements(requirementDefinitions, storedComponent, false);
if (requirementsValidationEither.isRight()) {
return Either.right(requirementsValidationEither.right().value());
}
Either<Boolean, ResponseFormat> lockResult = lockComponentResult(lock, storedComponent, errorContext);
if (lockResult.isRight()) {
return Either.right(lockResult.right().value());
}
try {
Either<List<RequirementDefinition>, StorageOperationStatus> result;
List<RequirementDefinition> requirementsListStoredInComponent = null;
Map<String, List<RequirementDefinition>> storedComponentRequirements = storedComponent.getRequirements();
if (org.apache.commons.collections.MapUtils.isNotEmpty(storedComponentRequirements)) {
RequirementDefinition requirementDefinitionToGetType = requirementDefinitions.get(0);
if (Objects.isNull(requirementDefinitionToGetType)) {
return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
}
requirementsListStoredInComponent = getRequirementStoredInComponentByType(requirementDefinitionToGetType.getCapability(),
storedComponentRequirements);
}
List<RequirementDefinition> requirementsToReturn;
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(requirementsListStoredInComponent)) {
List<RequirementDefinition> requirementDefToCreate = requirementDefinitions.stream()
.map(requirementDefinition -> initiateNewRequirement(storedComponent, requirementDefinition)).collect(Collectors.toList());
requirementsToReturn = requirementDefToCreate;
requirementDefToCreate.addAll(requirementsListStoredInComponent);
result = requirementOperation.updateRequirement(componentId, requirementDefToCreate);
} else {
requirementsToReturn = requirementDefinitions.stream()
.map(requirementDefinition -> initiateNewRequirement(storedComponent, requirementDefinition)).collect(Collectors.toList());
result = requirementOperation.addRequirement(componentId, requirementsToReturn);
}
if (result.isRight()) {
janusGraphDao.rollback();
return Either.right(componentsUtils
.getResponseFormat(componentsUtils.convertFromStorageResponse(result.right().value(), storedComponent.getComponentType()), ""));
}
janusGraphDao.commit();
return Either.left(requirementsToReturn);
} catch (Exception e) {
janusGraphDao.rollback();
LOGGER.error(EXCEPTION_OCCURRED_DURING_REQUIREMENTS, "addOrUpdate", e);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
} finally {
if (lockResult.isLeft() && lockResult.left().value()) {
graphLockOperation
.unlockComponent(storedComponent.getUniqueId(), NodeTypeEnum.getByNameIgnoreCase(storedComponent.getComponentType().getValue()));
}
}
}
public Either<List<RequirementDefinition>, ResponseFormat> updateRequirements(String componentId,
List<RequirementDefinition> requirementDefinitions, User user,
String errorContext, boolean lock) {
validateUserExists(user.getUserId());
Either<org.openecomp.sdc.be.model.Component, ResponseFormat> componentEither = getComponentDetails(componentId);
if (componentEither.isRight()) {
return Either.right(componentEither.right().value());
}
org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value();
Either<Boolean, ResponseFormat> requirementsValidationEither = requirementValidation
.validateRequirements(requirementDefinitions, storedComponent, true);
if (requirementsValidationEither.isRight()) {
return Either.right(requirementsValidationEither.right().value());
}
Either<Boolean, ResponseFormat> lockResult = lockComponentResult(lock, storedComponent, errorContext);
if (lockResult.isRight()) {
return Either.right(lockResult.right().value());
}
try {
Either<List<RequirementDefinition>, StorageOperationStatus> result;
List<RequirementDefinition> requirementsListStoredInComponent = null;
Map<String, List<RequirementDefinition>> storedComponentRequirements = storedComponent.getRequirements();
if (org.apache.commons.collections.MapUtils.isNotEmpty(storedComponentRequirements)) {
RequirementDefinition requirementDefinitionToGetType = requirementDefinitions.get(0);
if (Objects.isNull(requirementDefinitionToGetType)) {
return Either.right(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
}
requirementsListStoredInComponent = getRequirementStoredInComponentByType(requirementDefinitionToGetType.getCapability(),
storedComponentRequirements);
}
List<RequirementDefinition> requirementsToReturn = null;
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(requirementsListStoredInComponent)) {
List<RequirementDefinition> requirementDefToUpdate = new ArrayList<>();
if (requirementDefinitions.stream()
.anyMatch(requirementDefinition -> isRequirementUsedInServiceComposition(requirementDefinition, storedComponent))) {
LOGGER.error("Requirement can't be edited, since it is used in service composition");
return Either.right(componentsUtils.getResponseFormat(ActionStatus.REQUIREMENT_UPDATE_NOT_ALLOWED_USED_IN_COMPOSITION));
}
for (RequirementDefinition requirementDefinitionToUpdate : requirementDefinitions) {
requirementsToReturn = requirementsListStoredInComponent.stream()
.filter(reqToUpdate -> reqToUpdate.getUniqueId().equals(requirementDefinitionToUpdate.getUniqueId()))
.map(requirementDefinition -> updateRequirement(requirementDefinition, requirementDefinitionToUpdate))
.collect(Collectors.toList());
requirementsListStoredInComponent
.removeIf(reqToUpdate -> reqToUpdate.getUniqueId().equals(requirementDefinitionToUpdate.getUniqueId()));
if (CollectionUtils.isNotEmpty(requirementsToReturn)) {
requirementsListStoredInComponent.addAll(requirementsToReturn);
requirementDefToUpdate.addAll(requirementsListStoredInComponent);
} else {
Either<List<RequirementDefinition>, ResponseFormat> updateCapTypeEither = handleUpdateRequirementCapabilityWhenNewCapabilityExist(
storedComponent, storedComponentRequirements, requirementsToReturn, requirementDefinitionToUpdate);
if (updateCapTypeEither.isRight()) {
return Either.right(updateCapTypeEither.right().value());
}
requirementDefToUpdate = updateCapTypeEither.left().value();
}
}
result = requirementOperation.updateRequirement(componentId, requirementDefToUpdate);
} else {
Either<List<RequirementDefinition>, ResponseFormat> requirementDefinitionToDelete = handleRequirementCapabilityUpdateWhenNewCapabilityNotExist(
requirementDefinitions, storedComponent, storedComponentRequirements);
if (requirementDefinitionToDelete != null) {
return requirementDefinitionToDelete;
}
requirementsToReturn = requirementDefinitions.stream()
.map(requirementDefinition -> initiateNewRequirement(storedComponent, requirementDefinition)).collect(Collectors.toList());
result = requirementOperation.addRequirement(componentId, requirementsToReturn);
}
if (result.isRight()) {
janusGraphDao.rollback();
return Either.right(componentsUtils
.getResponseFormat(componentsUtils.convertFromStorageResponse(result.right().value(), storedComponent.getComponentType()), ""));
}
janusGraphDao.commit();
return Either.left(requirementsToReturn);
} catch (Exception e) {
janusGraphDao.rollback();
LOGGER.error(EXCEPTION_OCCURRED_DURING_REQUIREMENTS, "addOrUpdate", e);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
} finally {
if (lockResult.isLeft() && lockResult.left().value()) {
graphLockOperation
.unlockComponent(storedComponent.getUniqueId(), NodeTypeEnum.getByNameIgnoreCase(storedComponent.getComponentType().getValue()));
}
}
}
private Either<List<RequirementDefinition>, ResponseFormat> handleUpdateRequirementCapabilityWhenNewCapabilityExist(
org.openecomp.sdc.be.model.Component storedComponent, Map<String, List<RequirementDefinition>> storedComponentRequirements,
List<RequirementDefinition> requirementsToReturn, RequirementDefinition requirementDefinitionToUpdate) {
List<RequirementDefinition> requirementsListStoredInComponent;
List<RequirementDefinition> requirementDefsToCreateOrUpdate = new ArrayList<>();
Optional<RequirementDefinition> definitionOptional = storedComponentRequirements.values().stream().flatMap(Collection::stream)
.filter(requirementDefinition -> requirementDefinition.getUniqueId().equals(requirementDefinitionToUpdate.getUniqueId())).findAny();
if (!definitionOptional.isPresent()) {
return Either.right(componentsUtils.getResponseFormat(ActionStatus.REQUIREMENT_NOT_FOUND, storedComponent.getUniqueId()));
}
RequirementDefinition requirementDefinitionToDelete = definitionOptional.get();
requirementsListStoredInComponent = getRequirementStoredInComponentByType(requirementDefinitionToUpdate.getCapability(),
storedComponentRequirements);
Either<List<RequirementDefinition>, StorageOperationStatus> deleteRequirementEither = deleteRequirement(storedComponent,
storedComponentRequirements, requirementDefinitionToDelete);
if (deleteRequirementEither.isRight()) {
janusGraphDao.rollback();
return Either.right(componentsUtils.getResponseFormat(deleteRequirementEither.right().value()));
}
requirementsToReturn.add(initiateNewRequirement(storedComponent, requirementDefinitionToUpdate));
requirementDefsToCreateOrUpdate.addAll(requirementsToReturn);
requirementDefsToCreateOrUpdate.addAll(requirementsListStoredInComponent);
return Either.left(requirementDefsToCreateOrUpdate);
}
private Either<List<RequirementDefinition>, ResponseFormat> handleRequirementCapabilityUpdateWhenNewCapabilityNotExist(
List<RequirementDefinition> requirementDefinitions, org.openecomp.sdc.be.model.Component storedComponent,
Map<String, List<RequirementDefinition>> storedComponentRequirements) {
for (RequirementDefinition requirementDefinitionToUpdate : requirementDefinitions) {
Optional<RequirementDefinition> definitionOptional = storedComponentRequirements.values().stream().flatMap(Collection::stream)
.filter(requirementDefinition -> requirementDefinition.getUniqueId().equals(requirementDefinitionToUpdate.getUniqueId())).findAny();
if (!definitionOptional.isPresent()) {
return Either.right(componentsUtils.getResponseFormat(ActionStatus.REQUIREMENT_NOT_FOUND, storedComponent.getUniqueId()));
}
RequirementDefinition requirementDefinitionToDelete = definitionOptional.get();
Boolean isRequirementUsedInServiceComposition = isRequirementUsedInServiceComposition(requirementDefinitionToDelete, storedComponent);
if (isRequirementUsedInServiceComposition) {
LOGGER.error("Requirement {} can't be edited, since it is used in service composition", requirementDefinitionToDelete.getUniqueId());
return Either.right(componentsUtils
.getResponseFormat(ActionStatus.REQUIREMENT_UPDATE_NOT_ALLOWED_USED_IN_COMPOSITION, requirementDefinitionToDelete.getName()));
}
Either<List<RequirementDefinition>, StorageOperationStatus> deleteRequirementEither = deleteRequirement(storedComponent,
storedComponentRequirements, requirementDefinitionToDelete);
if (deleteRequirementEither.isRight()) {
janusGraphDao.rollback();
return Either.right(componentsUtils.getResponseFormat(deleteRequirementEither.right().value()));
}
}
return null;
}
public Either<RequirementDefinition, ResponseFormat> getRequirement(String componentId, String requirementIdToGet, User user, boolean lock) {
validateUserExists(user.getUserId());
Either<org.openecomp.sdc.be.model.Component, ResponseFormat> componentEither = getComponentDetails(componentId);
if (componentEither.isRight()) {
return Either.right(componentEither.right().value());
}
org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value();
Either<Boolean, ResponseFormat> lockResult = lockComponentResult(lock, storedComponent, GET_REQUIREMENTS);
if (lockResult.isRight()) {
return Either.right(lockResult.right().value());
}
try {
List<RequirementDefinition> requirementDefinitions = storedComponent.getRequirements().values().stream().flatMap(Collection::stream)
.collect(Collectors.toList());
if (requirementDefinitions.isEmpty()) {
return Either.right(componentsUtils.getResponseFormat(ActionStatus.REQUIREMENT_NOT_FOUND, componentId));
}
RequirementDefinition requirementDefinitionToReturn;
Optional<RequirementDefinition> requirementDefinitionOptional = requirementDefinitions.stream()
.filter(requirementDefinition -> requirementDefinition.getUniqueId().equals(requirementIdToGet)).findAny();
if (requirementDefinitionOptional.isPresent()) {
requirementDefinitionToReturn = requirementDefinitionOptional.get();
} else {
return Either.right(componentsUtils.getResponseFormat(ActionStatus.REQUIREMENT_NOT_FOUND, componentId));
}
return Either.left(requirementDefinitionToReturn);
} catch (Exception e) {
LOGGER.error(EXCEPTION_OCCURRED_DURING_REQUIREMENTS, "get", e);
return Either.right(componentsUtils.getResponseFormat(ActionStatus.REQUIREMENT_NOT_FOUND, componentId));
} finally {
if (lockResult.isLeft() && lockResult.left().value()) {
graphLockOperation
.unlockComponent(storedComponent.getUniqueId(), NodeTypeEnum.getByNameIgnoreCase(storedComponent.getComponentType().getValue()));
}
}
}
public Either<RequirementDefinition, ResponseFormat> deleteRequirement(String componentId, String requirementIdToDelete, User user,
boolean lock) {
validateUserExists(user.getUserId());
Either<org.openecomp.sdc.be.model.Component, ResponseFormat> componentEither = getComponentDetails(componentId);
if (componentEither.isRight()) {
return Either.right(componentEither.right().value());
}
org.openecomp.sdc.be.model.Component storedComponent = componentEither.left().value();
Either<Boolean, ResponseFormat> lockResult = lockComponentResult(lock, storedComponent, DELETE_REQUIREMENTS);
if (lockResult.isRight()) {
return Either.right(lockResult.right().value());
}
try {
Map<String, List<RequirementDefinition>> storedComponentRequirements = storedComponent.getRequirements();
if (storedComponentRequirements.isEmpty()) {
return Either.right(componentsUtils.getResponseFormat(ActionStatus.REQUIREMENT_NOT_FOUND, componentId));
}
Optional<RequirementDefinition> definitionOptional = storedComponentRequirements.values().stream().flatMap(Collection::stream)
.filter(requirementDefinition -> requirementDefinition.getUniqueId().equals(requirementIdToDelete)).findAny();
if (!definitionOptional.isPresent()) {
return Either.right(componentsUtils.getResponseFormat(ActionStatus.REQUIREMENT_NOT_FOUND, componentId));
}
RequirementDefinition requirementDefinitionToDelete = definitionOptional.get();
Boolean isRequirementUsedInServiceComposition = isRequirementUsedInServiceComposition(requirementDefinitionToDelete, storedComponent);
if (isRequirementUsedInServiceComposition) {
LOGGER.error("Requirement {} can't be deleted, since it is used in service composition", requirementDefinitionToDelete.getUniqueId());
return Either.right(componentsUtils
.getResponseFormat(ActionStatus.CAPABILITY_DELETION_NOT_ALLOWED_USED_IN_COMPOSITION, requirementDefinitionToDelete.getName()));
}
Either<List<RequirementDefinition>, StorageOperationStatus> result = deleteRequirement(storedComponent, storedComponentRequirements,
requirementDefinitionToDelete);
if (result.isRight()) {
janusGraphDao.rollback();
LOGGER.error("Failed to delete requirement from component {}. Response is {}", storedComponent.getName(), result.right().value());
return Either.right(componentsUtils
.getResponseFormat(componentsUtils.convertFromStorageResponse(result.right().value(), storedComponent.getComponentType())));
}
janusGraphDao.commit();
return Either.left(requirementDefinitionToDelete);
} catch (Exception e) {
LOGGER.error(EXCEPTION_OCCURRED_DURING_REQUIREMENTS, "delete", e);
janusGraphDao.rollback();
return Either.right(componentsUtils.getResponseFormat(ActionStatus.REQUIREMENT_NOT_FOUND));
} finally {
if (lockResult.isLeft() && lockResult.left().value()) {
graphLockOperation
.unlockComponent(storedComponent.getUniqueId(), NodeTypeEnum.getByNameIgnoreCase(storedComponent.getComponentType().getValue()));
}
}
}
private Either<List<RequirementDefinition>, StorageOperationStatus> deleteRequirement(org.openecomp.sdc.be.model.Component storedComponent,
Map<String, List<RequirementDefinition>> storedComponentRequirements,
RequirementDefinition requirementDefinitionToDelete) {
List<RequirementDefinition> requirementStoredInComponentByType = getRequirementStoredInComponentByType(
requirementDefinitionToDelete.getCapability(), storedComponentRequirements);
if (requirementStoredInComponentByType == null) {
return Either.right(StorageOperationStatus.BAD_REQUEST);
}
requirementStoredInComponentByType
.removeIf(requirementDefinition -> requirementDefinition.getUniqueId().equals(requirementDefinitionToDelete.getUniqueId()));
Either<List<RequirementDefinition>, StorageOperationStatus> result;
if (requirementStoredInComponentByType.isEmpty()) {
StorageOperationStatus operationStatus = requirementOperation
.deleteRequirements(storedComponent, requirementDefinitionToDelete.getCapability());
if (operationStatus.equals(StorageOperationStatus.OK)) {
result = Either.left(Collections.singletonList(requirementDefinitionToDelete));
} else {
result = Either.right(operationStatus);
}
} else {
result = requirementOperation.updateRequirement(storedComponent.getUniqueId(), requirementStoredInComponentByType);
}
return result;
}
private Either<org.openecomp.sdc.be.model.Component, ResponseFormat> getComponentDetails(String componentId) {
ComponentParametersView filter = new ComponentParametersView(true);
filter.setIgnoreRequirements(false);
Either<org.openecomp.sdc.be.model.Component, StorageOperationStatus> componentStorageOperationStatusEither = toscaOperationFacade
.getToscaElement(componentId, filter);
if (componentStorageOperationStatusEither.isRight()) {
StorageOperationStatus errorStatus = componentStorageOperationStatusEither.right().value();
LOGGER.error("Failed to fetch component information by component id {}, Response is {}", componentId, errorStatus);
return Either.right(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(errorStatus)));
}
return Either.left(componentStorageOperationStatusEither.left().value());
}
private Either<Boolean, ResponseFormat> lockComponentResult(boolean lock, org.openecomp.sdc.be.model.Component component, String action) {
if (lock) {
try {
lockComponent(component.getUniqueId(), component, action);
} catch (ComponentException e) {
LOGGER.debug(FAILED_TO_LOCK_COMPONENT_RESPONSE_IS, component.getName(), e.getMessage());
janusGraphDao.rollback();
throw e;
}
}
return Either.left(true);
}
private List<RequirementDefinition> getRequirementStoredInComponentByType(String capabilityType,
Map<String, List<RequirementDefinition>> requirements) {
Optional<Map.Entry<String, List<RequirementDefinition>>> entryOptional = requirements.entrySet().stream()
.filter(map -> map.getKey().equals(capabilityType)).findFirst();
return entryOptional.map(Map.Entry::getValue).orElse(null);
}
private RequirementDefinition initiateNewRequirement(org.openecomp.sdc.be.model.Component component,
RequirementDefinition requirementDefinition) {
if (StringUtils.isEmpty(requirementDefinition.getUniqueId())) {
requirementDefinition.setUniqueId(UUID.randomUUID().toString());
}
if (StringUtils.isEmpty(requirementDefinition.getOwnerId())) {
requirementDefinition.setOwnerId(component.getUniqueId());
}
if (StringUtils.isEmpty(requirementDefinition.getOwnerName())) {
requirementDefinition.setOwnerName(component.getName());
}
requirementDefinition.setLeftOccurrences(requirementDefinition.getMaxOccurrences());
return requirementDefinition;
}
private RequirementDefinition updateRequirement(RequirementDefinition storedRequirement, RequirementDefinition requirementToUpdate) {
storedRequirement.setName(requirementToUpdate.getName());
storedRequirement.setCapability(requirementToUpdate.getCapability());
storedRequirement.setNode(requirementToUpdate.getNode());
storedRequirement.setRelationship(requirementToUpdate.getRelationship());
storedRequirement.setMinOccurrences(requirementToUpdate.getMinOccurrences());
storedRequirement.setMaxOccurrences(requirementToUpdate.getMaxOccurrences());
return storedRequirement;
}
private Boolean isRequirementUsedInServiceComposition(RequirementDefinition requirementDefinition,
org.openecomp.sdc.be.model.Component component) {
Either<List<org.openecomp.sdc.be.model.Component>, StorageOperationStatus> componentList = toscaOperationFacade
.getParentComponents(component.getUniqueId());
if (componentList.isRight()) {
return Boolean.FALSE;
}
return componentList.left().value().stream().flatMap(parentComponent -> parentComponent.getComponentInstancesRelations().stream())
.flatMap(requirementCapabilityRelDef -> requirementCapabilityRelDef.getRelationships().stream()).anyMatch(
capabilityRequirementRelationship -> capabilityRequirementRelationship.getRelation().getRequirementUid()
.equals(requirementDefinition.getUniqueId()));
}
}
|