*/
'use strict';
-import {
- PROPERTY_TYPES, ModalsHandler, ValidationUtils, PROPERTY_VALUE_CONSTRAINTS, FormState, PROPERTY_DATA} from "app/utils";
+import * as _ from "lodash";
+import {FormState, PROPERTY_DATA, PROPERTY_TYPES, PROPERTY_VALUE_CONSTRAINTS, ValidationUtils} from "app/utils";
import {DataTypesService} from "app/services";
-import {PropertyModel, DataTypesMap, Component} from "app/models";
+import {DataTypesMap, PropertyModel} from "app/models";
import {ComponentInstance} from "../../../../models/componentsInstances/componentInstance";
+import {ComponentInstanceServiceNg2} from "app/ng2/services/component-instance-services/component-instance.service";
+import {SdcUiCommon, SdcUiComponents, SdcUiServices} from "onap-ui-angular";
+import {CompositionService} from "app/ng2/pages/composition/composition.service";
+import {WorkspaceService} from "app/ng2/pages/workspace/workspace.service";
+import {Observable} from "rxjs";
+import {TopologyTemplateService} from "app/ng2/services/component-services/topology-template.service";
+import {InstanceFeDetails} from "../../../../models/instance-fe-details";
+import {ToscaGetFunction} from "../../../../models/tosca-get-function";
export interface IEditPropertyModel {
property:PropertyModel;
types:Array<string>;
simpleTypes:Array<string>;
+ hasGetFunctionValue: boolean;
+ isGetFunctionValid: boolean;
}
interface IPropertyFormViewModelScope extends ng.IScope {
footerButtons:Array<any>;
isNew:boolean;
isLoading:boolean;
- isService:boolean;
+ componentMetadata: { isService: boolean, isVfc: boolean }
validationPattern:RegExp;
propertyNameValidationPattern:RegExp;
commentValidationPattern:RegExp;
- editPropertyModel:IEditPropertyModel;
+ editPropertyModel: IEditPropertyModel;
+ componentInstanceMap: Map<string, InstanceFeDetails>;
modalInstanceProperty:ng.ui.bootstrap.IModalServiceInstance;
currentPropertyIndex:number;
isLastProperty:boolean;
maxLength:number;
isPropertyValueOwner:boolean;
isVnfConfiguration:boolean;
+ constraints:string[];
+ modelNameFilter:string;
+ isGetFunctionValueType: boolean;
validateJson(json:string):boolean;
save(doNotCloseModal?:boolean):void;
getNext():void;
isSimpleType(typeName:string):boolean;
getDefaultValue():any;
+ onValueTypeChange(): void;
}
export class PropertyFormViewModel {
'PropertyNameValidationPattern',
'CommentValidationPattern',
'ValidationUtils',
- 'component',
+ // 'component',
'$filter',
- 'ModalsHandler',
+ 'ModalServiceSdcUI',
'filteredProperties',
'$timeout',
- 'isPropertyValueOwner'
+ 'isPropertyValueOwner',
+ 'propertyOwnerType',
+ 'propertyOwnerId',
+ 'ComponentInstanceServiceNg2',
+ 'TopologyTemplateService',
+ 'CompositionService',
+ 'workspaceService'
];
private formState:FormState;
private PropertyNameValidationPattern:RegExp,
private CommentValidationPattern:RegExp,
private ValidationUtils:ValidationUtils,
- private component:Component,
+ // private component:Component,
private $filter:ng.IFilterService,
- private ModalsHandler:ModalsHandler,
+ private modalService:SdcUiServices.ModalService,
private filteredProperties:Array<PropertyModel>,
private $timeout:ng.ITimeoutService,
- private isPropertyValueOwner:boolean) {
+ private isPropertyValueOwner:boolean,
+ private propertyOwnerType:string,
+ private propertyOwnerId:string,
+ private ComponentInstanceServiceNg2: ComponentInstanceServiceNg2,
+ private topologyTemplateService: TopologyTemplateService,
+ private compositionService: CompositionService,
+ private workspaceService: WorkspaceService) {
this.formState = angular.isDefined(property.name) ? FormState.UPDATE : FormState.CREATE;
this.initScope();
private initResource = ():void => {
this.$scope.editPropertyModel.property = new PropertyModel(this.property);
this.$scope.editPropertyModel.property.type = this.property.type ? this.property.type : null;
- this.$scope.editPropertyModel.property.value = this.$scope.editPropertyModel.property.value || this.$scope.editPropertyModel.property.defaultValue;
+ this.$scope.constraints = this.property.constraints && this.property.constraints[0] ? this.property.constraints[0]["validValues"] : null;
+ this.initToscaGetFunction();
this.setMaxLength();
- this.initAddOnLabels();
};
- //init property add-ons labels that show up at the left side of the input.
- private initAddOnLabels = () => {
- if (this.$scope.editPropertyModel.property.name == 'network_role' && this.$scope.isService) {
- //the server sends back the normalized name. Remove it (to prevent interference with validation) and set the addon label to the component name directly.
- //Note: this cant be done in properties.ts because we dont have access to the component
- if (this.$scope.editPropertyModel.property.value) {
- let splitProp = this.$scope.editPropertyModel.property.value.split(new RegExp(this.component.normalizedName + '.', "gi"));
- this.$scope.editPropertyModel.property.value = splitProp.pop();
- }
- this.$scope.editPropertyModel.property.addOn = this.component.name;
- }
+ private initToscaGetFunction() {
+ this.$scope.editPropertyModel.hasGetFunctionValue = this.$scope.editPropertyModel.property.isToscaFunction();
+ this.$scope.editPropertyModel.isGetFunctionValid = true;
}
- private initEditPropertyModel = ():void => {
- this.$scope.editPropertyModel = {
- property: null,
- types: PROPERTY_DATA.TYPES,
- simpleTypes: PROPERTY_DATA.SIMPLE_TYPES
- };
-
- this.initResource();
- };
-
private initForNotSimpleType = ():void => {
- let property = this.$scope.editPropertyModel.property;
+ const property = this.$scope.editPropertyModel.property;
this.$scope.isTypeDataType = this.DataTypesService.isDataTypeForPropertyType(this.$scope.editPropertyModel.property);
- if (property.type && this.$scope.editPropertyModel.simpleTypes.indexOf(property.type) == -1) {
- if (!(property.value || property.defaultValue)) {
- switch (property.type) {
- case PROPERTY_TYPES.MAP:
- this.$scope.myValue = {'': null};
- break;
- case PROPERTY_TYPES.LIST:
- this.$scope.myValue = [];
- break;
- default:
- this.$scope.myValue = {};
- }
- } else {
+ if (property.isToscaFunction()) {
+ this.initValueForGetFunction();
+ return;
+ }
+
+ if (this.isComplexType(property.type)) {
+ if (property.value || property.defaultValue) {
this.$scope.myValue = JSON.parse(property.value || property.defaultValue);
+ } else {
+ this.initEmptyComplexValue(property.type);
}
}
};
+ private initValueForGetFunction(): void {
+ const property = this.$scope.editPropertyModel.property;
+ if (property.defaultValue) {
+ this.$scope.myValue = JSON.parse(property.defaultValue);
+ return;
+ }
+ if (this.isComplexType(property.type)) {
+ this.initEmptyComplexValue(property.type);
+ return;
+ }
+
+ this.$scope.myValue = undefined;
+ }
+
+ private initComponentInstanceMap() {
+ this.$scope.componentInstanceMap = new Map<string, InstanceFeDetails>();
+ if (this.compositionService.componentInstances) {
+ this.compositionService.componentInstances.forEach(value => {
+ this.$scope.componentInstanceMap.set(value.uniqueId, <InstanceFeDetails>{
+ name: value.name
+ });
+ });
+ }
+ }
+
+ private initEmptyComplexValue(type: string): any {
+ switch (type) {
+ case PROPERTY_TYPES.MAP:
+ this.$scope.myValue = {'': null};
+ break;
+ case PROPERTY_TYPES.LIST:
+ this.$scope.myValue = [];
+ break;
+ default:
+ this.$scope.myValue = {};
+ }
+ }
+
+ private isComplexType(type: string): boolean {
+ if (!type) {
+ return false;
+ }
+ return PROPERTY_DATA.SIMPLE_TYPES.indexOf(type) == -1;
+ }
+
private setMaxLength = ():void => {
switch (this.$scope.editPropertyModel.property.type) {
case PROPERTY_TYPES.MAP:
private initScope = ():void => {
//scope properties
+ this.$scope.isLoading = true;
this.$scope.forms = {};
this.$scope.validationPattern = this.ValidationPattern;
this.$scope.propertyNameValidationPattern = this.PropertyNameValidationPattern;
this.$scope.commentValidationPattern = this.CommentValidationPattern;
- this.$scope.isLoading = false;
this.$scope.isNew = (this.formState === FormState.CREATE);
- this.$scope.isService = this.component.isService();
+ this.$scope.componentMetadata = {
+ isService: this.workspaceService.metadata.isService(),
+ isVfc: this.workspaceService.metadata.isVfc()
+ }
this.$scope.modalInstanceProperty = this.$uibModalInstance;
this.$scope.currentPropertyIndex = _.findIndex(this.filteredProperties, i=> i.name == this.property.name);
this.$scope.isLastProperty = this.$scope.currentPropertyIndex == (this.filteredProperties.length - 1);
- this.$scope.dataTypes = this.DataTypesService.getAllDataTypes();
+ const property = new PropertyModel(this.property);
+ this.$scope.editPropertyModel = {
+ 'property': property,
+ types: PROPERTY_DATA.TYPES,
+ simpleTypes: PROPERTY_DATA.SIMPLE_TYPES,
+ hasGetFunctionValue: property.isToscaFunction(),
+ isGetFunctionValid: true,
+ };
this.$scope.isPropertyValueOwner = this.isPropertyValueOwner;
- this.initEditPropertyModel();
-
+ this.$scope.propertyOwnerType = this.propertyOwnerType;
+ this.$scope.modelNameFilter = this.workspaceService.metadata.model;
//check if property of VnfConfiguration
this.$scope.isVnfConfiguration = false;
- if(angular.isArray(this.component.componentInstances)) {
- var componentPropertyOwner:ComponentInstance = this.component.componentInstances.find((ci:ComponentInstance) => {
+ if(this.propertyOwnerType == "component" && angular.isArray(this.compositionService.componentInstances)) {
+ const componentPropertyOwner:ComponentInstance = this.compositionService.componentInstances.find((ci:ComponentInstance) => {
return ci.uniqueId === this.property.resourceInstanceUniqueId;
});
- if (componentPropertyOwner.componentName === 'vnfConfiguration') {
+ if (componentPropertyOwner && componentPropertyOwner.componentName === 'vnfConfiguration') {
this.$scope.isVnfConfiguration = true;
}
}
-
- this.$scope.nonPrimitiveTypes = _.filter(Object.keys(this.$scope.dataTypes), (type:string)=> {
- return this.$scope.editPropertyModel.types.indexOf(type) == -1;
- });
+ this.initResource();
this.initForNotSimpleType();
-
+ this.initComponentInstanceMap();
this.$scope.validateJson = (json:string):boolean => {
if (!json) {
return this.ValidationUtils.validateJson(json);
};
+ this.DataTypesService.fetchDataTypesByModel(this.workspaceService.metadata.model).then(response => {
+ this.$scope.dataTypes = response.data as DataTypesMap;
+ this.$scope.nonPrimitiveTypes = _.filter(Object.keys(this.$scope.dataTypes), (type:string)=> {
+ return this.$scope.editPropertyModel.types.indexOf(type) == -1;
+ });
+
+ this.$scope.isLoading = false;
+ });
//scope methods
this.$scope.save = (doNotCloseModal?:boolean):void => {
this.$scope.isLoading = true;
- let onPropertyFaild = (response):void => {
- console.info('onFaild', response);
+ let onPropertyFailure = (response):void => {
+ console.error('Failed to update property', response);
this.$scope.isLoading = false;
};
let onPropertySuccess = (propertyFromBE:PropertyModel):void => {
- console.info('onPropertyResourceSuccess : ', propertyFromBE);
this.$scope.isLoading = false;
this.filteredProperties[this.$scope.currentPropertyIndex] = propertyFromBE;
if (!doNotCloseModal) {
}
};
- //in case we have uniqueId we call update method
- if (this.$scope.isPropertyValueOwner) {
- if (!this.$scope.editPropertyModel.property.simpleType && !this.$scope.isSimpleType(property.type)) {
- let myValueString:string = JSON.stringify(this.$scope.myValue);
- property.value = myValueString;
+ //Not clean, but doing this as a temporary fix until we update the property right panel modals
+ if (this.propertyOwnerType === "group"){
+ this.ComponentInstanceServiceNg2.updateComponentGroupInstanceProperties(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, this.propertyOwnerId, [property])
+ .subscribe((propertiesFromBE) => { onPropertySuccess(<PropertyModel>propertiesFromBE[0])}, error => onPropertyFailure(error));
+ } else if (this.propertyOwnerType === "policy"){
+ if (!this.$scope.editPropertyModel.property.simpleType &&
+ !this.$scope.isSimpleType(this.$scope.editPropertyModel.property.type) &&
+ !_.isNil(this.$scope.myValue)) {
+ property.value = JSON.stringify(this.$scope.myValue);
}
- this.component.updateInstanceProperty(property).then(onPropertySuccess, onPropertyFaild);
+ this.ComponentInstanceServiceNg2.updateComponentPolicyInstanceProperties(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, this.propertyOwnerId, [property])
+ .subscribe((propertiesFromBE) => { onPropertySuccess(<PropertyModel>propertiesFromBE[0])}, error => onPropertyFailure(error));
} else {
- if (!this.$scope.editPropertyModel.property.simpleType && !this.$scope.isSimpleType(property.type)) {
- let myValueString:string = JSON.stringify(this.$scope.myValue);
- property.defaultValue = myValueString;
+ //in case we have uniqueId we call update method
+ if (this.$scope.isPropertyValueOwner) {
+ if (!this.$scope.editPropertyModel.property.simpleType && !this.$scope.isSimpleType(property.type)) {
+ property.value = JSON.stringify(this.$scope.myValue);
+ }
+ this.updateInstanceProperties(property.resourceInstanceUniqueId, [property]).subscribe((propertiesFromBE) => onPropertySuccess(propertiesFromBE[0]),
+ error => onPropertyFailure(error));
} else {
- this.$scope.editPropertyModel.property.defaultValue = this.$scope.editPropertyModel.property.value;
+ if (!this.$scope.editPropertyModel.property.simpleType && !this.$scope.isSimpleType(property.type)) {
+ property.defaultValue = JSON.stringify(this.$scope.myValue);
+ } else {
+ this.$scope.editPropertyModel.property.defaultValue = this.$scope.editPropertyModel.property.value;
+ }
+ this.addOrUpdateProperty(property).subscribe(onPropertySuccess, error => onPropertyFailure(error));
}
- this.component.addOrUpdateProperty(property).then(onPropertySuccess, onPropertyFaild);
}
};
{'name': 'Cancel', 'css': 'grey', 'callback': this.$scope.close}
];
- this.$scope.$watch("forms.editForm.$invalid", (newVal, oldVal) => {
- this.$scope.footerButtons[0].disabled = this.$scope.forms.editForm.$invalid;
+ this.$scope.$watch("forms.editForm.$invalid", (newVal) => {
+ if (this.$scope.editPropertyModel.hasGetFunctionValue) {
+ this.$scope.footerButtons[0].disabled = newVal || !this.$scope.editPropertyModel.property.toscaFunction;
+ } else {
+ this.$scope.footerButtons[0].disabled = newVal;
+ }
+ });
+
+ this.$scope.$watch("forms.editForm.$valid", (newVal) => {
+ if (this.$scope.editPropertyModel.hasGetFunctionValue) {
+ this.$scope.footerButtons[0].disabled = !newVal || !this.$scope.editPropertyModel.property.toscaFunction;
+ } else {
+ this.$scope.footerButtons[0].disabled = !newVal;
+ }
});
this.$scope.getDefaultValue = ():any => {
};
this.$scope.delete = (property:PropertyModel):void => {
- let onOk = ():void => {
- this.component.deleteProperty(property.uniqueId).then(
+ let onOk: Function = ():void => {
+ this.deleteProperty(property.uniqueId).subscribe(
this.$scope.close
);
};
let title:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TITLE");
let message:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TEXT", "{'name': '" + property.name + "'}");
- this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
+ const okButton = {testId: "OK", text: "OK", type: SdcUiCommon.ButtonType.info, callback: onOk, closeModal: true} as SdcUiComponents.ModalButtonComponent;
+ this.modalService.openInfoModal(title, message, 'delete-modal', [okButton]);
};
+
+ this.$scope.onValueTypeChange = (): void => {
+ this.setEmptyValue();
+ if (this.$scope.editPropertyModel.hasGetFunctionValue) {
+ this.$scope.editPropertyModel.isGetFunctionValid = undefined;
+ } else {
+ this.$scope.editPropertyModel.property.toscaFunction = undefined;
+ this.$scope.editPropertyModel.isGetFunctionValid = true;
+ }
+ }
+
+ this.$scope.onGetFunctionValidFunction = (toscaGetFunction: ToscaGetFunction): void => {
+ this.$scope.editPropertyModel.property.toscaFunction = toscaGetFunction;
+ }
+
+ this.$scope.onGetFunctionValidityChange = (isValid): void => {
+ if (isValid.isValid) {
+ this.$scope.editPropertyModel.isGetFunctionValid = true;
+ return;
+ }
+ this.$scope.editPropertyModel.isGetFunctionValid = undefined;
+ }
+
+ };
+
+ private setEmptyValue() {
+ const property1 = this.$scope.editPropertyModel.property;
+ property1.value = undefined;
+ if (this.isComplexType(property1.type)) {
+ this.initEmptyComplexValue(property1.type);
+ return;
+ }
+ this.$scope.myValue = '';
}
-}
+
+ private updateInstanceProperties = (componentInstanceId:string, properties:PropertyModel[]):Observable<PropertyModel[]> => {
+
+ return this.ComponentInstanceServiceNg2.updateInstanceProperties(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, componentInstanceId, properties)
+ .map(newProperties => {
+ newProperties.forEach((newProperty) => {
+ if (!_.isNil(newProperty.path)) {
+ if (newProperty.path[0] === newProperty.resourceInstanceUniqueId) newProperty.path.shift();
+ // find exist instance property in parent component for update the new value ( find bu uniqueId & path)
+ let existProperty: PropertyModel = <PropertyModel>_.find(this.compositionService.componentInstancesProperties[newProperty.resourceInstanceUniqueId], {
+ uniqueId: newProperty.uniqueId,
+ path: newProperty.path
+ });
+ let index = this.compositionService.componentInstancesProperties[newProperty.resourceInstanceUniqueId].indexOf(existProperty);
+ this.compositionService.componentInstancesProperties[newProperty.resourceInstanceUniqueId][index] = newProperty;
+ }
+ });
+ return newProperties;
+ });
+ };
+
+ private addOrUpdateProperty = (property: PropertyModel): Observable<PropertyModel> => {
+ if (!property.uniqueId) {
+ let onSuccess = (newProperty: PropertyModel): PropertyModel => {
+ this.filteredProperties.push(newProperty);
+ return newProperty;
+ };
+ return this.topologyTemplateService.addProperty(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, property)
+ .map(onSuccess);
+ } else {
+ let onSuccess = (newProperty: PropertyModel): PropertyModel => {
+ // find exist instance property in parent component for update the new value ( find bu uniqueId )
+ let existProperty: PropertyModel = <PropertyModel>_.find(this.filteredProperties, {uniqueId: newProperty.uniqueId});
+ let propertyIndex = this.filteredProperties.indexOf(existProperty);
+ this.filteredProperties[propertyIndex] = newProperty;
+ return newProperty;
+ };
+ return this.topologyTemplateService.updateProperty(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, property).map(onSuccess);
+ }
+ };
+
+ public deleteProperty = (propertyId:string):Observable<void> => {
+ let onSuccess = ():void => {
+ console.debug("Property deleted");
+ delete _.remove(this.filteredProperties, {uniqueId: propertyId})[0];
+ };
+ let onFailed = ():void => {
+ console.debug("Failed to delete property");
+ };
+ return this.topologyTemplateService.deleteProperty(this.workspaceService.metadata.componentType, this.workspaceService.metadata.uniqueId, propertyId).map(onSuccess, onFailed);
+ };
+
+}
\ No newline at end of file