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