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 | */ |
| 16 | import { 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 | |
| 21 | /** |
| 22 | * ONOS GUI -- Util -- User Preference Service |
| 23 | */ |
Sean Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 24 | @Injectable({ |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 25 | providedIn: 'root', |
Sean Condon | a00bf38 | 2018-06-23 07:54:01 +0100 | [diff] [blame] | 26 | }) |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 27 | export class PrefsService { |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 28 | protected Prefs; |
| 29 | protected handlers: string[] = []; |
| 30 | cache: any; |
| 31 | listeners: any; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 32 | constructor( |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 33 | protected fs: FnService, |
| 34 | protected log: LogService, |
| 35 | protected wss: WebSocketService |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 36 | ) { |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 37 | 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 Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 43 | this.log.debug('PrefsService constructed'); |
| 44 | } |
| 45 | |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 46 | 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 Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 117 | } |