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