Providing the capability to add the index token to th tosca function of all types
Issue-ID: SDC-4472
Signed-off-by: Imam hussain <imam.hussain@est.tech>
Change-Id: Ib7ac80f31710101f50de76bdb7c79abdc637cfe3
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;
}
final List<String> functionParameters;
try {
- functionParameters = (List<String>) functionValueObj;
+ functionParameters = ((List<Object>) functionValueObj).stream()
+ .map(object -> Objects.toString(object, null))
+ .collect(Collectors.toList());
} catch (final ClassCastException ignored) {
return Optional.empty();
}
toscaGetFunction.setPropertySource(PropertySource.INSTANCE);
toscaGetFunction.setSourceName(propertySourceType);
}
- toscaGetFunction.setPropertyPathFromSource(functionParameters.subList(1, functionParameters.size()));
+ List<String> 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);
} else {
final List<String> functionParameters;
try {
- functionParameters = (List<String>) functionValueObj;
+ functionParameters = ((List<Object>) 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);
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();
}
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();
}
sourceName: string;
functionType: ToscaGetFunctionType;
propertyPathFromSource: Array<string>;
- value: any
+ value: any;
+ toscaIndex: string;
constructor(toscaGetFunction?: ToscaGetFunction) {
if (!toscaGetFunction) {
this.sourceUniqueId = toscaGetFunction.sourceUniqueId;
this.sourceName = toscaGetFunction.sourceName;
this.functionType = toscaGetFunction.functionType;
+ this.toscaIndex = toscaGetFunction.toscaIndex;
if (toscaGetFunction.propertyPathFromSource) {
this.propertyPathFromSource = [...toscaGetFunction.propertyPathFromSource];
}
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]
};
}
}
</div>
<div *ngIf="showPropertyDropdown()" class="i-sdc-form-item">
<label class="i-sdc-form-label required">{{dropdownValuesLabel}}</label>
- <select formControlName="selectedProperty">
+ <select formControlName="selectedProperty" (change)="onPropertyValueChange()">
<option *ngFor="let value of propertyDropdownList" [ngValue]="value">{{value.propertyLabel}}</option>
</select>
+ <label class="i-sdc-form-label required" *ngIf="toscaIndexFlag">Index</label>
+ <input type="text" *ngIf="toscaIndexFlag" formControlName="toscaIndex" (change)="indexTokenChange()"/>
</div>
<div *ngIf="dropDownErrorMsg" class="tosca-error">{{dropDownErrorMsg}}</div>
</form>
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;
instanceNameAndIdMap: Map<string, string> = new Map<string, string>();
dropdownValuesLabel: string;
dropDownErrorMsg: string;
+ toscaIndexFlag: boolean = false;
private isInitialized: boolean = false;
private componentMetadata: ComponentMetadata;
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) {
} else {
subscriber.next();
}
+ if (this.toscaGetFunction.toscaIndex != null) {
+ this.toscaIndexFlag = true;
+ this.toscaIndex.setValue(this.toscaGetFunction.toscaIndex);
+ }
});
}
toscaGetFunction.propertyUniqueId = selectedProperty.propertyId;
toscaGetFunction.propertyName = selectedProperty.propertyName;
toscaGetFunction.propertyPathFromSource = selectedProperty.propertyPath;
+ toscaGetFunction.toscaIndex = this.toscaIndex.value;
return toscaGetFunction;
}
private resetPropertyDropdown(): void {
this.dropDownErrorMsg = undefined;
this.selectedProperty.reset();
+ this.toscaIndex.reset();
this.propertyDropdownList = [];
}
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);
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])
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 && (<PropertyDeclareAPIModel> this.property).input instanceof DerivedFEProperty) || this.compositionMap) {
let childObject : DerivedFEProperty = (<DerivedFEProperty>(<PropertyDeclareAPIModel> 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 && (<PropertyDeclareAPIModel>this.property).propertiesName){
let typeToMatch = (<PropertyDeclareAPIModel> this.property).input.type;
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 {
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();
}
return this.formGroup.get('selectedProperty') as FormControl;
}
+ private get toscaIndex(): FormControl {
+ return this.formGroup.get('toscaIndex') as FormControl;
+ }
+
}
export interface PropertyDropdownValue {
propertyId: string;
propertyLabel: string;
propertyPath: Array<string>;
+ isList: boolean;
}
export interface ToscaGetFunctionValidationEvent {
"=========== 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",
toscaGetFunction.setSourceUniqueId(getAsTextOrElseNull(node, "sourceUniqueId"));
toscaGetFunction.setPropertyName(getAsTextOrElseNull(node, "propertyName"));
toscaGetFunction.setPropertyUniqueId(getAsTextOrElseNull(node, "propertyUniqueId"));
+ toscaGetFunction.setToscaIndex(getNumberAsTextOrElseNull(node, "toscaIndex"));
final String propertySource = getAsTextOrElseNull(node, "propertySource");
if (StringUtils.isNotEmpty(propertySource)) {
final PropertySource propertySource1 = PropertySource.findType(propertySource).orElseThrow(() ->
return jsonNode.asText();
}
+ private Object getNumberAsTextOrElseNull(final JsonNode node, final String fieldName) {
+ final JsonNode jsonNode = node.get(fieldName);
+ if (jsonNode == null) {
+ return null;
+ }
+ if (jsonNode.asText().equalsIgnoreCase("INDEX")) {
+ return jsonNode.asText();
+ }
+ try {
+ Integer.parseInt(jsonNode.asText());
+ } catch(Exception e) {
+ return null;
+ }
+ return Integer.parseInt(jsonNode.asText());
+ }
+
private ToscaConcatFunction deserializeConcatFunction(final JsonNode concatFunctionJsonNode,
final DeserializationContext context) throws IOException {
final var toscaConcatFunction = new ToscaConcatFunction();
import java.util.stream.Stream;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
+import org.apache.commons.lang3.StringUtils;
import org.openecomp.sdc.be.datatypes.enums.PropertySource;
import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
private String sourceName;
private ToscaGetFunctionType functionType;
private List<String> propertyPathFromSource = new ArrayList<>();
+ private Object toscaIndex;
public ToscaGetFunctionDataDefinition() {
//necessary for JSON conversions
);
}
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())
);
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())
);
}
private Map<String, Object> buildGetInputFunctionValue() {
+ List<Object> propertySourceCopy = new ArrayList<Object>(this.propertyPathFromSource);
+ List<Object> 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
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<String>)value).get(0), propertyName);
}
@Test