blob: 248fe51faf128a95ca5c77d561cbf2c0214c6950 [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
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 */
17import { Injectable } from '@angular/core';
18import { LogService } from '../../log.service';
19import { WebSocketService } from '../remote/websocket.service';
20
21/**
22 * A definition of Lion data
23 */
24interface Lion {
25 locale: any;
26 lion: any;
27}
28
29/**
30 * ONOS GUI -- Lion -- Localization Utilities
31 */
Sean Condonfd6d11b2018-06-02 20:29:49 +010032@Injectable({
33 providedIn: 'root',
34})
Sean Condon83fc39f2018-04-19 18:56:13 +010035export class LionService {
36
Sean Condon2bd11b72018-06-15 08:00:48 +010037 ubercache: any[] = [];
38 loadCb; // Function
Sean Condon83fc39f2018-04-19 18:56:13 +010039
Sean Condonfd6d11b2018-06-02 20:29:49 +010040 /**
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 Condon2bd11b72018-06-15 08:00:48 +010054 if (this.loadCb) {
55 this.log.debug('Calling the load callback');
56 this.loadCb();
57 }
Sean Condonfd6d11b2018-06-02 20:29:49 +010058
59 this.log.debug('LION service: uber-lion bundle received:', data);
60 }
61
Sean Condon83fc39f2018-04-19 18:56:13 +010062 constructor(
63 private log: LogService,
64 private wss: WebSocketService
65 ) {
Sean Condonfd6d11b2018-06-02 20:29:49 +010066 this.wss.bindHandlers(new Map<string, (data) => void>([
67 ['uberlion', (data) => this.uberlion(data) ]
68 ]));
Sean Condon83fc39f2018-04-19 18:56:13 +010069 this.log.debug('LionService constructed');
70 }
71
Sean Condonfd6d11b2018-06-02 20:29:49 +010072 /**
73 * Returns a lion bundle (function) for the given bundle ID (string)
Sean Condon83fc39f2018-04-19 18:56:13 +010074 * returns a function that takes a string and returns a string
75 */
76 bundle(bundleId: string): (string) => string {
Sean Condon2bd11b72018-06-15 08:00:48 +010077 let bundleObj = this.ubercache[bundleId];
Sean Condon83fc39f2018-04-19 18:56:13 +010078
Sean Condon2bd11b72018-06-15 08:00:48 +010079 if (!bundleObj) {
Sean Condon83fc39f2018-04-19 18:56:13 +010080 this.log.warn('No lion bundle registered:', bundleId);
Sean Condon2bd11b72018-06-15 08:00:48 +010081 bundleObj = {};
Sean Condon83fc39f2018-04-19 18:56:13 +010082 }
83
Sean Condon2bd11b72018-06-15 08:00:48 +010084 return (key) => {
85 return bundleObj[key] || '%' + key + '%';
86 };
Sean Condon83fc39f2018-04-19 18:56:13 +010087 }
Sean Condon83fc39f2018-04-19 18:56:13 +010088}