Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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'; |
| 18 | import { LogService } from '../../log.service'; |
| 19 | import { WebSocketService } from '../remote/websocket.service'; |
| 20 | |
| 21 | /** |
| 22 | * A definition of Lion data |
| 23 | */ |
| 24 | interface Lion { |
| 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 | |
| 37 | ubercache: any[]; |
| 38 | |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 39 | /** |
| 40 | * Handler for uberlion event from WSS |
| 41 | */ |
| 42 | uberlion(data: Lion) { |
| 43 | this.ubercache = data.lion; |
| 44 | |
| 45 | this.log.info('LION service: Locale... [' + data.locale + ']'); |
| 46 | this.log.info('LION service: Bundles installed...'); |
| 47 | |
| 48 | for (const p in this.ubercache) { |
| 49 | if (this.ubercache[p]) { |
| 50 | this.log.info(' :=> ', p); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | this.log.debug('LION service: uber-lion bundle received:', data); |
| 55 | } |
| 56 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 57 | constructor( |
| 58 | private log: LogService, |
| 59 | private wss: WebSocketService |
| 60 | ) { |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 61 | this.wss.bindHandlers(new Map<string, (data) => void>([ |
| 62 | ['uberlion', (data) => this.uberlion(data) ] |
| 63 | ])); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 64 | this.log.debug('LionService constructed'); |
| 65 | } |
| 66 | |
Sean Condon | fd6d11b | 2018-06-02 20:29:49 +0100 | [diff] [blame] | 67 | /** |
| 68 | * Returns a lion bundle (function) for the given bundle ID (string) |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 69 | * returns a function that takes a string and returns a string |
| 70 | */ |
| 71 | bundle(bundleId: string): (string) => string { |
| 72 | let bundle = this.ubercache[bundleId]; |
| 73 | |
| 74 | if (!bundle) { |
| 75 | this.log.warn('No lion bundle registered:', bundleId); |
| 76 | bundle = {}; |
| 77 | } |
| 78 | |
| 79 | return this.getKey; |
| 80 | } |
| 81 | |
| 82 | getKey(key: string): string { |
| 83 | return this.bundle[key] || '%' + key + '%'; |
| 84 | } |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 85 | } |