All files / src/app/ceph/pool/erasure-code-profile-form erasure-code-profile-form.component.ts

98.32% Statements 117/119
83.67% Branches 41/49
95.83% Functions 23/24
98.15% Lines 106/108

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 2602x 2x   2x 2x   2x 2x 2x   2x 2x 2x 2x             2x   32x             32x 32x 32x   32x           32x         32x 32x 32x 32x 32x 32x   32x 32x 32x 32x     32x 32x               113x                                         32x     2x 22x 22x 1x 21x 7x 14x 7x 7x 7x       2x 115x 115x 87x   115x     54x 108x     2x 33x 33x       33x 33x                     2x 7x 7x 7x 7x             2x 7x 7x 7x       7x     2x 7x 7x 7x             54x 54x 122x 122x         32x 32x       32x 32x 32x 32x 32x               32x 32x 32x 32x 32x         12x 12x             12x 12x 12x   156x 156x 156x 156x             44x   12x     2x 44x             44x     13x 13x 1x 1x   12x 12x               12x               2x  
import { Component, EventEmitter, OnInit, Output } from '@angular/core';
import { Validators } from '@angular/forms';
 
import { I18n } from '@ngx-translate/i18n-polyfill';
import { BsModalRef } from 'ngx-bootstrap/modal';
 
import { ErasureCodeProfileService } from '../../../shared/api/erasure-code-profile.service';
import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
import { CdFormGroup } from '../../../shared/forms/cd-form-group';
import { CdValidators } from '../../../shared/forms/cd-validators';
import { ErasureCodeProfile } from '../../../shared/models/erasure-code-profile';
import { FinishedTask } from '../../../shared/models/finished-task';
import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
 
@Component({
  selector: 'cd-erasure-code-profile-form',
  template: require('./erasure-code-profile-form.component.html'),
  styles: []
})
export class ErasureCodeProfileFormComponent implements OnInit {
  @Output()
  submitAction = new EventEmitter();
 
  form: CdFormGroup;
  failureDomains: string[];
  plugins: string[];
  names: string[];
  techniques: string[];
  requiredControls: string[] = [];
  devices: string[] = [];
  tooltips = this.ecpService.formTooltips;
 
  PLUGIN = {
    LRC: 'lrc', // Locally Repairable Erasure Code
    SHEC: 'shec', // Shingled Erasure Code
    JERASURE: 'jerasure', // default
    ISA: 'isa' // Intel Storage Acceleration
  };
  plugin = this.PLUGIN.JERASURE;
  action: string;
  resource: string;
 
  constructor(
    private formBuilder: CdFormBuilder,
    public bsModalRef: BsModalRef,
    private taskWrapper: TaskWrapperService,
    private ecpService: ErasureCodeProfileService,
    private i18n: I18n,
    public actionLabels: ActionLabelsI18n
  ) {
    this.action = this.actionLabels.CREATE;
    this.resource = this.i18n('EC Profile');
    this.createForm();
    this.setJerasureDefaults();
  }
 
  createForm() {
    this.form = this.formBuilder.group({
      name: [
        null,
        [
          Validators.required,
          Validators.pattern('[A-Za-z0-9_-]+'),
          CdValidators.custom(
            'uniqueName',
            (value) => this.names && this.names.indexOf(value) !== -1
          )
        ]
      ],
      plugin: [this.PLUGIN.JERASURE, [Validators.required]],
      k: [1], // Will be replaced by plugin defaults
      m: [1], // Will be replaced by plugin defaults
      crushFailureDomain: ['host'],
      crushRoot: ['default'], // default for all - is a list possible???
      crushDeviceClass: [''], // set none to empty at submit - get list from configs?
      directory: [''],
      // Only for 'jerasure' and 'isa' use
      technique: ['reed_sol_van'],
      // Only for 'jerasure' use
      packetSize: [2048, [Validators.min(1)]],
      // Only for 'lrc' use
      l: [1, [Validators.required, Validators.min(1)]],
      crushLocality: [''], // set to none at the end (same list as for failure domains)
      // Only for 'shec' use
      c: [1, [Validators.required, Validators.min(1)]]
    });
    this.form.get('plugin').valueChanges.subscribe((plugin) => this.onPluginChange(plugin));
  }
 
  onPluginChange(plugin) {
    this.plugin = plugin;
    if (plugin === this.PLUGIN.JERASURE) {
      this.setJerasureDefaults();
    } else if (plugin === this.PLUGIN.LRC) {
      this.setLrcDefaults();
    } else if (plugin === this.PLUGIN.ISA) {
      this.setIsaDefaults();
    } else Eif (plugin === this.PLUGIN.SHEC) {
      this.setShecDefaults();
    }
  }
 
  private setNumberValidators(name: string, required: boolean) {
    const validators = [Validators.min(1)];
    if (required) {
      validators.push(Validators.required);
    }
    this.form.get(name).setValidators(validators);
  }
 
  private setKMValidators(required: boolean) {
    ['k', 'm'].forEach((name) => this.setNumberValidators(name, required));
  }
 
  private setJerasureDefaults() {
    this.requiredControls = ['k', 'm'];
    this.setDefaults({
      k: 4,
      m: 2
    });
    this.setKMValidators(true);
    this.techniques = [
      'reed_sol_van',
      'reed_sol_r6_op',
      'cauchy_orig',
      'cauchy_good',
      'liberation',
      'blaum_roth',
      'liber8tion'
    ];
  }
 
  private setLrcDefaults() {
    this.requiredControls = ['k', 'm', 'l'];
    this.setKMValidators(true);
    this.setNumberValidators('l', true);
    this.setDefaults({
      k: 4,
      m: 2,
      l: 3
    });
  }
 
  private setIsaDefaults() {
    this.requiredControls = [];
    this.setKMValidators(false);
    this.setDefaults({
      k: 7,
      m: 3
    });
    this.techniques = ['reed_sol_van', 'cauchy'];
  }
 
  private setShecDefaults() {
    this.requiredControls = [];
    this.setKMValidators(false);
    this.setDefaults({
      k: 4,
      m: 3,
      c: 2
    });
  }
 
  private setDefaults(defaults: object) {
    Object.keys(defaults).forEach((controlName) => {
      Eif (this.form.get(controlName).pristine) {
        this.form.silentSet(controlName, defaults[controlName]);
      }
    });
  }
 
  ngOnInit() {
    this.ecpService
      .getInfo()
      .subscribe(
        ({
          failure_domains,
          plugins,
          names,
          directory,
          devices
        }: {
          failure_domains: string[];
          plugins: string[];
          names: string[];
          directory: string;
          devices: string[];
        }) => {
          this.failureDomains = failure_domains;
          this.plugins = plugins;
          this.names = names;
          this.devices = devices;
          this.form.silentSet('directory', directory);
        }
      );
  }
 
  private createJson() {
    const pluginControls = {
      technique: [this.PLUGIN.ISA, this.PLUGIN.JERASURE],
      packetSize: [this.PLUGIN.JERASURE],
      l: [this.PLUGIN.LRC],
      crushLocality: [this.PLUGIN.LRC],
      c: [this.PLUGIN.SHEC]
    };
    const ecp = new ErasureCodeProfile();
    const plugin = this.form.getValue('plugin');
    Object.keys(this.form.controls)
      .filter((name) => {
        const pluginControl = pluginControls[name];
        const control = this.form.get(name);
        const usable = (pluginControl && pluginControl.includes(plugin)) || !pluginControl;
        return (
          usable &&
          (control.dirty || this.requiredControls.includes(name)) &&
          this.form.getValue(name)
        );
      })
      .forEach((name) => {
        this.extendJson(name, ecp);
      });
    return ecp;
  }
 
  private extendJson(name: string, ecp: ErasureCodeProfile) {
    const differentApiAttributes = {
      crushFailureDomain: 'crush-failure-domain',
      crushRoot: 'crush-root',
      crushDeviceClass: 'crush-device-class',
      packetSize: 'packetsize',
      crushLocality: 'crush-locality'
    };
    ecp[differentApiAttributes[name] || name] = this.form.getValue(name);
  }
 
  onSubmit() {
    if (this.form.invalid) {
      this.form.setErrors({ cdSubmitButton: true });
      return;
    }
    const profile = this.createJson();
    this.taskWrapper
      .wrapTaskAroundCall({
        task: new FinishedTask('ecp/create', { name: profile.name }),
        call: this.ecpService.create(profile)
      })
      .subscribe(
        undefined,
        () => {
          this.form.setErrors({ cdSubmitButton: true });
        },
        () => {
          this.bsModalRef.hide();
          this.submitAction.emit(profile);
        }
      );
  }
}