blob: c177568f2880ac6da5787b71e6d5bf774234872b [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolod58d5042016-12-16 16:59:21 -080019import * as _ from 'lodash';
20import * as pluralize from 'pluralize';
Matteo Scandolocb466ed2017-01-04 17:16:24 -080021import {IXosTableColumn, IXosTableCfg} from '../../table/table';
Matteo Scandolo1aee1982017-02-17 08:33:23 -080022import {IXosModeldef} from '../../../datasources/rest/modeldefs.rest';
Matteo Scandoloe7e052d2017-07-31 19:54:31 -070023import {IXosFormCfg, IXosFormInput, IXosFormInputValidator, IXosFormInputOptions} from '../../form/form';
Matteo Scandolo47860fe2017-02-02 12:05:55 -080024import {IXosModelStoreService} from '../../../datasources/stores/model.store';
Matteo Scandolo1aee1982017-02-17 08:33:23 -080025import {IXosState} from '../runtime-states';
26
27export interface IXosModelDefsFieldValidators {
28 name: string;
29 bool_value?: boolean;
30 int_value?: number;
31}
Matteo Scandolod58d5042016-12-16 16:59:21 -080032
33export interface IXosModelDefsField {
34 name: string;
35 type: string;
Matteo Scandolo1aee1982017-02-17 08:33:23 -080036 validators?: IXosModelDefsFieldValidators[];
Matteo Scandolo04964232017-01-07 12:53:46 -080037 hint?: string;
38 relation?: {
39 model: string;
40 type: string;
41 };
Matteo Scandoloe7e052d2017-07-31 19:54:31 -070042 options?: IXosFormInputOptions[];
43 default?: any | null;
Matteo Scandolod58d5042016-12-16 16:59:21 -080044}
45
46export interface IXosConfigHelpersService {
Matteo Scandoloee655a12016-12-19 15:38:43 -080047 excluded_fields: string[];
Matteo Scandoloc8a58c82017-08-17 17:14:38 -070048 form_excluded_fields: string[];
Matteo Scandolo1aee1982017-02-17 08:33:23 -080049 modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[];
50 modelToTableCfg(model: IXosModeldef, modelName: string): IXosTableCfg;
Matteo Scandolo80c3a652017-01-06 10:48:31 -080051 modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[];
Matteo Scandolo1aee1982017-02-17 08:33:23 -080052 modelToFormCfg(model: IXosModeldef): IXosFormCfg;
Matteo Scandolod58d5042016-12-16 16:59:21 -080053 pluralize(string: string, quantity?: number, count?: boolean): string;
54 toLabel(string: string, pluralize?: boolean): string;
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -080055 toLabels(string: string[], pluralize?: boolean): string[];
Matteo Scandoloa242c872017-01-12 15:13:00 -080056 stateFromCoreModel(name: string): string;
57 stateWithParams(name: string, model: any): string;
Matteo Scandolo8248bca2017-08-09 13:46:04 -070058 relatedStateWithParams(name: string, id: string): string;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080059 stateWithParamsForJs(name: string, model: any): any;
Matteo Scandolod58d5042016-12-16 16:59:21 -080060}
61
Matteo Scandolo8b2370c2017-02-02 17:19:07 -080062export class ConfigHelpers implements IXosConfigHelpersService {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080063 static $inject = [
64 '$state',
65 'toastr',
Matteo Scandolo1aee1982017-02-17 08:33:23 -080066 'XosModelStore'];
Matteo Scandolod58d5042016-12-16 16:59:21 -080067
Matteo Scandolo1aee1982017-02-17 08:33:23 -080068 public excluded_fields = [
Matteo Scandoloee655a12016-12-19 15:38:43 -080069 'created',
70 'updated',
71 'enacted',
72 'policed',
73 'backend_register',
74 'deleted',
75 'write_protect',
76 'lazy_blocked',
77 'no_sync',
78 'no_policy',
79 'omf_friendly',
80 'enabled',
Matteo Scandolod62ea792016-12-22 14:02:28 -080081 'validators',
Matteo Scandolo80c3a652017-01-06 10:48:31 -080082 'password',
83 'backend_need_delete',
Matteo Scandolod53ac1d2017-08-01 15:06:09 -070084 'backend_need_reap',
85 'leaf_model_name'
Matteo Scandoloee655a12016-12-19 15:38:43 -080086 ];
87
Matteo Scandolod53ac1d2017-08-01 15:06:09 -070088 public form_excluded_fields = this.excluded_fields.concat([
89 'id',
90 'policy_status',
91 'backend_status',
92 ]);
93
Matteo Scandolocb466ed2017-01-04 17:16:24 -080094 constructor(
Matteo Scandoloa242c872017-01-12 15:13:00 -080095 private $state: ng.ui.IStateService,
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080096 private toastr: ng.toastr.IToastrService,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080097 private XosModelStore: IXosModelStoreService
Matteo Scandolocb466ed2017-01-04 17:16:24 -080098 ) {
Matteo Scandolo08464e52017-01-17 13:35:27 -080099 pluralize.addIrregularRule('xos', 'xoses');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800100 pluralize.addPluralRule(/slice$/i, 'slices');
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800101 pluralize.addSingularRule(/slice$/i, 'slice');
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800102 pluralize.addPluralRule(/library$/i, 'librarys');
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700103 pluralize.addPluralRule(/imagedeployments/i, 'imagedeploymentss');
104 pluralize.addPluralRule(/controllerimages/i, 'controllerimagess');
105 pluralize.addPluralRule(/servicedependency/i, 'servicedependencys');
106 pluralize.addPluralRule(/servicemonitoringagentinfo/i, 'servicemonitoringagentinfoes');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800107 }
108
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800109 public pluralize(string: string, quantity?: number, count?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800110 return pluralize(string, quantity, count);
111 }
112
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800113 public toLabels(strings: string[], pluralize?: boolean): string[] {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800114 if (angular.isArray(strings)) {
115 return _.map(strings, s => {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800116 return this.toLabel(s, pluralize);
117 });
118 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800119 }
120
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800121 public toLabel(string: string, pluralize?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800122
123 if (pluralize) {
124 string = this.pluralize(string);
125 }
126
127 string = this.fromCamelCase(string);
128 string = this.fromSnakeCase(string);
129 string = this.fromKebabCase(string);
130
131 return this.capitalizeFirst(string);
132 }
133
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800134 public modelToTableCfg(model: IXosModeldef, baseUrl: string): IXosTableCfg {
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800135 const cfg = {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800136 columns: this.modelFieldsToColumnsCfg(model),
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800137 filter: 'fulltext',
138 order: {field: 'id', reverse: false},
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800139 pagination: {
140 pageSize: 10
141 },
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800142 actions: [
143 {
Matteo Scandolocc4bce82017-08-07 13:11:47 -0700144 label: 'details',
145 icon: 'search',
146 cb: (item) => {
147 this.$state.go(this.$state.current.name, {id: item.id});
148 }
149 },
150 {
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800151 label: 'delete',
152 icon: 'remove',
153 color: 'red',
154 cb: (item) => {
155 let obj = angular.copy(item);
156
157 item.$delete()
158 .then((res) => {
159 if (res.status === 404) {
160 // TODO understand why it does not go directly in catch
161 throw new Error();
162 }
163 this.toastr.info(`${model.name} ${obj.name} succesfully deleted`);
164 })
165 .catch(() => {
166 this.toastr.error(`Error while deleting ${obj.name}`);
167 });
168 }
169 }
170 ]
171 };
172 return cfg;
173 }
174
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800175 public modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[] {
176 const fields: IXosModelDefsField[] = model.fields;
177 const modelName: string = model.name;
Matteo Scandolo231de262017-01-04 16:33:14 -0800178 const columns = _.map(fields, (f) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800179 if (!angular.isDefined(f) || this.excluded_fields.indexOf(f.name) > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800180 return;
181 }
182 const col: IXosTableColumn = {
183 label: this.toLabel(f.name),
184 prop: f.name
185 };
186
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800187 if (f.name === 'id' || f.name === 'name') {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800188 col.link = item => this.stateWithParams(modelName, item);
Matteo Scandoloee655a12016-12-19 15:38:43 -0800189 }
190
Matteo Scandolo04964232017-01-07 12:53:46 -0800191 // if the field identify a relation, create a link
Matteo Scandolo18975142017-08-01 14:48:04 -0700192 if (f.relation && f.relation.type === 'manytoone') {
Matteo Scandolo04964232017-01-07 12:53:46 -0800193 col.type = 'custom';
194 col.formatter = item => {
195 this.populateRelated(item, item[f.name], f);
196 return item[f.name];
197 };
Matteo Scandolo8248bca2017-08-09 13:46:04 -0700198 col.link = item => this.relatedStateWithParams(f.relation.model, item[col.prop]);
Matteo Scandolo04964232017-01-07 12:53:46 -0800199 }
200
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700201 if (f.name === 'backend_status' || f.name === 'policy_status') {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800202 col.type = 'icon';
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800203 col.hover = (item) => {
204 return item[f.name];
205 };
Matteo Scandolod58d5042016-12-16 16:59:21 -0800206 col.formatter = (item) => {
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700207 if (item[f.name].indexOf('1') > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800208 return 'check';
209 }
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700210 if (item[f.name].indexOf('2') > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800211 return 'exclamation-circle';
212 }
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700213 if (item[f.name].indexOf('0') > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800214 return 'clock-o';
215 }
216 };
217 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800218
Matteo Scandolod58d5042016-12-16 16:59:21 -0800219 return col;
220 })
221 .filter(v => angular.isDefined(v));
222
Matteo Scandolo231de262017-01-04 16:33:14 -0800223 return columns;
Matteo Scandolod58d5042016-12-16 16:59:21 -0800224 };
225
Matteo Scandoloa242c872017-01-12 15:13:00 -0800226 public stateFromCoreModel(name: string): string {
227 const state: ng.ui.IState = _.find(this.$state.get(), (s: IXosState) => {
228 if (s.data) {
229 return s.data.model === name;
230 }
231 return false;
232 });
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800233 return state ? state.name : null;
Matteo Scandoloa242c872017-01-12 15:13:00 -0800234 }
235
236 public stateWithParams(name: string, model: any): string {
237 const state = this.stateFromCoreModel(name);
238 return `${state}({id: ${model['id']}})`;
239 }
240
Matteo Scandolo8248bca2017-08-09 13:46:04 -0700241 public relatedStateWithParams(name: string, id: string): string {
242 const state = this.stateFromCoreModel(name);
243 return `${state}({id: ${id}})`;
244 }
245
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800246 public stateWithParamsForJs(name: string, model: any): any {
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800247 const state = this.stateFromCoreModel(name);
248 return {name: state, params: {id: model.id}};
249 }
250
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800251 public modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[] {
252
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800253 return _.map(fields, (f: IXosModelDefsField) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800254 const input: IXosFormInput = {
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800255 name: f.name,
256 label: this.toLabel(f.name),
257 type: f.type,
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800258 validators: this.formatValidators(f.validators),
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700259 hint: f.hint,
260 default: f.default || null
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800261 };
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700262
263 // NOTE populate drop-downs based on relation
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800264 if (f.relation) {
265 input.type = 'select';
266 this.populateSelectField(f, input);
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700267 }
268 // NOTE if static options are defined in modeldefs
269 // the f.options field is already populated,
270 // we just need to move it to the input
271 else if (f.options && f.options.length > 0) {
272 input.options = f.options;
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800273 }
274 return input;
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800275 })
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700276 .filter(f => this.form_excluded_fields.indexOf(f.name) === -1);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800277 }
278
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800279 public modelToFormCfg(model: IXosModeldef): IXosFormCfg {
280 const formCfg: IXosFormCfg = {
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800281 formName: `${model.name}Form`,
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700282 exclude: this.form_excluded_fields,
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800283 actions: [{
284 label: 'Save',
285 class: 'success',
286 icon: 'ok',
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800287 cb: null
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800288 }],
289 inputs: this.modelFieldToInputCfg(model.fields)
290 };
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800291
292 formCfg.actions[0].cb = (item, form: angular.IFormController) => {
293
294 if (!form.$valid) {
295 formCfg.feedback = {
296 show: true,
297 message: 'Form is invalid',
298 type: 'danger',
299 closeBtn: true
300 };
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800301
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800302 return;
303 }
304
305 const model = angular.copy(item);
306
307 // TODO remove ManyToMany relations and save them separately (how??)
308 delete item.networks;
309
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800310 // remove field added by xosTable
311 _.forEach(Object.keys(item), prop => {
312 if (prop.indexOf('-formatted') > -1) {
313 delete item[prop];
314 }
315 });
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800316
317 item.$save()
318 .then((res) => {
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800319 formCfg.feedback = {
320 show: true,
321 message: `${model.name} succesfully saved`,
322 type: 'success',
323 closeBtn: true
324 };
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800325 this.toastr.success(`${model.name} succesfully saved`);
326 })
327 .catch(err => {
Matteo Scandolo42c66922017-05-01 17:24:59 -0700328 formCfg.feedback = {
329 show: true,
330 message: `Error while saving ${model.name}: ${err.error}. ${err.specific_error || ''}`,
331 type: 'danger',
332 closeBtn: true
333 };
334 this.toastr.error(err.specific_error || '', `Error while saving ${model.name}: ${err.error}`);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800335 });
336 };
337
338 return formCfg;
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800339 }
340
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800341 private formatValidators(validators: IXosModelDefsFieldValidators[]): IXosFormInputValidator {
342 // convert validators as expressed from modelDefs,
343 // to the object required by xosForm
344 return _.reduce(validators, (formValidators: IXosFormInputValidator, v: IXosModelDefsFieldValidators) => {
345 formValidators[v.name] = v.bool_value ? v.bool_value : v.int_value;
346 return formValidators;
347 }, {});
348 }
349
Matteo Scandolod58d5042016-12-16 16:59:21 -0800350 private fromCamelCase(string: string): string {
351 return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' ');
352 }
353
354 private fromSnakeCase(string: string): string {
355 return string.split('_').join(' ').trim();
356 }
357
358 private fromKebabCase(string: string): string {
359 return string.split('-').join(' ').trim();
360 }
361
362 private capitalizeFirst(string: string): string {
363 return string.slice(0, 1).toUpperCase() + string.slice(1);
364 }
Matteo Scandolo04964232017-01-07 12:53:46 -0800365
366 private populateRelated(item: any, fk: string, field: IXosModelDefsField): any {
367 // if the relation is not defined return
368 if (!fk || angular.isUndefined(fk) || fk === null) {
369 return;
370 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800371 this.XosModelStore.query(field.relation.model)
Matteo Scandolo04964232017-01-07 12:53:46 -0800372 .subscribe(res => {
373 if (angular.isDefined(res) && angular.isDefined(fk)) {
374 let ri = _.find(res, {id: fk});
375 if (angular.isDefined(ri)) {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800376 if (angular.isDefined(ri.name)) {
377 item[`${field.name}-formatted`] = ri.name;
378 }
379 else if (angular.isDefined(ri.humanReadableName)) {
380 item[`${field.name}-formatted`] = ri.humanReadableName;
381 }
382 else {
383 item[`${field.name}-formatted`] = ri.id;
384 }
Matteo Scandolo04964232017-01-07 12:53:46 -0800385 }
386 }
387 });
388 }
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800389
390 // augment a select field with related model informations
391 private populateSelectField(field: IXosModelDefsField, input: IXosFormInput): void {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800392 this.XosModelStore.query(field.relation.model)
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800393 .subscribe(res => {
394 input.options = _.map(res, item => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800395 let opt = {id: item.id, label: item.humanReadableName ? item.humanReadableName : item.name};
396 if (!angular.isDefined(item.humanReadableName) && !angular.isDefined(item.name)) {
397 opt.label = item.id;
398 }
399 return opt;
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800400 });
401 });
402 }
Matteo Scandolod58d5042016-12-16 16:59:21 -0800403}