e503f4d2a8929925f69aa83a0da20408328f22e1
[vid.git] / vid-webpack-master / src / app / shared / components / genericForm / formControlsServices / vnfGroupGenerator / vnfGroup.control.generator.ts
1 import {Injectable} from "@angular/core";
2 import {AaiService} from "../../../../services/aaiService/aai.service";
3 import {NgRedux} from "@angular-redux/store";
4 import {BasicControlGenerator} from "../basic.control.generator";
5 import {
6   FormControlModel,
7   ValidatorModel,
8   ValidatorOptions
9 } from "../../../../models/formControlModels/formControl.model";
10 import {LogService} from "../../../../utils/log/log.service";
11 import {AppState} from "../../../../store/reducers";
12 import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model";
13 import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum";
14 import {SelectOption} from "../../../../models/selectOption";
15 import {VnfGroupModel} from "../../../../models/vnfGroupModel";
16 import * as _ from 'lodash';
17 import {Observable, of} from "rxjs";
18
19
20 export enum FormControlNames {
21   INSTANCE_NAME = 'instanceName',
22   ROLLBACK_ON_FAILURE = 'rollbackOnFailure',
23 }
24
25 enum InputType {
26   ROLLBACK = "rollbackOnFailure"
27 }
28
29 @Injectable()
30 export class VnfGroupControlGenerator {
31   aaiService: AaiService;
32   constructor(private _basicControlGenerator: BasicControlGenerator,
33               private store: NgRedux<AppState>,
34               private _aaiService: AaiService,
35               private _logService: LogService) {
36     this.aaiService = _aaiService;
37   }
38
39   getVnfGroupInstance = (serviceId: string, vnfGroupStoreKey: string): any => {
40     let vnfGroupInstance = null;
41     if (this.store.getState().service.serviceInstance[serviceId] && _.has(this.store.getState().service.serviceInstance[serviceId].vnfGroups, vnfGroupStoreKey)) {
42       vnfGroupInstance = Object.assign({}, this.store.getState().service.serviceInstance[serviceId].vnfGroups[vnfGroupStoreKey]);
43     }
44     return vnfGroupInstance;
45   };
46
47   getMacroFormControls(serviceId: string, vnfGroupStoreKey: string, vnfGroupName: string, dynamicInputs?: Array<any>): Array<FormControlModel> {
48     vnfGroupStoreKey = _.isNil(vnfGroupStoreKey) ? vnfGroupName : vnfGroupStoreKey;
49
50     if (_.isNil(serviceId) || _.isNil(vnfGroupStoreKey) || _.isNil(vnfGroupName)) {
51       this._logService.error('should provide serviceId, vnfGroupName, vnfGroupStoreKey', serviceId);
52       return [];
53     }
54
55     const vnfGroupInstance = this.getVnfGroupInstance(serviceId, vnfGroupStoreKey);
56     const vnfGroupModel = new VnfGroupModel(this.store.getState().service.serviceHierarchy[serviceId].vnfGroups[vnfGroupName]);
57     let result: FormControlModel[] = [];
58
59     if (!_.isNil(vnfGroupModel)) {
60       result.push(this.getInstanceName(vnfGroupInstance, serviceId, vnfGroupName, vnfGroupModel.isEcompGeneratedNaming));
61     }
62     return result;
63   }
64
65   getAlaCarteFormControls(serviceId: string, vnfGroupStoreKey: string, vnfGroupName: string, dynamicInputs?: any[]): FormControlModel[] {
66     vnfGroupStoreKey = _.isNil(vnfGroupStoreKey) ? vnfGroupName : vnfGroupStoreKey;
67     if (_.isNil(serviceId) || _.isNil(vnfGroupStoreKey) || _.isNil(vnfGroupName)) {
68       this._logService.error('should provide serviceId, vnfGroupName, vnfGroupStoreKey', serviceId);
69       return [];
70     }
71
72     let result: FormControlModel[] = [];
73     const vnfGroupInstance = this.getVnfGroupInstance(serviceId, vnfGroupStoreKey);
74     const vnfGroupModel = new VnfGroupModel(this.store.getState().service.serviceHierarchy[serviceId].vnfGroups[vnfGroupName]);
75
76     if (!_.isNil(vnfGroupModel)) {
77       result.push(this.getInstanceName(vnfGroupInstance, serviceId, vnfGroupName, vnfGroupModel.isEcompGeneratedNaming));
78       result.push(this.getRollbackOnFailureControl(vnfGroupInstance, result));
79     }
80     return result;
81   }
82
83   isInputShouldBeShown = (inputType: any): boolean => {
84     let vnfGroupInputs = [InputType.ROLLBACK];
85     return vnfGroupInputs.indexOf(inputType) > -1;
86   };
87
88   getDefaultInstanceName(instance : any, serviceId : string, vnfGroupName : string) : string {
89     const vnfGroupModel: VnfGroupModel = this.store.getState().service.serviceHierarchy[serviceId].vnfGroups[vnfGroupName];
90     return this._basicControlGenerator.getDefaultInstanceName(instance, vnfGroupModel);
91   }
92
93   getInstanceName(instance : any, serviceId : string, vnfGroupName : string, isEcompGeneratedNaming: boolean): FormControlModel {
94     const vnfGroupModel : VnfGroupModel = this.store.getState().service.serviceHierarchy[serviceId].vnfGroups[vnfGroupName];
95     return this._basicControlGenerator.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, vnfGroupModel);
96   }
97
98   getRollbackOnFailureControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
99     return new DropdownFormControl({
100       type: FormControlType.DROPDOWN,
101       controlName: FormControlNames.ROLLBACK_ON_FAILURE,
102       displayName: 'Rollback on failure',
103       dataTestId: 'rollback',
104       placeHolder: 'Rollback on failure',
105       isDisabled: false,
106       validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
107       value: instance ? instance.rollbackOnFailure : 'true',
108       onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions)
109     })
110   };
111
112   getRollBackOnFailureOptions = (): Observable<SelectOption[]> => {
113     return of([
114       new SelectOption({id: 'true', name: 'Rollback'}),
115       new SelectOption({id: 'false', name: 'Don\'t Rollback'})
116     ]);
117   };
118 }