blob: 8145921cc303c1df6ea4db6b7f127e8a221d8817 [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 */
16import { 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
21/**
22 * ONOS GUI -- Util -- User Preference Service
23 */
Sean Condona00bf382018-06-23 07:54:01 +010024@Injectable({
Bhavesh72ead492018-07-19 16:29:18 +053025 providedIn: 'root',
Sean Condona00bf382018-06-23 07:54:01 +010026})
Sean Condon83fc39f2018-04-19 18:56:13 +010027export class PrefsService {
Bhavesh72ead492018-07-19 16:29:18 +053028 protected Prefs;
29 protected handlers: string[] = [];
30 cache: any;
31 listeners: any;
Sean Condon83fc39f2018-04-19 18:56:13 +010032 constructor(
Bhavesh72ead492018-07-19 16:29:18 +053033 protected fs: FnService,
34 protected log: LogService,
35 protected wss: WebSocketService
Sean Condon83fc39f2018-04-19 18:56:13 +010036 ) {
Bhavesh72ead492018-07-19 16:29:18 +053037 this.cache = {};
38 this.wss.bindHandlers(new Map<string, (data) => void>([
39 [this.Prefs, (data) => this.updatePrefs(data)]
40 ]));
41 this.handlers.push(this.Prefs);
42
Sean Condon83fc39f2018-04-19 18:56:13 +010043 this.log.debug('PrefsService constructed');
44 }
45
Bhavesh72ead492018-07-19 16:29:18 +053046 setPrefs(name: string, obj: any) {
47 // keep a cached copy of the object and send an update to server
48 this.cache[name] = obj;
49 this.wss.sendEvent('updatePrefReq', { key: name, value: obj });
50 }
51 updatePrefs(data: any) {
52 this.cache = data;
53 this.listeners.forEach(function (lsnr) { lsnr(); });
54 }
55
56 asNumbers(obj: any, keys?: any, not?: any) {
57 if (!obj) {
58 return null;
59 }
60
61 const skip = {};
62 if (not) {
63 keys.forEach(k => {
64 skip[k] = 1;
65 }
66 );
67 }
68
69 if (!keys || not) {
70 // do them all
71 Array.from(obj).forEach((v, k) => {
72 if (!not || !skip[k]) {
73 obj[k] = Number(obj[k]);
74 }
75 });
76 } else {
77 // do the explicitly named keys
78 keys.forEach(k => {
79 obj[k] = Number(obj[k]);
80 });
81 }
82 return obj;
83 }
84
85 getPrefs(name: string, defaults: any, qparams?: string) {
86 const obj = Object.assign({}, defaults || {}, this.cache[name] || {});
87
88 // if query params are specified, they override...
89 if (this.fs.isO(qparams)) {
90 obj.forEach(k => {
91 if (qparams.hasOwnProperty(k)) {
92 obj[k] = qparams[k];
93 }
94 });
95 }
96 return obj;
97 }
98
99 // merge preferences:
100 // The assumption here is that obj is a sparse object, and that the
101 // defined keys should overwrite the corresponding values, but any
102 // existing keys that are NOT explicitly defined here should be left
103 // alone (not deleted).
104 mergePrefs(name: string, obj: any) {
105 const merged = this.cache[name] || {};
106 this.setPrefs(name, Object.assign(merged, obj));
107 }
108
109 addListener(listener: any) {
110 this.listeners.push(listener);
111 }
112
113 removeListener(listener: any) {
114 this.listeners = this.listeners.filter(function (obj) { return obj === listener; });
115 }
116
Sean Condon83fc39f2018-04-19 18:56:13 +0100117}