ae72977b80cfa16a409e8b5d83269e115e5adb82
[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 {SubPropertyToscaFunction} from "../sub-property-tosca-function";
27 import {ToscaFunction} from "../tosca-function";
28 import {ToscaGetFunction} from "../tosca-get-function";
29 import {ToscaGetFunctionTypeConverter} from "../tosca-get-function-type-converter";
30 import {ToscaGetFunctionDto} from "../tosca-get-function-dto";
31
32 export enum DerivedPropertyType {
33     SIMPLE,
34     LIST,
35     MAP,
36     COMPLEX,
37     RANGE
38 }
39 export class PropertyPolicyDetail {
40     policyId: string;
41     propertyName: string;
42     constructor(propertyPolicy?: PropertyPolicyDetail) {
43         if(propertyPolicy) {
44             this.policyId = propertyPolicy.policyId;
45             this.propertyName = propertyPolicy.propertyName;
46         }
47     }
48 }
49
50 export class PropertyBEModel {
51
52     constraints: any[];
53     defaultValue: string;
54     definition: boolean;
55     description: string;
56     fromDerived: boolean;
57     getInputValues: PropertyInputDetail[];
58     getPolicyValues: PropertyPolicyDetail[];
59     name: string;
60     origName: string;
61     parentUniqueId: string;
62     password: boolean;
63     required: boolean;
64     schema: SchemaPropertyGroupModel;
65     schemaType: string;
66     type: string;
67     uniqueId: string;
68     value: string;
69     parentPropertyType: string;
70     subPropertyInputPath: string;
71     inputPath: string;
72     toscaPresentation: ToscaPresentationData;
73     metadata: Metadata;
74     propertyConstraints: any;
75     /**
76      * @deprecated Use toscaFunction instead
77      */
78     toscaGetFunction: ToscaGetFunctionDto;
79     toscaFunction: ToscaFunction;
80     subPropertyToscaFunctions: SubPropertyToscaFunction[];
81
82     constructor(property?: PropertyBEModel) {
83         if (property) {
84             this.constraints = property.constraints;
85             this.propertyConstraints = property.propertyConstraints;
86             this.defaultValue = property.defaultValue;
87             this.description = property.description;
88             this.fromDerived = property.fromDerived;
89             this.name = property.name;
90             this.origName = property.origName;
91             this.parentUniqueId = property.parentUniqueId;
92             this.password = property.password;
93             this.required = property.required;
94             this.schema = property.schema;
95             this.schemaType = property.schemaType;
96             this.type = property.type;
97             this.uniqueId = property.uniqueId;
98             this.value = property.value;
99             this.definition = property.definition;
100             this.getInputValues = property.getInputValues;
101             this.parentPropertyType = property.parentPropertyType;
102             this.subPropertyInputPath = property.subPropertyInputPath;
103             this.toscaPresentation = property.toscaPresentation;
104             this.getPolicyValues = property.getPolicyValues;
105             this.inputPath = property.inputPath;
106             this.metadata = property.metadata;
107             if (property.toscaFunction) {
108                 this.toscaFunction = property.toscaFunction;
109             } else if (property.toscaGetFunction) {
110                 //support for legacy tosca function
111                 const toscaGetFunction1 = new ToscaGetFunction();
112                 toscaGetFunction1.type = ToscaGetFunctionTypeConverter.convertToToscaFunctionType(property.toscaGetFunction.functionType);
113                 toscaGetFunction1.propertyUniqueId = property.toscaGetFunction.propertyUniqueId;
114                 toscaGetFunction1.propertyName = property.toscaGetFunction.propertyName;
115                 toscaGetFunction1.propertySource = property.toscaGetFunction.propertySource;
116                 toscaGetFunction1.sourceUniqueId = property.toscaGetFunction.sourceUniqueId;
117                 toscaGetFunction1.sourceName = property.toscaGetFunction.sourceName;
118                 toscaGetFunction1.functionType = property.toscaGetFunction.functionType;
119                 toscaGetFunction1.propertyPathFromSource = property.toscaGetFunction.propertyPathFromSource;
120                 this.toscaFunction = toscaGetFunction1;
121             }
122             this.subPropertyToscaFunctions = property.subPropertyToscaFunctions;
123         }
124
125         if (!this.schema || !this.schema.property) {
126             this.schema = new SchemaPropertyGroupModel(new SchemaProperty());
127         } else { // forcing creating new object, so editing different one than the object in the table
128             this.schema = new SchemaPropertyGroupModel(new SchemaProperty(this.schema.property));
129         }
130     }
131
132     public toJSON = (): any => {
133         const temp = angular.copy(this);
134         temp.value = temp.value === '{}' || temp.value === '[]' ? undefined : temp.value;
135         temp.defaultValue = temp.defaultValue === '{}' || temp.defaultValue === '[]' ? undefined : temp.defaultValue;
136         return temp;
137     }
138
139     public getDerivedPropertyType = (): DerivedPropertyType => {
140         if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) > -1) {
141             return DerivedPropertyType.SIMPLE;
142         }
143         if (this.type === PROPERTY_TYPES.LIST) {
144             return DerivedPropertyType.LIST;
145         }
146         if (this.type === PROPERTY_TYPES.RANGE) {
147             return DerivedPropertyType.RANGE;
148         }
149         if (this.type === PROPERTY_TYPES.MAP) {
150             return DerivedPropertyType.MAP;
151         }
152         return DerivedPropertyType.COMPLEX;
153     }
154
155     /**
156      * Parses default value to JSON.
157      */
158     public parseDefaultValueToJson(): any {
159         if (this.defaultValue == undefined) {
160             return undefined;
161         }
162
163         const propertyType: DerivedPropertyType = this.getDerivedPropertyType();
164         if (propertyType == DerivedPropertyType.SIMPLE) {
165             return this.parseDefaultSimpleValue();
166         }
167
168         try {
169             return JSON.parse(this.defaultValue);
170         } catch (e) {
171             console.error(`Could not parse the property of type '${this.type}' default value to JSON '${this.defaultValue}'`, e);
172         }
173
174         return undefined;
175     }
176
177     private parseDefaultSimpleValue() {
178         switch (this.type) {
179             case PROPERTY_TYPES.INTEGER:
180                 try {
181                     return parseInt(this.defaultValue);
182                 } catch (e) {
183                     console.error(`Could not parse the property of type '${this.type}' default value to int '${this.defaultValue}'`, e);
184                 }
185                 return undefined;
186             case PROPERTY_TYPES.FLOAT:
187                 try {
188                     return parseFloat(this.defaultValue);
189                 } catch (e) {
190                     console.error(`Could not parse the property of type '${this.type}' default value to float '${this.defaultValue}'`, e);
191                 }
192                 return undefined;
193             case PROPERTY_TYPES.BOOLEAN:
194                 return this.defaultValue === 'true';
195             default:
196                 return this.defaultValue;
197         }
198     }
199
200     /**
201      * Checks whether the property value is a TOSCA function (e.g. get_input, get_property, get_attribute, concat, etc.)
202      */
203     public isToscaFunction(): boolean {
204         return this.toscaFunction != null;
205     }
206
207     /**
208      * Gets the schema type, if there is a schema. Otherwise, returns undefined.
209      *
210      * @return the schema type.
211      */
212     public getSchemaType(): string {
213         if (this.schema && this.schema.property) {
214             return this.schema.property.type;
215         }
216         return undefined;
217     }
218 }
219