From a1ba3abf29613ee9e576a7c96a76ceb921086044 Mon Sep 17 00:00:00 2001 From: imamSidero Date: Wed, 12 Apr 2023 16:02:46 +0100 Subject: [PATCH] Support for addition of INDEX token to tosca functions Providing the capability to add the index token to th tosca function of all types Issue-ID: SDC-4472 Signed-off-by: Imam hussain Change-Id: Ib7ac80f31710101f50de76bdb7c79abdc637cfe3 --- .../csar/ToscaFunctionYamlParsingHandler.java | 28 ++++++++-- .../impl/ComponentInstanceBusinessLogic.java | 4 +- .../validation/ToscaFunctionValidatorImpl.java | 4 +- catalog-ui/src/app/models/tosca-get-function.ts | 12 +++-- .../tosca-get-function.component.html | 4 +- .../tosca-get-function.component.ts | 62 ++++++++++++++++++---- catalog-ui/src/assets/languages/en_US.json | 1 + .../elements/ToscaFunctionJsonDeserializer.java | 17 ++++++ .../elements/ToscaGetFunctionDataDefinition.java | 25 ++++++++- .../ToscaGetFunctionDataDefinitionTest.java | 4 +- 10 files changed, 133 insertions(+), 28 deletions(-) diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ToscaFunctionYamlParsingHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ToscaFunctionYamlParsingHandler.java index 2a7af62a0c..0b9432b3a1 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ToscaFunctionYamlParsingHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ToscaFunctionYamlParsingHandler.java @@ -23,8 +23,11 @@ package org.openecomp.sdc.be.components.csar; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; import java.util.stream.Stream; +import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.be.datatypes.elements.CustomYamlFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaCustomFunction; @@ -51,7 +54,9 @@ public class ToscaFunctionYamlParsingHandler { } final List functionParameters; try { - functionParameters = (List) functionValueObj; + functionParameters = ((List) functionValueObj).stream() + .map(object -> Objects.toString(object, null)) + .collect(Collectors.toList()); } catch (final ClassCastException ignored) { return Optional.empty(); } @@ -66,7 +71,14 @@ public class ToscaFunctionYamlParsingHandler { toscaGetFunction.setPropertySource(PropertySource.INSTANCE); toscaGetFunction.setSourceName(propertySourceType); } - toscaGetFunction.setPropertyPathFromSource(functionParameters.subList(1, functionParameters.size())); + List propertySourceIndex = functionParameters.subList(1, functionParameters.size()); + String toscaIndexValue = propertySourceIndex.get((propertySourceIndex.size() - 1)); + if (propertySourceIndex.size() > 1 && (toscaIndexValue.equalsIgnoreCase("INDEX") || StringUtils.isNumeric(toscaIndexValue))) { + toscaGetFunction.setPropertyPathFromSource(propertySourceIndex.subList(0,(propertySourceIndex.size() - 1))); + toscaGetFunction.setToscaIndex(toscaIndexValue); + } else { + toscaGetFunction.setPropertyPathFromSource(propertySourceIndex); + } final String propertyName = toscaGetFunction.getPropertyPathFromSource().get(toscaGetFunction.getPropertyPathFromSource().size() - 1); toscaGetFunction.setPropertyName(propertyName); return Optional.of(toscaGetFunction); @@ -85,11 +97,19 @@ public class ToscaFunctionYamlParsingHandler { } else { final List functionParameters; try { - functionParameters = (List) functionValueObj; + functionParameters = ((List) functionValueObj).stream() + .map(object -> Objects.toString(object, null)) + .collect(Collectors.toList()); } catch (final ClassCastException ignored) { return Optional.empty(); } - toscaGetFunction.setPropertyPathFromSource(functionParameters); + String toscaIndexValue = functionParameters.get((functionParameters.size() - 1)); + if (functionParameters.size() > 1 && (toscaIndexValue.equalsIgnoreCase("INDEX") || StringUtils.isNumeric(toscaIndexValue))) { + toscaGetFunction.setPropertyPathFromSource(functionParameters.subList(0,(functionParameters.size() - 1))); + toscaGetFunction.setToscaIndex(toscaIndexValue); + } else { + toscaGetFunction.setPropertyPathFromSource(functionParameters); + } } final String propertyName = toscaGetFunction.getPropertyPathFromSource().get(toscaGetFunction.getPropertyPathFromSource().size() - 1); toscaGetFunction.setPropertyName(propertyName); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java index f43f7de860..16ec9ade96 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java @@ -2543,11 +2543,11 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { referredProperty = findSubProperty(referredProperty, toscaGetFunction, model); } - if (!property.getType().equals(referredProperty.getType())) { + if (!property.getType().equals(referredProperty.getType()) && !"list".equalsIgnoreCase(referredProperty.getType())) { throw ToscaGetFunctionExceptionSupplier .propertyTypeDiverge(toscaGetFunction.getType(), referredProperty.getType(), property.getType()).get(); } - if (PropertyType.typeHasSchema(referredProperty.getType()) && !referredProperty.getSchemaType().equals(property.getSchemaType())) { + if (PropertyType.typeHasSchema(referredProperty.getType()) && !referredProperty.getSchemaType().equals(property.getType()) && !referredProperty.getSchemaType().equals(property.getSchemaType())) { throw ToscaGetFunctionExceptionSupplier .propertySchemaDiverge(toscaGetFunction.getType(), referredProperty.getSchemaType(), property.getSchemaType()).get(); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/ToscaFunctionValidatorImpl.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/ToscaFunctionValidatorImpl.java index 083a03fb42..1e485d9b27 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/ToscaFunctionValidatorImpl.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/validation/ToscaFunctionValidatorImpl.java @@ -129,11 +129,11 @@ public class ToscaFunctionValidatorImpl implements ToscaFunctionValidator { referredProperty = findSubProperty(referredProperty, toscaGetFunction, model); } - if (!property.getType().equals(referredProperty.getType())) { + if (!property.getType().equals(referredProperty.getType()) && !"list".equalsIgnoreCase(referredProperty.getType())) { throw ToscaGetFunctionExceptionSupplier .propertyTypeDiverge(toscaGetFunction.getType(), referredProperty.getType(), property.getType()).get(); } - if (PropertyType.typeHasSchema(referredProperty.getType()) && !referredProperty.getSchemaType().equals(property.getSchemaType())) { + if (PropertyType.typeHasSchema(referredProperty.getType()) && !referredProperty.getSchemaType().equals(property.getType()) && !referredProperty.getSchemaType().equals(property.getSchemaType())) { throw ToscaGetFunctionExceptionSupplier .propertySchemaDiverge(toscaGetFunction.getType(), referredProperty.getSchemaType(), property.getSchemaType()).get(); } diff --git a/catalog-ui/src/app/models/tosca-get-function.ts b/catalog-ui/src/app/models/tosca-get-function.ts index 7e6c5ad739..3bb207700b 100644 --- a/catalog-ui/src/app/models/tosca-get-function.ts +++ b/catalog-ui/src/app/models/tosca-get-function.ts @@ -34,7 +34,8 @@ export class ToscaGetFunction implements ToscaFunction, ToscaFunctionParameter { sourceName: string; functionType: ToscaGetFunctionType; propertyPathFromSource: Array; - value: any + value: any; + toscaIndex: string; constructor(toscaGetFunction?: ToscaGetFunction) { if (!toscaGetFunction) { @@ -48,6 +49,7 @@ export class ToscaGetFunction implements ToscaFunction, ToscaFunctionParameter { this.sourceUniqueId = toscaGetFunction.sourceUniqueId; this.sourceName = toscaGetFunction.sourceName; this.functionType = toscaGetFunction.functionType; + this.toscaIndex = toscaGetFunction.toscaIndex; if (toscaGetFunction.propertyPathFromSource) { this.propertyPathFromSource = [...toscaGetFunction.propertyPathFromSource]; } @@ -69,20 +71,20 @@ export class ToscaGetFunction implements ToscaFunction, ToscaFunctionParameter { private buildGetInputFunctionValue(): Object { if (this.propertyPathFromSource.length === 1) { - return {[this.functionType.toLowerCase()]: this.propertyPathFromSource[0]}; + return {[this.functionType.toLowerCase()]: [this.propertyPathFromSource[0], this.toscaIndex]}; } - return {[this.functionType.toLowerCase()]: this.propertyPathFromSource}; + return {[this.functionType.toLowerCase()]: [this.propertyPathFromSource, this.toscaIndex]}; } private buildFunctionValueWithPropertySource(): Object { if (this.propertySource == PropertySource.SELF) { return { - [this.functionType.toLowerCase()]: [PropertySource.SELF, ...this.propertyPathFromSource] + [this.functionType.toLowerCase()]: [PropertySource.SELF, ...this.propertyPathFromSource, this.toscaIndex] }; } if (this.propertySource == PropertySource.INSTANCE) { return { - [this.functionType.toLowerCase()]: [this.sourceName, ...this.propertyPathFromSource] + [this.functionType.toLowerCase()]: [this.sourceName, ...this.propertyPathFromSource,this.toscaIndex] }; } } diff --git a/catalog-ui/src/app/ng2/pages/properties-assignment/tosca-function/tosca-get-function/tosca-get-function.component.html b/catalog-ui/src/app/ng2/pages/properties-assignment/tosca-function/tosca-get-function/tosca-get-function.component.html index 62cd697f8c..387bb5cbcb 100644 --- a/catalog-ui/src/app/ng2/pages/properties-assignment/tosca-function/tosca-get-function/tosca-get-function.component.html +++ b/catalog-ui/src/app/ng2/pages/properties-assignment/tosca-function/tosca-get-function/tosca-get-function.component.html @@ -28,9 +28,11 @@
- + +
{{dropDownErrorMsg}}
diff --git a/catalog-ui/src/app/ng2/pages/properties-assignment/tosca-function/tosca-get-function/tosca-get-function.component.ts b/catalog-ui/src/app/ng2/pages/properties-assignment/tosca-function/tosca-get-function/tosca-get-function.component.ts index 36ead13117..fe6f2f143c 100644 --- a/catalog-ui/src/app/ng2/pages/properties-assignment/tosca-function/tosca-get-function/tosca-get-function.component.ts +++ b/catalog-ui/src/app/ng2/pages/properties-assignment/tosca-function/tosca-get-function/tosca-get-function.component.ts @@ -52,7 +52,8 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges { formGroup: FormGroup = new FormGroup({ 'selectedProperty': new FormControl(undefined, Validators.required), - 'propertySource': new FormControl(undefined, Validators.required) + 'propertySource': new FormControl(undefined, Validators.required), + 'toscaIndex' : new FormControl(undefined) }); isLoading: boolean = false; @@ -61,6 +62,7 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges { instanceNameAndIdMap: Map = new Map(); dropdownValuesLabel: string; dropDownErrorMsg: string; + toscaIndexFlag: boolean = false; private isInitialized: boolean = false; private componentMetadata: ComponentMetadata; @@ -78,8 +80,14 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges { if (!this.isInitialized) { return; } + let formGroupStatus : boolean = this.formGroup.valid; + const selectedProperty: PropertyDropdownValue = this.formGroup.value.selectedProperty; + if (selectedProperty != null && selectedProperty.isList && formGroupStatus + && (this.toscaIndex.value == null || this.toscaIndex.value == '')) { + formGroupStatus = false; + } this.onValidityChange.emit({ - isValid: this.formGroup.valid, + isValid: formGroupStatus, toscaGetFunction: this.formGroup.valid ? this.buildGetFunctionFromForm() : undefined }); if (this.formGroup.valid) { @@ -132,6 +140,10 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges { } else { subscriber.next(); } + if (this.toscaGetFunction.toscaIndex != null) { + this.toscaIndexFlag = true; + this.toscaIndex.setValue(this.toscaGetFunction.toscaIndex); + } }); } @@ -154,6 +166,7 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges { toscaGetFunction.propertyUniqueId = selectedProperty.propertyId; toscaGetFunction.propertyName = selectedProperty.propertyName; toscaGetFunction.propertyPathFromSource = selectedProperty.propertyPath; + toscaGetFunction.toscaIndex = this.toscaIndex.value; return toscaGetFunction; } @@ -219,6 +232,7 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges { private resetPropertyDropdown(): void { this.dropDownErrorMsg = undefined; this.selectedProperty.reset(); + this.toscaIndex.reset(); this.propertyDropdownList = []; } @@ -357,7 +371,8 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges { propertyName: property.name, propertyId: property.uniqueId, propertyLabel: property.name, - propertyPath: [property.name] + propertyPath: [property.name], + isList: property.type === PROPERTY_TYPES.LIST }); } else if (this.isComplexType(property.type)) { this.fillPropertyDropdownWithMatchingChildProperties(property); @@ -378,7 +393,8 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges { propertyName: dataTypeProperty.name, propertyId: parentPropertyList[0].uniqueId, propertyLabel: parentPropertyList.map(property => property.name).join('->') + '->' + dataTypeProperty.name, - propertyPath: [...parentPropertyList.map(property => property.name), dataTypeProperty.name] + propertyPath: [...parentPropertyList.map(property => property.name), dataTypeProperty.name], + isList : dataTypeProperty.type === PROPERTY_TYPES.LIST }); } else if (this.isComplexType(dataTypeProperty.type)) { this.fillPropertyDropdownWithMatchingChildProperties(dataTypeProperty, [...parentPropertyList]) @@ -390,23 +406,24 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges { if (this.property.type === PROPERTY_TYPES.ANY) { return true; } + let validPropertyType = property.type === PROPERTY_TYPES.LIST ? property.schemaType : property.type; if (this.typeHasSchema(this.property.type)) { if ((this.property instanceof PropertyDeclareAPIModel && ( this.property).input instanceof DerivedFEProperty) || this.compositionMap) { let childObject : DerivedFEProperty = (( this.property).input); let childSchemaType = this.property.schemaType != null ? this.property.schemaType : childObject.type; if(this.isComplexType(childSchemaType) && !this.compositionMap){ if (childObject.type == PROPERTY_TYPES.MAP && childObject.isChildOfListOrMap) { - return property.type === PROPERTY_TYPES.STRING; + return validPropertyType === PROPERTY_TYPES.STRING; } - return property.type === childObject.type; + return validPropertyType === childObject.type; }else{ - return property.type === this.property.schema.property.type; + return validPropertyType === this.property.schema.property.type; } } if (!property.schema || !property.schema.property) { return false; } - return property.type === this.property.type && this.property.schema.property.type === property.schema.property.type; + return validPropertyType === this.property.type && this.property.schema.property.type === property.schema.property.type; } if (this.property.schema.property.isDataType && this.property instanceof PropertyDeclareAPIModel && (this.property).propertiesName){ let typeToMatch = ( this.property).input.type; @@ -414,10 +431,10 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges { if (childObject.type == PROPERTY_TYPES.MAP && childObject.isChildOfListOrMap) { typeToMatch = PROPERTY_TYPES.STRING; } - return property.type === typeToMatch; + return validPropertyType === typeToMatch; } - return property.type === this.property.type; + return validPropertyType === this.property.type; } private getType(propertyPath:string[], type: string): string { @@ -467,12 +484,32 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges { onPropertySourceChange(): void { this.selectedProperty.reset(); + this.toscaIndex.reset(); if (!this.functionType || !this.propertySource.valid) { return; } this.loadPropertyDropdown(); } + onPropertyValueChange(): void { + this.toscaIndexFlag = false; + this.toscaIndex.reset(); + const selectedProperty: PropertyDropdownValue = this.selectedProperty.value; + if (selectedProperty.isList) { + this.toscaIndexFlag = true; + } + } + + indexTokenChange(): void { + if ((this.toscaIndex.value).toLowerCase() === 'index') { + return; + } + let indexTokenValue = Number(this.toscaIndex.value); + if (isNaN(indexTokenValue)) { + this.toscaIndex.reset(); + } + } + showPropertySourceDropdown(): boolean { return this.isGetProperty() || this.isGetAttribute(); } @@ -489,6 +526,10 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges { return this.formGroup.get('selectedProperty') as FormControl; } + private get toscaIndex(): FormControl { + return this.formGroup.get('toscaIndex') as FormControl; + } + } export interface PropertyDropdownValue { @@ -496,6 +537,7 @@ export interface PropertyDropdownValue { propertyId: string; propertyLabel: string; propertyPath: Array; + isList: boolean; } export interface ToscaGetFunctionValidationEvent { diff --git a/catalog-ui/src/assets/languages/en_US.json b/catalog-ui/src/assets/languages/en_US.json index 18813280d6..ff90fb854e 100644 --- a/catalog-ui/src/assets/languages/en_US.json +++ b/catalog-ui/src/assets/languages/en_US.json @@ -523,6 +523,7 @@ "=========== PROPERTIES ASSIGNMENT TOSCA FUNCTION BUTTON ===========": "", "TOSCA_FUNCTION_LABEL": "TOSCA function", "TOSCA_FUNCTION_PROPERTY_SOURCE_LABEL": "Property Source", + "TOSCA_FUNCTION_PROPERTY_INDEX": "Index", "TOSCA_FUNCTION_CLEAR_VALUE_BUTTON": "Clear Value", "TOSCA_FUNCTION_MODAL_TITLE": "Set value using TOSCA functions", "INPUT_DROPDOWN_LABEL": "Input", diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java index ec3459989e..2e57a4158a 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java @@ -113,6 +113,7 @@ public class ToscaFunctionJsonDeserializer extends StdDeserializer @@ -145,6 +146,22 @@ public class ToscaFunctionJsonDeserializer extends StdDeserializer propertyPathFromSource = new ArrayList<>(); + private Object toscaIndex; public ToscaGetFunctionDataDefinition() { //necessary for JSON conversions @@ -87,6 +89,12 @@ public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunct ); } if (propertySource == PropertySource.SELF) { + if (toscaIndex != null) { + Object toscaIndexValue = StringUtils.isNumeric(toscaIndex.toString()) ? Integer.parseInt(toscaIndex.toString()) : toscaIndex; + return Map.of(functionType.getFunctionName(), + Stream.concat(Stream.of(PropertySource.SELF.getName()), Stream.concat(propertyPathFromSource.stream(),Stream.of(toscaIndexValue))).collect(Collectors.toList()) + ); + } return Map.of(functionType.getFunctionName(), Stream.concat(Stream.of(PropertySource.SELF.getName()), propertyPathFromSource.stream()).collect(Collectors.toList()) ); @@ -97,6 +105,12 @@ public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunct String.format("sourceName is required in order to generate the %s from INSTANCE value", functionType.getFunctionName()) ); } + if (toscaIndex != null) { + Object toscaIndexValue = StringUtils.isNumeric(toscaIndex.toString()) ? Integer.parseInt(toscaIndex.toString()) : toscaIndex; + return Map.of(functionType.getFunctionName(), + Stream.concat(Stream.of(sourceName), Stream.concat(propertyPathFromSource.stream(),Stream.of(toscaIndexValue))).collect(Collectors.toList()) + ); + } return Map.of(functionType.getFunctionName(), Stream.concat(Stream.of(sourceName), propertyPathFromSource.stream()).collect(Collectors.toList()) ); @@ -106,10 +120,17 @@ public class ToscaGetFunctionDataDefinition implements ToscaFunction, ToscaFunct } private Map buildGetInputFunctionValue() { + List propertySourceCopy = new ArrayList(this.propertyPathFromSource); + List propertySourceOneCopy = new ArrayList<>(); + propertySourceOneCopy.add(this.propertyPathFromSource.get(0)); + if (toscaIndex != null) { + propertySourceCopy.add(toscaIndex); + propertySourceOneCopy.add(toscaIndex); + } if (this.propertyPathFromSource.size() == 1) { - return Map.of(this.functionType.getFunctionName(), this.propertyPathFromSource.get(0)); + return Map.of(this.functionType.getFunctionName(), propertySourceOneCopy); } - return Map.of(this.functionType.getFunctionName(), this.propertyPathFromSource); + return Map.of(this.functionType.getFunctionName(), propertySourceCopy); } @Override diff --git a/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinitionTest.java b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinitionTest.java index 5daeaa5aad..581f62a91f 100644 --- a/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinitionTest.java +++ b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaGetFunctionDataDefinitionTest.java @@ -61,8 +61,8 @@ class ToscaGetFunctionDataDefinitionTest { final Map getInputJsonAsMap = convertJsonStringToMap(actualValue); assertTrue(getInputJsonAsMap.containsKey(ToscaGetFunctionType.GET_INPUT.getFunctionName())); final Object value = getInputJsonAsMap.get(ToscaGetFunctionType.GET_INPUT.getFunctionName()); - assertTrue(value instanceof String); - assertEquals(value, propertyName); + assertTrue(value instanceof List); + assertEquals(((List)value).get(0), propertyName); } @Test -- 2.16.6