Support TOSCA functions in Node Filters
[sdc.git] / catalog-ui / src / app / models / tosca-get-function.ts
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 import {PropertySource} from "./property-source";
23 import {ToscaGetFunctionType} from "./tosca-get-function-type";
24 import {ToscaFunction} from "./tosca-function";
25 import {ToscaFunctionType} from "./tosca-function-type.enum";
26 import {ToscaFunctionParameter} from "./tosca-function-parameter";
27
28 export class ToscaGetFunction implements ToscaFunction, ToscaFunctionParameter {
29     type: ToscaFunctionType;
30     propertyUniqueId: string;
31     propertyName: string;
32     propertySource: PropertySource;
33     sourceUniqueId: string;
34     sourceName: string;
35     functionType: ToscaGetFunctionType;
36     propertyPathFromSource: Array<string>;
37     value: any
38
39     constructor(toscaGetFunction?: ToscaGetFunction) {
40         if (!toscaGetFunction) {
41             return;
42         }
43         this.type = toscaGetFunction.type;
44         this.value = toscaGetFunction.value;
45         this.propertyUniqueId = toscaGetFunction.propertyUniqueId;
46         this.propertyName = toscaGetFunction.propertyName;
47         this.propertySource = toscaGetFunction.propertySource;
48         this.sourceUniqueId = toscaGetFunction.sourceUniqueId;
49         this.sourceName = toscaGetFunction.sourceName;
50         this.functionType = toscaGetFunction.functionType;
51         if (toscaGetFunction.propertyPathFromSource) {
52             this.propertyPathFromSource = [...toscaGetFunction.propertyPathFromSource];
53         }
54     }
55
56     public buildValueString(): string {
57         return JSON.stringify(this.buildValueObject());
58     }
59
60     public buildValueObject(): Object {
61         if (this.functionType == ToscaGetFunctionType.GET_PROPERTY || this.functionType == ToscaGetFunctionType.GET_ATTRIBUTE) {
62             return this.buildFunctionValueWithPropertySource();
63         }
64         if (this.functionType == ToscaGetFunctionType.GET_INPUT) {
65             return this.buildGetInputFunctionValue();
66         }
67         return undefined;
68     }
69
70     private buildGetInputFunctionValue(): Object {
71         if (this.propertyPathFromSource.length === 1) {
72             return {[this.functionType.toLowerCase()]: this.propertyPathFromSource[0]};
73         }
74         return {[this.functionType.toLowerCase()]: this.propertyPathFromSource};
75     }
76
77     private buildFunctionValueWithPropertySource(): Object {
78         if (this.propertySource == PropertySource.SELF) {
79             return {
80                 [this.functionType.toLowerCase()]: [PropertySource.SELF, ...this.propertyPathFromSource]
81             };
82         }
83         if (this.propertySource == PropertySource.INSTANCE) {
84             return {
85                 [this.functionType.toLowerCase()]: [this.sourceName, ...this.propertyPathFromSource]
86             };
87         }
88     }
89
90 }