784c7ea407d1a5d4b3701f7f79fd4594a8123d42
[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     toscaIndexList: Array<string>;
39
40     constructor(toscaGetFunction?: ToscaGetFunction) {
41         if (!toscaGetFunction) {
42             return;
43         }
44         this.type = toscaGetFunction.type;
45         this.value = toscaGetFunction.value;
46         this.propertyUniqueId = toscaGetFunction.propertyUniqueId;
47         this.propertyName = toscaGetFunction.propertyName;
48         this.propertySource = toscaGetFunction.propertySource;
49         this.sourceUniqueId = toscaGetFunction.sourceUniqueId;
50         this.sourceName = toscaGetFunction.sourceName;
51         this.functionType = toscaGetFunction.functionType;
52         this.toscaIndexList = toscaGetFunction.toscaIndexList;
53         if (toscaGetFunction.propertyPathFromSource) {
54             this.propertyPathFromSource = [...toscaGetFunction.propertyPathFromSource];
55         }
56     }
57
58     public buildValueString(): string {
59         return JSON.stringify(this.buildValueObject());
60     }
61
62     public buildValueObject(): Object {
63         if (this.functionType == ToscaGetFunctionType.GET_PROPERTY || this.functionType == ToscaGetFunctionType.GET_ATTRIBUTE) {
64             return this.buildFunctionValueWithPropertySource();
65         }
66         if (this.functionType == ToscaGetFunctionType.GET_INPUT) {
67             return this.buildGetInputFunctionValue();
68         }
69         return undefined;
70     }
71
72     private buildGetInputFunctionValue(): Object {
73         if (this.propertyPathFromSource.length === 1) {
74             if (this.toscaIndexList) {
75                 return {[this.functionType.toLowerCase()]: [this.propertyPathFromSource[0], this.toscaIndexList]};
76             }
77             return {[this.functionType.toLowerCase()]: [this.propertyPathFromSource[0]]};
78         }
79         return {[this.functionType.toLowerCase()]: [this.propertyPathFromSource, this.toscaIndexList]};
80     }
81
82     private buildFunctionValueWithPropertySource(): Object {
83         if (this.propertySource == PropertySource.SELF) {
84             if (this.toscaIndexList) {
85                 return {
86                     [this.functionType.toLowerCase()]: [PropertySource.SELF, ...this.propertyPathFromSource, this.toscaIndexList]
87                 };
88             }
89             return {
90                 [this.functionType.toLowerCase()]: [PropertySource.SELF, ...this.propertyPathFromSource]
91             };
92         }
93         if (this.propertySource == PropertySource.INSTANCE) {
94             if (this.toscaIndexList) {
95                 return {
96                     [this.functionType.toLowerCase()]: [this.sourceName, ...this.propertyPathFromSource, this.toscaIndexList]
97                 };
98             }
99             return {
100                 [this.functionType.toLowerCase()]: [this.sourceName, ...this.propertyPathFromSource]
101             };
102         }
103     }
104 }