No properties found when trying to add a node filter to a VF
[sdc.git] / catalog-ui / src / app / ng2 / pages / properties-assignment / tosca-function / tosca-get-function / tosca-get-function.component.ts
index 7987ba2..8c84c0e 100644 (file)
@@ -33,6 +33,7 @@ import {InstanceFeDetails} from "../../../../../models/instance-fe-details";
 import {ToscaGetFunction} from "../../../../../models/tosca-get-function";
 import {FormControl, FormGroup, Validators} from "@angular/forms";
 import {ToscaGetFunctionTypeConverter} from "../../../../../models/tosca-get-function-type-converter";
+import {ResourceType} from "app/utils";
 
 @Component({
     selector: 'app-tosca-get-function',
@@ -63,6 +64,7 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
     dropdownValuesLabel: string;
     dropDownErrorMsg: string;
     indexListValues:Array<ToscaIndexObject>;
+    parentListTypeFlag : boolean;
 
     private isInitialized: boolean = false;
     private componentMetadata: ComponentMetadata;
@@ -77,6 +79,10 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
     ngOnInit(): void {
         this.componentMetadata = this.workspaceService.metadata;
         this.indexListValues = [];
+        if (this.property != null) {
+            this.parentListTypeFlag = (this.property.type != PROPERTY_TYPES.LIST && (!this.isComplexType(this.property.type) || (this.isComplexType(this.property.type) 
+                                    && this.property instanceof PropertyDeclareAPIModel && (<PropertyDeclareAPIModel> this.property).input instanceof DerivedFEProperty && this.property.input.type != PROPERTY_TYPES.LIST)));
+        }
         this.formGroup.valueChanges.subscribe(() => {
             this.formValidation();
         });
@@ -120,7 +126,7 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
                 this.loadPropertyDropdown(() => {
                     this.selectedProperty
                     .setValue(this.propertyDropdownList.find(property => property.propertyName === this.toscaGetFunction.propertyName));
-                    if (this.toscaGetFunction.toscaIndexList.length > 0) {
+                    if (this.toscaGetFunction.toscaIndexList && this.toscaGetFunction.toscaIndexList.length > 0) {
                         let tempSelectedProperty : PropertyDropdownValue = this.selectedProperty.value;
                         this.toscaGetFunction.toscaIndexList.forEach((indexValue: string, index) => {
                             let tempIndexFlag = false;
@@ -128,7 +134,7 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
                             let tempIndexValue = "0";
                             let tempIndexProperty = tempSelectedProperty;
                             let subPropertyDropdownList : Array<PropertyDropdownValue> = [];
-                            if (index%2 == 0) {
+                            if (!isNaN(Number(indexValue)) || indexValue.toLowerCase() === 'index') {
                                 tempIndexFlag = true;
                                 tempIndexValue = indexValue;
                                 tempSelectedProperty = null;
@@ -138,6 +144,9 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
                                         const dataTypeFound: DataTypeModel = this.dataTypeService.getDataTypeByModelAndTypeName(this.componentMetadata.model, tempIndexProperty.schemaType);
                                         this.addPropertiesToDropdown(dataTypeFound.properties, subPropertyDropdownList);
                                         tempSelectedProperty = subPropertyDropdownList.find(property => property.propertyName === this.toscaGetFunction.toscaIndexList[index+1])
+                                        if (tempSelectedProperty == null && this.toscaGetFunction.toscaIndexList[index+2]) {
+                                            tempSelectedProperty = subPropertyDropdownList.find(property => property.propertyName === this.toscaGetFunction.toscaIndexList[index+2])
+                                        }
                                     }
                                 }
                                 let tempIndexValueMap : ToscaIndexObject = {indexFlag : tempIndexFlag, nestedFlag : tempNestedFlag, indexValue: tempIndexValue, indexProperty: tempSelectedProperty, subPropertyArray: subPropertyDropdownList};
@@ -177,7 +186,7 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
             this.indexListValues.forEach((indexObject : ToscaIndexObject) => {
                 indexAndProperty.push(indexObject.indexValue);
                 if(indexObject.nestedFlag && indexObject.indexProperty != null) {
-                    indexAndProperty.push(indexObject.indexProperty.propertyName);
+                    indexAndProperty.push(...indexObject.indexProperty.propertyPath);
                 }
             });
             toscaGetFunction.toscaIndexList = indexAndProperty;
@@ -221,7 +230,7 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
         const selectedProperty: PropertyDropdownValue = this.formGroup.value.selectedProperty;
         if (selectedProperty != null && selectedProperty.isList && formGroupStatus && this.indexListValues.length > 0) {
             this.indexListValues.forEach((indexObject : ToscaIndexObject, index) => {
-                if (indexObject.indexValue == null) {
+                if (indexObject.indexValue == '') {
                     formGroupStatus = false;
                     return;
                 }
@@ -321,7 +330,7 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
            if (this.isSubProperty()){
             if ((this.property instanceof PropertyDeclareAPIModel && (<PropertyDeclareAPIModel> this.property).input instanceof DerivedFEProperty)
                 || this.compositionMap) {
-                if(this.isComplexType(this.property.schemaType) && !this.compositionMap){
+                if(this.isComplexType(this.property.schemaType)){
                     let mapChildProp : DerivedFEProperty = (<DerivedFEProperty> (<PropertyDeclareAPIModel> this.property).input);
                     let propertySchemaType = mapChildProp.type;
                     if (this.property.type == PROPERTY_TYPES.MAP || propertySchemaType == PROPERTY_TYPES.MAP) {
@@ -359,6 +368,10 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
             if (this.isPropertySourceSelf()) {
                 return componentGenericResponse.properties;
             }
+            let componentInstanceInput = componentGenericResponse.componentInstances.find(compInst => this.isInput(compInst.originType) && compInst.uniqueId === instanceId);
+            if ( componentInstanceInput) {
+                return this.removeSelectedProperty(componentGenericResponse.componentInstancesInputs[instanceId]);
+            }
             return this.removeSelectedProperty(componentGenericResponse.componentInstancesProperties[instanceId]);
         }
         if (this.isPropertySourceSelf()) {
@@ -368,6 +381,10 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
             ...(componentGenericResponse.componentInstancesProperties[instanceId] || [])];
     }
 
+    private isInput (instanceType:string): boolean {
+        return instanceType === ResourceType.VF || instanceType === ResourceType.PNF || instanceType === ResourceType.CVFC || instanceType === ResourceType.CR;
+    }
+
     private isPropertySourceSelf() {
         return this.propertySource.value === PropertySource.SELF;
     }
@@ -380,7 +397,8 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
             if (this.isPropertySourceSelf()) {
                 return this.topologyTemplateService.findAllComponentProperties(this.componentMetadata.componentType, this.componentMetadata.uniqueId);
             }
-            return this.topologyTemplateService.getComponentInstanceProperties(this.componentMetadata.componentType, this.componentMetadata.uniqueId);
+            return this.topologyTemplateService.getComponentInstancesAndInputsAndProperties(this.componentMetadata.componentType, this.componentMetadata.uniqueId);
+            // return this.topologyTemplateService.getComponentInstanceInputsAndProperties(this.componentMetadata.componentType, this.componentMetadata.uniqueId);
         }
         if (this.isGetAttribute()) {
             if (this.isPropertySourceSelf()) {
@@ -401,8 +419,10 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
     }
 
     private addPropertyToDropdown(propertyDropdownValue: PropertyDropdownValue, propertyList: Array<PropertyDropdownValue>): void {
-        propertyList.push(propertyDropdownValue);
-        propertyList.sort((a, b) => a.propertyLabel.localeCompare(b.propertyLabel));
+        if (!propertyList.find(prop => prop.propertyName === propertyDropdownValue.propertyName)) {
+            propertyList.push(propertyDropdownValue);
+            propertyList.sort((a, b) => a.propertyLabel.localeCompare(b.propertyLabel));
+        }
     }
 
     private addPropertiesToDropdown(properties: Array<PropertyBEModel | AttributeBEModel>, propertyList: Array<PropertyDropdownValue>): void {
@@ -452,8 +472,8 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
         if (this.property.type === PROPERTY_TYPES.ANY) {
             return true;
         }
-        let validPropertyType = (this.property.type != PROPERTY_TYPES.LIST && property.type === PROPERTY_TYPES.LIST) ? property.schema.property.type : property.type;
-        if (this.property.type != PROPERTY_TYPES.LIST && property.type === PROPERTY_TYPES.LIST && this.isComplexType(validPropertyType)) {
+        let validPropertyType = (this.parentListTypeFlag && property.type === PROPERTY_TYPES.LIST) ? property.schema.property.type : property.type;
+        if (this.parentListTypeFlag && property.type === PROPERTY_TYPES.LIST && this.isComplexType(validPropertyType)) {
             let returnFlag : boolean = false;
             const dataTypeFound: DataTypeModel = this.dataTypeService.getDataTypeByModelAndTypeName(this.componentMetadata.model, validPropertyType);
             if (dataTypeFound && dataTypeFound.properties) {
@@ -461,6 +481,16 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
                     if (this.hasSameType(dataTypeProperty)) {
                         returnFlag =  true;
                     }
+                    if (!returnFlag && this.isComplexType(dataTypeProperty.type)) {
+                        const nestedDataTypeFound: DataTypeModel = this.dataTypeService.getDataTypeByModelAndTypeName(this.componentMetadata.model, dataTypeProperty.type);
+                        if (nestedDataTypeFound && nestedDataTypeFound.properties) {
+                            nestedDataTypeFound.properties.forEach( nestedDateTypeProperty => {
+                                if (this.hasSameType(nestedDateTypeProperty)) {
+                                    returnFlag =  true;
+                                }
+                            });
+                        }
+                    }
                 });
             }
             return returnFlag;
@@ -468,8 +498,8 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
         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){
+                let childSchemaType = (this.property != null && this.property.schemaType != null) ? this.property.schemaType : childObject.type;
+                if(this.isComplexType(childSchemaType)){
                     if (childObject.type == PROPERTY_TYPES.MAP && childObject.isChildOfListOrMap) {
                         return validPropertyType === PROPERTY_TYPES.STRING;
                     }
@@ -483,12 +513,15 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
             }
             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){
+        if ((this.property.schema.property.isDataType || this.isComplexType(this.property.type)) && this.property instanceof PropertyDeclareAPIModel && (<PropertyDeclareAPIModel>this.property).propertiesName){
             let typeToMatch = (<PropertyDeclareAPIModel> this.property).input.type;
             let childObject : DerivedFEProperty = (<DerivedFEProperty>(<PropertyDeclareAPIModel> this.property).input);
             if (childObject.type == PROPERTY_TYPES.MAP && childObject.isChildOfListOrMap) {
                 typeToMatch = PROPERTY_TYPES.STRING;
             }
+            if ((typeToMatch === PROPERTY_TYPES.LIST || typeToMatch === PROPERTY_TYPES.MAP) && (<PropertyDeclareAPIModel> this.property).input.schema.property.type && this.compositionMap && !isNaN(Number(this.compositionMapKey))) {
+                typeToMatch = (<PropertyDeclareAPIModel> this.property).input.schema.property.type;
+            }
             return validPropertyType === typeToMatch;
         }
 
@@ -555,7 +588,7 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
         this.indexListValues = [];
         let subPropertyDropdownList : Array<PropertyDropdownValue> = [];
         const selectedProperty: PropertyDropdownValue = this.selectedProperty.value;
-        if (selectedProperty.isList) {
+        if (this.parentListTypeFlag && selectedProperty.isList) {
             toscaIndexFlag = true;
             if (selectedProperty.schemaType != null) {
                 nestedToscaFlag = true;
@@ -599,17 +632,23 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
     }
 
     indexTokenChange(indexObject : ToscaIndexObject): void {
-        if ((indexObject.indexValue).toLowerCase() === 'index') {
+        if ((indexObject.indexValue).toLowerCase() === 'index' ) {
             this.formValidation();
-            return;
         }
-        let indexTokenValue = Number(indexObject.indexValue);
-        if (isNaN(indexTokenValue)) {
-            indexObject.indexValue = "0";
+
+        const regEx = /^[0-9]*$/;
+        const error = document.getElementById('error');
+
+        if (!(regEx.test(indexObject.indexValue)) && (indexObject.indexValue).toLowerCase() !== 'index') {
+            error.textContent='Invalid value - must be an integer or INDEX';
+            this.onValidityChange.emit({
+                isValid: false,
+                toscaGetFunction: this.formGroup.valid ? this.buildGetFunctionFromForm() : undefined
+            });
+        } else {
+            error.textContent='';
             this.formValidation();
-            return;
         }
-        this.formValidation();
     }
 
     showPropertySourceDropdown(): boolean {
@@ -628,6 +667,9 @@ export class ToscaGetFunctionComponent implements OnInit, OnChanges {
         return this.formGroup.get('selectedProperty') as FormControl;
     }
 
+    onChangeIndexValue(index: ToscaIndexObject, value: any) {
+        this.indexTokenChange(index);
+    }
 }
 
 export interface PropertyDropdownValue {