Support a custom yaml value in tosca function
[sdc.git] / catalog-ui / src / app / models / properties-inputs / property-be-model.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import {PROPERTY_DATA, PROPERTY_TYPES} from 'app/utils/constants';
22 import {SchemaProperty, SchemaPropertyGroupModel} from '../schema-property';
23 import {ToscaPresentationData} from '../tosca-presentation';
24 import {PropertyInputDetail} from './property-input-detail';
25 import {Metadata} from '../metadata';
26 import {ToscaFunction} from "../tosca-function";
27 import {ToscaGetFunction} from "../tosca-get-function";
28 import {ToscaGetFunctionTypeConverter} from "../tosca-get-function-type-converter";
29 import {ToscaGetFunctionDto} from "../tosca-get-function-dto";
30
31 export enum DerivedPropertyType {
32     SIMPLE,
33     LIST,
34     MAP,
35     COMPLEX
36 }
37 export class PropertyPolicyDetail {
38     policyId: string;
39     propertyName: string;
40     constructor(propertyPolicy?: PropertyPolicyDetail) {
41         if(propertyPolicy) {
42             this.policyId = propertyPolicy.policyId;
43             this.propertyName = propertyPolicy.propertyName;
44         }
45     }
46 }
47
48 export class PropertyBEModel {
49
50     constraints: any[];
51     defaultValue: string;
52     definition: boolean;
53     description: string;
54     fromDerived: boolean;
55     getInputValues: PropertyInputDetail[];
56     getPolicyValues: PropertyPolicyDetail[];
57     name: string;
58     origName: string;
59     parentUniqueId: string;
60     password: boolean;
61     required: boolean;
62     schema: SchemaPropertyGroupModel;
63     schemaType: string;
64     type: string;
65     uniqueId: string;
66     value: string;
67     parentPropertyType: string;
68     subPropertyInputPath: string;
69     inputPath: string;
70     toscaPresentation: ToscaPresentationData;
71     metadata: Metadata;
72     /**
73      * @deprecated Use toscaFunction instead
74      */
75     toscaGetFunction: ToscaGetFunctionDto;
76     toscaFunction: ToscaFunction;
77
78     constructor(property?: PropertyBEModel) {
79         if (property) {
80             this.constraints = property.constraints;
81             this.defaultValue = property.defaultValue;
82             this.description = property.description;
83             this.fromDerived = property.fromDerived;
84             this.name = property.name;
85             this.origName = property.origName;
86             this.parentUniqueId = property.parentUniqueId;
87             this.password = property.password;
88             this.required = property.required;
89             this.schema = property.schema;
90             this.schemaType = property.schemaType;
91             this.type = property.type;
92             this.uniqueId = property.uniqueId;
93             this.value = property.value;
94             this.definition = property.definition;
95             this.getInputValues = property.getInputValues;
96             this.parentPropertyType = property.parentPropertyType;
97             this.subPropertyInputPath = property.subPropertyInputPath;
98             this.toscaPresentation = property.toscaPresentation;
99             this.getPolicyValues = property.getPolicyValues;
100             this.inputPath = property.inputPath;
101             this.metadata = property.metadata;
102             if (property.toscaFunction) {
103                 this.toscaFunction = property.toscaFunction;
104             } else if (property.toscaGetFunction) {
105                 //support for legacy tosca function
106                 const toscaGetFunction1 = new ToscaGetFunction();
107                 toscaGetFunction1.type = ToscaGetFunctionTypeConverter.convertToToscaFunctionType(property.toscaGetFunction.functionType);
108                 toscaGetFunction1.propertyUniqueId = property.toscaGetFunction.propertyUniqueId;
109                 toscaGetFunction1.propertyName = property.toscaGetFunction.propertyName;
110                 toscaGetFunction1.propertySource = property.toscaGetFunction.propertySource;
111                 toscaGetFunction1.sourceUniqueId = property.toscaGetFunction.sourceUniqueId;
112                 toscaGetFunction1.sourceName = property.toscaGetFunction.sourceName;
113                 toscaGetFunction1.functionType = property.toscaGetFunction.functionType;
114                 toscaGetFunction1.propertyPathFromSource = property.toscaGetFunction.propertyPathFromSource;
115                 this.toscaFunction = toscaGetFunction1;
116             }
117         }
118
119         if (!this.schema || !this.schema.property) {
120             this.schema = new SchemaPropertyGroupModel(new SchemaProperty());
121         } else { // forcing creating new object, so editing different one than the object in the table
122             this.schema = new SchemaPropertyGroupModel(new SchemaProperty(this.schema.property));
123         }
124     }
125
126     public toJSON = (): any => {
127         const temp = angular.copy(this);
128         temp.value = temp.value === '{}' || temp.value === '[]' ? undefined : temp.value;
129         temp.defaultValue = temp.defaultValue === '{}' || temp.defaultValue === '[]' ? undefined : temp.defaultValue;
130         return temp;
131     }
132
133     public getDerivedPropertyType = (): DerivedPropertyType => {
134         if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) > -1) {
135             return DerivedPropertyType.SIMPLE;
136         }
137         if (this.type === PROPERTY_TYPES.LIST) {
138             return DerivedPropertyType.LIST;
139         }
140         if (this.type === PROPERTY_TYPES.MAP) {
141             return DerivedPropertyType.MAP;
142         }
143         return DerivedPropertyType.COMPLEX;
144     }
145
146     /**
147      * Parses default value to JSON.
148      */
149     public parseDefaultValueToJson(): any {
150         if (this.defaultValue == undefined) {
151             return undefined;
152         }
153
154         const propertyType: DerivedPropertyType = this.getDerivedPropertyType();
155         if (propertyType == DerivedPropertyType.SIMPLE) {
156             return this.parseDefaultSimpleValue();
157         }
158
159         try {
160             return JSON.parse(this.defaultValue);
161         } catch (e) {
162             console.error(`Could not parse the property of type '${this.type}' default value to JSON '${this.defaultValue}'`, e);
163         }
164
165         return undefined;
166     }
167
168     private parseDefaultSimpleValue() {
169         switch (this.type) {
170             case PROPERTY_TYPES.INTEGER:
171                 try {
172                     return parseInt(this.defaultValue);
173                 } catch (e) {
174                     console.error(`Could not parse the property of type '${this.type}' default value to int '${this.defaultValue}'`, e);
175                 }
176                 return undefined;
177             case PROPERTY_TYPES.FLOAT:
178                 try {
179                     return parseFloat(this.defaultValue);
180                 } catch (e) {
181                     console.error(`Could not parse the property of type '${this.type}' default value to float '${this.defaultValue}'`, e);
182                 }
183                 return undefined;
184             case PROPERTY_TYPES.BOOLEAN:
185                 return this.defaultValue === 'true';
186             default:
187                 return this.defaultValue;
188         }
189     }
190
191     /**
192      * Checks whether the property value is a TOSCA function (e.g. get_input, get_property, get_attribute, concat, etc.)
193      */
194     public isToscaFunction(): boolean {
195         return this.toscaFunction != null;
196     }
197 }
198