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 | */ |
| 17 | import { Injectable } from '@angular/core'; |
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 | * A definition of Lion data |
| 23 | */ |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 24 | export interface Lion { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 25 | locale: any; |
| 26 | lion: any; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * ONOS GUI -- Lion -- Localization Utilities |
| 31 | */ |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 32 | @Injectable({ |
| 33 | providedIn: 'root', |
| 34 | }) |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 35 | export class LionService { |
| 36 | |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 37 | ubercache: any[] = []; |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 38 | loadCbs = new Map<string, () => void>([]); // A map of functions |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 39 | |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 40 | /** |
| 41 | * Handler for uberlion event from WSS |
| 42 | */ |
| 43 | uberlion(data: Lion) { |
| 44 | this.ubercache = data.lion; |
| 45 | |
| 46 | this.log.info('LION service: Locale... [' + data.locale + ']'); |
| 47 | this.log.info('LION service: Bundles installed...'); |
| 48 | |
| 49 | for (const p in this.ubercache) { |
| 50 | if (this.ubercache[p]) { |
| 51 | this.log.info(' :=> ', p); |
| 52 | } |
| 53 | } |
Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame] | 54 | // If any component had registered a callback, call it now |
| 55 | // that LION is loaded |
| 56 | for (const cbname of this.loadCbs.keys()) { |
| 57 | this.log.debug('Updating', cbname, 'with LION'); |
| 58 | this.loadCbs.get(cbname)(); |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 59 | } |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 60 | |
| 61 | this.log.debug('LION service: uber-lion bundle received:', data); |
| 62 | } |
| 63 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 64 | constructor( |
| 65 | private log: LogService, |
| 66 | private wss: WebSocketService |
| 67 | ) { |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 68 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
| 69 | ['uberlion', (data) => this.uberlion(data) ] |
| 70 | ])); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 71 | this.log.debug('LionService constructed'); |
| 72 | } |
| 73 | |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 74 | /** |
| 75 | * Returns a lion bundle (function) for the given bundle ID (string) |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 76 | * returns a function that takes a string and returns a string |
| 77 | */ |
| 78 | bundle(bundleId: string): (string) => string { |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 79 | let bundleObj = this.ubercache[bundleId]; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 80 | |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 81 | if (!bundleObj) { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 82 | this.log.warn('No lion bundle registered:', bundleId); |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 83 | bundleObj = {}; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 86 | return (key) => { |
Sean Condon | b0a196a | 2019-04-19 09:50:44 +0100 | [diff] [blame] | 87 | return bundleObj[key] || '?' + key + '?'; |
Sean Condon | 2bd11b7 | 2018-06-15 08:00:48 +0100 | [diff] [blame] | 88 | }; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 89 | } |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 90 | } |