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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
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";
28 export class ToscaGetFunction implements ToscaFunction, ToscaFunctionParameter {
29 type: ToscaFunctionType;
30 propertyUniqueId: string;
32 propertySource: PropertySource;
33 sourceUniqueId: string;
35 functionType: ToscaGetFunctionType;
36 propertyPathFromSource: Array<string>;
38 toscaIndexList: Array<string>;
40 constructor(toscaGetFunction?: ToscaGetFunction) {
41 if (!toscaGetFunction) {
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];
58 public buildValueString(): string {
59 return JSON.stringify(this.buildValueObject());
62 public buildValueObject(): Object {
63 if (this.functionType == ToscaGetFunctionType.GET_PROPERTY || this.functionType == ToscaGetFunctionType.GET_ATTRIBUTE) {
64 return this.buildFunctionValueWithPropertySource();
66 if (this.functionType == ToscaGetFunctionType.GET_INPUT) {
67 return this.buildGetInputFunctionValue();
72 private buildGetInputFunctionValue(): Object {
73 if (this.propertyPathFromSource.length === 1) {
74 return {[this.functionType.toLowerCase()]: [this.propertyPathFromSource[0], this.toscaIndexList]};
76 return {[this.functionType.toLowerCase()]: [this.propertyPathFromSource, this.toscaIndexList]};
79 private buildFunctionValueWithPropertySource(): Object {
80 if (this.propertySource == PropertySource.SELF) {
82 [this.functionType.toLowerCase()]: [PropertySource.SELF, ...this.propertyPathFromSource, this.toscaIndexList]
85 if (this.propertySource == PropertySource.INSTANCE) {
87 [this.functionType.toLowerCase()]: [this.sourceName, ...this.propertyPathFromSource,this.toscaIndexList]