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;
50 constructor(property: PropertyBEModel, parentName?: string, createChildOfListOrMap?: boolean, key?:string, value?:any) {
51 if (!createChildOfListOrMap) { //creating a standard derived prop
53 this.parentName = parentName ? parentName : null;
54 this.propertiesName = (parentName) ? parentName + '#' + property.name : property.name;
55 this.canBeDeclared = true; //defaults to true
56 } else { //creating a direct child of list or map (ie. Item that can be deleted, with UUID instead of name)
58 if(property.subPropertyToscaFunctions != null){
59 property.subPropertyToscaFunctions.forEach((item : SubPropertyToscaFunction) => {
60 if(item.subPropertyPath[0] === key){
61 this.toscaFunction = item.toscaFunction;
65 this.isChildOfListOrMap = true;
66 this.canBeDeclared = false;
67 this.name = UUID.UUID();
68 this.parentName = parentName;
69 this.propertiesName = parentName + '#' + this.name;
71 if (property.type == PROPERTY_TYPES.LIST) {
72 if(property.value != null) {
73 const valueJson = JSON.parse(property.value);
77 let indexNumber = Number(Object.keys(valueJson).sort().reverse()[0]) + 1;
78 this.mapKey = indexNumber.toString();
83 this.mapKeyError = null;
84 this.type = property.schema.property.type;
85 if (this.type == PROPERTY_TYPES.MAP){
86 this.mapInlist = true;
89 this.schema = new SchemaPropertyGroupModel(new SchemaProperty(property.schema.property));
93 this.mapKeyError = null;
96 this.mapKeyError = 'Key cannot be empty.';
98 this.type = property.type;
99 if (property.schema.property.type == PROPERTY_TYPES.MAP){
100 const schProp = new SchemaProperty();
101 schProp.isSimpleType = true;
102 schProp.isDataType = false;
103 schProp.simpleType = PROPERTY_TYPES.STRING;
104 this.schema = new SchemaPropertyGroupModel(schProp);
105 this.schemaType = PROPERTY_TYPES.STRING;
107 this.schema = new SchemaPropertyGroupModel(new SchemaProperty(property.schema.property));
111 this.valueObj = (this.type == PROPERTY_TYPES.JSON && typeof value == 'object') ? JSON.stringify(value) : value;
113 this.value = typeof value == 'object' ? JSON.stringify(value) : value;
115 this.updateValueObjOrig();
117 // this.constraints = property ? property.constraints : null;
118 this.valueObjIsValid = true;
119 this.derivedDataType = this.getDerivedPropertyType();
122 public getActualMapKey() {
123 return (this.mapKeyError) ? this.name : this.mapKey;
126 public updateValueObj(valueObj:any, isValid:boolean) {
127 this.valueObj = PropertyFEModel.cleanValueObj(valueObj);
128 this.valueObjIsValid = isValid;
129 this.valueObjIsChanged = this.hasValueObjChanged();
132 public updateValueObjOrig() {
133 this.valueObjOrig = _.cloneDeep(this.valueObj);
134 this.valueObjIsChanged = false;
137 public hasValueObjChanged() {
138 return !_.isEqual(this.valueObj, this.valueObjOrig);
142 export class DerivedFEPropertyMap {
143 [parentPath: string]: Array<DerivedFEProperty>;