blob: 9477699b426419abcd28930d52d860c9cc315bdd [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
Sean Condonf4f54a12018-10-10 23:25:46 +01002 * Copyright 2018-present Open Networking Foundation
Sean Condon83fc39f2018-04-19 18:56:13 +01003 *
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 */
Daniele Moro3fd3ad82020-01-22 12:07:35 +000016import {
17 Component,
18 OnInit,
19 AfterViewInit,
20 OnDestroy,
21 Inject
22} from '@angular/core';
Sean Condon28ecc5f2018-06-25 12:50:16 +010023import { Observable, Subscription, fromEvent } from 'rxjs';
Sean Condonf4f54a12018-10-10 23:25:46 +010024import * as d3 from 'd3';
Sean Condon5ca00262018-09-06 17:55:25 +010025import {
26 LionService,
27 LogService,
28 ThemeService,
29 GlyphService,
30 WebSocketService,
Sean Condonf4f54a12018-10-10 23:25:46 +010031 WsOptions,
32 KeysService
Sean Condon5ca00262018-09-06 17:55:25 +010033} from 'gui2-fw-lib';
Sean Condon83fc39f2018-04-19 18:56:13 +010034import { OnosService, View } from './onos.service';
35
36// secret sauce
37const sauce: string[] = [
38 '6:69857666',
39 '9:826970',
40 '22:8069828667',
41 '6:698570688669887967',
42 '7:6971806889847186',
43 '22:8369867682',
44 '13:736583',
45 '7:667186698384',
46 '1:857780888778876787',
47 '20:70717066',
48 '24:886774868469',
49 '17:7487696973687580739078',
50 '14:70777086',
51 '17:7287687967',
52 '11:65678869706783687184',
53 '1:80777778',
54 '9:72696982',
55 '7:857165828967',
56 '11:8867696882678869759071'
57 // Add more sauce...
58];
59
60function cap(s: string): string {
61 return s ? s[0].toUpperCase() + s.slice(1) : s;
62}
63
64/**
65 * The main ONOS Component - the root of the whole user interface
66 */
67@Component({
68 selector: 'onos-root',
69 templateUrl: './onos.component.html',
Sean Condonfd6d11b2018-06-02 20:29:49 +010070 styleUrls: ['./onos.component.css', './onos.common.css']
Sean Condon83fc39f2018-04-19 18:56:13 +010071})
Sean Condon28ecc5f2018-06-25 12:50:16 +010072export class OnosComponent implements OnInit, AfterViewInit, OnDestroy {
73 private quickHelpSub: Subscription;
74 private quickHelpHandler: Observable<string>;
Sean Condon83fc39f2018-04-19 18:56:13 +010075
76 // view ID to help page url map.. injected via the servlet
77 viewMap: View[] = [
78 // {INJECTED-VIEW-DATA-START}
79 // {INJECTED-VIEW-DATA-END}
80 ];
81
82 defaultView = 'topo';
83 // TODO Move this to OnosModule - warning servlet will have to be updated too
84 viewDependencies: string[] = [];
85
86 constructor (
87 private lion: LionService,
Sean Condon83fc39f2018-04-19 18:56:13 +010088 private ts: ThemeService,
89 private gs: GlyphService,
Sean Condonf4f54a12018-10-10 23:25:46 +010090 private ks: KeysService,
Sean Condona00bf382018-06-23 07:54:01 +010091 public wss: WebSocketService,
Sean Condon83fc39f2018-04-19 18:56:13 +010092 private log: LogService,
Daniele Moro3fd3ad82020-01-22 12:07:35 +000093 public onos: OnosService,
94 @Inject('Window') private window: any
Sean Condon83fc39f2018-04-19 18:56:13 +010095 ) {
96
97// This is not like onos.js of AngularJS 1.x In this new structure modules are
98// imported instead in the OnosModule. view dependencies should be there too
99// const moduleDependencies = coreDependencies.concat(this.viewDependencies);
100
101 // Testing of debugging
102 log.debug('OnosComponent: testing logger.debug()');
103 log.info('OnosComponent: testing logger.info()');
104 log.warn('OnosComponent: testing logger.warn()');
105 log.error('OnosComponent: testing logger.error()');
106
Daniele Moro3fd3ad82020-01-22 12:07:35 +0000107 this.wss.createWebSocket(<WsOptions>{ wsport: this.window.location.port});
Sean Condonfd6d11b2018-06-02 20:29:49 +0100108
Sean Condon83fc39f2018-04-19 18:56:13 +0100109 log.debug('OnosComponent constructed');
110 }
111
112 ngOnInit() {
113 this.viewMap.forEach(view =>
114 this.viewDependencies.push('ov' + cap(view.id)));
115
116 this.onos.viewMap = this.viewMap;
117
Sean Condon83fc39f2018-04-19 18:56:13 +0100118 // TODO: Enable this this.saucy(this.ee, this.ks);
119 this.log.debug('OnosComponent initialized');
120 }
121
Sean Condon28ecc5f2018-06-25 12:50:16 +0100122 /**
123 * Start the listener for keystrokes for QuickHelp
124 *
125 * This creates an observable that listens to the '/','\' and Esc keystrokes
126 * anywhere in the web page - it strips the keyCode out of the keystroke
127 * and passes this to another observable that filters only for these keystrokes
128 * and finally maps these key code to text literals to drive the
129 * quick help feature
130 */
131 ngAfterViewInit() {
Sean Condonf4f54a12018-10-10 23:25:46 +0100132 // const keyStrokeHandler =
133 // fromEvent(document, 'keyup').pipe(map((x: KeyboardEvent) => x.keyCode));
134 // this.quickHelpHandler = keyStrokeHandler.pipe(
135 // filter(x => {
136 // return [27, 191, 220].includes(x);
137 // })
138 // ).pipe(
139 // map(x => {
140 // let direction;
141 // switch (x) {
142 // case 27:
143 // direction = 'esc';
144 // break;
145 // case 191:
146 // direction = 'fwdslash';
147 // break;
148 // case 220:
149 // direction = 'backslash';
150 // break;
151 // default:
152 // direction = 'esc';
153 // }
154 // return direction;
155 // })
156 // );
157 //
158 // // TODO: Make a Quick Help component popup
159 // this.quickHelpSub = this.quickHelpHandler.subscribe((keyname) => {
160 // this.log.debug('Keystroke', keyname);
161 // });
162 this.ks.installOn(d3.select('body'));
163 this.log.debug('OnosComponent after view initialized');
Sean Condon28ecc5f2018-06-25 12:50:16 +0100164 }
165
Sean Condon2bd11b72018-06-15 08:00:48 +0100166 ngOnDestroy() {
167 if (this.wss.isConnected()) {
168 this.log.debug('Stopping Web Socket connection');
169 this.wss.closeWebSocket();
170 }
171
Sean Condon28ecc5f2018-06-25 12:50:16 +0100172 this.quickHelpSub.unsubscribe();
Sean Condon2bd11b72018-06-15 08:00:48 +0100173 this.log.debug('OnosComponent destroyed');
174 }
175
Sean Condon83fc39f2018-04-19 18:56:13 +0100176 saucy(ee, ks) {
Sean Condon28ecc5f2018-06-25 12:50:16 +0100177 const map1 = ee.genMap(sauce);
178 Object.keys(map1).forEach(function (k) {
179 ks.addSeq(k, map1[k]);
Sean Condon83fc39f2018-04-19 18:56:13 +0100180 });
181 }
182}