blob: b68b4dd57b22a97832e872a1eed7af336589825d [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
Sean Condon5ca00262018-09-06 17:55:25 +01002 * Copyright 2018-present Open Networking Foundation
Sean Condon83fc39f2018-04-19 18:56:13 +01003 *
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 */
Sean Condonb2c483c2019-01-16 20:28:55 +000016import {Inject, Injectable} from '@angular/core';
Bhavesh72ead492018-07-19 16:29:18 +053017import { FnService } from './fn.service';
Sean Condon5ca00262018-09-06 17:55:25 +010018import { LogService } from '../log.service';
Sean Condon83fc39f2018-04-19 18:56:13 +010019import { WebSocketService } from '../remote/websocket.service';
20
Sean Condonb2c483c2019-01-16 20:28:55 +000021const UPDATE_PREFS: string = 'updatePrefs';
22const UPDATE_PREFS_REQ: string = 'updatePrefReq';
Sean Condon83fc39f2018-04-19 18:56:13 +010023/**
24 * ONOS GUI -- Util -- User Preference Service
25 */
Sean Condona00bf382018-06-23 07:54:01 +010026@Injectable({
Bhavesh72ead492018-07-19 16:29:18 +053027 providedIn: 'root',
Sean Condona00bf382018-06-23 07:54:01 +010028})
Sean Condon83fc39f2018-04-19 18:56:13 +010029export class PrefsService {
Bhavesh72ead492018-07-19 16:29:18 +053030 protected handlers: string[] = [];
Sean Condonb2c483c2019-01-16 20:28:55 +000031 cache: Object;
32 listeners: ((data) => void)[] = [];
Sean Condon83fc39f2018-04-19 18:56:13 +010033 constructor(
Bhavesh72ead492018-07-19 16:29:18 +053034 protected fs: FnService,
35 protected log: LogService,
Sean Condonb2c483c2019-01-16 20:28:55 +000036 protected wss: WebSocketService,
37 @Inject('Window') private window: any
Sean Condon83fc39f2018-04-19 18:56:13 +010038 ) {
Bhavesh72ead492018-07-19 16:29:18 +053039 this.wss.bindHandlers(new Map<string, (data) => void>([
Sean Condonb2c483c2019-01-16 20:28:55 +000040 [UPDATE_PREFS, (data) => this.updatePrefs(data)]
Bhavesh72ead492018-07-19 16:29:18 +053041 ]));
Sean Condonb2c483c2019-01-16 20:28:55 +000042 this.handlers.push(UPDATE_PREFS);
43
44 // When index.html is fetched it is served up by MainIndexResource.java
45 // which fetches userPrefs in to the global scope.
46 // After that updates are done through WebSocket
47 this.cache = Object.assign({}, this.window['userPrefs']);
Bhavesh72ead492018-07-19 16:29:18 +053048
Sean Condon83fc39f2018-04-19 18:56:13 +010049 this.log.debug('PrefsService constructed');
50 }
51
Sean Condonb2c483c2019-01-16 20:28:55 +000052 setPrefs(name: string, obj: Object) {
Bhavesh72ead492018-07-19 16:29:18 +053053 // keep a cached copy of the object and send an update to server
54 this.cache[name] = obj;
Sean Condonb2c483c2019-01-16 20:28:55 +000055 this.wss.sendEvent(UPDATE_PREFS_REQ, { key: name, value: obj });
Bhavesh72ead492018-07-19 16:29:18 +053056 }
57 updatePrefs(data: any) {
58 this.cache = data;
Sean Condonb2c483c2019-01-16 20:28:55 +000059 this.listeners.forEach((lsnr) => lsnr(data) );
Bhavesh72ead492018-07-19 16:29:18 +053060 }
61
62 asNumbers(obj: any, keys?: any, not?: any) {
63 if (!obj) {
64 return null;
65 }
66
67 const skip = {};
68 if (not) {
69 keys.forEach(k => {
70 skip[k] = 1;
71 }
72 );
73 }
74
75 if (!keys || not) {
76 // do them all
77 Array.from(obj).forEach((v, k) => {
78 if (!not || !skip[k]) {
79 obj[k] = Number(obj[k]);
80 }
81 });
82 } else {
83 // do the explicitly named keys
84 keys.forEach(k => {
85 obj[k] = Number(obj[k]);
86 });
87 }
88 return obj;
89 }
90
91 getPrefs(name: string, defaults: any, qparams?: string) {
92 const obj = Object.assign({}, defaults || {}, this.cache[name] || {});
93
94 // if query params are specified, they override...
95 if (this.fs.isO(qparams)) {
96 obj.forEach(k => {
97 if (qparams.hasOwnProperty(k)) {
98 obj[k] = qparams[k];
99 }
100 });
101 }
102 return obj;
103 }
104
105 // merge preferences:
106 // The assumption here is that obj is a sparse object, and that the
107 // defined keys should overwrite the corresponding values, but any
108 // existing keys that are NOT explicitly defined here should be left
109 // alone (not deleted).
Sean Condonb2c483c2019-01-16 20:28:55 +0000110 mergePrefs(name: string, obj: any): void {
Bhavesh72ead492018-07-19 16:29:18 +0530111 const merged = this.cache[name] || {};
112 this.setPrefs(name, Object.assign(merged, obj));
113 }
114
Sean Condonb2c483c2019-01-16 20:28:55 +0000115 /**
116 * Add a listener function
117 * This will get called back when an 'updatePrefs' message is received on WSS
118 * @param listener a function that can accept one param - data
119 */
120 addListener(listener: (data) => void): void {
Bhavesh72ead492018-07-19 16:29:18 +0530121 this.listeners.push(listener);
122 }
123
Sean Condonb2c483c2019-01-16 20:28:55 +0000124 removeListener(listener: (data) => void) {
125 this.listeners = this.listeners.filter((obj) => obj !== listener);
Bhavesh72ead492018-07-19 16:29:18 +0530126 }
127
Sean Condon83fc39f2018-04-19 18:56:13 +0100128}