blob: 69fe1494d3a1780da44d1f2d7d4861ae1c0fed64 [file] [log] [blame]
Matteo Scandolo22f8e822017-09-28 16:35:20 -07001/*
2 * Copyright 2017-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import * as _ from 'lodash';
18import './subscriber-dashboard.scss';
Matteo Scandolo9b0c1be2017-09-28 16:35:20 -070019
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070020import {IRCordSubscriber, IRCordSubscriberDevice} from '../interfaces/rcord-subscriber.interface';
Matteo Scandolo22f8e822017-09-28 16:35:20 -070021
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070022class RcordSubscriberDashboardCtrl {
Matteo Scandolo22f8e822017-09-28 16:35:20 -070023
24 static $inject = [
25 'toastr',
26 'XosModelStore',
27 'XosModeldefsCache'
28 ];
29
30 public levelOptions = [];
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070031 public subscribers: IRCordSubscriber[] = [];
Matteo Scandolo22f8e822017-09-28 16:35:20 -070032 public statusFieldOptions = [];
33 public slider = {
34 floor: 0,
35 ceil: 1000000000,
36 translate: (value) => {
Matteo Scandolo9b0c1be2017-09-28 16:35:20 -070037 return `${Math.floor(value / 1000000)} MB`;
Matteo Scandolo22f8e822017-09-28 16:35:20 -070038 }
39 };
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070040 public selectedSubscriber: IRCordSubscriber;
Matteo Scandolo22f8e822017-09-28 16:35:20 -070041
42 private subscriberDef;
43
44 constructor (
45 private toastr: any,
46 private XosModelStore: any,
47 private XosModeldefsCache: any
48 ) {
49
50 }
51
52 $onInit() {
53 this.XosModelStore.query('CordSubscriberRoot', '/rcord/cordsubscriberroots')
54 .subscribe(
55 res => {
56 this.subscribers = this.parseSubscribers(res);
57 }
58 );
59
60 this.levelOptions = [
61 `G`,
62 `PG`,
63 `PG_13`,
64 `R`,
65 `X`
66 ];
67
68 this.subscriberDef = this.XosModeldefsCache.get('CordSubscriberRoot');
69 this.statusFieldOptions = _.find(this.subscriberDef.fields, {name: 'status'}).options;
70 }
71
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070072 public addDevice(subscriber: IRCordSubscriber) {
73 if (angular.isUndefined(subscriber.service_specific_attribute.devices)) {
74 subscriber.service_specific_attribute.devices = [];
75 }
Matteo Scandolo9b0c1be2017-09-28 16:35:20 -070076
Matteo Scandolo22f8e822017-09-28 16:35:20 -070077 subscriber.service_specific_attribute.devices.push({
78 name: '',
79 mac: '',
80 level: 'PG_13'
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070081 });
Matteo Scandolo22f8e822017-09-28 16:35:20 -070082 }
83
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070084 public removeDevice(subscriber: IRCordSubscriber, device: IRCordSubscriberDevice) {
85 _.remove(subscriber.service_specific_attribute.devices, {name: device.name});
Matteo Scandolo22f8e822017-09-28 16:35:20 -070086 }
87
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070088 public save(subscriber: IRCordSubscriber) {
Matteo Scandolo22f8e822017-09-28 16:35:20 -070089 const item: any = angular.copy(subscriber);
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070090
Matteo Scandolo9b0c1be2017-09-28 16:35:20 -070091 delete item.updated;
92
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070093 _.forEach(Object.keys(item), prop => {
94 if (prop.indexOf('-formatted') > -1 || prop.indexOf('_ptr') > -1) {
95 delete item[prop];
96 }
97 });
98
Matteo Scandolo22f8e822017-09-28 16:35:20 -070099 item.service_specific_attribute = JSON.stringify(item.service_specific_attribute);
100 item.$save()
101 .then(() => {
102 this.toastr.success(`Subscriber successfully saved`);
Matteo Scandolo9fbf8452017-10-16 09:47:41 -0700103 })
104 .catch(e => {
105 this.toastr.error(`Error while saving subscriber`);
106 console.error(e);
Matteo Scandolo22f8e822017-09-28 16:35:20 -0700107 });
108 }
109
Matteo Scandolo9fbf8452017-10-16 09:47:41 -0700110 private parseSubscribers(subscribers: IRCordSubscriber) {
Matteo Scandolo22f8e822017-09-28 16:35:20 -0700111 return _.map(subscribers, (s) => {
112 if (angular.isString(s.service_specific_attribute)) {
Matteo Scandolo9fbf8452017-10-16 09:47:41 -0700113 try {
114 s.service_specific_attribute = JSON.parse(s.service_specific_attribute);
115 } catch (e) {
116 s.service_specific_attribute = {};
117 }
Matteo Scandolo22f8e822017-09-28 16:35:20 -0700118 }
119 return s;
Matteo Scandolo9fbf8452017-10-16 09:47:41 -0700120 });
121 }
122
123 $onDestroy() {
124 this.selectedSubscriber.service_specific_attribute = JSON.stringify(this.selectedSubscriber.service_specific_attribute);
Matteo Scandolo22f8e822017-09-28 16:35:20 -0700125 }
126}
127
128export const rcordSubscriberDashboard: angular.IComponentOptions = {
129 template: require('./subscriber-dashboard.html'),
130 controllerAs: 'vm',
Matteo Scandolo9fbf8452017-10-16 09:47:41 -0700131 controller: RcordSubscriberDashboardCtrl
Matteo Scandolo9b0c1be2017-09-28 16:35:20 -0700132}