blob: 891d68adf9445d3d7bf1a957ac64769bbb37088c [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',
Scott Bakere5dcb9a2017-09-07 10:18:06 -070084 'backend_need_delete_policy',
Matteo Scandolod53ac1d2017-08-01 15:06:09 -070085 'backend_need_reap',
Scott Bakere5dcb9a2017-09-07 10:18:06 -070086 'leaf_model_name',
87 'link_deleted_count',
Matteo Scandoloee655a12016-12-19 15:38:43 -080088 ];
89
Matteo Scandolod53ac1d2017-08-01 15:06:09 -070090 public form_excluded_fields = this.excluded_fields.concat([
91 'id',
92 'policy_status',
93 'backend_status',
94 ]);
95
Matteo Scandolocb466ed2017-01-04 17:16:24 -080096 constructor(
Matteo Scandoloa242c872017-01-12 15:13:00 -080097 private $state: ng.ui.IStateService,
Matteo Scandolo0a8b02e2017-01-06 14:43:36 -080098 private toastr: ng.toastr.IToastrService,
Matteo Scandolo1aee1982017-02-17 08:33:23 -080099 private XosModelStore: IXosModelStoreService
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800100 ) {
Matteo Scandolo08464e52017-01-17 13:35:27 -0800101 pluralize.addIrregularRule('xos', 'xoses');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800102 pluralize.addPluralRule(/slice$/i, 'slices');
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800103 pluralize.addSingularRule(/slice$/i, 'slice');
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800104 pluralize.addPluralRule(/library$/i, 'librarys');
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700105 pluralize.addPluralRule(/imagedeployments/i, 'imagedeploymentss');
106 pluralize.addPluralRule(/controllerimages/i, 'controllerimagess');
107 pluralize.addPluralRule(/servicedependency/i, 'servicedependencys');
108 pluralize.addPluralRule(/servicemonitoringagentinfo/i, 'servicemonitoringagentinfoes');
Matteo Scandolod58d5042016-12-16 16:59:21 -0800109 }
110
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800111 public pluralize(string: string, quantity?: number, count?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800112 return pluralize(string, quantity, count);
113 }
114
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800115 public toLabels(strings: string[], pluralize?: boolean): string[] {
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800116 if (angular.isArray(strings)) {
117 return _.map(strings, s => {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800118 return this.toLabel(s, pluralize);
119 });
120 }
Matteo Scandoloe0d71ea2016-12-19 11:56:12 -0800121 }
122
Matteo Scandolo1c5905f2017-01-04 17:41:15 -0800123 public toLabel(string: string, pluralize?: boolean): string {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800124
125 if (pluralize) {
126 string = this.pluralize(string);
127 }
128
129 string = this.fromCamelCase(string);
130 string = this.fromSnakeCase(string);
131 string = this.fromKebabCase(string);
132
133 return this.capitalizeFirst(string);
134 }
135
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800136 public modelToTableCfg(model: IXosModeldef, baseUrl: string): IXosTableCfg {
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800137 const cfg = {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800138 columns: this.modelFieldsToColumnsCfg(model),
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800139 filter: 'fulltext',
140 order: {field: 'id', reverse: false},
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800141 pagination: {
142 pageSize: 10
143 },
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800144 actions: [
145 {
Matteo Scandolocc4bce82017-08-07 13:11:47 -0700146 label: 'details',
147 icon: 'search',
148 cb: (item) => {
149 this.$state.go(this.$state.current.name, {id: item.id});
150 }
151 },
152 {
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800153 label: 'delete',
154 icon: 'remove',
155 color: 'red',
156 cb: (item) => {
157 let obj = angular.copy(item);
Max Chubab0a582017-09-06 08:32:34 -0700158 const objName = (angular.isUndefined(obj.name)) ? 'instance' : obj.name;
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800159
160 item.$delete()
161 .then((res) => {
162 if (res.status === 404) {
163 // TODO understand why it does not go directly in catch
164 throw new Error();
165 }
Max Chubab0a582017-09-06 08:32:34 -0700166 this.toastr.info(`${model.name} ${objName} successfully deleted`);
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800167 })
168 .catch(() => {
Max Chubab0a582017-09-06 08:32:34 -0700169 this.toastr.error(`Error while deleting ${objName}`);
Matteo Scandolocb466ed2017-01-04 17:16:24 -0800170 });
171 }
172 }
173 ]
174 };
175 return cfg;
176 }
177
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800178 public modelFieldsToColumnsCfg(model: IXosModeldef): IXosTableColumn[] {
179 const fields: IXosModelDefsField[] = model.fields;
180 const modelName: string = model.name;
Matteo Scandolo231de262017-01-04 16:33:14 -0800181 const columns = _.map(fields, (f) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800182 if (!angular.isDefined(f) || this.excluded_fields.indexOf(f.name) > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800183 return;
184 }
185 const col: IXosTableColumn = {
186 label: this.toLabel(f.name),
187 prop: f.name
188 };
189
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800190 if (f.name === 'id' || f.name === 'name') {
Matteo Scandoloa242c872017-01-12 15:13:00 -0800191 col.link = item => this.stateWithParams(modelName, item);
Matteo Scandoloee655a12016-12-19 15:38:43 -0800192 }
193
Matteo Scandolo04964232017-01-07 12:53:46 -0800194 // if the field identify a relation, create a link
Matteo Scandolo18975142017-08-01 14:48:04 -0700195 if (f.relation && f.relation.type === 'manytoone') {
Matteo Scandolo04964232017-01-07 12:53:46 -0800196 col.type = 'custom';
197 col.formatter = item => {
198 this.populateRelated(item, item[f.name], f);
199 return item[f.name];
200 };
Matteo Scandolo8248bca2017-08-09 13:46:04 -0700201 col.link = item => this.relatedStateWithParams(f.relation.model, item[col.prop]);
Matteo Scandolo04964232017-01-07 12:53:46 -0800202 }
203
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700204 if (f.name === 'backend_status' || f.name === 'policy_status') {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800205 col.type = 'icon';
Matteo Scandolo8b2370c2017-02-02 17:19:07 -0800206 col.hover = (item) => {
207 return item[f.name];
208 };
Matteo Scandolod58d5042016-12-16 16:59:21 -0800209 col.formatter = (item) => {
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700210 if (item[f.name].indexOf('1') > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800211 return 'check';
212 }
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700213 if (item[f.name].indexOf('2') > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800214 return 'exclamation-circle';
215 }
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700216 if (item[f.name].indexOf('0') > -1) {
Matteo Scandolod58d5042016-12-16 16:59:21 -0800217 return 'clock-o';
218 }
219 };
220 }
Matteo Scandoloa8a6fbb2016-12-21 16:59:08 -0800221
Matteo Scandolod58d5042016-12-16 16:59:21 -0800222 return col;
223 })
224 .filter(v => angular.isDefined(v));
225
Matteo Scandolo231de262017-01-04 16:33:14 -0800226 return columns;
Matteo Scandolod58d5042016-12-16 16:59:21 -0800227 };
228
Matteo Scandoloa242c872017-01-12 15:13:00 -0800229 public stateFromCoreModel(name: string): string {
230 const state: ng.ui.IState = _.find(this.$state.get(), (s: IXosState) => {
231 if (s.data) {
232 return s.data.model === name;
233 }
234 return false;
235 });
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800236 return state ? state.name : null;
Matteo Scandoloa242c872017-01-12 15:13:00 -0800237 }
238
239 public stateWithParams(name: string, model: any): string {
240 const state = this.stateFromCoreModel(name);
241 return `${state}({id: ${model['id']}})`;
242 }
243
Matteo Scandolo8248bca2017-08-09 13:46:04 -0700244 public relatedStateWithParams(name: string, id: string): string {
245 const state = this.stateFromCoreModel(name);
246 return `${state}({id: ${id}})`;
247 }
248
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800249 public stateWithParamsForJs(name: string, model: any): any {
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800250 const state = this.stateFromCoreModel(name);
251 return {name: state, params: {id: model.id}};
252 }
253
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800254 public modelFieldToInputCfg(fields: IXosModelDefsField[]): IXosFormInput[] {
255
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800256 return _.map(fields, (f: IXosModelDefsField) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800257 const input: IXosFormInput = {
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800258 name: f.name,
259 label: this.toLabel(f.name),
260 type: f.type,
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800261 validators: this.formatValidators(f.validators),
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700262 hint: f.hint,
Matteo Scandolof1e68cd2017-09-05 17:30:34 -0700263 default: this.formatDefaultValues(f.default)
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800264 };
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700265
266 // NOTE populate drop-downs based on relation
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800267 if (f.relation) {
268 input.type = 'select';
269 this.populateSelectField(f, input);
Matteo Scandoloe7e052d2017-07-31 19:54:31 -0700270 }
271 // NOTE if static options are defined in modeldefs
272 // the f.options field is already populated,
273 // we just need to move it to the input
274 else if (f.options && f.options.length > 0) {
275 input.options = f.options;
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800276 }
277 return input;
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800278 })
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700279 .filter(f => this.form_excluded_fields.indexOf(f.name) === -1);
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800280 }
281
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800282 public modelToFormCfg(model: IXosModeldef): IXosFormCfg {
Matteo Scandolof1e68cd2017-09-05 17:30:34 -0700283
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800284 const formCfg: IXosFormCfg = {
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800285 formName: `${model.name}Form`,
Matteo Scandolod53ac1d2017-08-01 15:06:09 -0700286 exclude: this.form_excluded_fields,
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800287 actions: [{
288 label: 'Save',
289 class: 'success',
290 icon: 'ok',
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800291 cb: null
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800292 }],
293 inputs: this.modelFieldToInputCfg(model.fields)
294 };
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800295
296 formCfg.actions[0].cb = (item, form: angular.IFormController) => {
297
298 if (!form.$valid) {
299 formCfg.feedback = {
300 show: true,
301 message: 'Form is invalid',
302 type: 'danger',
303 closeBtn: true
304 };
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800305
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800306 return;
307 }
308
Max Chubab0a582017-09-06 08:32:34 -0700309 const itemCopy = angular.copy(item);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800310
311 // TODO remove ManyToMany relations and save them separately (how??)
312 delete item.networks;
313
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800314 // remove field added by xosTable
315 _.forEach(Object.keys(item), prop => {
316 if (prop.indexOf('-formatted') > -1) {
317 delete item[prop];
318 }
319 });
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800320
Max Chubab0a582017-09-06 08:32:34 -0700321 const itemName = (angular.isUndefined(itemCopy.name)) ? model.name : itemCopy.name;
322
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800323 item.$save()
324 .then((res) => {
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800325 formCfg.feedback = {
326 show: true,
Max Chubab0a582017-09-06 08:32:34 -0700327 message: `${itemName} successfully saved`,
Matteo Scandoloac8c8c22017-01-09 15:04:32 -0800328 type: 'success',
329 closeBtn: true
330 };
Max Chubab0a582017-09-06 08:32:34 -0700331 this.toastr.success(`${itemName} successfully saved`);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800332 })
333 .catch(err => {
Matteo Scandolo42c66922017-05-01 17:24:59 -0700334 formCfg.feedback = {
335 show: true,
Max Chubab0a582017-09-06 08:32:34 -0700336 message: `Error while saving ${itemName}: ${err.error}. ${err.specific_error || ''}`,
Matteo Scandolo42c66922017-05-01 17:24:59 -0700337 type: 'danger',
338 closeBtn: true
339 };
Max Chubab0a582017-09-06 08:32:34 -0700340 this.toastr.error(err.specific_error || '', `Error while saving ${itemName}: ${err.error}`);
Matteo Scandolo6f45e262017-01-09 14:47:26 -0800341 });
342 };
343
344 return formCfg;
Matteo Scandolo80c3a652017-01-06 10:48:31 -0800345 }
346
Matteo Scandolof1e68cd2017-09-05 17:30:34 -0700347 private formatDefaultValues(val: any): any {
348
349 if (angular.isString(val)) {
350 const unquoted = val.split('"').join('').toLowerCase();
351 if (unquoted === 'true') {
352 return true;
353 }
354 else if (unquoted === 'false') {
355 return false;
356 }
357 }
358
359 return val || undefined;
360 }
361
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800362 private formatValidators(validators: IXosModelDefsFieldValidators[]): IXosFormInputValidator {
363 // convert validators as expressed from modelDefs,
364 // to the object required by xosForm
365 return _.reduce(validators, (formValidators: IXosFormInputValidator, v: IXosModelDefsFieldValidators) => {
366 formValidators[v.name] = v.bool_value ? v.bool_value : v.int_value;
367 return formValidators;
368 }, {});
369 }
370
Matteo Scandolod58d5042016-12-16 16:59:21 -0800371 private fromCamelCase(string: string): string {
372 return string.split(/(?=[A-Z])/).map(w => w.toLowerCase()).join(' ');
373 }
374
375 private fromSnakeCase(string: string): string {
376 return string.split('_').join(' ').trim();
377 }
378
379 private fromKebabCase(string: string): string {
380 return string.split('-').join(' ').trim();
381 }
382
383 private capitalizeFirst(string: string): string {
384 return string.slice(0, 1).toUpperCase() + string.slice(1);
385 }
Matteo Scandolo04964232017-01-07 12:53:46 -0800386
387 private populateRelated(item: any, fk: string, field: IXosModelDefsField): any {
388 // if the relation is not defined return
389 if (!fk || angular.isUndefined(fk) || fk === null) {
390 return;
391 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800392 this.XosModelStore.query(field.relation.model)
Matteo Scandolo04964232017-01-07 12:53:46 -0800393 .subscribe(res => {
394 if (angular.isDefined(res) && angular.isDefined(fk)) {
395 let ri = _.find(res, {id: fk});
396 if (angular.isDefined(ri)) {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800397 if (angular.isDefined(ri.name)) {
398 item[`${field.name}-formatted`] = ri.name;
399 }
400 else if (angular.isDefined(ri.humanReadableName)) {
401 item[`${field.name}-formatted`] = ri.humanReadableName;
402 }
403 else {
404 item[`${field.name}-formatted`] = ri.id;
405 }
Matteo Scandolo04964232017-01-07 12:53:46 -0800406 }
407 }
408 });
409 }
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800410
411 // augment a select field with related model informations
412 private populateSelectField(field: IXosModelDefsField, input: IXosFormInput): void {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800413 this.XosModelStore.query(field.relation.model)
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800414 .subscribe(res => {
415 input.options = _.map(res, item => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800416 let opt = {id: item.id, label: item.humanReadableName ? item.humanReadableName : item.name};
417 if (!angular.isDefined(item.humanReadableName) && !angular.isDefined(item.name)) {
418 opt.label = item.id;
419 }
420 return opt;
Matteo Scandolo07e2f622017-01-09 10:54:13 -0800421 });
422 });
423 }
Matteo Scandolod58d5042016-12-16 16:59:21 -0800424}