1 import {Injectable} from "@angular/core";
 
   2 import {DropdownFormControl} from "../../../models/formControlModels/dropdownFormControl.model";
 
   3 import {FormGroup} from "@angular/forms";
 
   5   CustomValidatorOptions,
 
   9 } from "../../../models/formControlModels/formControl.model";
 
  10 import {InputFormControl} from "../../../models/formControlModels/inputFormControl.model";
 
  11 import {AppState} from "../../../store/reducers";
 
  12 import {NgRedux} from "@angular-redux/store";
 
  13 import {NumberFormControl} from "../../../models/formControlModels/numberFormControl.model";
 
  14 import {FormControlType} from "../../../models/formControlModels/formControlTypes.enum";
 
  15 import {FileFormControl} from "../../../models/formControlModels/fileFormControl.model";
 
  16 import {SelectOption} from "../../../models/selectOption";
 
  17 import {DynamicInputLabelPipe} from "../../../pipes/dynamicInputLabel/dynamic-input-label.pipe";
 
  18 import {FormGeneralErrorsService} from "../../formGeneralErrors/formGeneralErrors.service";
 
  19 import {Observable, of} from "rxjs";
 
  20 import {NodeModel} from "../../../models/nodeModel";
 
  21 import {Constants} from "../../../utils/constants";
 
  22 import {FileUnit} from "../../formControls/component/file/fileUnit.enum";
 
  23 import * as _ from 'lodash';
 
  25 export const SUPPLEMENTARY_FILE = 'supplementaryFile';
 
  26 export const SDN_C_PRE_LOAD = 'sdncPreLoad';
 
  27 export const PAUSE_INSTANTIATION = 'pauseInstantiation';
 
  30 export class ControlGeneratorUtil {
 
  32   public static readonly INSTANCE_NAME_REG_EX: RegExp = /^[a-zA-Z0-9._-]*$/;
 
  33   public static readonly GENERATED_NAME_REG_EX: RegExp = /[^a-zA-Z0-9._-]/g;
 
  35   constructor(private _store: NgRedux<AppState>) {
 
  38   getSubscribeResult(subscribeFunction: Function, control: DropdownFormControl): Observable<any> {
 
  39     return subscribeFunction(this).subscribe((res) => {
 
  40       control.options$ = res;
 
  41       control.hasEmptyOptions = res.length === 0;
 
  42       FormGeneralErrorsService.checkForErrorTrigger.next();
 
  47   getSubscribeInitResult(subscribeFunction: Function, control: DropdownFormControl, form: FormGroup): Observable<any> {
 
  48     return subscribeFunction(this).subscribe((res) => {
 
  49       if (!_.isNil(control['onInitSelectedField'])) {
 
  51         for (let key of control['onInitSelectedField']) {
 
  52           result = !_.isNil(result[key]) ? result[key] : [];
 
  54         control.options$ = result;
 
  55         control.hasEmptyOptions = _.isNil(result) || result.length === 0;
 
  57         control.options$ = !_.isNil(res) ? res : [];
 
  58         control.hasEmptyOptions = _.isNil(res) || res.length === 0;
 
  61       FormGeneralErrorsService.checkForErrorTrigger.next();
 
  66   isLegacyRegionShouldBeVisible(instance: any): boolean {
 
  67     if (!_.isNil(instance) && !_.isNil(instance.lcpCloudRegionId)) {
 
  68       return Constants.LegacyRegion.MEGA_REGION.indexOf(instance.lcpCloudRegionId) !== -1;
 
  73   createValidationsForInstanceName(instance: any, serviceId: string, isEcompGeneratedNaming: boolean): ValidatorModel[] {
 
  74     let validations: ValidatorModel[] = [
 
  75       new ValidatorModel(ValidatorOptions.pattern, 'Instance name may include only alphanumeric characters and underscore.', ControlGeneratorUtil.INSTANCE_NAME_REG_EX),
 
  76       new ValidatorModel(CustomValidatorOptions.uniqueInstanceNameValidator, 'some error', [this._store, serviceId, instance && instance.instanceName])
 
  78     if (!isEcompGeneratedNaming) {
 
  79       validations.push(new ValidatorModel(ValidatorOptions.required, 'is required'));
 
  84   getInputsOptions = (options: any[]): Observable<SelectOption[]> => {
 
  85     let optionList: SelectOption[] = [];
 
  86     options.forEach((option) => {
 
  87       optionList.push(new SelectOption({
 
  88         id: option.id || option.name,
 
  92     return of(optionList);
 
  95   getDynamicInputsByType(dynamicInputs: any, serviceModelId: string, storeKey: string, type: string): FormControlModel[] {
 
  96     let result: FormControlModel[] = [];
 
  98       let nodeInstance = null;
 
  99       if (_.has(this._store.getState().service.serviceInstance[serviceModelId][type], storeKey)) {
 
 100         nodeInstance = Object.assign({}, this._store.getState().service.serviceInstance[serviceModelId][type][storeKey]);
 
 102       result = this.getDynamicInputs(dynamicInputs, nodeInstance);
 
 107   getServiceDynamicInputs(dynamicInputs: any, serviceModelId: string): FormControlModel[] {
 
 108     let result: FormControlModel[] = [];
 
 110       let serviceInstance = null;
 
 111       if (_.has(this._store.getState().service.serviceInstance, serviceModelId)) {
 
 112         serviceInstance = Object.assign({}, this._store.getState().service.serviceInstance[serviceModelId]);
 
 114       result = this.getDynamicInputs(dynamicInputs, serviceInstance);
 
 119   getDynamicInputs(dynamicInputs: any, instance: any): FormControlModel[] {
 
 120     let result: FormControlModel[] = [];
 
 122       dynamicInputs.forEach((input) => {
 
 123         let validations: ValidatorModel[] = [];
 
 124         if (input.isRequired) {
 
 125           validations.push(new ValidatorModel(ValidatorOptions.required, 'is required'))
 
 127         if (input.minLength) {
 
 128           validations.push(new ValidatorModel(ValidatorOptions.minLength, '', input.minLength))
 
 130         if (input.maxLength) {
 
 131           validations.push(new ValidatorModel(ValidatorOptions.maxLength, '', input.maxLength))
 
 134         let dynamicInputLabelPipe: DynamicInputLabelPipe = new DynamicInputLabelPipe();
 
 136           controlName: input.name,
 
 137           displayName: dynamicInputLabelPipe.transform(input.name).slice(0, -1),
 
 138           dataTestId: input.id,
 
 139           placeHolder: input.prompt,
 
 140           tooltip: input.description,
 
 141           validations: validations,
 
 142           isVisible: input.isVisible,
 
 143           value: !_.isNil(instance) && !_.isNil(instance.instanceParams) && instance.instanceParams.length > 0 ? instance.instanceParams[0][input.name] : input.value
 
 146         switch (input.type) {
 
 149             data.value = data.value || input.optionList.filter((option) => option.isDefault ? option.id || option.name : null);
 
 150             data.onInit = this.getSubscribeInitResult.bind(null, this.getInputsOptions.bind(this, input.optionList));
 
 151             result.push(new DropdownFormControl(data));
 
 155             data.type = FormControlType.CHECKBOX;
 
 156             result.push(new FormControlModel(data));
 
 160             data.min = input.min;
 
 161             data.max = input.max;
 
 162             result.push(new NumberFormControl(data));
 
 166             result.push(new FileFormControl(data));
 
 170             result.push(new InputFormControl(data));
 
 179   getDefaultInstanceName(instance: any, model: NodeModel): string {
 
 180     const initialInstanceName = (!_.isNil(instance) && instance.instanceName) || (!_.isNil(model.name) ? model.name.replace(ControlGeneratorUtil.GENERATED_NAME_REG_EX, "") : model.name);
 
 181     return initialInstanceName;
 
 184   concatSupplementaryFile(originalArray: FormControlModel[], vfModuleInstance): FormControlModel[] {
 
 185     let suppFileInput: FileFormControl = <FileFormControl>(this.getSupplementaryFile(vfModuleInstance));
 
 186     return originalArray.concat([suppFileInput], suppFileInput.hiddenFile);
 
 189   getSupplementaryFile(instance: any): FileFormControl {
 
 190     return new FileFormControl({
 
 191       controlName: SUPPLEMENTARY_FILE,
 
 192       displayName: 'Supplementary Data File (JSON format)',
 
 193       dataTestId: 'SupplementaryFile',
 
 194       placeHolder: 'Choose file',
 
 195       selectedFile: !_.isNil(instance) ? instance.supplementaryFileName : null,
 
 197       acceptedExtentions: "application/json",
 
 198       hiddenFile: [new InputFormControl({
 
 199         controlName: SUPPLEMENTARY_FILE + "_hidden",
 
 201         validations: [new ValidatorModel(CustomValidatorOptions.isFileTooBig, "File size exceeds 5MB.", [FileUnit.MB, 5])]
 
 203         new InputFormControl({
 
 204           controlName: SUPPLEMENTARY_FILE + "_hidden_content",
 
 206           validations: [new ValidatorModel(CustomValidatorOptions.isValidJson,
 
 207             "File is invalid, please make sure a legal JSON file is uploaded using name:value pairs.", []),
 
 208             new ValidatorModel(CustomValidatorOptions.isStringContainTags,
 
 209               "File is invalid, please remove tags <>.", [])],
 
 210           value: !_.isNil(instance) ? (instance.supplementaryFile_hidden_content) : null,
 
 213       onDelete: this.getOnDeleteForSupplementaryFile(),
 
 214       onChange: this.getOnChangeForSupplementaryFile()
 
 218   retrieveInstanceIfUpdateMode(store: NgRedux<AppState>, instance: any): any {
 
 219     return store.getState().global.isUpdateModalMode ? instance : null;
 
 222   private getOnDeleteForSupplementaryFile() {
 
 223     return (form: FormGroup) => {
 
 224       form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(null);
 
 225       form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null);
 
 229   private getOnChangeForSupplementaryFile() {
 
 230     return (files: FileList, form: FormGroup) => {
 
 231       if (files.length > 0) {
 
 232         const file = files.item(0);
 
 233         let reader = new FileReader();
 
 234         reader.onload = function (event) {
 
 235           form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(reader.result);
 
 236           form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(file);
 
 238         reader.readAsText(file);
 
 240         form.controls[SUPPLEMENTARY_FILE + "_hidden"].setValue(null);
 
 241         form.controls[SUPPLEMENTARY_FILE + "_hidden_content"].setValue(null);
 
 246   getRollBackOnFailureOptions = (): Observable<SelectOption[]> => {
 
 248       new SelectOption({id: 'true', name: 'Rollback'}),
 
 249       new SelectOption({id: 'false', name: 'Don\'t Rollback'})