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
|
/*
* 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.validation;
import com.google.common.collect.Sets;
import fj.data.Either;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
import org.openecomp.sdc.be.components.utils.InterfaceOperationUtils;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
import org.openecomp.sdc.be.model.InterfaceDefinition;
import org.openecomp.sdc.be.model.Operation;
import org.openecomp.sdc.exception.ResponseFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.getOperationOutputName;
import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.getOtherOperationOutputsOfComponent;
import static org.openecomp.sdc.be.components.utils.InterfaceOperationUtils.isOperationInputMappedToComponentInput;
import static org.openecomp.sdc.be.components.utils.PropertiesUtils.isCapabilityProperty;
@Component("interfaceOperationValidation")
public class InterfaceOperationValidation {
private static final String TYPE_VALIDATION_REGEX = "^[a-zA-Z0-9_]{1,200}$";
private static final Logger LOGGER = LoggerFactory.getLogger(InterfaceOperationValidation.class);
public Either<Boolean, ResponseFormat> validateInterfaceOperations(
InterfaceDefinition inputInterfaceDefinition, org.openecomp.sdc.be.model.Component component,
InterfaceDefinition storedInterfaceDefinition, Map<String, InterfaceDefinition> globalInterfaceTypes,
boolean isUpdate) {
Either<Boolean, ResponseFormat> validateAllowedOperationsOnGlobalInterfaceType =
validateAllowedOperationsOnGlobalInterfaceType(inputInterfaceDefinition, globalInterfaceTypes);
if (validateAllowedOperationsOnGlobalInterfaceType.isRight()) {
return validateAllowedOperationsOnGlobalInterfaceType;
}
Either<Boolean, ResponseFormat> validateOperationNameUniqueness =
validateOperationNameUniquenessInCollection(inputInterfaceDefinition.getOperationsMap().values());
if (validateOperationNameUniqueness.isRight()) {
return validateOperationNameUniqueness;
}
for (Operation interfaceOperation : inputInterfaceDefinition.getOperationsMap().values()) {
Either<Boolean, ResponseFormat> interfaceOperationValidatorResponse = validateInterfaceOperation(
interfaceOperation, storedInterfaceDefinition, inputInterfaceDefinition, component, isUpdate);
if (interfaceOperationValidatorResponse.isRight()) {
return interfaceOperationValidatorResponse;
}
}
return Either.left(Boolean.TRUE);
}
public Either<Boolean, ResponseFormat> validateDeleteOperationContainsNoMappedOutput(
Operation interfaceOperationToDelete, org.openecomp.sdc.be.model.Component component,
InterfaceDefinition storedInterfaceDefinition) {
ResponseFormatManager responseFormatManager = getResponseFormatManager();
List<OperationOutputDefinition> existingOperationOutputs =
getInterfaceOperationOutputs(interfaceOperationToDelete.getUniqueId(), component.getInterfaces());
if (existingOperationOutputs.isEmpty()) {
return Either.left(Boolean.TRUE);
}
String mappedOutputPrefix = storedInterfaceDefinition.getType() + "." + interfaceOperationToDelete.getName();
List<OperationInputDefinition> interfaceOperationInputs =
getOtherOperationInputsOfComponent(mappedOutputPrefix, component.getInterfaces());
Set<String> mappedOutputsInDeletedOperation = new HashSet<>();
Set<String> existingOperationOutputNames = existingOperationOutputs.stream()
.map(OperationOutputDefinition::getName)
.collect(Collectors.toSet());
for (String existingOperationOutputName : existingOperationOutputNames) {
Set<String> matchedOutputsMappedToInputs = interfaceOperationInputs.stream()
.filter(operationInputDefinition -> operationInputDefinition.getInputId()
.equals(mappedOutputPrefix + "." + existingOperationOutputName))
.map(operationInputDefinition -> getOperationOutputName(operationInputDefinition.getInputId()))
.collect(Collectors.toSet());
mappedOutputsInDeletedOperation.addAll(matchedOutputsMappedToInputs);
}
if (CollectionUtils.isNotEmpty(mappedOutputsInDeletedOperation)) {
return getMappedOutputErrorResponse(responseFormatManager, mappedOutputsInDeletedOperation,
"Cannot delete interface operation with output(s) '{}' mapped to another operation input",
ActionStatus.INTERFACE_OPERATION_DELETE_WITH_MAPPED_OUTPUT);
}
return Either.left(Boolean.TRUE);
}
private Either<Boolean, ResponseFormat> validateAllowedOperationsOnGlobalInterfaceType(
InterfaceDefinition interfaceDefinition, Map<String, InterfaceDefinition> globalInterfaceTypes) {
if (globalInterfaceTypes == null) {
return Either.left(Boolean.TRUE);
}
boolean isOperationValidOnGlobalInterfaceType = Stream.of(interfaceDefinition)
.filter(interfaceDef -> globalInterfaceTypes.values().stream().anyMatch(interfaceDef1 ->
interfaceDef1.getType().equalsIgnoreCase(interfaceDef.getType())))
.flatMap(interfaceDef -> interfaceDef.getOperationsMap().values().stream().map(Operation::getName))
.allMatch(operationName -> globalInterfaceTypes.values().stream()
.flatMap(interfaceDef -> interfaceDef.getOperationsMap().keySet().stream())
.anyMatch(opName -> opName.equalsIgnoreCase(operationName)));
if (!isOperationValidOnGlobalInterfaceType) {
return Either.right(getResponseFormatManager()
.getResponseFormat(ActionStatus.INTERFACE_OPERATION_INVALID_FOR_GLOBAL_TYPE,
interfaceDefinition.getType()));
}
return Either.left(Boolean.TRUE);
}
private Either<Boolean, ResponseFormat> validateOperationNameUniquenessInCollection(
Collection<Operation> operationList) {
HashSet<String> operationNames = new HashSet<>();
for (Operation operation : operationList) {
if (!operationNames.add(operation.getName())) {
return Either.right(getResponseFormatManager()
.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NAME_ALREADY_IN_USE,
operation.getName()));
}
}
return Either.left(Boolean.TRUE);
}
private Either<Boolean, ResponseFormat> validateInterfaceOperation(Operation interfaceOperation,
InterfaceDefinition storedInterfaceDefinition, InterfaceDefinition inputInterfaceDefinition,
org.openecomp.sdc.be.model.Component component, boolean isUpdate) {
ResponseFormatManager responseFormatManager = getResponseFormatManager();
Either<Boolean, ResponseFormat> interfaceOperationTypeResponse =
isInterfaceOperationTypeValid(interfaceOperation, responseFormatManager, storedInterfaceDefinition,
isUpdate);
if (interfaceOperationTypeResponse.isRight()) {
return Either.right(interfaceOperationTypeResponse.right().value());
}
if (null != interfaceOperation.getInputs()
&& CollectionUtils.isNotEmpty(interfaceOperation.getInputs().getListToscaDataDefinition())) {
Either<Boolean, ResponseFormat> inputParametersResponse =
validateInputParameters(interfaceOperation, responseFormatManager);
if (inputParametersResponse.isRight()) {
return Either.right(inputParametersResponse.right().value());
}
Either<Boolean, ResponseFormat> inputPropertyExistInComponent =
validateInputPropertyExistInComponent(interfaceOperation,
inputInterfaceDefinition, component, responseFormatManager);
if (inputPropertyExistInComponent.isRight()) {
return Either.right(inputPropertyExistInComponent.right().value());
}
}
if (null != interfaceOperation.getOutputs()
&& CollectionUtils.isNotEmpty(interfaceOperation.getOutputs().getListToscaDataDefinition())) {
Either<Boolean, ResponseFormat> outputParametersResponse =
validateOutputParameters(interfaceOperation, responseFormatManager);
if (outputParametersResponse.isRight()) {
return Either.right(outputParametersResponse.right().value());
}
}
if (MapUtils.isNotEmpty(component.getInterfaces()) && isUpdate) {
Either<Boolean, ResponseFormat> mappedOutputModifiedResponse =
validateMappedOutputNotModified(interfaceOperation, component, inputInterfaceDefinition,
responseFormatManager);
if (mappedOutputModifiedResponse.isRight()) {
return Either.right(mappedOutputModifiedResponse.right().value());
}
}
return Either.left(Boolean.TRUE);
}
private Either<Boolean, ResponseFormat> validateMappedOutputNotModified(Operation interfaceOperation,
org.openecomp.sdc.be.model.Component component, InterfaceDefinition interfaceDefinition,
ResponseFormatManager responseFormatManager) {
List<OperationOutputDefinition> existingOperationOutputs =
getInterfaceOperationOutputs(interfaceOperation.getUniqueId(), component.getInterfaces());
if (existingOperationOutputs.isEmpty()) {
return Either.left(Boolean.TRUE);
}
Set<String> existingOperationOutputNames = existingOperationOutputs.stream()
.map(OperationOutputDefinition::getName)
.collect(Collectors.toSet());
ListDataDefinition<OperationOutputDefinition> currentOutputs = interfaceOperation.getOutputs();
Set<String> currentOperationOutputNames = new HashSet<>();
if (currentOutputs != null && !currentOutputs.isEmpty()) {
currentOperationOutputNames = currentOutputs.getListToscaDataDefinition().stream()
.map(OperationOutputDefinition::getName)
.collect(Collectors.toSet());
}
String mappedOutputPrefix = interfaceDefinition.getType() + "." + interfaceOperation.getName();
//Get the deleted outputs (name changed also equivalent to deleted)
Set<String> deletedOutputs = Sets.difference(existingOperationOutputNames, currentOperationOutputNames);
Set<String> deletedMappedOutputs = getModifiedMappedOutputs(deletedOutputs, mappedOutputPrefix,
component.getInterfaces());
if (CollectionUtils.isNotEmpty(deletedMappedOutputs)) {
return getMappedOutputErrorResponse(responseFormatManager, deletedMappedOutputs,
"Cannot update or delete interface operation output(s) '{}' mapped to an operation input",
ActionStatus.INTERFACE_OPERATION_MAPPED_OUTPUT_MODIFIED);
}
if (currentOutputs != null && !currentOutputs.isEmpty()) {
//Get the unchanged outputs based on name to see if other attributes (type/mandatory) have not been changed
Set<String> unchangedOutputNames = Sets.intersection(existingOperationOutputNames,
currentOperationOutputNames);
Set<String> modifiedOutputNames =
getModifiedOutputNames(currentOutputs.getListToscaDataDefinition(),
existingOperationOutputs, unchangedOutputNames);
Set<String> modifiedMappedOutputNames = getModifiedMappedOutputs(modifiedOutputNames, mappedOutputPrefix,
component.getInterfaces());
if (CollectionUtils.isNotEmpty(modifiedMappedOutputNames)) {
return getMappedOutputErrorResponse(responseFormatManager, modifiedMappedOutputNames,
"Cannot update or delete interface operation output(s) '{}' mapped to an operation input",
ActionStatus.INTERFACE_OPERATION_MAPPED_OUTPUT_MODIFIED);
}
}
return Either.left(Boolean.TRUE);
}
private Set<String> getModifiedMappedOutputs(Set<String> modifiedOutputNames, String mappedOutputPrefix,
Map<String, InterfaceDefinition> componentInterfaces) {
return modifiedOutputNames.stream()
.filter(modifiedOutputName -> isMappedOutputModified(mappedOutputPrefix, modifiedOutputName,
componentInterfaces))
.map(InterfaceOperationUtils::getOperationOutputName)
.collect(Collectors.toSet());
}
private boolean isMappedOutputModified(String mappedOutputPrefix, String outputName,
Map<String, InterfaceDefinition> componentInterfaces) {
List<OperationInputDefinition> interfaceOperationInputs =
getOtherOperationInputsOfComponent(mappedOutputPrefix, componentInterfaces);
return interfaceOperationInputs.stream()
.anyMatch(operationInputDefinition -> operationInputDefinition.getInputId()
.equals(mappedOutputPrefix + "." + outputName));
}
private static Set<String> getModifiedOutputNames(List<OperationOutputDefinition> currentOperationOutputs,
List<OperationOutputDefinition> existingOperationOutputs,
Set<String> unchangedOutputNames) {
Set<String> modifiedOutputDefinitionNames = new HashSet<>();
Map<String, OperationOutputDefinition> newOutputMap = currentOperationOutputs.stream()
.collect(Collectors.toMap(OperationOutputDefinition::getName,
(OperationOutputDefinition operationOutputDefinition) -> operationOutputDefinition));
Map<String, OperationOutputDefinition> existingOutputMap = existingOperationOutputs.stream()
.collect(Collectors.toMap(OperationOutputDefinition::getName,
(OperationOutputDefinition operationOutputDefinition) -> operationOutputDefinition));
for (String outputName : unchangedOutputNames) {
OperationOutputDefinition existingOutputDefinition = existingOutputMap.get(outputName);
OperationOutputDefinition newOutputDefinition = newOutputMap.get(outputName);
if (!existingOutputDefinition.getType().equals(newOutputDefinition.getType())
|| !existingOutputDefinition.isRequired().equals(newOutputDefinition.isRequired())) {
modifiedOutputDefinitionNames.add(outputName);
}
}
return modifiedOutputDefinitionNames;
}
private Either<Boolean, ResponseFormat> getMappedOutputErrorResponse(ResponseFormatManager responseFormatManager,
Set<String> modifiedMappedOutputs,
String message,
ActionStatus errorStatus) {
String modifiedOutputNameList = String.join(",", modifiedMappedOutputs);
LOGGER.error(message, modifiedOutputNameList);
ResponseFormat errorResponse = responseFormatManager.getResponseFormat(errorStatus, modifiedOutputNameList);
return Either.right(errorResponse);
}
protected ResponseFormatManager getResponseFormatManager() {
return ResponseFormatManager.getInstance();
}
private Either<Boolean, ResponseFormat> isInterfaceOperationTypeValid(Operation interfaceOperation,
ResponseFormatManager responseFormatManager,
InterfaceDefinition interfaceDefinition,
boolean isUpdate) {
Either<Boolean, ResponseFormat> operationTypeEmptyEither =
isOperationTypeEmpty(responseFormatManager, interfaceOperation.getName());
if (operationTypeEmptyEither.isRight()) {
return Either.right(operationTypeEmptyEither.right().value());
}
Either<Boolean, ResponseFormat> operationTypeRegexValidationResponse =
isOperationTypeRegexValid(responseFormatManager, interfaceOperation.getName());
if (operationTypeRegexValidationResponse.isRight()) {
return Either.right(operationTypeRegexValidationResponse.right().value());
}
Either<Boolean, ResponseFormat> operationTypeUniqueResponse = validateOperationTypeUnique(interfaceOperation,
interfaceDefinition, isUpdate);
if (operationTypeUniqueResponse.isRight()) {
return Either.right(operationTypeUniqueResponse.right().value());
}
if (!operationTypeUniqueResponse.left().value()) {
LOGGER.error("Interface Operation type {} already in use ", interfaceOperation.getName());
ResponseFormat errorResponse = responseFormatManager.getResponseFormat(
ActionStatus.INTERFACE_OPERATION_NAME_ALREADY_IN_USE, interfaceOperation.getName());
return Either.right(errorResponse);
}
return Either.left(Boolean.TRUE);
}
private Either<Boolean, ResponseFormat> validateInputParameters(Operation interfaceOperation,
ResponseFormatManager responseFormatManager) {
if (isInputParameterNameEmpty(interfaceOperation)) {
LOGGER.error("Interface operation input parameter name can't be empty");
ResponseFormat inputResponse =
responseFormatManager.getResponseFormat(ActionStatus.INTERFACE_OPERATION_INPUT_NAME_MANDATORY);
return Either.right(inputResponse);
}
Either<Boolean, Set<String>> validateInputParametersUniqueResponse =
isInputParametersUnique(interfaceOperation);
if (validateInputParametersUniqueResponse.isRight()) {
LOGGER.error("Interface operation input parameter names {} already in use",
validateInputParametersUniqueResponse.right().value());
ResponseFormat inputResponse =
responseFormatManager.getResponseFormat(ActionStatus.INTERFACE_OPERATION_INPUT_NAME_ALREADY_IN_USE,
validateInputParametersUniqueResponse.right().value().toString());
return Either.right(inputResponse);
}
return Either.left(Boolean.TRUE);
}
private Either<Boolean, ResponseFormat> validateOutputParameters(Operation interfaceOperation,
ResponseFormatManager responseFormatManager) {
if (isOutputParameterNameEmpty(interfaceOperation)) {
LOGGER.error("Interface operation output parameter name can't be empty");
ResponseFormat inputResponse =
responseFormatManager.getResponseFormat(ActionStatus.INTERFACE_OPERATION_OUTPUT_NAME_MANDATORY);
return Either.right(inputResponse);
}
Either<Boolean, Set<String>> validateOutputParametersUniqueResponse =
isOutputParametersUnique(interfaceOperation);
if (validateOutputParametersUniqueResponse.isRight()) {
LOGGER.error("Interface operation output parameter names {} already in use",
validateOutputParametersUniqueResponse.right().value());
ResponseFormat inputResponse =
responseFormatManager.getResponseFormat(ActionStatus.INTERFACE_OPERATION_OUTPUT_NAME_ALREADY_IN_USE,
validateOutputParametersUniqueResponse.right().value().toString());
return Either.right(inputResponse);
}
return Either.left(Boolean.TRUE);
}
private Either<Boolean, ResponseFormat> isOperationTypeEmpty(ResponseFormatManager responseFormatManager,
String operationType) {
if (StringUtils.isEmpty(operationType)) {
LOGGER.error("Interface Operation type is mandatory");
ResponseFormat errorResponse =
responseFormatManager.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NAME_MANDATORY);
return Either.right(errorResponse);
}
return Either.left(Boolean.TRUE);
}
private Either<Boolean, ResponseFormat> isOperationTypeRegexValid(ResponseFormatManager responseFormatManager,
String operationType) {
if (!isValidOperationType(operationType)) {
LOGGER.error("Interface Operation type {} is invalid, Operation type should not contain"
+ "Special character, space, numbers and should not be greater than 200 characters",
operationType);
ResponseFormat errorResponse = responseFormatManager
.getResponseFormat(ActionStatus.INTERFACE_OPERATION_NAME_INVALID,
operationType);
return Either.right(errorResponse);
}
return Either.left(Boolean.TRUE);
}
private Either<Boolean, ResponseFormat> validateOperationTypeUnique(Operation interfaceOperation,
InterfaceDefinition interfaceDefinition, boolean isUpdate) {
boolean isOperationTypeUnique = false;
if (interfaceDefinition == null || CollectionUtils.isEmpty(interfaceDefinition.getOperationsMap().values())) {
return Either.left(true);
}
Map<String, String> operationTypes = new HashMap<>();
interfaceDefinition.getOperationsMap().values()
.forEach(operationType -> operationTypes.put(operationType.getUniqueId(), operationType.getName()));
if (!operationTypes.values().contains(interfaceOperation.getName())) {
isOperationTypeUnique = true;
}
if (!isOperationTypeUnique && isUpdate) {
Optional<String> id = operationTypes.entrySet().stream().filter(entry -> Objects.equals(entry.getValue(),
interfaceOperation.getName())).map(Map.Entry::getKey).findAny();
if (id.isPresent() && id.get().equalsIgnoreCase(interfaceOperation.getUniqueId())) {
isOperationTypeUnique = true;
}
}
return Either.left(isOperationTypeUnique);
}
private Boolean isInputParameterNameEmpty(Operation operationDataDefinition) {
return operationDataDefinition.getInputs().getListToscaDataDefinition().stream().anyMatch(
inputParam -> inputParam.getName() == null || inputParam.getName().trim().equals(StringUtils.EMPTY));
}
private Either<Boolean, Set<String>> isInputParametersUnique(Operation operationDataDefinition) {
Set<String> inputParamNamesSet = new HashSet<>();
Set<String> duplicateParamNamesToReturn = new HashSet<>();
operationDataDefinition.getInputs().getListToscaDataDefinition()
.forEach(inputParam -> {
if (!inputParamNamesSet.add(inputParam.getName().trim())) {
duplicateParamNamesToReturn.add(inputParam.getName().trim());
}
});
if (!duplicateParamNamesToReturn.isEmpty()) {
return Either.right(duplicateParamNamesToReturn);
}
return Either.left(Boolean.TRUE);
}
private Boolean isOutputParameterNameEmpty(Operation operationDataDefinition) {
return operationDataDefinition.getOutputs().getListToscaDataDefinition().stream().anyMatch(
outputParam -> outputParam.getName() == null || outputParam.getName().trim().equals(StringUtils.EMPTY));
}
private Either<Boolean, Set<String>> isOutputParametersUnique(Operation operationDataDefinition) {
Set<String> outputParamNamesSet = new HashSet<>();
Set<String> duplicateParamNamesToReturn = new HashSet<>();
operationDataDefinition.getOutputs().getListToscaDataDefinition()
.forEach(outputParam -> {
if (!outputParamNamesSet.add(outputParam.getName().trim())) {
duplicateParamNamesToReturn.add(outputParam.getName().trim());
}
});
if (!duplicateParamNamesToReturn.isEmpty()) {
return Either.right(duplicateParamNamesToReturn);
}
return Either.left(Boolean.TRUE);
}
private Either<Boolean, ResponseFormat> validateInputPropertyExistInComponent(Operation operation,
InterfaceDefinition inputInterfaceDefinition, org.openecomp.sdc.be.model.Component component,
ResponseFormatManager responseFormatManager) {
boolean isOperationInputToInputPropertyMappingValid = false;
boolean isOperationInputToOtherOperationOutputMappingValid = false;
String mappingName = "";
List<OperationInputDefinition> inputListToscaDataDefinition =
operation.getInputs().getListToscaDataDefinition();
for (OperationInputDefinition inputDefinition : inputListToscaDataDefinition) {
if (isOperationInputMappedToComponentInput(inputDefinition, component.getInputs())
|| isCapabilityProperty(inputDefinition.getInputId(), component).isPresent()) {
isOperationInputToInputPropertyMappingValid = true;
} else {
mappingName = inputDefinition.getInputId().contains(".")
? inputDefinition.getInputId().substring(inputDefinition.getInputId().lastIndexOf('.') + 1)
: inputDefinition.getInputId();
break;
}
}
if (isOperationInputToInputPropertyMappingValid) {
return Either.left(Boolean.TRUE);
}
//Mapped property not found in the component properties.. Check in other operation output parameters of
// component (other operation => not having the same full name)
String actualOperationIdentifier = inputInterfaceDefinition.getType() + "." + operation.getName();
ListDataDefinition<OperationOutputDefinition> outputListDataDefinition =
getOtherOperationOutputsOfComponent(actualOperationIdentifier, component.getInterfaces());
List<OperationOutputDefinition> componentOutputsFromOtherOperations =
outputListDataDefinition.getListToscaDataDefinition();
if (validateOutputExistsInComponent(mappingName, componentOutputsFromOtherOperations)) {
isOperationInputToOtherOperationOutputMappingValid = true;
} else {
//Get the output parameter display name from the full name
mappingName = getOperationOutputName(mappingName);
}
if (!isOperationInputToOtherOperationOutputMappingValid) {
LOGGER.error("Interface operation input parameter property {} not found in component input properties or"
+ "capability properties or outputs of other operations.", mappingName);
ResponseFormat inputResponse = responseFormatManager
.getResponseFormat(ActionStatus.INTERFACE_OPERATION_INPUT_PROPERTY_NOT_FOUND_IN_COMPONENT,
mappingName, component.getComponentType().getValue());
return Either.right(inputResponse);
}
return Either.left(Boolean.TRUE);
}
private boolean validateOutputExistsInComponent(String mappedOutputName,
List<OperationOutputDefinition> outputs) {
return outputs.stream()
.anyMatch(output -> output.getName().equals(mappedOutputName));
}
/**
* Get the input definitions of other operations of the component from current as well as other interfaces.
* @param currentOperationIdentifier Identifier for the request operation (interface_name.operation_name)
* @param componentInterfaces Interfaces of the component
*/
private List<OperationInputDefinition> getOtherOperationInputsOfComponent(String currentOperationIdentifier,
Map<String, InterfaceDefinition>
componentInterfaces) {
List<OperationInputDefinition> otherOperationInputs = new ArrayList<>();
if (MapUtils.isEmpty(componentInterfaces)) {
return otherOperationInputs;
}
for (Map.Entry<String, InterfaceDefinition> interfaceDefinitionEntry : componentInterfaces.entrySet()) {
final Map<String, OperationDataDefinition> operations = interfaceDefinitionEntry.getValue().getOperations();
if (MapUtils.isEmpty(operations)) {
continue;
}
for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
ListDataDefinition<OperationInputDefinition> inputs = operationEntry.getValue().getInputs();
String expectedOperationIdentifier =
interfaceDefinitionEntry.getValue().getType() + "." + operationEntry.getValue().getName();
if (!currentOperationIdentifier.equals(expectedOperationIdentifier) && Objects.nonNull(inputs) && !inputs.isEmpty()) {
otherOperationInputs.addAll(inputs.getListToscaDataDefinition());
}
}
}
return otherOperationInputs;
}
/**
* Get the output of an operation in an interface.
* @param inputOperationId Unique identifier for the request operation
* @param componentInterfaces Interfaces of the component
*/
private List<OperationOutputDefinition> getInterfaceOperationOutputs(String inputOperationId,
Map<String, InterfaceDefinition>
componentInterfaces) {
List<OperationOutputDefinition> operationOutputDefinitions = new ArrayList<>();
if (MapUtils.isEmpty(componentInterfaces)) {
return operationOutputDefinitions;
}
for (Map.Entry<String, InterfaceDefinition> interfaceDefinitionEntry : componentInterfaces.entrySet()) {
final Map<String, OperationDataDefinition> operations = interfaceDefinitionEntry.getValue().getOperations();
if (MapUtils.isEmpty(operations)) {
continue;
}
for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
String expectedOperationId = operationEntry.getValue().getUniqueId();
if (expectedOperationId.equals(inputOperationId)) {
ListDataDefinition<OperationOutputDefinition> operationOutputs =
operationEntry.getValue().getOutputs();
return (Objects.isNull(operationOutputs) || operationOutputs.isEmpty())
? operationOutputDefinitions
: operationOutputs.getListToscaDataDefinition();
}
}
}
return operationOutputDefinitions;
}
private boolean isValidOperationType(String operationType) {
return Pattern.matches(TYPE_VALIDATION_REGEX, operationType);
}
}
|