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