2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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";
32 export enum DerivedPropertyType {
39 export class PropertyPolicyDetail {
42 constructor(propertyPolicy?: PropertyPolicyDetail) {
44 this.policyId = propertyPolicy.policyId;
45 this.propertyName = propertyPolicy.propertyName;
50 export class PropertyBEModel {
57 getInputValues: PropertyInputDetail[];
58 getPolicyValues: PropertyPolicyDetail[];
61 parentUniqueId: string;
64 schema: SchemaPropertyGroupModel;
69 parentPropertyType: string;
70 subPropertyInputPath: string;
73 toscaPresentation: ToscaPresentationData;
75 propertyConstraints: any;
77 * @deprecated Use toscaFunction instead
79 toscaGetFunction: ToscaGetFunctionDto;
80 toscaFunction: ToscaFunction;
81 subPropertyToscaFunctions: SubPropertyToscaFunction[];
82 parentToscaPath: string[] = [];
84 constructor(property?: PropertyBEModel) {
86 this.constraints = property.constraints;
87 this.propertyConstraints = property.propertyConstraints;
88 this.defaultValue = property.defaultValue;
89 this.description = property.description;
90 this.fromDerived = property.fromDerived;
91 this.name = property.name;
92 this.origName = property.origName;
93 this.parentUniqueId = property.parentUniqueId;
94 this.password = property.password;
95 this.required = property.required;
96 this.schema = property.schema;
97 this.schemaType = property.schemaType;
98 this.type = property.type;
99 this.uniqueId = property.uniqueId;
100 this.value = property.value;
101 this.definition = property.definition;
102 this.getInputValues = property.getInputValues;
103 this.parentPropertyType = property.parentPropertyType;
104 this.subPropertyInputPath = property.subPropertyInputPath;
105 this.toscaPresentation = property.toscaPresentation;
106 this.getPolicyValues = property.getPolicyValues;
107 this.inputPath = property.inputPath;
108 this.inputName = property.inputName;
109 this.metadata = property.metadata;
110 this.parentToscaPath = property.parentToscaPath;
111 if (property.toscaFunction) {
112 this.toscaFunction = property.toscaFunction;
113 } else if (property.toscaGetFunction) {
114 //support for legacy tosca function
115 const toscaGetFunction1 = new ToscaGetFunction();
116 toscaGetFunction1.type = ToscaGetFunctionTypeConverter.convertToToscaFunctionType(property.toscaGetFunction.functionType);
117 toscaGetFunction1.propertyUniqueId = property.toscaGetFunction.propertyUniqueId;
118 toscaGetFunction1.propertyName = property.toscaGetFunction.propertyName;
119 toscaGetFunction1.propertySource = property.toscaGetFunction.propertySource;
120 toscaGetFunction1.sourceUniqueId = property.toscaGetFunction.sourceUniqueId;
121 toscaGetFunction1.sourceName = property.toscaGetFunction.sourceName;
122 toscaGetFunction1.functionType = property.toscaGetFunction.functionType;
123 toscaGetFunction1.propertyPathFromSource = property.toscaGetFunction.propertyPathFromSource;
124 this.toscaFunction = toscaGetFunction1;
126 this.subPropertyToscaFunctions = property.subPropertyToscaFunctions;
129 if (!this.schema || !this.schema.property) {
130 this.schema = new SchemaPropertyGroupModel(new SchemaProperty());
131 } else { // forcing creating new object, so editing different one than the object in the table
132 this.schema = new SchemaPropertyGroupModel(new SchemaProperty(this.schema.property));
136 public toJSON = (): any => {
137 const temp = angular.copy(this);
138 temp.value = temp.value === '{}' || temp.value === '[]' ? undefined : temp.value;
139 temp.defaultValue = temp.defaultValue === '{}' || temp.defaultValue === '[]' ? undefined : temp.defaultValue;
143 public getDerivedPropertyType = (): DerivedPropertyType => {
144 if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) > -1) {
145 return DerivedPropertyType.SIMPLE;
147 if (this.type === PROPERTY_TYPES.LIST) {
148 return DerivedPropertyType.LIST;
150 if (this.type === PROPERTY_TYPES.RANGE) {
151 return DerivedPropertyType.RANGE;
153 if (this.type === PROPERTY_TYPES.MAP) {
154 return DerivedPropertyType.MAP;
156 return DerivedPropertyType.COMPLEX;
160 * Parses default value to JSON.
162 public parseDefaultValueToJson(): any {
163 if (this.defaultValue == undefined) {
167 const propertyType: DerivedPropertyType = this.getDerivedPropertyType();
168 if (propertyType == DerivedPropertyType.SIMPLE) {
169 return this.parseDefaultSimpleValue();
173 return JSON.parse(this.defaultValue);
175 console.error(`Could not parse the property of type '${this.type}' default value to JSON '${this.defaultValue}'`, e);
181 private parseDefaultSimpleValue() {
183 case PROPERTY_TYPES.INTEGER:
185 return parseInt(this.defaultValue);
187 console.error(`Could not parse the property of type '${this.type}' default value to int '${this.defaultValue}'`, e);
190 case PROPERTY_TYPES.FLOAT:
192 return parseFloat(this.defaultValue);
194 console.error(`Could not parse the property of type '${this.type}' default value to float '${this.defaultValue}'`, e);
197 case PROPERTY_TYPES.BOOLEAN:
198 return this.defaultValue === 'true';
200 return this.defaultValue;
205 * Checks whether the property value is a TOSCA function (e.g. get_input, get_property, get_attribute, concat, etc.)
207 public isToscaFunction(): boolean {
208 return this.toscaFunction != null;
212 * Gets the schema type, if there is a schema. Otherwise, returns undefined.
214 * @return the schema type.
216 public getSchemaType(): string {
217 if (this.schema && this.schema.property) {
218 return this.schema.property.type;