1 import { Component, EventEmitter, OnInit, Output } from '@angular/core';
2 import { Validators } from '@angular/forms';
4 import { I18n } from '@ngx-translate/i18n-polyfill';
5 import { BsModalRef } from 'ngx-bootstrap/modal';
7 import { ErasureCodeProfileService } from '../../../shared/api/erasure-code-profile.service';
8 import { ActionLabelsI18n } from '../../../shared/constants/app.constants';
9 import { CdFormBuilder } from '../../../shared/forms/cd-form-builder';
10 import { CdFormGroup } from '../../../shared/forms/cd-form-group';
11 import { CdValidators } from '../../../shared/forms/cd-validators';
12 import { ErasureCodeProfile } from '../../../shared/models/erasure-code-profile';
13 import { FinishedTask } from '../../../shared/models/finished-task';
14 import { TaskWrapperService } from '../../../shared/services/task-wrapper.service';
17 selector: 'cd-erasure-code-profile-form-modal',
18 templateUrl: './erasure-code-profile-form-modal.component.html',
19 styleUrls: ['./erasure-code-profile-form-modal.component.scss']
21 export class ErasureCodeProfileFormModalComponent implements OnInit {
23 submitAction = new EventEmitter();
26 failureDomains: string[];
30 requiredControls: string[] = [];
31 devices: string[] = [];
32 tooltips = this.ecpService.formTooltips;
35 LRC: 'lrc', // Locally Repairable Erasure Code
36 SHEC: 'shec', // Shingled Erasure Code
37 JERASURE: 'jerasure', // default
38 ISA: 'isa' // Intel Storage Acceleration
40 plugin = this.PLUGIN.JERASURE;
45 private formBuilder: CdFormBuilder,
46 public bsModalRef: BsModalRef,
47 private taskWrapper: TaskWrapperService,
48 private ecpService: ErasureCodeProfileService,
50 public actionLabels: ActionLabelsI18n
52 this.action = this.actionLabels.CREATE;
53 this.resource = this.i18n('EC Profile');
55 this.setJerasureDefaults();
59 this.form = this.formBuilder.group({
64 Validators.pattern('[A-Za-z0-9_-]+'),
67 (value: string) => this.names && this.names.indexOf(value) !== -1
71 plugin: [this.PLUGIN.JERASURE, [Validators.required]],
72 k: [1], // Will be replaced by plugin defaults
73 m: [1], // Will be replaced by plugin defaults
74 crushFailureDomain: ['host'],
75 crushRoot: ['default'], // default for all - is a list possible???
76 crushDeviceClass: [''], // set none to empty at submit - get list from configs?
78 // Only for 'jerasure' and 'isa' use
79 technique: ['reed_sol_van'],
80 // Only for 'jerasure' use
81 packetSize: [2048, [Validators.min(1)]],
83 l: [1, [Validators.required, Validators.min(1)]],
84 crushLocality: [''], // set to none at the end (same list as for failure domains)
85 // Only for 'shec' use
86 c: [1, [Validators.required, Validators.min(1)]]
88 this.form.get('plugin').valueChanges.subscribe((plugin) => this.onPluginChange(plugin));
91 onPluginChange(plugin: string) {
93 if (plugin === this.PLUGIN.JERASURE) {
94 this.setJerasureDefaults();
95 } else if (plugin === this.PLUGIN.LRC) {
96 this.setLrcDefaults();
97 } else if (plugin === this.PLUGIN.ISA) {
98 this.setIsaDefaults();
99 } else if (plugin === this.PLUGIN.SHEC) {
100 this.setShecDefaults();
104 private setNumberValidators(name: string, required: boolean) {
105 const validators = [Validators.min(1)];
107 validators.push(Validators.required);
109 this.form.get(name).setValidators(validators);
112 private setKMValidators(required: boolean) {
113 ['k', 'm'].forEach((name) => this.setNumberValidators(name, required));
116 private setJerasureDefaults() {
117 this.requiredControls = ['k', 'm'];
122 this.setKMValidators(true);
134 private setLrcDefaults() {
135 this.requiredControls = ['k', 'm', 'l'];
136 this.setKMValidators(true);
137 this.setNumberValidators('l', true);
145 private setIsaDefaults() {
146 this.requiredControls = [];
147 this.setKMValidators(false);
152 this.techniques = ['reed_sol_van', 'cauchy'];
155 private setShecDefaults() {
156 this.requiredControls = [];
157 this.setKMValidators(false);
165 private setDefaults(defaults: object) {
166 Object.keys(defaults).forEach((controlName) => {
167 if (this.form.get(controlName).pristine) {
168 this.form.silentSet(controlName, defaults[controlName]);
184 failure_domains: string[];
190 this.failureDomains = failure_domains;
191 this.plugins = plugins;
193 this.devices = devices;
194 this.form.silentSet('directory', directory);
199 private createJson() {
200 const pluginControls = {
201 technique: [this.PLUGIN.ISA, this.PLUGIN.JERASURE],
202 packetSize: [this.PLUGIN.JERASURE],
203 l: [this.PLUGIN.LRC],
204 crushLocality: [this.PLUGIN.LRC],
205 c: [this.PLUGIN.SHEC]
207 const ecp = new ErasureCodeProfile();
208 const plugin = this.form.getValue('plugin');
209 Object.keys(this.form.controls)
211 const pluginControl = pluginControls[name];
212 const control = this.form.get(name);
213 const usable = (pluginControl && pluginControl.includes(plugin)) || !pluginControl;
216 (control.dirty || this.requiredControls.includes(name)) &&
217 this.form.getValue(name)
221 this.extendJson(name, ecp);
226 private extendJson(name: string, ecp: ErasureCodeProfile) {
227 const differentApiAttributes = {
228 crushFailureDomain: 'crush-failure-domain',
229 crushRoot: 'crush-root',
230 crushDeviceClass: 'crush-device-class',
231 packetSize: 'packetsize',
232 crushLocality: 'crush-locality'
234 ecp[differentApiAttributes[name] || name] = this.form.getValue(name);
238 if (this.form.invalid) {
239 this.form.setErrors({ cdSubmitButton: true });
242 const profile = this.createJson();
244 .wrapTaskAroundCall({
245 task: new FinishedTask('ecp/create', { name: profile.name }),
246 call: this.ecpService.create(profile)
251 this.form.setErrors({ cdSubmitButton: true });
254 this.bsModalRef.hide();
255 this.submitAction.emit(profile);