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 {
38 export class PropertyPolicyDetail {
41 constructor(propertyPolicy?: PropertyPolicyDetail) {
43 this.policyId = propertyPolicy.policyId;
44 this.propertyName = propertyPolicy.propertyName;
49 export class PropertyBEModel {
56 getInputValues: PropertyInputDetail[];
57 getPolicyValues: PropertyPolicyDetail[];
60 parentUniqueId: string;
63 schema: SchemaPropertyGroupModel;
68 parentPropertyType: string;
69 subPropertyInputPath: string;
71 toscaPresentation: ToscaPresentationData;
74 * @deprecated Use toscaFunction instead
76 toscaGetFunction: ToscaGetFunctionDto;
77 toscaFunction: ToscaFunction;
78 subPropertyToscaFunctions: SubPropertyToscaFunction[];
80 constructor(property?: PropertyBEModel) {
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;
119 this.subPropertyToscaFunctions = property.subPropertyToscaFunctions;
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));
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;
136 public getDerivedPropertyType = (): DerivedPropertyType => {
137 if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.type) > -1) {
138 return DerivedPropertyType.SIMPLE;
140 if (this.type === PROPERTY_TYPES.LIST) {
141 return DerivedPropertyType.LIST;
143 if (this.type === PROPERTY_TYPES.MAP) {
144 return DerivedPropertyType.MAP;
146 return DerivedPropertyType.COMPLEX;
150 * Parses default value to JSON.
152 public parseDefaultValueToJson(): any {
153 if (this.defaultValue == undefined) {
157 const propertyType: DerivedPropertyType = this.getDerivedPropertyType();
158 if (propertyType == DerivedPropertyType.SIMPLE) {
159 return this.parseDefaultSimpleValue();
163 return JSON.parse(this.defaultValue);
165 console.error(`Could not parse the property of type '${this.type}' default value to JSON '${this.defaultValue}'`, e);
171 private parseDefaultSimpleValue() {
173 case PROPERTY_TYPES.INTEGER:
175 return parseInt(this.defaultValue);
177 console.error(`Could not parse the property of type '${this.type}' default value to int '${this.defaultValue}'`, e);
180 case PROPERTY_TYPES.FLOAT:
182 return parseFloat(this.defaultValue);
184 console.error(`Could not parse the property of type '${this.type}' default value to float '${this.defaultValue}'`, e);
187 case PROPERTY_TYPES.BOOLEAN:
188 return this.defaultValue === 'true';
190 return this.defaultValue;
195 * Checks whether the property value is a TOSCA function (e.g. get_input, get_property, get_attribute, concat, etc.)
197 public isToscaFunction(): boolean {
198 return this.toscaFunction != null;
202 * Gets the schema type, if there is a schema. Otherwise, returns undefined.
204 * @return the schema type.
206 public getSchemaType(): string {
207 if (this.schema && this.schema.property) {
208 return this.schema.property.type;