blob: c5f5b3ae9dc1b701253edcaeac5fb9b0a5b7e0af [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
2 * Copyright 2014-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 */
Sean Condon28ecc5f2018-06-25 12:50:16 +010016import { Component, OnInit, AfterViewInit, OnDestroy } from '@angular/core';
17import { Observable, Subscription, fromEvent } from 'rxjs';
18import { map, filter } from 'rxjs/operators';
19
Sean Condon5ca00262018-09-06 17:55:25 +010020import {
21 LionService,
22 LogService,
23 ThemeService,
24 GlyphService,
25 WebSocketService,
26 WsOptions
27} 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 Condona00bf382018-06-23 07:54:01 +010084 public wss: WebSocketService,
Sean Condon83fc39f2018-04-19 18:56:13 +010085 private log: LogService,
86 private onos: OnosService
87 ) {
88
89// This is not like onos.js of AngularJS 1.x In this new structure modules are
90// imported instead in the OnosModule. view dependencies should be there too
91// const moduleDependencies = coreDependencies.concat(this.viewDependencies);
92
93 // Testing of debugging
94 log.debug('OnosComponent: testing logger.debug()');
95 log.info('OnosComponent: testing logger.info()');
96 log.warn('OnosComponent: testing logger.warn()');
97 log.error('OnosComponent: testing logger.error()');
98
Sean Condonfd6d11b2018-06-02 20:29:49 +010099 this.wss.createWebSocket(<WsOptions>{ wsport: 8181});
100
Sean Condon83fc39f2018-04-19 18:56:13 +0100101 log.debug('OnosComponent constructed');
102 }
103
104 ngOnInit() {
105 this.viewMap.forEach(view =>
106 this.viewDependencies.push('ov' + cap(view.id)));
107
108 this.onos.viewMap = this.viewMap;
109
Sean Condon83fc39f2018-04-19 18:56:13 +0100110 // TODO: Enable this this.saucy(this.ee, this.ks);
111 this.log.debug('OnosComponent initialized');
112 }
113
Sean Condon28ecc5f2018-06-25 12:50:16 +0100114 /**
115 * Start the listener for keystrokes for QuickHelp
116 *
117 * This creates an observable that listens to the '/','\' and Esc keystrokes
118 * anywhere in the web page - it strips the keyCode out of the keystroke
119 * and passes this to another observable that filters only for these keystrokes
120 * and finally maps these key code to text literals to drive the
121 * quick help feature
122 */
123 ngAfterViewInit() {
124 const keyStrokeHandler =
125 fromEvent(document, 'keyup').pipe(map((x: KeyboardEvent) => x.keyCode));
126 this.quickHelpHandler = keyStrokeHandler.pipe(
127 filter(x => {
128 return [27, 191, 220].includes(x);
129 })
130 ).pipe(
131 map(x => {
132 let direction;
133 switch (x) {
134 case 27:
135 direction = 'esc';
136 break;
137 case 191:
138 direction = 'fwdslash';
139 break;
140 case 220:
141 direction = 'backslash';
142 break;
143 default:
144 direction = 'esc';
145 }
146 return direction;
147 })
148 );
149
150 // TODO: Make a Quick Help component popup
151 this.quickHelpSub = this.quickHelpHandler.subscribe((keyname) => {
152 this.log.debug('Keystroke', keyname);
153 });
154 }
155
Sean Condon2bd11b72018-06-15 08:00:48 +0100156 ngOnDestroy() {
157 if (this.wss.isConnected()) {
158 this.log.debug('Stopping Web Socket connection');
159 this.wss.closeWebSocket();
160 }
161
Sean Condon28ecc5f2018-06-25 12:50:16 +0100162 this.quickHelpSub.unsubscribe();
Sean Condon2bd11b72018-06-15 08:00:48 +0100163 this.log.debug('OnosComponent destroyed');
164 }
165
Sean Condon83fc39f2018-04-19 18:56:13 +0100166 saucy(ee, ks) {
Sean Condon28ecc5f2018-06-25 12:50:16 +0100167 const map1 = ee.genMap(sauce);
168 Object.keys(map1).forEach(function (k) {
169 ks.addSeq(k, map1[k]);
Sean Condon83fc39f2018-04-19 18:56:13 +0100170 });
171 }
172}