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;
72 toscaPresentation: ToscaPresentationData;
75 * @deprecated Use toscaFunction instead
77 toscaGetFunction: ToscaGetFunctionDto;
78 toscaFunction: ToscaFunction;
79 subPropertyToscaFunctions: SubPropertyToscaFunction[];
81 constructor(property?: PropertyBEModel) {
83 this.constraints = property.constraints;
84 this.defaultValue = property.defaultValue;
85 this.description = property.description;
86 this.fromDerived = property.fromDerived;
87 this.name = property.name;
88 this.origName = property.origName;
89 this.parentUniqueId = property.parentUniqueId;
90 this.password = property.password;
91 this.required = property.required;
92 this.schema = property.schema;
93 this.schemaType = property.schemaType;
94 this.type = property.type;
95 this.uniqueId = property.uniqueId;
96 this.value = property.value;
97 this.definition = property.definition;
98 this.getInputValues = property.getInputValues;
99 this.parentPropertyType = property.parentPropertyType;
100 this.subPropertyInputPath = property.subPropertyInputPath;
101 this.toscaPresentation = property.toscaPresentation;
102 this.getPolicyValues = property.getPolicyValues;
103 this.inputPath = property.inputPath;
104 this.metadata = property.metadata;
105 if (property.toscaFunction) {
106 this.toscaFunction = property.toscaFunction;
107 } else if (property.toscaGetFunction) {
108 //support for legacy tosca function
109 const toscaGetFunction1 = new ToscaGetFunction();
110 toscaGetFunction1.type = ToscaGetFunctionTypeConverter.convertToToscaFunctionType(property.toscaGetFunction.functionType);
111 toscaGetFunction1.propertyUniqueId = property.toscaGetFunction.propertyUniqueId;
112 toscaGetFunction1.propertyName = property.toscaGetFunction.propertyName;
113 toscaGetFunction1.propertySource = property.toscaGetFunction.propertySource;
114 toscaGetFunction1.sourceUniqueId = property.toscaGetFunction.sourceUniqueId;
115 toscaGetFunction1.sourceName = property.toscaGetFunction.sourceName;
116 toscaGetFunction1.functionType = property.toscaGetFunction.functionType;
117 toscaGetFunction1.propertyPathFromSource = property.toscaGetFunction.propertyPathFromSource;
118 this.toscaFunction = toscaGetFunction1;
120 this.subPropertyToscaFunctions = property.subPropertyToscaFunctions;
123 if (!this.schema || !this.schema.property) {
124 this.schema = new SchemaPropertyGroupModel(new SchemaProperty());
125 } else { // forcing creating new object, so editing different one than the object in the table
126 this.schema = new SchemaPropertyGroupModel(new SchemaProperty(this.schema.property));
130 public toJSON = (): any => {
131 const temp = angular.copy(this);
132 temp.value = temp.value === '{}' || temp.value === '[]' ? undefined : temp.value;
133 temp.defaultValue = temp.defaultValue === '{}' || temp.defaultValue === '[]' ? undefined : temp.defaultValue;
137 public getDerivedPropertyType = (): DerivedPropertyType => {
138 if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) > -1) {
139 return DerivedPropertyType.SIMPLE;
141 if (this.type === PROPERTY_TYPES.LIST) {
142 return DerivedPropertyType.LIST;
144 if (this.type === PROPERTY_TYPES.RANGE) {
145 return DerivedPropertyType.RANGE;
147 if (this.type === PROPERTY_TYPES.MAP) {
148 return DerivedPropertyType.MAP;
150 return DerivedPropertyType.COMPLEX;
154 * Parses default value to JSON.
156 public parseDefaultValueToJson(): any {
157 if (this.defaultValue == undefined) {
161 const propertyType: DerivedPropertyType = this.getDerivedPropertyType();
162 if (propertyType == DerivedPropertyType.SIMPLE) {
163 return this.parseDefaultSimpleValue();
167 return JSON.parse(this.defaultValue);
169 console.error(`Could not parse the property of type '${this.type}' default value to JSON '${this.defaultValue}'`, e);
175 private parseDefaultSimpleValue() {
177 case PROPERTY_TYPES.INTEGER:
179 return parseInt(this.defaultValue);
181 console.error(`Could not parse the property of type '${this.type}' default value to int '${this.defaultValue}'`, e);
184 case PROPERTY_TYPES.FLOAT:
186 return parseFloat(this.defaultValue);
188 console.error(`Could not parse the property of type '${this.type}' default value to float '${this.defaultValue}'`, e);
191 case PROPERTY_TYPES.BOOLEAN:
192 return this.defaultValue === 'true';
194 return this.defaultValue;
199 * Checks whether the property value is a TOSCA function (e.g. get_input, get_property, get_attribute, concat, etc.)
201 public isToscaFunction(): boolean {
202 return this.toscaFunction != null;
206 * Gets the schema type, if there is a schema. Otherwise, returns undefined.
208 * @return the schema type.
210 public getSchemaType(): string {
211 if (this.schema && this.schema.property) {
212 return this.schema.property.type;