Add Pause Functionality from Inside VF Module's Edit Dialog
[vid.git] / vid-webpack-master / src / app / shared / components / genericForm / formControlsServices / vfModuleGenerator / vfModule.control.generator.ts
1 import {Injectable} from "@angular/core";
2 import {GenericFormService} from "../../generic-form.service";
3 import {AaiService} from "../../../../services/aaiService/aai.service";
4 import {NgRedux} from "@angular-redux/store";
5 import {HttpClient} from "@angular/common/http";
6 import {ControlGeneratorUtil, SDN_C_PRE_LOAD} from "../control.generator.util.service";
7 import {
8   CustomValidatorOptions,
9   FormControlModel,
10   ValidatorModel,
11   ValidatorOptions
12 } from "../../../../models/formControlModels/formControl.model";
13 import {LogService} from "../../../../utils/log/log.service";
14 import {AppState} from "../../../../store/reducers";
15 import {FormGroup} from "@angular/forms";
16 import {InputFormControl} from "../../../../models/formControlModels/inputFormControl.model";
17 import {VfModuleInstance} from "../../../../models/vfModuleInstance";
18 import {VfModule} from "../../../../models/vfModule";
19 import {VNFModel} from "../../../../models/vnfModel";
20 import {VnfInstance} from "../../../../models/vnfInstance";
21 import * as _ from 'lodash';
22 import {SharedControllersService} from "../sharedControlles/shared.controllers.service";
23 import {MessageModal} from "../../../messageModal/message-modal.service";
24 import {ButtonType} from "../../../customModal/models/button.type";
25 import {SharedTreeService} from "../../../../../drawingBoard/service-planning/objectsToTree/shared.tree.service";
26 import {FeatureFlagsService, Features} from "../../../../services/featureFlag/feature-flags.service";
27 import {PauseStatus} from "../../../../models/serviceInstanceActions";
28
29 export enum FormControlNames {
30   INSTANCE_NAME = 'instanceName',
31   VOLUME_GROUP_NAME = 'volumeGroupName',
32   LCPCLOUD_REGION_ID = 'lcpCloudRegionId',
33   LEGACY_REGION = 'legacyRegion',
34   TENANT_ID = 'tenantId',
35   ROLLBACK_ON_FAILURE = 'rollbackOnFailure',
36 }
37
38
39 @Injectable()
40 export class VfModuleControlGenerator {
41   aaiService: AaiService;
42   vfModuleModel: VfModule;
43   isUpdateMode: boolean;
44
45   constructor(private genericFormService: GenericFormService,
46               private _basicControlGenerator: ControlGeneratorUtil,
47               private _sharedControllersService: SharedControllersService,
48               private _sharedTreeService: SharedTreeService,
49               private store: NgRedux<AppState>,
50               private http: HttpClient,
51               private _aaiService: AaiService,
52               private _logService: LogService,
53               private _featureFlagsService:FeatureFlagsService) {
54     this.aaiService = _aaiService;
55   }
56
57   getVfModuleInstance = (serviceId: string, vnfStoreKey: string, UUIDData: Object, isUpdateMode: boolean): VfModuleInstance => {
58     let vfModuleInstance: VfModuleInstance = null;
59     if (isUpdateMode && this.store.getState().service.serviceInstance[serviceId] &&
60       _.has(this.store.getState().service.serviceInstance[serviceId].vnfs, vnfStoreKey) &&
61       _.has(this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey].vfModules, UUIDData['modelName'])) {
62       vfModuleInstance = Object.assign({}, this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey].vfModules[UUIDData['modelName']][UUIDData['vFModuleStoreKey']]);
63     }
64     return vfModuleInstance;
65   };
66
67   extractVfAccordingToVfModuleUuid(serviceId: string, UUIDData: Object): VfModule {
68     const vfModule = this.store.getState().service.serviceHierarchy[serviceId].vfModules[UUIDData['modelName']];
69     this.vfModuleModel = vfModule;
70     return vfModule;
71   }
72
73   getMacroFormControls(serviceId: string, vnfStoreKey: string, vfModuleStoreKey: string, uuidData: Object, isUpdateMode: boolean): FormControlModel[] {
74     this.isUpdateMode = isUpdateMode;
75     this.extractVfAccordingToVfModuleUuid(serviceId, uuidData);
76     if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vfModuleStoreKey)) {
77       if (isUpdateMode) {
78         this._logService.error('should provide serviceId, vfModuleStoreKey, vnfStoreKey', serviceId);
79         return [];
80       }
81     }
82
83     const vfModuleInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store, this.getVfModuleInstance(serviceId, vnfStoreKey, uuidData, isUpdateMode));
84     const vfModuleModel = this.vfModuleModel;
85     const vnf: VnfInstance = this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey];
86     const vnfModel = this.newVNFModel(serviceId, vnf);
87
88     let result: FormControlModel[] = [];
89
90     if (!_.isNil(vfModuleModel)) {
91       result = this.pushInstanceAndVGToForm(result, vfModuleInstance, serviceId, vnfModel, false);
92     }
93     if (this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
94       result = this._basicControlGenerator.concatSupplementaryFile(result, vfModuleInstance);
95     }
96     return result;
97   }
98
99   private newVNFModel(serviceId: string, vnf: VnfInstance) {
100     const vnfModelName: string = this._sharedTreeService.modelUniqueNameOrId(vnf);
101
102     const serviceModelFromHierarchy = this.store.getState().service.serviceHierarchy[serviceId];
103     const model = this._sharedTreeService.modelByIdentifiers(serviceModelFromHierarchy, "vnfs", vnfModelName);
104     return new VNFModel(model);
105   }
106
107   pushInstanceAndVGToForm(result: FormControlModel[], vfModuleElement: any, serviceId: string, vnfModel: any, isALaCarte: boolean): FormControlModel[] {
108     result.push(this.getInstanceName(vfModuleElement, serviceId, vnfModel.isEcompGeneratedNaming));
109     if (this.vfModuleModel.volumeGroupAllowed) {
110       result.push(this.getVolumeGroupData(vfModuleElement, serviceId, vnfModel.isEcompGeneratedNaming, isALaCarte));
111     }
112     return result;
113   }
114
115   getAlaCarteFormControls(serviceId: string, vnfStoreKey: string, vfModuleStoreKey: string, uuidData: Object, isUpdateMode: boolean): FormControlModel[] {
116     this.isUpdateMode = isUpdateMode;
117     this.extractVfAccordingToVfModuleUuid(serviceId, uuidData);
118     if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vfModuleStoreKey)) {
119       if (isUpdateMode) {
120         this._logService.error('should provide serviceId, vfModuleStoreKey, vnfStoreKey', serviceId);
121         return [];
122       }
123     }
124     const vnf: VnfInstance = this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey];
125     const vnfModel = this.newVNFModel(serviceId, vnf);
126     const vfModuleInstance = this._basicControlGenerator.retrieveInstanceIfUpdateMode(this.store, this.getVfModuleInstance(serviceId, vnfStoreKey, uuidData, isUpdateMode));
127     let result: FormControlModel[] = [];
128     this.pushInstanceAndVGToForm(result, vfModuleInstance, serviceId, vnfModel, true);
129     if( !this._featureFlagsService.getFlagState(Features.FLAG_2006_VFMODULE_TAKES_TENANT_AND_REGION_FROM_VNF)) {
130       result.push(this._sharedControllersService.getLcpRegionControl(serviceId, vfModuleInstance, result));
131       result.push(this._sharedControllersService.getLegacyRegion(vfModuleInstance));
132       result.push(this._sharedControllersService.getTenantControl(serviceId, vfModuleInstance));
133     }
134     result.push(this._sharedControllersService.getRollbackOnFailureControl(vfModuleInstance));
135     result.push(this._sharedControllersService.getSDNCControl(vfModuleInstance, false, this.getSdncExtraContents()));
136
137     if(this._featureFlagsService.getFlagState(Features.FLAG_2008_PAUSE_INSTANTIATION_ON_VFMODULE_POPUP)){
138       console.log(Features.FLAG_2008_PAUSE_INSTANTIATION_ON_VFMODULE_POPUP);
139       result.push(this._sharedControllersService.getPauseInstantiation(vfModuleInstance));
140     }
141     if (this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
142       result = this._basicControlGenerator.concatSupplementaryFile(result, vfModuleInstance);
143     }
144     return result;
145   }
146
147   getSdncExtraContents() : object[] {
148     return _.compact([
149       !!this.store.getState().global.flags['FLAG_2006_VFM_SDNC_PRELOAD_FILES'] ? {
150         type: 'UPLOAD_FILE',
151         dataTestId: 'sdnc_pereload_upload_link',
152         uploadMethod: (form: FormGroup) : Promise<boolean> => {
153           // this -> files item
154           return this._aaiService.sdncPreload().toPromise()
155             .then((response : boolean)=>{
156               return response;
157             }).catch(err => {
158               return false;
159             });
160         },
161         isDisabled: (form: FormGroup): boolean => {
162           return !form.controls[SDN_C_PRE_LOAD].value;
163         },
164         onSuccess: (form: FormGroup): void => {
165           MessageModal.showMessageModal({
166             text: 'The pre-load file(s) have been uploaded successfully.',
167             type: "success",
168             title: 'Success',
169             buttons: [{type: ButtonType.success, size: 'large', text: 'OK', closeModal: true}]
170           })
171         },
172         onFailed: (form: FormGroup) : void=> {
173           MessageModal.showMessageModal({
174             text: 'Failed to upload one or more of the files, please retry.',
175             type: "error",
176             title: 'Failure',
177             buttons: [{type: ButtonType.error, size: 'large', text: 'OK', closeModal: true}]
178           })
179         }
180       } : null
181     ]);
182   }
183
184
185   getInstanceName(instance: any, serviceId: string, isEcompGeneratedNaming: boolean): FormControlModel {
186     let formControlModel: FormControlModel = this._sharedControllersService.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, this.vfModuleModel);
187     formControlModel.onBlur = (event, form: FormGroup) => {
188       if (!_.isNil(form.controls['volumeGroupName']) && event.target.value.length > 0) {
189         form.controls['volumeGroupName'].setValue(event.target.value + "_vol");
190       }
191     };
192
193     return formControlModel;
194   }
195
196   getDefaultVolumeGroupName(instance: any, isEcompGeneratedNaming: boolean): string {
197     if ((!_.isNil(instance) && instance.volumeGroupName)) {
198       return instance.volumeGroupName;
199     }
200     if (isEcompGeneratedNaming) {
201       return null;
202     }
203     return this._basicControlGenerator.getDefaultInstanceName(instance, this.vfModuleModel) + "_vol";
204   }
205
206   getVolumeGroupData(instance: any, serviceId: string, isEcompGeneratedNaming: boolean, isALaCarte: boolean): FormControlModel {
207     let validations: ValidatorModel[] = [
208       new ValidatorModel(ValidatorOptions.pattern, 'Instance name may include only alphanumeric characters and underscore.', ControlGeneratorUtil.INSTANCE_NAME_REG_EX),
209       new ValidatorModel(CustomValidatorOptions.uniqueInstanceNameValidator, 'Volume Group instance name is already in use, please pick another name', [this.store, serviceId, instance && instance.volumeGroupName])
210     ];
211
212     return new InputFormControl({
213       controlName: 'volumeGroupName',
214       displayName: 'Volume Group Name',
215       dataTestId: 'volumeGroupName',
216       validations: validations,
217       tooltip: 'When filled, VID will create a Volume Group by this name and associate with this module.\n' +
218         'When empty, the module is created without a Volume Group.',
219       isVisible: this.shouldVGNameBeVisible(isEcompGeneratedNaming, isALaCarte),
220       value: this.getDefaultVolumeGroupName(instance, isEcompGeneratedNaming),
221       onKeypress: (event) => {
222         const pattern: RegExp = ControlGeneratorUtil.INSTANCE_NAME_REG_EX;
223         if (pattern) {
224           if (!pattern.test(event['key'])) {
225             event.preventDefault();
226           }
227         }
228         return event;
229       }
230     });
231   }
232
233   private shouldVGNameBeVisible(isEcompGeneratedNaming: boolean, isALaCarte: boolean) {
234     if ((!isALaCarte && !isEcompGeneratedNaming) || isALaCarte) {
235       return true;
236     }
237     return false;
238
239   }
240 }