Provide validation to the json type property in the operation inputs
[sdc.git] / catalog-ui / src / app / ng2 / pages / composition / interface-operatons / operation-creator / input-list / input-list-item / input-list-item.component.ts
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 Nordix Foundation.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
23 import {DataTypeModel} from '../../../../../../../models/data-types';
24 import {SchemaProperty, SchemaPropertyGroupModel} from '../../../../../../../models/schema-property';
25 import {PropertyBEModel} from '../../../../../../../models/properties-inputs/property-be-model';
26 import {PROPERTY_DATA, PROPERTY_TYPES} from '../../../../../../../utils/constants';
27 import {ToscaFunction} from '../../../../../../../models/tosca-function';
28 import {ToscaFunctionType} from "../../../../../../../models/tosca-function-type.enum";
29 import {ToscaFunctionValidationEvent} from "../../../../../properties-assignment/tosca-function/tosca-function.component";
30 import {InstanceFeDetails} from "../../../../../../../models/instance-fe-details";
31 import {ToscaTypeHelper} from "app/utils/tosca-type-helper";
32 import {CustomToscaFunction} from "../../../../../../../models/default-custom-functions";
33 import {SubPropertyToscaFunction} from "../../../../../../../models/sub-property-tosca-function";
34
35 @Component({
36   selector: 'app-input-list-item',
37   templateUrl: './input-list-item.component.html',
38   styleUrls: ['./input-list-item.component.less']
39 })
40 export class InputListItemComponent implements OnInit {
41
42   @Input() valueObjRef: any;
43   @Input() name: string;
44   @Input() dataTypeMap: Map<string, DataTypeModel>;
45   @Input() type: DataTypeModel;
46   @Input() schema: SchemaPropertyGroupModel;
47   @Input() nestingLevel: number;
48   @Input() isExpanded: boolean = false;
49   @Input() isListChild: boolean = false;
50   @Input() isMapChild: boolean = false;
51   @Input() showToscaFunctionOption: boolean = false;
52   @Input() listIndex: number;
53   @Input() subPropertyToscaFunctions: SubPropertyToscaFunction[];
54   @Input() isViewOnly: boolean;
55   @Input() allowDeletion: boolean = false;
56   @Input() toscaFunction: ToscaFunction;
57   @Input() componentInstanceMap: Map<string, InstanceFeDetails> = new Map();
58   @Input() customToscaFunctions: Array<CustomToscaFunction> = [];
59   @Output('onValueChange') onValueChangeEvent: EventEmitter<any> = new EventEmitter<any>();
60   @Output('onDelete') onDeleteEvent: EventEmitter<string> = new EventEmitter<string>();
61   @Output('onChildListItemDelete') onChildListItemDeleteEvent: EventEmitter<number> = new EventEmitter<number>();
62
63   mapEntryName: string;
64   isToscaFunction: boolean = false;
65   property: PropertyBEModel;
66
67   ngOnInit(): void {
68     if (!this.nestingLevel) {
69       this.nestingLevel = 0;
70     }
71     if (this.type.properties) {
72       this.type.properties.forEach(property => {
73         this.initEmptyPropertyInValueObjRef(property);
74       });
75     }
76
77     this.property = new PropertyBEModel();
78     this.property.type = this.type.name;
79     if (this.schema) {
80       this.property.schema = this.schema;
81       this.property.schemaType = this.schema.property.type;
82     }
83     if (this.toscaFunction) {
84       this.property.toscaFunction = this.toscaFunction;
85       this.valueObjRef = this.toscaFunction.value;
86       this.isToscaFunction = true;
87     }
88     if (this.property.type == PROPERTY_TYPES.JSON) {
89       this.valueObjRef = JSON.stringify(this.valueObjRef);
90     }
91   }
92
93   ngOnChanges(): void {
94     if (this.isToscaFunction) {
95       this.property.toscaFunction = this.toscaFunction;
96       this.valueObjRef = this.toscaFunction.value;
97     } else {
98       this.property.toscaFunction = undefined;
99     }
100   }
101
102   private initEmptyPropertyInValueObjRef(property: PropertyBEModel) {
103     if (this.valueObjRef[property.name] == undefined) {
104       if (this.isTypeComplex(property.type) || this.isTypeMap(property.type)) {
105         this.valueObjRef[property.name] = {};
106       } else if (this.isTypeList(property.type) || this.isTypeRange(property.type)) {
107         this.valueObjRef[property.name] = [];
108       } else {
109         this.valueObjRef[property.name] = null;
110       }
111     }
112   }
113
114   getToscaFunction(key: any): any {
115     if (this.subPropertyToscaFunctions) {
116       for (let subPropertyToscaFunction of this.subPropertyToscaFunctions) {
117         let found = subPropertyToscaFunction.subPropertyPath ? subPropertyToscaFunction.subPropertyPath.find(value => value === key) : false;
118         if (found) {
119           return subPropertyToscaFunction.toscaFunction;
120         }
121       }
122     }
123     return undefined;
124   }
125
126   isTypeSimple(typeName: string): boolean {
127     return ToscaTypeHelper.isTypeSimple(typeName) || this.isTypeDerivedFromSimple(typeName) && (this.isTypeWithoutProperties(typeName));
128   }
129
130   isTypeRange(typeName: string): boolean {
131     return ToscaTypeHelper.isTypeRange(typeName);
132   }
133
134   isTypeWithoutProperties(typeName: string): boolean {
135     if (this.dataTypeMap.get(typeName) === undefined) {
136       return true;
137     }
138     return this.dataTypeMap.get(typeName).properties === undefined ||
139         this.dataTypeMap.get(typeName).properties.length == 0;
140   }
141
142   isTypeDerivedFromSimple(typeName: string): boolean {
143     if (typeName === undefined) {
144       return false;
145     }
146     if (this.dataTypeMap.get(typeName) !== undefined) {
147       if (this.isTypeRange(typeName)) {
148         return false;
149       }
150       if (this.dataTypeMap.get(typeName).derivedFromName == PROPERTY_DATA.ROOT_DATA_TYPE) {
151         return false;
152       } else if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.dataTypeMap.get(typeName).derivedFromName) > -1) {
153         return true;
154       } else {
155         return this.isTypeDerivedFromSimple(this.dataTypeMap.get(typeName).derivedFromName);
156       }
157     }
158     return true;
159   }
160
161   isTypeList(typeName: string): boolean {
162     return ToscaTypeHelper.isTypeList(typeName);
163   }
164
165   isTypeMap(typeName: string): boolean {
166     return ToscaTypeHelper.isTypeMap(typeName);
167   }
168
169   isTypeComplex(typeName: string): boolean {
170     return ToscaTypeHelper.isTypeComplex(typeName);
171   }
172
173   isTypeNumber(type: string): boolean {
174     return ToscaTypeHelper.isTypeNumber(type);
175   }
176
177   isTypeBoolean(type: string): boolean {
178     return ToscaTypeHelper.isTypeBoolean(type);
179   }
180
181   isTypeLiteral(type: string): boolean {
182     return ToscaTypeHelper.isTypeLiteral(type);
183   }
184
185   expandAndCollapse() {
186     this.isExpanded = !this.isExpanded;
187   }
188
189   getDataType(type: string) {
190     return this.dataTypeMap.get(type);
191   }
192
193   onValueTypeChange () {
194     if ( !this.isToscaFunction ) {
195       this.onValueChange(this.valueObjRef);
196     }
197   }
198
199   onToscaFunctionValidityChange(validationEvent: ToscaFunctionValidationEvent):void {
200     if (validationEvent.isValid) {
201       this.emitValueChangeEvent(validationEvent.toscaFunction, true);
202       return;
203     }
204     this.emitValueChangeEvent(undefined, true);
205   }
206
207   onValueChange(value: any): void {
208     if (this.isTypeNumber(this.type.name)) {
209       this.emitValueChangeEvent(this.parseNumber(value));
210       return;
211     }
212     if (this.type.name == PROPERTY_TYPES.BOOLEAN) {
213       this.emitValueChangeEvent(this.parseBoolean(value));
214       return;
215     }
216     this.emitValueChangeEvent(value);
217   }
218
219   onListValueChange(): void {
220     this.emitValueChangeEvent(this.valueObjRef);
221   }
222
223   onPropertyValueChange($event: any) {
224     this.valueObjRef[$event.name] = $event.value;
225     this.emitValueChangeEvent(this.valueObjRef);
226   }
227
228   private emitValueChangeEvent(value: any, isToscaFunction=false) {
229     let emitValue = {
230       name: this.name,
231       value: value,
232       isToscaFunction:isToscaFunction
233     };
234     this.onValueChangeEvent.emit(emitValue);
235   }
236
237   isRoot(): boolean {
238     return this.nestingLevel === 0;
239   }
240
241   showListItemDelete(): boolean {
242     return !this.isViewOnly && (this.isListChild && this.nestingLevel > 0);
243   }
244
245   showInputDelete(): boolean {
246     return this.allowDeletion && !this.isViewOnly && (this.isRoot() || this.isMapChild);
247   }
248
249   resolveType(): string {
250     if (this.isTypeList(this.type.name)) {
251       return `list of value type ${this.schema.property.type}`
252     }
253     if (this.isTypeMap(this.type.name)) {
254       return `map of 'string' keys and '${this.schema.property.type}' values`
255     }
256     return this.type.name;
257   }
258
259   onInputDelete() {
260     this.onDeleteEvent.emit(this.name);
261   }
262
263   onListItemDelete(index: number): void {
264     this.valueObjRef.splice(index, 1);
265     this.emitValueChangeEvent(this.valueObjRef);
266   }
267
268   addListElement() {
269     if (!this.valueObjRef) {
270       this.valueObjRef = [];
271     }
272     if (this.isTypeSimple(this.schema.property.type)) {
273       this.valueObjRef.push('');
274     } else if (this.isTypeComplex(this.schema.property.type) || this.isTypeMap(this.schema.property.type)) {
275       this.valueObjRef.push({});
276     } else if (this.isTypeList(this.schema.property.type)) {
277       this.valueObjRef.push([]);
278     }
279   }
280
281   trackByIndex(index: number, value: string): number {
282     return index;
283   }
284
285   onChildListItemDelete() {
286     this.onChildListItemDeleteEvent.emit(this.listIndex);
287   }
288
289   getObjectEntries(valueObjRef: object) {
290     return Object.keys(valueObjRef);
291   }
292
293   onMapValueChange() {
294     this.emitValueChangeEvent(this.valueObjRef);
295   }
296
297   onMapKeyDelete(key: string) {
298     delete this.valueObjRef[key]
299     this.emitValueChangeEvent(this.valueObjRef);
300   }
301
302   addMapEntry() {
303     let newKey;
304     if (this.mapEntryName) {
305       newKey = this.mapEntryName.trim();
306     }
307     if (!newKey) {
308       return;
309     }
310     if (Object.keys(this.valueObjRef).indexOf(newKey) !== -1) {
311       return;
312     }
313     this.mapEntryName = '';
314     if (this.isTypeSimple(this.schema.property.type)) {
315       this.valueObjRef[newKey] = '';
316     } else if (this.isTypeComplex(this.schema.property.type) || this.isTypeMap(this.schema.property.type)) {
317       this.valueObjRef[newKey] = {};
318     } else if (this.isTypeList(this.schema.property.type)) {
319       this.valueObjRef[newKey] = [];
320     }
321     this.emitValueChangeEvent(this.valueObjRef);
322   }
323
324   getSimpleValueInputType() {
325     if (this.isTypeNumber(this.type.name)){
326       return 'number';
327     }
328     return 'text';
329   }
330
331   buildSchemaGroupProperty(): SchemaPropertyGroupModel {
332     const schemaProperty = new SchemaProperty();
333     if (this.schema.property.type === PROPERTY_TYPES.MAP || this.schema.property.type === PROPERTY_TYPES.LIST) {
334       schemaProperty.type = PROPERTY_TYPES.STRING;
335     } else {
336       schemaProperty.type = this.schema.property.type
337     }
338     return new SchemaPropertyGroupModel(schemaProperty);
339   }
340
341   private parseBoolean(value: any) {
342     if (value === 'true') {
343       return true;
344     }
345     if (value === 'false') {
346       return false;
347     }
348     return null;
349   }
350
351   private parseNumber(value: any) {
352     const number = parseInt(value);
353     return isNaN(number) ? null : number;
354   }
355
356 }