Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 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 | */ |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 16 | import {Inject, Injectable} from '@angular/core'; |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 17 | import { FnService } from './fn.service'; |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 18 | import { LogService } from '../log.service'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 19 | import { WebSocketService } from '../remote/websocket.service'; |
| 20 | |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 21 | const UPDATE_PREFS: string = 'updatePrefs'; |
| 22 | const UPDATE_PREFS_REQ: string = 'updatePrefReq'; |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 23 | |
| 24 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 25 | /** |
| 26 | * ONOS GUI -- Util -- User Preference Service |
| 27 | */ |
Sean Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 28 | @Injectable({ |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 29 | providedIn: 'root', |
Sean Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 30 | }) |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 31 | export class PrefsService { |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 32 | protected handlers: string[] = []; |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 33 | cache: Object; |
| 34 | listeners: ((data) => void)[] = []; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 35 | constructor( |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 36 | protected fs: FnService, |
| 37 | protected log: LogService, |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 38 | protected wss: WebSocketService, |
| 39 | @Inject('Window') private window: any |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 40 | ) { |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 41 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 42 | [UPDATE_PREFS, (data) => this.updatePrefs(data)] |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 43 | ])); |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 44 | this.handlers.push(UPDATE_PREFS); |
| 45 | |
| 46 | // When index.html is fetched it is served up by MainIndexResource.java |
| 47 | // which fetches userPrefs in to the global scope. |
| 48 | // After that updates are done through WebSocket |
Sean Condon | 98b6ddb | 2019-12-24 08:07:40 +0000 | [diff] [blame] | 49 | this.cache = (<any>Object).assign({}, this.window['userPrefs']); |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 50 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 51 | this.log.debug('PrefsService constructed'); |
| 52 | } |
| 53 | |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 54 | setPrefs(name: string, obj: Object) { |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 55 | // keep a cached copy of the object and send an update to server |
| 56 | this.cache[name] = obj; |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 57 | this.wss.sendEvent(UPDATE_PREFS_REQ, { key: name, value: obj }); |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 58 | } |
| 59 | updatePrefs(data: any) { |
| 60 | this.cache = data; |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 61 | this.listeners.forEach((lsnr) => lsnr(data) ); |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | asNumbers(obj: any, keys?: any, not?: any) { |
| 65 | if (!obj) { |
| 66 | return null; |
| 67 | } |
| 68 | |
| 69 | const skip = {}; |
| 70 | if (not) { |
| 71 | keys.forEach(k => { |
| 72 | skip[k] = 1; |
| 73 | } |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | if (!keys || not) { |
| 78 | // do them all |
| 79 | Array.from(obj).forEach((v, k) => { |
| 80 | if (!not || !skip[k]) { |
| 81 | obj[k] = Number(obj[k]); |
| 82 | } |
| 83 | }); |
| 84 | } else { |
| 85 | // do the explicitly named keys |
| 86 | keys.forEach(k => { |
| 87 | obj[k] = Number(obj[k]); |
| 88 | }); |
| 89 | } |
| 90 | return obj; |
| 91 | } |
| 92 | |
Sean Condon | 0d064ec | 2019-02-04 21:53:53 +0000 | [diff] [blame] | 93 | getPrefs(name: string, defaults: Object, qparams?: string) { |
Sean Condon | 98b6ddb | 2019-12-24 08:07:40 +0000 | [diff] [blame] | 94 | const obj = (<any>Object).assign({}, defaults || {}, this.cache[name] || {}); |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 95 | |
| 96 | // if query params are specified, they override... |
| 97 | if (this.fs.isO(qparams)) { |
| 98 | obj.forEach(k => { |
| 99 | if (qparams.hasOwnProperty(k)) { |
| 100 | obj[k] = qparams[k]; |
| 101 | } |
| 102 | }); |
| 103 | } |
| 104 | return obj; |
| 105 | } |
| 106 | |
| 107 | // merge preferences: |
| 108 | // The assumption here is that obj is a sparse object, and that the |
| 109 | // defined keys should overwrite the corresponding values, but any |
| 110 | // existing keys that are NOT explicitly defined here should be left |
| 111 | // alone (not deleted). |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 112 | mergePrefs(name: string, obj: any): void { |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 113 | const merged = this.cache[name] || {}; |
Sean Condon | 98b6ddb | 2019-12-24 08:07:40 +0000 | [diff] [blame] | 114 | this.setPrefs(name, (<any>Object).assign(merged, obj)); |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 115 | } |
| 116 | |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 117 | /** |
| 118 | * Add a listener function |
| 119 | * This will get called back when an 'updatePrefs' message is received on WSS |
| 120 | * @param listener a function that can accept one param - data |
| 121 | */ |
| 122 | addListener(listener: (data) => void): void { |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 123 | this.listeners.push(listener); |
| 124 | } |
| 125 | |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 126 | removeListener(listener: (data) => void) { |
| 127 | this.listeners = this.listeners.filter((obj) => obj !== listener); |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 128 | } |
| 129 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 130 | } |