blob: 909b74b86841ad8917292c1233f209c0ff7d7a92 [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 */
Sean Condon28ecc5f2018-06-25 12:50:16 +010016import { Component, OnInit, AfterViewInit, OnDestroy } from '@angular/core';
17import { Observable, Subscription, fromEvent } from 'rxjs';
Sean Condonf4f54a12018-10-10 23:25:46 +010018import * as d3 from 'd3';
Sean Condon5ca00262018-09-06 17:55:25 +010019import {
20 LionService,
21 LogService,
22 ThemeService,
23 GlyphService,
24 WebSocketService,
Sean Condonf4f54a12018-10-10 23:25:46 +010025 WsOptions,
26 KeysService
Sean Condon5ca00262018-09-06 17:55:25 +010027} from 'gui2-fw-lib';
Sean Condon83fc39f2018-04-19 18:56:13 +010028import { OnosService, View } from './onos.service';
29
30// secret sauce
31const sauce: string[] = [
32 '6:69857666',
33 '9:826970',
34 '22:8069828667',
35 '6:698570688669887967',
36 '7:6971806889847186',
37 '22:8369867682',
38 '13:736583',
39 '7:667186698384',
40 '1:857780888778876787',
41 '20:70717066',
42 '24:886774868469',
43 '17:7487696973687580739078',
44 '14:70777086',
45 '17:7287687967',
46 '11:65678869706783687184',
47 '1:80777778',
48 '9:72696982',
49 '7:857165828967',
50 '11:8867696882678869759071'
51 // Add more sauce...
52];
53
54function cap(s: string): string {
55 return s ? s[0].toUpperCase() + s.slice(1) : s;
56}
57
58/**
59 * The main ONOS Component - the root of the whole user interface
60 */
61@Component({
62 selector: 'onos-root',
63 templateUrl: './onos.component.html',
Sean Condonfd6d11b2018-06-02 20:29:49 +010064 styleUrls: ['./onos.component.css', './onos.common.css']
Sean Condon83fc39f2018-04-19 18:56:13 +010065})
Sean Condon28ecc5f2018-06-25 12:50:16 +010066export class OnosComponent implements OnInit, AfterViewInit, OnDestroy {
67 private quickHelpSub: Subscription;
68 private quickHelpHandler: Observable<string>;
Sean Condon83fc39f2018-04-19 18:56:13 +010069
70 // view ID to help page url map.. injected via the servlet
71 viewMap: View[] = [
72 // {INJECTED-VIEW-DATA-START}
73 // {INJECTED-VIEW-DATA-END}
74 ];
75
76 defaultView = 'topo';
77 // TODO Move this to OnosModule - warning servlet will have to be updated too
78 viewDependencies: string[] = [];
79
80 constructor (
81 private lion: LionService,
Sean Condon83fc39f2018-04-19 18:56:13 +010082 private ts: ThemeService,
83 private gs: GlyphService,
Sean Condonf4f54a12018-10-10 23:25:46 +010084 private ks: KeysService,
Sean Condona00bf382018-06-23 07:54:01 +010085 public wss: WebSocketService,
Sean Condon83fc39f2018-04-19 18:56:13 +010086 private log: LogService,
Sean Condonb2c483c2019-01-16 20:28:55 +000087 public onos: OnosService
Sean Condon83fc39f2018-04-19 18:56:13 +010088 ) {
89
90// This is not like onos.js of AngularJS 1.x In this new structure modules are
91// imported instead in the OnosModule. view dependencies should be there too
92// const moduleDependencies = coreDependencies.concat(this.viewDependencies);
93
94 // Testing of debugging
95 log.debug('OnosComponent: testing logger.debug()');
96 log.info('OnosComponent: testing logger.info()');
97 log.warn('OnosComponent: testing logger.warn()');
98 log.error('OnosComponent: testing logger.error()');
99
Sean Condonfd6d11b2018-06-02 20:29:49 +0100100 this.wss.createWebSocket(<WsOptions>{ wsport: 8181});
101
Sean Condon83fc39f2018-04-19 18:56:13 +0100102 log.debug('OnosComponent constructed');
103 }
104
105 ngOnInit() {
106 this.viewMap.forEach(view =>
107 this.viewDependencies.push('ov' + cap(view.id)));
108
109 this.onos.viewMap = this.viewMap;
110
Sean Condon83fc39f2018-04-19 18:56:13 +0100111 // TODO: Enable this this.saucy(this.ee, this.ks);
112 this.log.debug('OnosComponent initialized');
113 }
114
Sean Condon28ecc5f2018-06-25 12:50:16 +0100115 /**
116 * Start the listener for keystrokes for QuickHelp
117 *
118 * This creates an observable that listens to the '/','\' and Esc keystrokes
119 * anywhere in the web page - it strips the keyCode out of the keystroke
120 * and passes this to another observable that filters only for these keystrokes
121 * and finally maps these key code to text literals to drive the
122 * quick help feature
123 */
124 ngAfterViewInit() {
Sean Condonf4f54a12018-10-10 23:25:46 +0100125 // const keyStrokeHandler =
126 // fromEvent(document, 'keyup').pipe(map((x: KeyboardEvent) => x.keyCode));
127 // this.quickHelpHandler = keyStrokeHandler.pipe(
128 // filter(x => {
129 // return [27, 191, 220].includes(x);
130 // })
131 // ).pipe(
132 // map(x => {
133 // let direction;
134 // switch (x) {
135 // case 27:
136 // direction = 'esc';
137 // break;
138 // case 191:
139 // direction = 'fwdslash';
140 // break;
141 // case 220:
142 // direction = 'backslash';
143 // break;
144 // default:
145 // direction = 'esc';
146 // }
147 // return direction;
148 // })
149 // );
150 //
151 // // TODO: Make a Quick Help component popup
152 // this.quickHelpSub = this.quickHelpHandler.subscribe((keyname) => {
153 // this.log.debug('Keystroke', keyname);
154 // });
155 this.ks.installOn(d3.select('body'));
156 this.log.debug('OnosComponent after view initialized');
Sean Condon28ecc5f2018-06-25 12:50:16 +0100157 }
158
Sean Condon2bd11b72018-06-15 08:00:48 +0100159 ngOnDestroy() {
160 if (this.wss.isConnected()) {
161 this.log.debug('Stopping Web Socket connection');
162 this.wss.closeWebSocket();
163 }
164
Sean Condon28ecc5f2018-06-25 12:50:16 +0100165 this.quickHelpSub.unsubscribe();
Sean Condon2bd11b72018-06-15 08:00:48 +0100166 this.log.debug('OnosComponent destroyed');
167 }
168
Sean Condon83fc39f2018-04-19 18:56:13 +0100169 saucy(ee, ks) {
Sean Condon28ecc5f2018-06-25 12:50:16 +0100170 const map1 = ee.genMap(sauce);
171 Object.keys(map1).forEach(function (k) {
172 ks.addSeq(k, map1[k]);
Sean Condon83fc39f2018-04-19 18:56:13 +0100173 });
174 }
175}