aboutsummaryrefslogtreecommitdiffstats
path: root/vid-webpack-master/src/app/shared/components/genericForm/formControlsServices/vfModuleGenerator/vfModule.control.generator.ts
blob: 3012c139c3b35afe0b665b7271a70f795b0e139a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import {Injectable} from "@angular/core";
import {GenericFormService} from "../../generic-form.service";
import {AaiService} from "../../../../services/aaiService/aai.service";
import {NgRedux} from "@angular-redux/store";
import {HttpClient} from "@angular/common/http";
import {BasicControlGenerator} from "../basic.control.generator";
import * as _ from 'lodash';
import {Observable, of} from "rxjs";

import {
  CustomValidatorOptions,
  FormControlModel,
  ValidatorModel,
  ValidatorOptions
} from "../../../../models/formControlModels/formControl.model";
import {LogService} from "../../../../utils/log/log.service";
import {AppState} from "../../../../store/reducers";
import {FormGroup} from "@angular/forms";
import {DropdownFormControl} from "../../../../models/formControlModels/dropdownFormControl.model";
import {FormControlType} from "../../../../models/formControlModels/formControlTypes.enum";
import {InputFormControl} from "../../../../models/formControlModels/inputFormControl.model";
import {SelectOption} from "../../../../models/selectOption";
import {VfModuleInstance} from "../../../../models/vfModuleInstance";
import {VfModule} from "../../../../models/vfModule";
import {VNFModel} from "../../../../models/vnfModel";
import {VnfInstance} from "../../../../models/vnfInstance";
import {FileFormControl} from "../../../../models/formControlModels/fileFormControl.model";
import {CheckboxFormControl} from "../../../../models/formControlModels/checkboxFormControl.model";
import {FileUnit} from "../../../formControls/component/file/fileUnit.enum";
import {Constants} from "../../../../utils/constants";


export enum FormControlNames {
  INSTANCE_NAME = 'instanceName',
  VOLUME_GROUP_NAME = 'volumeGroupName',
  LCPCLOUD_REGION_ID = 'lcpCloudRegionId',
  LEGACY_REGION = 'legacyRegion',
  TENANT_ID = 'tenantId',
  ROLLBACK_ON_FAILURE = 'rollbackOnFailure',
  SDN_C_PRE_LOAD = 'sdncPreLoad',
  SUPPLEMENTARY_FILE = 'supplementaryFile'
}


@Injectable()
export class VfModuleControlGenerator {
  aaiService: AaiService;
  vfModuleModel: VfModule;
  vfModuleName : string;
  isUpdateMode : boolean;

  constructor(private genericFormService: GenericFormService,
              private _basicControlGenerator: BasicControlGenerator,
              private store: NgRedux<AppState>,
              private http: HttpClient,
              private _aaiService: AaiService,
              private _logService: LogService) {
    this.aaiService = _aaiService;
  }

  setVFModuleStoreKey = (serviceId: string, vfModuleUuid: string) => {
      const vfModules = this.store.getState().service.serviceHierarchy[serviceId].vfModules;
      const vfModulesKeys = Object.keys(vfModules);
      for(let key of  vfModulesKeys){
        if(vfModules[key].uuid === vfModuleUuid){
          this.vfModuleName = key;
          return;
        }
      }
  };


  getVfModuleInstance = (serviceId: string, vnfStoreKey: string, UUIDData: Object, isUpdateMode: boolean): VfModuleInstance => {
    let vfModuleInstance: VfModuleInstance = null;
    if (isUpdateMode && this.store.getState().service.serviceInstance[serviceId] &&
      _.has(this.store.getState().service.serviceInstance[serviceId].vnfs, vnfStoreKey) &&
      _.has(this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey].vfModules, UUIDData['modelName'])) {
       vfModuleInstance = Object.assign({},this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey].vfModules[UUIDData['modelName']][UUIDData['vFModuleStoreKey']]);
    }
    return vfModuleInstance;
  };

  extractVfAccordingToVfModuleUuid(serviceId: string, UUIDData: Object): VfModule {
    const vfModule = this.store.getState().service.serviceHierarchy[serviceId].vfModules[UUIDData['modelName']];
    this.vfModuleModel = vfModule;
    return vfModule;
  }

  getMacroFormControls(serviceId: string, vnfStoreKey: string, vfModuleStoreKey: string, uuidData : Object, isUpdateMode: boolean): FormControlModel[] {
    this.isUpdateMode = isUpdateMode;
    this.extractVfAccordingToVfModuleUuid(serviceId, uuidData);
    if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vfModuleStoreKey)) {
      if(isUpdateMode){
        this._logService.error('should provide serviceId, vfModuleStoreKey, vnfStoreKey', serviceId);
        return [];
      }
    }

    const vfModuleInstance = this.getVfModuleInstance(serviceId, vnfStoreKey, uuidData, isUpdateMode);
    const vfModuleModel = this.vfModuleModel;
    const vnf: VnfInstance = this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey];
    const vnfModelName: string = vnf.originalName;
    const vnfModel = new VNFModel(this.store.getState().service.serviceHierarchy[serviceId].vnfs[vnfModelName]);

    let result: FormControlModel[] = [];

    if (!_.isNil(vfModuleModel)) {
      result.push(this.getInstanceName(vfModuleInstance, serviceId, vnfModel.isEcompGeneratedNaming));
      if (this.vfModuleModel.volumeGroupAllowed) {
        result.push(this.getVolumeGroupName(vfModuleInstance, serviceId, vnfStoreKey, vfModuleInstance && vfModuleInstance.volumeGroupName, vnfModel.isEcompGeneratedNaming));
     }
    }
    if(this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
      let suppFileInput:FileFormControl = <FileFormControl>(this.getSupplementaryFile(vfModuleInstance));
      result.push(suppFileInput);
      result = result.concat(suppFileInput.hiddenFile);
    }
    return result;
  }

  getAlaCarteFormControls(serviceId: string, vnfStoreKey: string, vfModuleStoreKey: string, uuidData : Object, isUpdateMode: boolean): FormControlModel[] {
    this.isUpdateMode = isUpdateMode;
    this.extractVfAccordingToVfModuleUuid(serviceId, uuidData);
    if (_.isNil(serviceId) || _.isNil(vnfStoreKey) || _.isNil(vfModuleStoreKey)) {
      if(isUpdateMode){
        this._logService.error('should provide serviceId, vfModuleStoreKey, vnfStoreKey', serviceId);
        return [];
      }
    }
    const vnf: VnfInstance = this.store.getState().service.serviceInstance[serviceId].vnfs[vnfStoreKey];
    const vnfModelName: string = vnf.originalName;
    const vnfModel = new VNFModel(this.store.getState().service.serviceHierarchy[serviceId].vnfs[vnfModelName]);

    const vfModuleInstance = this.getVfModuleInstance(serviceId, vnfStoreKey, uuidData, isUpdateMode);
    let result: FormControlModel[] = [];
    result.push(this.getInstanceName(vfModuleInstance, serviceId, vnfModel.isEcompGeneratedNaming));

    if (this.vfModuleModel.volumeGroupAllowed) {
      result.push(this.getVolumeGroupName(vfModuleInstance, serviceId, vnfStoreKey, this.vfModuleName, vnfModel.isEcompGeneratedNaming));
    }
    result.push(this.getLcpRegionControl(serviceId, vfModuleInstance, result));
    result.push(this._basicControlGenerator.getLegacyRegion(vfModuleInstance));
    result.push(this.getTenantControl(serviceId, vfModuleInstance, result));
    result.push(this.getRollbackOnFailureControl(vfModuleInstance, result));
    result.push(this.getSDNCControl(vfModuleInstance, result));
    if(this.store.getState().global.flags['FLAG_SUPPLEMENTARY_FILE']) {
      let suppFileInput:FileFormControl = <FileFormControl>(this.getSupplementaryFile(vfModuleInstance));
      result.push(suppFileInput);
      result = result.concat(suppFileInput.hiddenFile);
    }
    return result;

  }

  getInstanceName(instance: any, serviceId: string, isEcompGeneratedNaming: boolean): FormControlModel {
    let formControlModel:FormControlModel = this._basicControlGenerator.getInstanceNameController(instance, serviceId, isEcompGeneratedNaming, this.vfModuleModel);
    formControlModel.onBlur = (event, form : FormGroup) => {
        if(!_.isNil(form.controls['volumeGroupName'])&& event.target.value.length > 0){
          form.controls['volumeGroupName'].setValue(event.target.value + "_vol");
        }
      };

    return formControlModel;
  }

  getDefaultVolumeGroupName(instance: any, vfModuleName: string, isEcompGeneratedNaming: boolean): string {
    if ((!_.isNil(instance) && instance.volumeGroupName))  {
      return instance.volumeGroupName;
    }
    if (isEcompGeneratedNaming) {
      return null;
    }
    return this._basicControlGenerator.getDefaultInstanceName(instance, this.vfModuleModel) + "_vol";
  }

  getVolumeGroupName(instance: any, serviceId: string, vnfStoreKey: string, vfModuleName: string, isEcompGeneratedNaming: boolean): FormControlModel {
    let validations: ValidatorModel[] = [
      new ValidatorModel(ValidatorOptions.pattern, 'Instance name may include only alphanumeric characters and underscore.', BasicControlGenerator.INSTANCE_NAME_REG_EX),
      new ValidatorModel(CustomValidatorOptions.uniqueInstanceNameValidator, 'Volume Group instance name is already in use, please pick another name', [this.store, serviceId, instance && instance.volumeGroupName])
    ];
    // comment out because if not provided vid won't create VG
    // if (!isEcompGeneratedNaming)  {
    //   validations.push(new ValidatorModel(ValidatorOptions.required, 'is required'));
    // }
    return new InputFormControl({
      controlName: 'volumeGroupName',
      displayName: 'Volume Group Name',
      dataTestId: 'volumeGroupName',
      // placeHolder: (!isEcompGeneratedNaming) ? 'Volume Group Name' : 'Automatically generated when not provided',
      validations: validations,
      tooltip : 'When filled, VID will create a Volume Group by this name and associate with this module.\n' +
                'When empty, the module is created without a Volume Group.',
      isVisible: true,
      value: this.getDefaultVolumeGroupName(instance, vfModuleName, isEcompGeneratedNaming),
      onKeypress: (event) => {
        const pattern:RegExp = BasicControlGenerator.INSTANCE_NAME_REG_EX;
        if (pattern) {
          if (!pattern.test(event['key'])) {
            event.preventDefault();
          }
        }
        return event;
      }
    });
  }

  getSupplementaryFile(instance: any): FormControlModel {
    return new FileFormControl({
      controlName: FormControlNames.SUPPLEMENTARY_FILE,
      displayName: 'Supplementary Data File (JSON format)',
      dataTestId: 'SupplementaryFile',
      placeHolder: 'Choose file',
      selectedFile:  !_.isNil(instance) ? instance.supplementaryFileName: null,
      isVisible: true,
      acceptedExtentions: "application/json",
      hiddenFile : [new InputFormControl({
        controlName: FormControlNames.SUPPLEMENTARY_FILE + "_hidden",
        isVisible: false,
        validations: [new ValidatorModel(CustomValidatorOptions.isFileTooBig, "File size exceeds 5MB.", [FileUnit.MB, 5])]
      }),
        new InputFormControl({
          controlName: FormControlNames.SUPPLEMENTARY_FILE + "_hidden_content",
          isVisible: false,
          validations: [new ValidatorModel(CustomValidatorOptions.isValidJson,
            "File is invalid, please make sure a legal JSON file is uploaded using name:value pairs.",[]),
            new ValidatorModel(CustomValidatorOptions.isStringContainTags,
              "File is invalid, please remove tags <>.",[])],
          value: !_.isNil(instance) ? (instance.supplementaryFile_hidden_content): null,
        })
      ],
      onDelete : (form : FormGroup) => {
        form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden"].setValue(null);
        form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null);
      },
      onChange : (files: FileList, form : FormGroup)  => {
        if (files.length > 0) {
          const file = files.item(0);
          let reader = new FileReader();
          reader.onload = function(event) {
            form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden_content"].setValue(reader.result);
            form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden"].setValue(file);
          };
          reader.readAsText(file);
        }
        else {
          form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden"].setValue(null);
          form.controls[FormControlNames.SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null);
        }
      }
    })
  };

  getTenantControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
    const service = this.store.getState().service.serviceInstance[serviceId];
    const globalCustomerId: string = service.globalSubscriberId;
    const serviceType: string = service.subscriptionServiceType;
    return new DropdownFormControl({
      type: FormControlType.DROPDOWN,
      controlName: FormControlNames.TENANT_ID,
      displayName: 'Tenant',
      dataTestId: 'tenant',
      placeHolder: 'Select Tenant',
      name: "tenant",
      isDisabled: _.isNil(instance) || _.isNil(instance.lcpCloudRegionId),
      onInitSelectedField: instance ? ['lcpRegionsTenantsMap', instance.lcpCloudRegionId] : null,
      value: instance ? instance.tenantId : null,
      validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
      onInit: instance ? this._basicControlGenerator.getSubscribeInitResult.bind(
        this._aaiService,
        this.aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)) : () => {
      },
    })
  };

  getLcpRegionControl = (serviceId: string, instance: any, controls: FormControlModel[]): DropdownFormControl => {
    const service = this.store.getState().service.serviceInstance[serviceId];
    const globalCustomerId: string = service.globalSubscriberId;
    const serviceType: string = service.subscriptionServiceType;
    return new DropdownFormControl({
      type: FormControlType.DROPDOWN,
      controlName: 'lcpCloudRegionId',
      displayName: 'LCP region',
      dataTestId: 'lcpRegion',
      placeHolder: 'Select LCP Region',
      name: "lcpRegion",
      isDisabled: false,
      value: instance ? instance.lcpCloudRegionId : null,
      validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
      onInitSelectedField: ['lcpRegionList'],
      onInit: this._basicControlGenerator.getSubscribeInitResult.bind(
        this._aaiService,
        this._aaiService.getLcpRegionsAndTenants.bind(this, globalCustomerId, serviceType)),
      onChange: (param: string, form: FormGroup) => {
        form.controls[FormControlNames.TENANT_ID].enable();
        form.controls[FormControlNames.TENANT_ID].reset();
        if (!_.isNil(globalCustomerId) && !_.isNil(serviceType)) {
          this._basicControlGenerator.getSubscribeResult.bind(this, this._aaiService.getLcpRegionsAndTenants(globalCustomerId, serviceType).subscribe(res => {
            controls.find(item => item.controlName === FormControlNames.TENANT_ID)['options$'] = res.lcpRegionsTenantsMap[param];
            if (res.lcpRegionsTenantsMap[param]) {
              controls.find(item => item.controlName === FormControlNames.TENANT_ID)['hasEmptyOptions'] = res.lcpRegionsTenantsMap[param].length === 0;
            }
          }));
        }

        if (Constants.LegacyRegion.MEGA_REGION.indexOf(param) !== -1) {
          form.controls['legacyRegion'].enable();
          controls.find(item => item.controlName === 'legacyRegion').isVisible = true;

        } else {
          controls.find(item => item.controlName === 'legacyRegion').isVisible = false;
          form.controls['legacyRegion'].setValue(null);
          form.controls['legacyRegion'].reset();
          form.controls['legacyRegion'].disable();
        }
      }
    })
  };

  getSDNCControl = (instance: any, controls: FormControlModel[]): CheckboxFormControl => {
    return new CheckboxFormControl({
      type: FormControlType.CHECKBOX,
      controlName: 'sdncPreLoad',
      displayName: 'SDN-C pre-load',
      dataTestId: 'sdncPreLoad',
      value: instance ? instance.sdncPreLoad : false,
      validations: [new ValidatorModel(ValidatorOptions.required, 'is required')]
    })
  };

  getRollbackOnFailureControl = (instance: any, controls: FormControlModel[]): DropdownFormControl => {
    return new DropdownFormControl({
      type: FormControlType.DROPDOWN,
      controlName: FormControlNames.ROLLBACK_ON_FAILURE,
      displayName: 'Rollback on failure',
      dataTestId: 'rollback',
      isDisabled: false,
      validations: [new ValidatorModel(ValidatorOptions.required, 'is required')],
      value: instance ? instance.rollbackOnFailure : 'true',
      onInit: this._basicControlGenerator.getSubscribeInitResult.bind(null, this.getRollBackOnFailureOptions)
    })
  };

  getRollBackOnFailureOptions = (): Observable<SelectOption[]> => {
    return of([
      new SelectOption({id: 'true', name: 'Rollback'}),
      new SelectOption({id: 'false', name: 'Don\'t Rollback'})
    ]);
  };
}