Support TOSCA functions in Node Filters
[sdc.git] / catalog-ui / src / app / utils / filter-constraint-helper.ts
1 import {FilterConstraint} from "../models/filter-constraint";
2 import {ToscaFunctionType} from "../models/tosca-function-type.enum";
3 import {ToscaConcatFunction} from "../models/tosca-concat-function";
4 import {ToscaGetFunction} from "../models/tosca-get-function";
5 import {YamlFunction} from "../models/yaml-function";
6 import {CapabilityFilterConstraint} from "../models/capability-filter-constraint";
7
8 export class FilterConstraintHelper {
9
10     public static buildFilterConstraintLabel(constraint: FilterConstraint | CapabilityFilterConstraint): string {
11         let value;
12         if (this.isValueToscaFunction(constraint.value)) {
13             switch (constraint.value.type) {
14                 case ToscaFunctionType.CONCAT:
15                     value = new ToscaConcatFunction(constraint.value).buildValueString();
16                     break;
17                 case ToscaFunctionType.GET_PROPERTY:
18                 case ToscaFunctionType.GET_INPUT:
19                 case ToscaFunctionType.GET_ATTRIBUTE:
20                     value = new ToscaGetFunction(constraint.value).buildValueString();
21                     break;
22                 case ToscaFunctionType.YAML:
23                     value = new YamlFunction(constraint.value).buildValueString();
24                     break;
25                 case ToscaFunctionType.STRING:
26                     value = constraint.value.value;
27                     break;
28                 default:
29                     value = JSON.stringify(constraint.value, null, 4);
30             }
31         } else {
32             value = JSON.stringify(constraint.value, null, 4);
33         }
34         if (constraint instanceof CapabilityFilterConstraint) {
35             return `${constraint.capabilityName}: ${constraint.servicePropertyName} ${this.convertToSymbol(constraint.constraintOperator)} ${value}`;
36         }
37
38         return `${constraint.servicePropertyName} ${this.convertToSymbol(constraint.constraintOperator)} ${value}`;
39     }
40
41     public static convertToSymbol(constraintOperator: string) {
42         switch (constraintOperator) {
43             case OPERATOR_TYPES.LESS_THAN: return '<';
44             case OPERATOR_TYPES.EQUAL: return '=';
45             case OPERATOR_TYPES.GREATER_THAN: return '>';
46             case OPERATOR_TYPES.GREATER_OR_EQUAL: return '>=';
47             case OPERATOR_TYPES.LESS_OR_EQUAL: return '<=';
48         }
49     }
50
51     private static isValueToscaFunction(value: any): boolean {
52         return value instanceof Object && 'type' in value && (<any>Object).values(ToscaFunctionType).includes(value.type);
53     }
54
55 }
56
57 export const OPERATOR_TYPES = {
58     EQUAL: 'equal',
59     GREATER_THAN: 'greater_than',
60     LESS_THAN: 'less_than',
61     GREATER_OR_EQUAL: 'greater_or_equal',
62     LESS_OR_EQUAL: 'less_or_equal'
63 };
64