c20d9fc4089ac7c36955a1911475a96acee831c4
[sdc.git] / catalog-ui / src / app / view-models / forms / property-forms / component-property-form / property-form-view-model.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 'use strict';
22 import * as _ from "lodash";
23 import {FormState, PROPERTY_DATA, PROPERTY_TYPES, PROPERTY_VALUE_CONSTRAINTS, ValidationUtils} from "app/utils";
24 import {DataTypesService} from "app/services";
25 import {DataTypesMap, PropertyModel, InputBEModel, Component, InputFEModel} from "app/models";
26 import {ComponentInstance} from "../../../../models/componentsInstances/componentInstance";
27 import {ComponentInstanceServiceNg2} from "app/ng2/services/component-instance-services/component-instance.service";
28 import {ComponentServiceNg2} from "app/ng2/services/component-services/component.service";
29 import {SdcUiCommon, SdcUiComponents, SdcUiServices} from "onap-ui-angular";
30 import {CompositionService} from "app/ng2/pages/composition/composition.service";
31 import {WorkspaceService} from "app/ng2/pages/workspace/workspace.service";
32 import {Observable} from "rxjs";
33 import {TopologyTemplateService} from "app/ng2/services/component-services/topology-template.service";
34 import {InstanceFeDetails} from "../../../../models/instance-fe-details";
35 import {ToscaGetFunction} from "../../../../models/tosca-get-function";
36 import {ToscaFunctionValidationEvent} from "../../../../ng2/pages/properties-assignment/tosca-function/tosca-function.component";
37
38 export interface IEditPropertyModel {
39     property:PropertyModel;
40     types:Array<string>;
41     simpleTypes:Array<string>;
42     hasGetFunctionValue: boolean;
43     isGetFunctionValid: boolean;
44 }
45
46 interface IPropertyFormViewModelScope extends ng.IScope {
47     forms:any;
48     editForm:ng.IFormController;
49     footerButtons:Array<any>;
50     isNew:boolean;
51     nameMaxLength:number;
52     isLoading:boolean;
53     componentMetadata: { isService: boolean, isVfc: boolean }
54     validationPattern:RegExp;
55     propertyNameValidationPattern:RegExp;
56     commentValidationPattern:RegExp;
57     editPropertyModel: IEditPropertyModel;
58     componentInstanceMap: Map<string, InstanceFeDetails>;
59     modalInstanceProperty:ng.ui.bootstrap.IModalServiceInstance;
60     currentPropertyIndex:number;
61     isLastProperty:boolean;
62     myValue:any;
63     nonPrimitiveTypes:Array<string>;
64     dataTypes:DataTypesMap;
65     isTypeDataType:boolean;
66     maxLength:number;
67     isViewOnly:boolean;
68     isPropertyValueOwner:boolean;
69     isVnfConfiguration:boolean;
70     constraints:string[];
71     modelNameFilter:string;
72     isGetFunctionValueType: boolean;
73     invalidMandatoryFields: boolean;
74
75     validateJson(json:string):boolean;
76     save(doNotCloseModal?:boolean):void;
77     getValidationPattern(type:string):RegExp;
78     validateIntRange(value:string):boolean;
79     close():void;
80     onValueChange():void;
81     onSchemaTypeChange():void;
82     onTypeChange(resetSchema:boolean):void;
83     showSchema():boolean;
84     delete(property:PropertyModel):void;
85     getPrev():void;
86     getNext():void;
87     isSimpleType(typeName:string):boolean;
88     getDefaultValue():any;
89     onValueTypeChange(): void;
90 }
91
92 export class PropertyFormViewModel {
93
94     static '$inject' = [
95         '$scope',
96         'Sdc.Services.DataTypesService',
97         '$uibModalInstance',
98         'property',
99         'ValidationPattern',
100         'PropertyNameValidationPattern',
101         'CommentValidationPattern',
102         'ValidationUtils',
103         'component',
104         '$filter',
105         'ModalServiceSdcUI',
106         'filteredProperties',
107         '$timeout',
108         'isViewOnly',
109         'isPropertyValueOwner',
110         'propertyOwnerType',
111         'propertyOwnerId',
112         'ComponentInstanceServiceNg2',
113         'ComponentServiceNg2',
114         'TopologyTemplateService',
115         'CompositionService',
116         'workspaceService',
117         'inputProperty'
118     ];
119
120     private formState:FormState;
121
122     constructor(private $scope:IPropertyFormViewModelScope,
123                 private DataTypesService:DataTypesService,
124                 private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
125                 private property:PropertyModel,
126                 private ValidationPattern:RegExp,
127                 private PropertyNameValidationPattern:RegExp,
128                 private CommentValidationPattern:RegExp,
129                 private ValidationUtils:ValidationUtils,
130                 private component:Component,
131                 private $filter:ng.IFilterService,
132                 private modalService:SdcUiServices.ModalService,
133                 private filteredProperties:Array<PropertyModel>,
134                 private $timeout:ng.ITimeoutService,
135                 private isViewOnly:boolean,
136                 private isPropertyValueOwner:boolean,
137                 private propertyOwnerType:string,
138                 private propertyOwnerId:string,
139                 private ComponentInstanceServiceNg2: ComponentInstanceServiceNg2,
140                 private ComponentServiceNg2: ComponentServiceNg2,
141                 private topologyTemplateService: TopologyTemplateService,
142                 private compositionService: CompositionService,
143                 private workspaceService: WorkspaceService,
144                 private inputProperty : InputFEModel) {
145
146         this.formState = angular.isDefined(property.name) ? FormState.UPDATE : FormState.CREATE;
147         this.initScope();
148     }
149
150     private initResource = ():void => {
151         this.$scope.editPropertyModel.property = new PropertyModel(this.property);
152         this.$scope.editPropertyModel.property.type = this.property.type ? this.property.type : null;
153         this.$scope.constraints = this.property.constraints && this.property.constraints[0] ? this.property.constraints[0]["validValues"] : null;
154         this.initToscaGetFunction();
155         this.setMaxLength();
156     };
157
158     private initToscaGetFunction() {
159         this.$scope.editPropertyModel.hasGetFunctionValue = this.$scope.editPropertyModel.property.isToscaFunction();
160         this.$scope.editPropertyModel.isGetFunctionValid = true;
161     }
162     
163     private isDataTypeForPropertyType = (property:PropertyModel):boolean=> {
164         property.simpleType = "";
165         if (property.type && PROPERTY_DATA.TYPES.indexOf(property.type) > -1) {
166             return false;
167         }
168         let simpleType = this.getTypeForDataTypeDerivedFromSimple(property.type);
169         if (simpleType) {
170             property.simpleType = simpleType;
171             return false;
172         }
173         return true;
174     };
175     
176     private getTypeForDataTypeDerivedFromSimple = (dataTypeName:string):string => {
177         if (!this.$scope.dataTypes[dataTypeName]) {
178             return 'string';
179         }
180         if (this.$scope.dataTypes[dataTypeName].derivedFromName == "tosca.datatypes.Root" || this.$scope.dataTypes[dataTypeName].properties) {
181             return null;
182         }
183         if (PROPERTY_DATA.SIMPLE_TYPES.indexOf(this.$scope.dataTypes[dataTypeName].derivedFromName) > -1) {
184             return this.$scope.dataTypes[dataTypeName].derivedFromName
185         }
186         return this.getTypeForDataTypeDerivedFromSimple(this.$scope.dataTypes[dataTypeName].derivedFromName);
187     };
188
189     private initForNotSimpleType = ():void => {
190         const property = this.$scope.editPropertyModel.property;
191         this.$scope.isTypeDataType = this.isDataTypeForPropertyType(this.$scope.editPropertyModel.property);
192         if (property.isToscaFunction()) {
193             this.initValueForGetFunction();
194             return;
195         }
196
197         if (this.isComplexType(property.type)) {
198             if (property.value || property.defaultValue) {
199                 this.$scope.myValue = JSON.parse(property.value || property.defaultValue);
200             } else {
201                 this.initEmptyComplexValue(property.type);
202             }
203         }
204     };
205
206     private initValueForGetFunction(): void {
207         const property = this.$scope.editPropertyModel.property;
208         if (property.defaultValue) {
209             this.$scope.myValue = JSON.parse(property.defaultValue);
210             return;
211         }
212         if (this.isComplexType(property.type)) {
213             this.initEmptyComplexValue(property.type);
214             return;
215         }
216
217         this.$scope.myValue = undefined;
218     }
219
220     private initComponentInstanceMap() {
221         this.$scope.componentInstanceMap = new Map<string, InstanceFeDetails>();
222         if (this.compositionService.componentInstances) {
223             this.compositionService.componentInstances.forEach(value => {
224                 this.$scope.componentInstanceMap.set(value.uniqueId, <InstanceFeDetails>{
225                     name: value.name
226                 });
227             });
228         }
229     }
230
231     private initEmptyComplexValue(type: string): any {
232         switch (type) {
233             case PROPERTY_TYPES.MAP:
234                 this.$scope.myValue = {'': null};
235                 break;
236             case PROPERTY_TYPES.LIST:
237                 this.$scope.myValue = [];
238                 break;
239             default:
240                 this.$scope.myValue = {};
241         }
242     }
243
244     private isComplexType(type: string): boolean {
245         if (!type) {
246             return false;
247         }
248         return PROPERTY_DATA.SIMPLE_TYPES.indexOf(type) == -1;
249     }
250
251     private setMaxLength = ():void => {
252         switch (this.$scope.editPropertyModel.property.type) {
253             case PROPERTY_TYPES.MAP:
254             case PROPERTY_TYPES.LIST:
255                 this.$scope.maxLength = this.$scope.editPropertyModel.property.schema.property.type == PROPERTY_TYPES.JSON ?
256                     PROPERTY_VALUE_CONSTRAINTS.JSON_MAX_LENGTH :
257                     PROPERTY_VALUE_CONSTRAINTS.MAX_LENGTH;
258                 break;
259             case PROPERTY_TYPES.JSON:
260                 this.$scope.maxLength = PROPERTY_VALUE_CONSTRAINTS.JSON_MAX_LENGTH;
261                 break;
262             default:
263                 this.$scope.maxLength =PROPERTY_VALUE_CONSTRAINTS.MAX_LENGTH;
264         }
265     };
266
267
268     private initScope = ():void => {
269
270         //scope properties
271         this.$scope.isViewOnly = this.isViewOnly;
272         this.$scope.isLoading = true;
273         this.$scope.forms = {};
274         this.$scope.validationPattern = this.ValidationPattern;
275         this.$scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
276         this.$scope.commentValidationPattern = this.CommentValidationPattern;
277         this.$scope.nameMaxLength = PROPERTY_VALUE_CONSTRAINTS.NAME_MAX_LENGTH;
278         this.$scope.isNew = (this.formState === FormState.CREATE);
279         this.$scope.componentMetadata = {
280             isService: this.workspaceService.metadata.isService(),
281             isVfc: this.workspaceService.metadata.isVfc()
282         }
283         this.$scope.modalInstanceProperty = this.$uibModalInstance;
284         this.$scope.currentPropertyIndex = _.findIndex(this.filteredProperties, i=> i.name == this.property.name);
285         this.$scope.isLastProperty = this.$scope.currentPropertyIndex == (this.filteredProperties.length - 1);
286         const property = new PropertyModel(this.property);
287         this.$scope.editPropertyModel = {
288             'property': property,
289             types: PROPERTY_DATA.TYPES,
290             simpleTypes: PROPERTY_DATA.SIMPLE_TYPES,
291             hasGetFunctionValue: property.isToscaFunction(),
292             isGetFunctionValid: true,
293         };
294         this.$scope.isPropertyValueOwner = this.isPropertyValueOwner;
295         this.$scope.propertyOwnerType = this.propertyOwnerType;
296         this.$scope.modelNameFilter = this.workspaceService.metadata.model;
297         //check if property of VnfConfiguration
298         this.$scope.isVnfConfiguration = false;
299         if(this.propertyOwnerType == "component" && angular.isArray(this.compositionService.componentInstances)) {
300             const componentPropertyOwner:ComponentInstance = this.compositionService.componentInstances.find((ci:ComponentInstance) => {
301                 return ci.uniqueId === this.property.resourceInstanceUniqueId;
302             });
303             if (componentPropertyOwner && componentPropertyOwner.componentName === 'vnfConfiguration') {
304                 this.$scope.isVnfConfiguration = true;
305             }
306         }
307         this.initResource();
308         this.initComponentInstanceMap();
309
310         this.$scope.validateJson = (json:string):boolean => {
311             if (!json) {
312                 return true;
313             }
314             return this.ValidationUtils.validateJson(json);
315         };
316
317         this.DataTypesService.fetchDataTypesByModel(this.workspaceService.metadata.model).then(response => {
318             this.$scope.dataTypes = response.data as DataTypesMap;
319
320             this.$scope.nonPrimitiveTypes = _.filter(Object.keys(this.$scope.dataTypes), (type:string)=> {
321                 return this.$scope.editPropertyModel.types.indexOf(type) == -1;
322             });
323             this.initForNotSimpleType();
324             this.$scope.isLoading = false;
325         });
326
327         //scope methods
328         this.$scope.save = (doNotCloseModal?:boolean):void => {
329             let property:PropertyModel = this.$scope.editPropertyModel.property;
330             this.$scope.isLoading = true;
331             if (property.propertyView){
332                 if (property.constraints.length == 0) {
333                     return;
334                 }
335                 let input : InputBEModel = this.inputProperty;
336                 input.constraints = property.constraints;
337                 this.ComponentServiceNg2.updateComponentInputs(this.component, [input]).subscribe(
338                     (response) => {
339                         console.debug("Input property updated");
340                         this.$uibModalInstance.close();
341                     },
342                     (error) => {
343                         console.debug("Failed to update input property");
344                         this.$uibModalInstance.close();
345                     }
346                 );
347                 return;
348             }
349             this.$scope.editPropertyModel.property.description = this.ValidationUtils.stripAndSanitize(this.$scope.editPropertyModel.property.description);
350             //if read only - or no changes made - just closes the modal
351             //need to check for property.value changes manually to detect if map properties deleted
352             if ((this.$scope.editPropertyModel.property.readonly && !this.$scope.isPropertyValueOwner)
353                 || (!this.$scope.forms.editForm.$dirty && angular.equals(JSON.stringify(this.$scope.myValue), this.$scope.editPropertyModel.property.value))) {
354                 this.$uibModalInstance.close();
355                 return;
356             }
357
358             this.$scope.isLoading = true;
359
360             let onPropertyFailure = (response):void => {
361                 console.error('Failed to update property', response);
362                 this.$scope.isLoading = false;
363             };
364
365             let onPropertySuccess = (propertyFromBE:PropertyModel):void => {
366                 this.$scope.isLoading = false;
367                 this.filteredProperties[this.$scope.currentPropertyIndex] = propertyFromBE;
368                 if (!doNotCloseModal) {
369                     this.$uibModalInstance.close(propertyFromBE);
370                 } else {
371                     this.$scope.forms.editForm.$setPristine();
372                     this.$scope.editPropertyModel.property = new PropertyModel();
373                 }
374             };
375
376             //Not clean, but doing this as a temporary fix until we update the property right panel modals
377             if (this.propertyOwnerType === "group"){
378                 this.ComponentInstanceServiceNg2.updateComponentGroupInstanceProperties(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, this.propertyOwnerId, [property])
379                     .subscribe((propertiesFromBE) => { onPropertySuccess(<PropertyModel>propertiesFromBE[0])}, error => onPropertyFailure(error));
380             } else if (this.propertyOwnerType === "policy"){
381                 if (!this.$scope.editPropertyModel.property.simpleType &&
382                     !this.$scope.isSimpleType(this.$scope.editPropertyModel.property.type) &&
383                     !_.isNil(this.$scope.myValue)) {
384                     property.value = JSON.stringify(this.$scope.myValue);
385                 }
386                 this.ComponentInstanceServiceNg2.updateComponentPolicyInstanceProperties(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, this.propertyOwnerId, [property])
387                     .subscribe((propertiesFromBE) => { onPropertySuccess(<PropertyModel>propertiesFromBE[0])}, error => onPropertyFailure(error));
388             } else {
389                 //in case we have uniqueId we call update method
390                 if (this.$scope.isPropertyValueOwner) {
391                     if (!this.$scope.editPropertyModel.property.simpleType && !this.$scope.isSimpleType(property.type)) {
392                         property.value = JSON.stringify(this.$scope.myValue);
393                     }
394                     this.updateInstanceProperties(property.resourceInstanceUniqueId, [property]).subscribe((propertiesFromBE) => onPropertySuccess(propertiesFromBE[0]),
395                         error => onPropertyFailure(error));
396                 } else {
397                     if (!this.$scope.editPropertyModel.property.simpleType && !this.$scope.isSimpleType(property.type)) {
398                         property.defaultValue = JSON.stringify(this.$scope.myValue);
399                     } else {
400                         this.$scope.editPropertyModel.property.defaultValue = this.$scope.editPropertyModel.property.value;
401                     }
402                     this.addOrUpdateProperty(property).subscribe(onPropertySuccess, error => onPropertyFailure(error));
403                 }
404             }
405         };
406
407         this.$scope.getPrev = ():void=> {
408             this.property = this.filteredProperties[--this.$scope.currentPropertyIndex];
409             this.initResource();
410             this.initForNotSimpleType();
411             this.$scope.isLastProperty = false;
412         };
413
414         this.$scope.getNext = ():void=> {
415             this.property = this.filteredProperties[++this.$scope.currentPropertyIndex];
416             this.initResource();
417             this.initForNotSimpleType();
418             this.$scope.isLastProperty = this.$scope.currentPropertyIndex == (this.filteredProperties.length - 1);
419         };
420
421         this.$scope.isSimpleType = (typeName:string):boolean=> {
422             return typeName && this.$scope.editPropertyModel.simpleTypes.indexOf(typeName) != -1;
423         };
424
425         this.$scope.showSchema = ():boolean => {
426             return [PROPERTY_TYPES.LIST, PROPERTY_TYPES.MAP].indexOf(this.$scope.editPropertyModel.property.type) > -1;
427         };
428
429         this.$scope.getValidationPattern = (type:string):RegExp => {
430             return this.ValidationUtils.getValidationPattern(type);
431         };
432
433         this.$scope.validateIntRange = (value:string):boolean => {
434             return !value || this.ValidationUtils.validateIntRange(value);
435         };
436
437         this.$scope.close = ():void => {
438             this.$uibModalInstance.close();
439         };
440
441         // put default value when instance value is empty
442         this.$scope.onValueChange = ():void => {
443             if (!this.$scope.editPropertyModel.property.value) {
444                 if (this.$scope.isPropertyValueOwner) {
445                     this.$scope.editPropertyModel.property.value = this.$scope.editPropertyModel.property.defaultValue;
446                 }
447             }
448         };
449
450         // Add the done button at the footer.
451         this.$scope.footerButtons = [
452             {'name': 'Save', 'css': 'blue', 'callback': this.$scope.save},
453             {'name': 'Cancel', 'css': 'grey', 'callback': this.$scope.close}
454         ];
455
456         this.$scope.$watch("forms.editForm.$invalid", (newVal) => {
457             if (this.$scope.editPropertyModel.hasGetFunctionValue) {
458                 this.$scope.invalidMandatoryFields = !newVal || !this.$scope.editPropertyModel.property.toscaFunction || this.isViewOnly;
459                 this.$scope.footerButtons[0].disabled = this.$scope.invalidMandatoryFields;
460             } else {
461                 this.$scope.invalidMandatoryFields = !newVal || this.isViewOnly;
462                 this.$scope.footerButtons[0].disabled = this.$scope.invalidMandatoryFields;
463             }
464         });
465
466         this.$scope.$watch("forms.editForm.$valid", (newVal) => {
467             if (this.$scope.editPropertyModel.hasGetFunctionValue) {
468                 this.$scope.invalidMandatoryFields = !newVal || !this.$scope.editPropertyModel.property.toscaFunction || this.isViewOnly;
469                 this.$scope.footerButtons[0].disabled = this.$scope.invalidMandatoryFields;
470             } else {
471                 this.$scope.invalidMandatoryFields = !newVal || this.isViewOnly;
472                 this.$scope.footerButtons[0].disabled = this.$scope.invalidMandatoryFields;
473             }
474         });
475
476         this.$scope.getDefaultValue = ():any => {
477             return this.$scope.isPropertyValueOwner ? this.$scope.editPropertyModel.property.defaultValue : null;
478         };
479
480         this.$scope.onTypeChange = ():void => {
481             this.$scope.editPropertyModel.property.value = '';
482             this.$scope.editPropertyModel.property.defaultValue = '';
483             this.setMaxLength();
484             this.initForNotSimpleType();
485         };
486
487         this.$scope.onSchemaTypeChange = ():void => {
488             if (this.$scope.editPropertyModel.property.type == PROPERTY_TYPES.MAP) {
489                 this.$scope.myValue = {};
490             } else if (this.$scope.editPropertyModel.property.type == PROPERTY_TYPES.LIST) {
491                 this.$scope.myValue = [];
492             }
493             this.setMaxLength();
494         };
495
496         this.$scope.delete = (property:PropertyModel):void => {
497             let onOk: Function = ():void => {
498                 this.deleteProperty(property.uniqueId).subscribe(
499                     this.$scope.close
500                 );
501             };
502             let title:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TITLE");
503             let message:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TEXT", "{'name': '" + property.name + "'}");
504             const okButton = {testId: "OK", text: "OK", type: SdcUiCommon.ButtonType.info, callback: onOk, closeModal: true} as SdcUiComponents.ModalButtonComponent;
505             this.modalService.openInfoModal(title, message, 'delete-modal', [okButton]);
506         };
507
508         this.$scope.onValueTypeChange = (): void => {
509             this.setEmptyValue();
510             if (this.$scope.editPropertyModel.hasGetFunctionValue) {
511                 this.$scope.editPropertyModel.isGetFunctionValid = undefined;
512             } else {
513                 this.$scope.editPropertyModel.property.toscaFunction = undefined;
514                 this.$scope.editPropertyModel.isGetFunctionValid = true;
515             }
516         }
517
518         this.$scope.onConstraintChange = (constraints: any): void => {
519             if (!this.$scope.invalidMandatoryFields) {
520                 this.$scope.footerButtons[0].disabled = !constraints.valid;
521             } else {
522                 this.$scope.footerButtons[0].disabled = this.$scope.invalidMandatoryFields;
523             }
524             if (!constraints.constraints || constraints.constraints.length == 0) {
525                 this.$scope.editPropertyModel.property.propertyConstraints = null;
526                 this.$scope.editPropertyModel.property.constraints = null;
527                 return;
528             }
529             this.$scope.editPropertyModel.property.propertyConstraints = this.serializePropertyConstraints(constraints.constraints);
530             this.$scope.editPropertyModel.property.constraints = constraints.constraints;
531         }
532
533         this.$scope.onGetFunctionValidFunction = (toscaGetFunction: ToscaGetFunction): void => {
534             this.$scope.editPropertyModel.property.toscaFunction = toscaGetFunction;
535         }
536
537         this.$scope.onToscaFunctionValidityChange = (validationEvent: ToscaFunctionValidationEvent): void => {
538             if (validationEvent.isValid) {
539                 this.$scope.editPropertyModel.isGetFunctionValid = true;
540                 return;
541             }
542             this.$scope.editPropertyModel.isGetFunctionValid = undefined;
543         }
544     };
545
546     private serializePropertyConstraints(constraints: any[]): string[] {
547         if (constraints) {
548             let stringConstrsints = new Array();
549             constraints.forEach((constraint) => {
550                 stringConstrsints.push(JSON.stringify(constraint));
551             })
552             return stringConstrsints;
553         }
554         return null;
555     }
556
557     private setEmptyValue() {
558         const property1 = this.$scope.editPropertyModel.property;
559         property1.value = undefined;
560         if (this.isComplexType(property1.type)) {
561             this.initEmptyComplexValue(property1.type);
562             return;
563         }
564         this.$scope.myValue = '';
565     }
566
567     private updateInstanceProperties = (componentInstanceId:string, properties:PropertyModel[]):Observable<PropertyModel[]> => {
568
569         return this.ComponentInstanceServiceNg2.updateInstanceProperties(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, componentInstanceId, properties)
570             .map(newProperties => {
571                 newProperties.forEach((newProperty) => {
572                     if (!_.isNil(newProperty.path)) {
573                         if (newProperty.path[0] === newProperty.resourceInstanceUniqueId) newProperty.path.shift();
574                         // find exist instance property in parent component for update the new value ( find bu uniqueId & path)
575                         let existProperty: PropertyModel = <PropertyModel>_.find(this.compositionService.componentInstancesProperties[newProperty.resourceInstanceUniqueId], {
576                             uniqueId: newProperty.uniqueId,
577                             path: newProperty.path
578                         });
579                         let index = this.compositionService.componentInstancesProperties[newProperty.resourceInstanceUniqueId].indexOf(existProperty);
580                         this.compositionService.componentInstancesProperties[newProperty.resourceInstanceUniqueId][index] = newProperty;
581                     }
582                 });
583                 return newProperties;
584             });
585     };
586
587     private addOrUpdateProperty = (property: PropertyModel): Observable<PropertyModel> => {
588         if (!property.uniqueId) {
589             let onSuccess = (newProperty: PropertyModel): PropertyModel => {
590                 this.filteredProperties.push(newProperty);
591                 return newProperty;
592             };
593             return this.topologyTemplateService.addProperty(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, property)
594                 .map(onSuccess);
595         } else {
596             let onSuccess = (newProperty: PropertyModel): PropertyModel => {
597                 // find exist instance property in parent component for update the new value ( find bu uniqueId )
598                 let existProperty: PropertyModel = <PropertyModel>_.find(this.filteredProperties, {uniqueId: newProperty.uniqueId});
599                 let propertyIndex = this.filteredProperties.indexOf(existProperty);
600                 this.filteredProperties[propertyIndex] = newProperty;
601                 return newProperty;
602             };
603             return this.topologyTemplateService.updateProperty(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, property).map(onSuccess);
604         }
605     };
606
607     public deleteProperty = (propertyId:string):Observable<void> => {
608         let onSuccess = ():void => {
609             console.debug("Property deleted");
610             delete _.remove(this.filteredProperties, {uniqueId: propertyId})[0];
611         };
612         let onFailed = ():void => {
613             console.debug("Failed to delete property");
614         };
615         return this.topologyTemplateService.deleteProperty(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, propertyId).map(onSuccess, onFailed);
616     };
617
618 }