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((property.valueObj.length).toString());
105 if (property instanceof PropertyFEModel && property.valueObj != '') {
109 let toscaIndex = Object.keys(property.valueObj).sort().reverse()[0];
110 parentKey = (Number(toscaIndex) + 1).toString();
114 if (property instanceof PropertyFEModel) {
115 parentKey = (property.flattenedChildren.length).toString();
118 this.toscaPath.push(parentKey);
120 if (property.schemaType != PROPERTY_TYPES.MAP) {
121 this.mapKey = parentKey;
123 this.mapKeyError = null;
124 this.type = property.schema.property.type;
125 if (this.type == PROPERTY_TYPES.MAP){
126 this.mapInlist = true;
127 this.parentMapKey = parentKey;
129 this.schema = new SchemaPropertyGroupModel(new SchemaProperty(property.schema.property));
133 this.mapKeyError = null;
136 this.mapKeyError = 'Key cannot be empty.';
138 this.type = property.type;
139 if (property.schema.property.type == PROPERTY_TYPES.MAP){
140 const schProp = new SchemaProperty();
141 schProp.isSimpleType = true;
142 schProp.isDataType = false;
143 schProp.simpleType = PROPERTY_TYPES.STRING;
144 this.schema = new SchemaPropertyGroupModel(schProp);
145 this.schemaType = PROPERTY_TYPES.STRING;
146 if (property instanceof DerivedFEProperty) {
147 this.parentMapKey = property.parentMapKey;
148 if (value != null && typeof value == 'object') {
149 this.toscaFunction = property.toscaFunction;
153 this.schema = new SchemaPropertyGroupModel(new SchemaProperty(property.schema.property));
155 if (this.toscaPath != null) {
156 let lastIndex = this.toscaPath[this.toscaPath.length - 1];
157 if(this.mapKey != lastIndex){
158 this.toscaPath.push(this.mapKey);
161 this.toscaPath.push(this.mapKey);
164 this.valueObj = (this.type == PROPERTY_TYPES.JSON && typeof value == 'object') ? JSON.stringify(value) : value;
166 this.value = typeof value == 'object' ? JSON.stringify(value) : value;
168 this.updateValueObjOrig();
170 this.parentToscaPath = this.toscaPath;
171 if(property.subPropertyToscaFunctions != null){
172 property.subPropertyToscaFunctions.forEach((item : SubPropertyToscaFunction) => {
173 if(item.subPropertyPath.toString() === this.toscaPath.toString() && this.uniqueId == null){
174 this.toscaFunction = item.toscaFunction;
178 this.valueObjIsValid = true;
179 this.derivedDataType = this.getDerivedPropertyType();
180 this.inputName = property.inputName;
183 public getActualMapKey() {
184 return (this.mapKeyError) ? this.name : this.mapKey;
187 public updateValueObj(valueObj:any, isValid:boolean) {
188 this.valueObj = PropertyFEModel.cleanValueObj(valueObj);
189 this.valueObjIsValid = isValid;
190 this.valueObjIsChanged = this.hasValueObjChanged();
193 public updateValueObjOrig() {
194 this.valueObjOrig = _.cloneDeep(this.valueObj);
195 this.valueObjIsChanged = false;
198 public hasValueObjChanged() {
199 return !_.isEqual(this.valueObj, this.valueObjOrig);
203 export class DerivedFEPropertyMap {
204 [parentPath: string]: Array<DerivedFEProperty>;