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 * as _ from "lodash";
22 import { SchemaPropertyGroupModel, SchemaProperty } from '../schema-property';
23 import { DerivedPropertyType, PropertyBEModel, PropertyFEModel } from '../../models';
24 import {SubPropertyToscaFunction} from "../sub-property-tosca-function";
25 import {ToscaFunction} from "../tosca-function";
26 import { PROPERTY_TYPES } from 'app/utils';
27 import { UUID } from "angular2-uuid";
30 export class DerivedFEProperty extends PropertyBEModel {
32 valueObjIsValid: boolean;
34 valueObjIsChanged: boolean;
37 propertiesName: string; //"network_assignments#ipv4_subnet#use_ipv4 = parentPath + name
38 derivedDataType: DerivedPropertyType;
39 toscaFunction: ToscaFunction;
44 isChildOfListOrMap: boolean;
45 canBeDeclared: boolean;
53 constructor(property: PropertyBEModel, parentName?: string, createChildOfListOrMap?: boolean, key?:string, value?:any) {
54 if (!createChildOfListOrMap) { //creating a standard derived prop
57 this.parentName = parentName ? parentName : null;
58 this.propertiesName = (parentName) ? parentName + '#' + property.name : property.name;
59 this.canBeDeclared = true; //defaults to true
60 if (property instanceof DerivedFEProperty) {
61 this.toscaPath = property.toscaPath != null ? property.toscaPath : [];
63 this.toscaPath = property.parentToscaPath != null ? property.parentToscaPath : property.parentToscaPath;
65 if (this.toscaPath.length == 0 && parentName != null && parentName.indexOf('#') != -1) {
66 let lastparent = parentName.split('#');
67 this.toscaPath.push(lastparent[lastparent.length - 1]);
69 this.toscaPath.push(property.name);
70 } else { //creating a direct child of list or map (ie. Item that can be deleted, with UUID instead of name)
72 let toscaPathCopy = null;
73 if (property instanceof DerivedFEProperty) {
74 toscaPathCopy = property.toscaPath != null ? property.toscaPath .toString() : null;
76 toscaPathCopy = property.parentToscaPath != null ? property.parentToscaPath.toString() : null;
78 this.toscaPath = toscaPathCopy != null ? toscaPathCopy.split(",") : [];
79 this.isChildOfListOrMap = true;
80 this.canBeDeclared = false;
81 this.name = UUID.UUID();
82 this.parentName = parentName;
83 this.propertiesName = parentName + '#' + this.name;
85 if (property.type == PROPERTY_TYPES.LIST) {
86 let parentKey : string = null;
87 if (property instanceof DerivedFEProperty) {
88 if (property.valueObj != '') {
90 let listIndex = Number(key);
91 if (!isNaN(listIndex)) {
92 this.toscaPath.push(key);
94 let newIndex = Object.keys(property.valueObj).findIndex(valueKey => (valueKey == key));
95 this.toscaPath.push(newIndex.toString());
98 let toscaIndex = Object.keys(property.valueObj).sort().reverse()[0];
99 this.toscaPath.push((Number(toscaIndex) + 1).toString());
102 this.toscaPath.push("0");
105 if (property instanceof PropertyFEModel && property.valueObj != '') {
109 let toscaIndex = Object.keys(property.valueObj).sort().reverse()[0];
110 parentKey = (Number(toscaIndex) + 1).toString();
115 this.toscaPath.push(parentKey);
117 if (property.schemaType != PROPERTY_TYPES.MAP) {
118 this.mapKey = parentKey;
120 this.mapKeyError = null;
121 this.type = property.schema.property.type;
122 if (this.type == PROPERTY_TYPES.MAP){
123 this.mapInlist = true;
124 this.parentMapKey = parentKey;
126 this.schema = new SchemaPropertyGroupModel(new SchemaProperty(property.schema.property));
130 this.mapKeyError = null;
133 this.mapKeyError = 'Key cannot be empty.';
135 this.type = property.type;
136 if (property.schema.property.type == PROPERTY_TYPES.MAP){
137 const schProp = new SchemaProperty();
138 schProp.isSimpleType = true;
139 schProp.isDataType = false;
140 schProp.simpleType = PROPERTY_TYPES.STRING;
141 this.schema = new SchemaPropertyGroupModel(schProp);
142 this.schemaType = PROPERTY_TYPES.STRING;
143 if (property instanceof DerivedFEProperty) {
144 this.parentMapKey = property.parentMapKey;
145 if (value != null && typeof value == 'object') {
146 this.toscaFunction = property.toscaFunction;
150 this.schema = new SchemaPropertyGroupModel(new SchemaProperty(property.schema.property));
152 if (this.toscaPath != null) {
153 let lastIndex = this.toscaPath[this.toscaPath.length - 1];
154 if(this.mapKey != lastIndex){
155 this.toscaPath.push(this.mapKey);
158 this.toscaPath.push(this.mapKey);
161 this.valueObj = (this.type == PROPERTY_TYPES.JSON && typeof value == 'object') ? JSON.stringify(value) : value;
163 this.value = typeof value == 'object' ? JSON.stringify(value) : value;
165 this.updateValueObjOrig();
167 this.parentToscaPath = this.toscaPath;
168 if(property.subPropertyToscaFunctions != null){
169 property.subPropertyToscaFunctions.forEach((item : SubPropertyToscaFunction) => {
170 if(item.subPropertyPath.toString() === this.toscaPath.toString() && this.uniqueId == null){
171 this.toscaFunction = item.toscaFunction;
175 this.valueObjIsValid = true;
176 this.derivedDataType = this.getDerivedPropertyType();
177 this.inputName = property.inputName;
180 public getActualMapKey() {
181 return (this.mapKeyError) ? this.name : this.mapKey;
184 public updateValueObj(valueObj:any, isValid:boolean) {
185 this.valueObj = PropertyFEModel.cleanValueObj(valueObj);
186 this.valueObjIsValid = isValid;
187 this.valueObjIsChanged = this.hasValueObjChanged();
190 public updateValueObjOrig() {
191 this.valueObjOrig = _.cloneDeep(this.valueObj);
192 this.valueObjIsChanged = false;
195 public hasValueObjChanged() {
196 return !_.isEqual(this.valueObj, this.valueObjOrig);
200 export class DerivedFEPropertyMap {
201 [parentPath: string]: Array<DerivedFEProperty>;