blob: 8dcff63d83d59b3cfec7d0717d60d1f45b902ad1 [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 Scandolo9fbf8452017-10-16 09:47:41 -070019import {IRCordSubscriber, IRCordSubscriberDevice} from '../interfaces/rcord-subscriber.interface';
Matteo Scandolo22f8e822017-09-28 16:35:20 -070020
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070021class RcordSubscriberDashboardCtrl {
Matteo Scandolo22f8e822017-09-28 16:35:20 -070022
23 static $inject = [
24 'toastr',
25 'XosModelStore',
26 'XosModeldefsCache'
27 ];
28
29 public levelOptions = [];
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070030 public subscribers: IRCordSubscriber[] = [];
Matteo Scandolo22f8e822017-09-28 16:35:20 -070031 public statusFieldOptions = [];
32 public slider = {
33 floor: 0,
34 ceil: 1000000000,
35 translate: (value) => {
36 return Math.floor(value / 1000000);
37 }
38 };
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070039 public selectedSubscriber: IRCordSubscriber;
Matteo Scandolo22f8e822017-09-28 16:35:20 -070040
41 private subscriberDef;
42
43 constructor (
44 private toastr: any,
45 private XosModelStore: any,
46 private XosModeldefsCache: any
47 ) {
48
49 }
50
51 $onInit() {
52 this.XosModelStore.query('CordSubscriberRoot', '/rcord/cordsubscriberroots')
53 .subscribe(
54 res => {
55 this.subscribers = this.parseSubscribers(res);
56 }
57 );
58
59 this.levelOptions = [
60 `G`,
61 `PG`,
62 `PG_13`,
63 `R`,
64 `X`
65 ];
66
67 this.subscriberDef = this.XosModeldefsCache.get('CordSubscriberRoot');
68 this.statusFieldOptions = _.find(this.subscriberDef.fields, {name: 'status'}).options;
69 }
70
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070071 public addDevice(subscriber: IRCordSubscriber) {
72 if (angular.isUndefined(subscriber.service_specific_attribute.devices)) {
73 subscriber.service_specific_attribute.devices = [];
74 }
Matteo Scandolo22f8e822017-09-28 16:35:20 -070075 subscriber.service_specific_attribute.devices.push({
76 name: '',
77 mac: '',
78 level: 'PG_13'
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070079 });
Matteo Scandolo22f8e822017-09-28 16:35:20 -070080 }
81
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070082 public removeDevice(subscriber: IRCordSubscriber, device: IRCordSubscriberDevice) {
83 _.remove(subscriber.service_specific_attribute.devices, {name: device.name});
Matteo Scandolo22f8e822017-09-28 16:35:20 -070084 }
85
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070086 public save(subscriber: IRCordSubscriber) {
Matteo Scandolo22f8e822017-09-28 16:35:20 -070087 const item: any = angular.copy(subscriber);
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070088
89 _.forEach(Object.keys(item), prop => {
90 if (prop.indexOf('-formatted') > -1 || prop.indexOf('_ptr') > -1) {
91 delete item[prop];
92 }
93 });
94
Matteo Scandolo22f8e822017-09-28 16:35:20 -070095 item.service_specific_attribute = JSON.stringify(item.service_specific_attribute);
96 item.$save()
97 .then(() => {
98 this.toastr.success(`Subscriber successfully saved`);
Matteo Scandolo9fbf8452017-10-16 09:47:41 -070099 })
100 .catch(e => {
101 this.toastr.error(`Error while saving subscriber`);
102 console.error(e);
Matteo Scandolo22f8e822017-09-28 16:35:20 -0700103 });
104 }
105
Matteo Scandolo9fbf8452017-10-16 09:47:41 -0700106 private parseSubscribers(subscribers: IRCordSubscriber) {
Matteo Scandolo22f8e822017-09-28 16:35:20 -0700107 return _.map(subscribers, (s) => {
108 if (angular.isString(s.service_specific_attribute)) {
Matteo Scandolo9fbf8452017-10-16 09:47:41 -0700109 try {
110 s.service_specific_attribute = JSON.parse(s.service_specific_attribute);
111 } catch (e) {
112 s.service_specific_attribute = {};
113 }
Matteo Scandolo22f8e822017-09-28 16:35:20 -0700114 }
115 return s;
Matteo Scandolo9fbf8452017-10-16 09:47:41 -0700116 });
117 }
118
119 $onDestroy() {
120 this.selectedSubscriber.service_specific_attribute = JSON.stringify(this.selectedSubscriber.service_specific_attribute);
Matteo Scandolo22f8e822017-09-28 16:35:20 -0700121 }
122}
123
124export const rcordSubscriberDashboard: angular.IComponentOptions = {
125 template: require('./subscriber-dashboard.html'),
126 controllerAs: 'vm',
Matteo Scandolo9fbf8452017-10-16 09:47:41 -0700127 controller: RcordSubscriberDashboardCtrl
128};