blob: 8ce1a6ef94bfbe5b3cb4a8d9c1e689f808606d9e [file] [log] [blame]
Sean Condonf4f54a12018-10-10 23:25:46 +01001/*
Sean Condon91481822019-01-01 13:56:14 +00002 * Copyright 2019-present Open Networking Foundation
Sean Condonf4f54a12018-10-10 23:25:46 +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 Condon0c577f62018-11-18 22:40:05 +000016import {
Sean Condonff85fbe2019-03-16 14:28:46 +000017 AfterContentInit,
18 Component, HostListener, Inject, Input,
Sean Condon0c577f62018-11-18 22:40:05 +000019 OnDestroy,
Sean Condon021f0fa2018-12-06 23:31:11 -080020 OnInit, SimpleChange,
Sean Condon0c577f62018-11-18 22:40:05 +000021 ViewChild
22} from '@angular/core';
Sean Condon55c30532018-10-29 12:26:57 +000023import * as d3 from 'd3';
Sean Condonf4f54a12018-10-10 23:25:46 +010024import {
Sean Condon28884332019-03-21 14:07:00 +000025 FnService,
26 IconService,
Sean Condon0c577f62018-11-18 22:40:05 +000027 KeysService,
Sean Condon28884332019-03-21 14:07:00 +000028 KeysToken,
29 LionService,
Sean Condon0c577f62018-11-18 22:40:05 +000030 LogService,
31 PrefsService,
32 SvgUtilService,
Sean Condon28884332019-03-21 14:07:00 +000033 TopoZoomPrefs,
Sean Condon0c577f62018-11-18 22:40:05 +000034 WebSocketService,
Sean Condon28884332019-03-21 14:07:00 +000035 ZoomUtils
Sean Condonf4f54a12018-10-10 23:25:46 +010036} from 'gui2-fw-lib';
Sean Condon0c577f62018-11-18 22:40:05 +000037import {InstanceComponent} from '../panel/instance/instance.component';
Sean Condon0c577f62018-11-18 22:40:05 +000038import {DetailsComponent} from '../panel/details/details.component';
39import {BackgroundSvgComponent} from '../layer/backgroundsvg/backgroundsvg.component';
40import {ForceSvgComponent} from '../layer/forcesvg/forcesvg.component';
41import {TopologyService} from '../topology.service';
Sean Condon91481822019-01-01 13:56:14 +000042import {
Sean Condon71910542019-02-16 18:16:42 +000043 GridDisplayToggle,
Sean Condon91481822019-01-01 13:56:14 +000044 HostLabelToggle,
45 LabelToggle,
46 UiElement
47} from '../layer/forcesvg/models';
Sean Condonb2c483c2019-01-16 20:28:55 +000048import {
Sean Condon28884332019-03-21 14:07:00 +000049 ALL_TRAFFIC,
50 BKGRND_SELECT,
51 BKGRND_TOGGLE,
52 CANCEL_TRAFFIC,
53 CYCLEGRIDDISPLAY_BTN,
54 CYCLEHOSTLABEL_BTN,
55 CYCLELABELS_BTN,
56 DETAILS_TOGGLE,
57 EQMASTER_BTN,
58 HOSTS_TOGGLE,
59 INSTANCE_TOGGLE,
60 LAYOUT_ACCESS_BTN,
61 LAYOUT_DEFAULT_BTN,
62 OFFLINE_TOGGLE,
63 PORTS_TOGGLE,
64 QUICKHELP_BTN,
65 RESETZOOM_BTN,
66 SUMMARY_TOGGLE
Sean Condonb2c483c2019-01-16 20:28:55 +000067} from '../panel/toolbar/toolbar.component';
Sean Condon50855cf2018-12-23 15:37:42 +000068import {TrafficService} from '../traffic.service';
Sean Condon91481822019-01-01 13:56:14 +000069import {ZoomableDirective} from '../layer/zoomable.directive';
Sean Condon0d064ec2019-02-04 21:53:53 +000070import {MapObject} from '../layer/maputils';
Sean Condon28884332019-03-21 14:07:00 +000071import {LayoutService, LayoutType} from '../layout.service';
Sean Condon64ea7d22019-04-12 19:39:13 +010072import {SelectedEvent} from '../layer/forcesvg/visuals/nodevisual';
Sean Condonf4f54a12018-10-10 23:25:46 +010073
Sean Condonb2c483c2019-01-16 20:28:55 +000074const TOPO2_PREFS = 'topo2_prefs';
Sean Condon0d064ec2019-02-04 21:53:53 +000075const TOPO_MAPID_PREFS = 'topo_mapid';
76
Sean Condonb2c483c2019-01-16 20:28:55 +000077const PREF_BG = 'bg';
78const PREF_DETAIL = 'detail';
79const PREF_DLBLS = 'dlbls';
80const PREF_HLBLS = 'hlbls';
Sean Condon71910542019-02-16 18:16:42 +000081const PREF_GRID = 'grid';
Sean Condonb2c483c2019-01-16 20:28:55 +000082const PREF_HOSTS = 'hosts';
83const PREF_INSTS = 'insts';
84const PREF_OFFDEV = 'offdev';
85const PREF_PORTHL = 'porthl';
86const PREF_SUMMARY = 'summary';
87const PREF_TOOLBAR = 'toolbar';
Sean Condon9de21352019-04-06 19:22:27 +010088const PREF_PINNED = 'pinned';
Sean Condonb2c483c2019-01-16 20:28:55 +000089
Sean Condon64ea7d22019-04-12 19:39:13 +010090const BACKGROUND_ELEMENTS = [
91 'svg topo2',
92 'path bgmap'
93];
94
Sean Condonb2c483c2019-01-16 20:28:55 +000095/**
Sean Condon0d064ec2019-02-04 21:53:53 +000096 * Model of the topo2_prefs object - this is a subset of the overall Prefs returned
Sean Condonb2c483c2019-01-16 20:28:55 +000097 * by the server
98 */
99export interface Topo2Prefs {
100 bg: number;
101 detail: number;
102 dlbls: number;
103 hlbls: number;
104 hosts: number;
105 insts: number;
106 offdev: number;
107 porthl: number;
108 spr: number;
109 ovid: string;
110 summary: number;
111 toolbar: number;
Sean Condon71910542019-02-16 18:16:42 +0000112 grid: number;
Sean Condon9de21352019-04-06 19:22:27 +0100113 pinned: number;
Sean Condonb2c483c2019-01-16 20:28:55 +0000114}
115
Sean Condonf4f54a12018-10-10 23:25:46 +0100116/**
117 * ONOS GUI Topology View
118 *
119 * This Topology View component is the top level component in a hierarchy that
120 * comprises the whole Topology View
121 *
122 * There are three main parts (panels, graphical and breadcrumbs)
123 * The panel hierarchy
124 * |-- Instances Panel (shows ONOS instances)
125 * |-- Summary Panel (summary of ONOS)
126 * |-- Toolbar Panel (the toolbar)
127 * |-- Details Panel (when a node is selected in the Force graphical view (see below))
128 *
129 * The graphical hierarchy contains
130 * Topology (this)
131 * |-- No Devices Connected (only of there are no nodes to show)
132 * |-- Zoom Layer (everything beneath this can be zoomed and panned)
133 * |-- Background (container for any backgrounds - can be toggled on and off)
134 * |-- Map
135 * |-- Forces (all of the nodes and links laid out by a d3.force simulation)
136 *
137 * The breadcrumbs
138 * |-- Breadcrumb (in region view a way of navigating back up through regions)
139 */
140@Component({
141 selector: 'onos-topology',
142 templateUrl: './topology.component.html',
143 styleUrls: ['./topology.component.css']
144})
Sean Condonff85fbe2019-03-16 14:28:46 +0000145export class TopologyComponent implements AfterContentInit, OnInit, OnDestroy {
146 @Input() bannerHeight: number = 48;
Sean Condonaa4366d2018-11-02 14:29:01 +0000147 // These are references to the components inserted in the template
Sean Condonf4f54a12018-10-10 23:25:46 +0100148 @ViewChild(InstanceComponent) instance: InstanceComponent;
Sean Condonf4f54a12018-10-10 23:25:46 +0100149 @ViewChild(DetailsComponent) details: DetailsComponent;
Sean Condonaa4366d2018-11-02 14:29:01 +0000150 @ViewChild(BackgroundSvgComponent) background: BackgroundSvgComponent;
151 @ViewChild(ForceSvgComponent) force: ForceSvgComponent;
Sean Condon91481822019-01-01 13:56:14 +0000152 @ViewChild(ZoomableDirective) zoomDirective: ZoomableDirective;
Sean Condonf4f54a12018-10-10 23:25:46 +0100153
154 flashMsg: string = '';
Sean Condonb2c483c2019-01-16 20:28:55 +0000155 // These are used as defaults if nothing is set on the server
156 prefsState: Topo2Prefs = <Topo2Prefs>{
157 bg: 0,
158 detail: 1,
159 dlbls: 0,
160 hlbls: 2,
161 hosts: 0,
162 insts: 1,
163 offdev: 1,
164 ovid: 'traffic', // default to traffic overlay
165 porthl: 1,
166 spr: 0,
167 summary: 1,
168 toolbar: 0,
Sean Condon71910542019-02-16 18:16:42 +0000169 grid: 0
Sean Condonb2c483c2019-01-16 20:28:55 +0000170 };
Sean Condon0d064ec2019-02-04 21:53:53 +0000171
172 mapIdState: MapObject = <MapObject>{
173 id: undefined,
174 scale: 1.0
175 };
176 mapSelShown: boolean = false;
Sean Condon91481822019-01-01 13:56:14 +0000177 lionFn; // Function
Sean Condon55c30532018-10-29 12:26:57 +0000178
Sean Condon71910542019-02-16 18:16:42 +0000179 gridShown: boolean = true;
180 geoGridShown: boolean = true;
181
Sean Condonf4f54a12018-10-10 23:25:46 +0100182 constructor(
183 protected log: LogService,
184 protected fs: FnService,
185 protected ks: KeysService,
186 protected sus: SvgUtilService,
187 protected ps: PrefsService,
Sean Condon55c30532018-10-29 12:26:57 +0000188 protected wss: WebSocketService,
Sean Condonaa4366d2018-11-02 14:29:01 +0000189 protected ts: TopologyService,
Sean Condon91481822019-01-01 13:56:14 +0000190 protected trs: TrafficService,
191 protected is: IconService,
192 private lion: LionService,
Sean Condon28884332019-03-21 14:07:00 +0000193 private layout: LayoutService,
Sean Condonff85fbe2019-03-16 14:28:46 +0000194 @Inject('Window') public window: any,
Sean Condonf4f54a12018-10-10 23:25:46 +0100195 ) {
Sean Condon91481822019-01-01 13:56:14 +0000196 if (this.lion.ubercache.length === 0) {
197 this.lionFn = this.dummyLion;
198 this.lion.loadCbs.set('topo-toolbar', () => this.doLion());
199 } else {
200 this.doLion();
201 }
Sean Condonf4f54a12018-10-10 23:25:46 +0100202
Sean Condon91481822019-01-01 13:56:14 +0000203 this.is.loadIconDef('bird');
204 this.is.loadIconDef('active');
205 this.is.loadIconDef('uiAttached');
206 this.is.loadIconDef('m_switch');
207 this.is.loadIconDef('m_roadm');
208 this.is.loadIconDef('m_router');
209 this.is.loadIconDef('m_uiAttached');
210 this.is.loadIconDef('m_endstation');
211 this.is.loadIconDef('m_ports');
212 this.is.loadIconDef('m_summary');
213 this.is.loadIconDef('m_details');
214 this.is.loadIconDef('m_map');
Sean Condon0d064ec2019-02-04 21:53:53 +0000215 this.is.loadIconDef('m_selectMap');
Sean Condon91481822019-01-01 13:56:14 +0000216 this.is.loadIconDef('m_cycleLabels');
Sean Condon71910542019-02-16 18:16:42 +0000217 this.is.loadIconDef('m_cycleGridDisplay');
Sean Condon91481822019-01-01 13:56:14 +0000218 this.is.loadIconDef('m_resetZoom');
219 this.is.loadIconDef('m_eqMaster');
220 this.is.loadIconDef('m_unknown');
221 this.is.loadIconDef('m_allTraffic');
222 this.is.loadIconDef('deviceTable');
223 this.is.loadIconDef('flowTable');
224 this.is.loadIconDef('portTable');
225 this.is.loadIconDef('groupTable');
226 this.is.loadIconDef('meterTable');
227 this.is.loadIconDef('triangleUp');
Sean Condon28884332019-03-21 14:07:00 +0000228 this.is.loadIconDef('m_disjointPaths');
229 this.is.loadIconDef('m_fiberSwitch');
Sean Condonf4f54a12018-10-10 23:25:46 +0100230 this.log.debug('Topology component constructed');
231 }
232
Sean Condon91481822019-01-01 13:56:14 +0000233 /**
234 * Static functions must come before member variables
Sean Condonff85fbe2019-03-16 14:28:46 +0000235 * @param index Corresponds to LabelToggle.Enum index
Sean Condon91481822019-01-01 13:56:14 +0000236 */
Sean Condon021f0fa2018-12-06 23:31:11 -0800237 private static deviceLabelFlashMessage(index: number): string {
238 switch (index) {
Sean Condon91481822019-01-01 13:56:14 +0000239 case 0: return 'fl_device_labels_hide';
240 case 1: return 'fl_device_labels_show_friendly';
241 case 2: return 'fl_device_labels_show_id';
Sean Condon021f0fa2018-12-06 23:31:11 -0800242 }
243 }
244
245 private static hostLabelFlashMessage(index: number): string {
246 switch (index) {
Sean Condon91481822019-01-01 13:56:14 +0000247 case 0: return 'fl_host_labels_hide';
248 case 1: return 'fl_host_labels_show_friendly';
249 case 2: return 'fl_host_labels_show_ip';
250 case 3: return 'fl_host_labels_show_mac';
Sean Condon021f0fa2018-12-06 23:31:11 -0800251 }
252 }
253
Sean Condon71910542019-02-16 18:16:42 +0000254 private static gridDisplayFlashMessage(index: number): string {
255 switch (index) {
256 case 0: return 'fl_grid_display_hide';
257 case 1: return 'fl_grid_display_1000';
258 case 2: return 'fl_grid_display_geo';
259 case 3: return 'fl_grid_display_both';
260 }
261 }
262
Sean Condon91481822019-01-01 13:56:14 +0000263 /**
264 * Pass the list of Key Commands to the KeyService, and initialize the Topology
265 * Service - which communicates with through the WebSocket to the ONOS server
266 * to get the nodes and links.
267 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100268 ngOnInit() {
269 this.bindCommands();
Sean Condonaa4366d2018-11-02 14:29:01 +0000270 // The components from the template are handed over to TopologyService here
271 // so that WebSocket responses can be passed back in to them
272 // The handling of the WebSocket call is delegated out to the Topology
273 // Service just to compartmentalize things a bit
274 this.ts.init(this.instance, this.background, this.force);
Sean Condonb2c483c2019-01-16 20:28:55 +0000275
276 this.ps.addListener((data) => this.prefsUpdateHandler(data));
277 this.prefsState = this.ps.getPrefs(TOPO2_PREFS, this.prefsState);
Sean Condon0d064ec2019-02-04 21:53:53 +0000278 this.mapIdState = this.ps.getPrefs(TOPO_MAPID_PREFS, this.mapIdState);
Sean Condonff85fbe2019-03-16 14:28:46 +0000279
Sean Condonf4f54a12018-10-10 23:25:46 +0100280 this.log.debug('Topology component initialized');
281 }
282
Sean Condonff85fbe2019-03-16 14:28:46 +0000283 ngAfterContentInit(): void {
284 // Scale the window initially - then after resize
285 const zoomMapExtents = ZoomUtils.zoomToWindowSize(
286 this.bannerHeight, this.window.innerWidth, this.window.innerHeight);
287 this.zoomDirective.changeZoomLevel(zoomMapExtents, true);
288 this.log.debug('Topology zoom initialized',
289 this.bannerHeight, this.window.innerWidth, this.window.innerHeight,
290 zoomMapExtents);
291 }
292
Sean Condon91481822019-01-01 13:56:14 +0000293 /**
Sean Condonb2c483c2019-01-16 20:28:55 +0000294 * Callback function that's called whenever new Prefs are received from WebSocket
295 *
296 * Note: At present the backend server does not filter updated by logged in user,
297 * so you might get updates pertaining to a different user
298 */
299 prefsUpdateHandler(data: any): void {
300 // Extract the TOPO2 prefs from it
Sean Condon0d064ec2019-02-04 21:53:53 +0000301 if (data[TOPO2_PREFS]) {
302 this.prefsState = data[TOPO2_PREFS];
303 }
Sean Condon0d064ec2019-02-04 21:53:53 +0000304 this.log.debug('Updated topo2 prefs', this.prefsState, this.mapIdState);
Sean Condonb2c483c2019-01-16 20:28:55 +0000305 }
306
307 /**
Sean Condon91481822019-01-01 13:56:14 +0000308 * When this component is being stopped, disconnect the TopologyService from
309 * the WebSocket
310 */
Sean Condonaa4366d2018-11-02 14:29:01 +0000311 ngOnDestroy() {
312 this.ts.destroy();
Sean Condonb2c483c2019-01-16 20:28:55 +0000313 this.ps.removeListener((data) => this.prefsUpdateHandler(data));
Sean Condonaa4366d2018-11-02 14:29:01 +0000314 this.log.debug('Topology component destroyed');
315 }
316
Sean Condonff85fbe2019-03-16 14:28:46 +0000317 @HostListener('window:resize', ['$event'])
318 onResize(event) {
319 const zoomMapExtents = ZoomUtils.zoomToWindowSize(
320 this.bannerHeight, event.target.innerWidth, event.target.innerHeight);
321 this.zoomDirective.changeZoomLevel(zoomMapExtents, true);
322 this.log.debug('Topology window resize',
323 event.target.innerWidth, event.target.innerHeight, this.bannerHeight, zoomMapExtents);
324 }
325
Sean Condon91481822019-01-01 13:56:14 +0000326 /**
327 * When ever a toolbar button is clicked, an event is sent up from toolbar
328 * component which is caught and passed on to here.
329 * @param name The name of the button that was clicked
330 */
331 toolbarButtonClicked(name: string) {
332 switch (name) {
Sean Condonb2c483c2019-01-16 20:28:55 +0000333 case INSTANCE_TOGGLE:
Sean Condon91481822019-01-01 13:56:14 +0000334 this.toggleInstancePanel();
335 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000336 case SUMMARY_TOGGLE:
Sean Condon91481822019-01-01 13:56:14 +0000337 this.toggleSummary();
338 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000339 case DETAILS_TOGGLE:
Sean Condon91481822019-01-01 13:56:14 +0000340 this.toggleDetails();
341 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000342 case HOSTS_TOGGLE:
Sean Condon91481822019-01-01 13:56:14 +0000343 this.toggleHosts();
344 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000345 case OFFLINE_TOGGLE:
Sean Condon91481822019-01-01 13:56:14 +0000346 this.toggleOfflineDevices();
347 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000348 case PORTS_TOGGLE:
Sean Condon91481822019-01-01 13:56:14 +0000349 this.togglePorts();
350 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000351 case BKGRND_TOGGLE:
Sean Condon91481822019-01-01 13:56:14 +0000352 this.toggleBackground();
353 break;
Sean Condon0d064ec2019-02-04 21:53:53 +0000354 case BKGRND_SELECT:
355 this.mapSelShown = !this.mapSelShown;
356 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000357 case CYCLELABELS_BTN:
Sean Condon91481822019-01-01 13:56:14 +0000358 this.cycleDeviceLabels();
359 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000360 case CYCLEHOSTLABEL_BTN:
Sean Condon91481822019-01-01 13:56:14 +0000361 this.cycleHostLabels();
362 break;
Sean Condon71910542019-02-16 18:16:42 +0000363 case CYCLEGRIDDISPLAY_BTN:
364 this.cycleGridDisplay();
365 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000366 case RESETZOOM_BTN:
Sean Condon91481822019-01-01 13:56:14 +0000367 this.resetZoom();
368 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000369 case EQMASTER_BTN:
Sean Condon91481822019-01-01 13:56:14 +0000370 this.equalizeMasters();
371 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000372 case CANCEL_TRAFFIC:
Sean Condon91481822019-01-01 13:56:14 +0000373 this.cancelTraffic();
374 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000375 case ALL_TRAFFIC:
Sean Condon91481822019-01-01 13:56:14 +0000376 this.monitorAllTraffic();
377 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000378 case QUICKHELP_BTN:
379 this.ks.quickHelpShown = true;
380 break;
Sean Condon28884332019-03-21 14:07:00 +0000381 case LAYOUT_DEFAULT_BTN:
382 this.layout.changeLayout(LayoutType.LAYOUT_DEFAULT);
383 break;
384 case LAYOUT_ACCESS_BTN:
385 this.layout.changeLayout(LayoutType.LAYOUT_ACCESS);
386 break;
Sean Condon91481822019-01-01 13:56:14 +0000387 default:
388 this.log.warn('Unhandled Toolbar action', name);
389 }
390 }
391
392 /**
393 * The list of key strokes that will be active in the Topology View.
394 *
395 * This action map is passed to the KeyService through the bindCommands()
396 * when this component is being initialized
397 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100398 actionMap() {
399 return {
Sean Condon50855cf2018-12-23 15:37:42 +0000400 A: [() => {this.monitorAllTraffic(); }, 'Monitor all traffic'],
Sean Condonf4f54a12018-10-10 23:25:46 +0100401 B: [(token) => {this.toggleBackground(token); }, 'Toggle background'],
402 D: [(token) => {this.toggleDetails(token); }, 'Toggle details panel'],
Sean Condon71910542019-02-16 18:16:42 +0000403 E: [() => {this.equalizeMasters(); }, 'Equalize mastership roles'],
404 H: [() => {this.toggleHosts(); }, 'Toggle host visibility'],
Sean Condonf4f54a12018-10-10 23:25:46 +0100405 I: [(token) => {this.toggleInstancePanel(token); }, 'Toggle ONOS Instance Panel'],
Sean Condon0d064ec2019-02-04 21:53:53 +0000406 G: [() => {this.mapSelShown = !this.mapSelShown; }, 'Show map selection dialog'],
Sean Condon71910542019-02-16 18:16:42 +0000407 L: [() => {this.cycleDeviceLabels(); }, 'Cycle device labels'],
Sean Condonf4f54a12018-10-10 23:25:46 +0100408 M: [() => {this.toggleOfflineDevices(); }, 'Toggle offline visibility'],
Sean Condon71910542019-02-16 18:16:42 +0000409 O: [() => {this.toggleSummary(); }, 'Toggle the Summary Panel'],
410 P: [(token) => {this.togglePorts(token); }, 'Toggle Port Highlighting'],
411 Q: [() => {this.cycleGridDisplay(); }, 'Cycle grid display'],
412 R: [() => {this.resetZoom(); }, 'Reset pan / zoom'],
Sean Condon9de21352019-04-06 19:22:27 +0100413 U: [() => {this.unpinOrFreezeNodes(); }, 'Unpin or freeze nodes'],
Sean Condon71910542019-02-16 18:16:42 +0000414 X: [() => {this.resetNodeLocation(); }, 'Reset Node Location'],
Sean Condonf4f54a12018-10-10 23:25:46 +0100415 dot: [() => {this.toggleToolbar(); }, 'Toggle Toolbar'],
Sean Condon50855cf2018-12-23 15:37:42 +0000416 0: [() => {this.cancelTraffic(); }, 'Cancel traffic monitoring'],
Sean Condonf4f54a12018-10-10 23:25:46 +0100417 'shift-L': [() => {this.cycleHostLabels(); }, 'Cycle host labels'],
418
419 // -- instance color palette debug
Sean Condon55c30532018-10-29 12:26:57 +0000420 9: () => {
421 this.sus.cat7().testCard(d3.select('svg#topo2'));
Sean Condonf4f54a12018-10-10 23:25:46 +0100422 },
423
Sean Condonb2c483c2019-01-16 20:28:55 +0000424 esc: [() => {this.handleEscape(); }, 'Cancel commands'],
Sean Condonf4f54a12018-10-10 23:25:46 +0100425
426 // TODO update after adding in Background Service
427 // topology overlay selections
428 // F1: function () { t2tbs.fnKey(0); },
429 // F2: function () { t2tbs.fnKey(1); },
430 // F3: function () { t2tbs.fnKey(2); },
431 // F4: function () { t2tbs.fnKey(3); },
432 // F5: function () { t2tbs.fnKey(4); },
433 //
434 // _keyListener: t2tbs.keyListener.bind(t2tbs),
435
436 _helpFormat: [
437 ['I', 'O', 'D', 'H', 'M', 'P', 'dash', 'B'],
438 ['X', 'Z', 'N', 'L', 'shift-L', 'U', 'R', 'E', 'dot'],
439 [], // this column reserved for overlay actions
440 ],
441 };
442 }
443
444
445 bindCommands(additional?: any) {
446
447 const am = this.actionMap();
448 const add = this.fs.isO(additional);
449
Sean Condonf4f54a12018-10-10 23:25:46 +0100450 this.ks.keyBindings(am);
451
452 this.ks.gestureNotes([
453 ['click', 'Select the item and show details'],
454 ['shift-click', 'Toggle selection state'],
455 ['drag', 'Reposition (and pin) device / host'],
456 ['cmd-scroll', 'Zoom in / out'],
457 ['cmd-drag', 'Pan'],
458 ]);
459 }
460
461 handleEscape() {
462
463 if (false) {
464 // TODO: Cancel show mastership
465 // TODO: Cancel Active overlay
466 // TODO: Reinstate with components
467 } else {
Sean Condonb2c483c2019-01-16 20:28:55 +0000468 this.nodeSelected(undefined);
Sean Condonf4f54a12018-10-10 23:25:46 +0100469 this.log.debug('Handling escape');
470 // } else if (t2rs.deselectAllNodes()) {
471 // // else if we have node selections, deselect them all
472 // // (work already done)
473 // } else if (t2rs.deselectLink()) {
474 // // else if we have a link selection, deselect it
475 // // (work already done)
476 // } else if (t2is.isVisible()) {
477 // // If the instance panel is visible, close it
478 // t2is.toggle();
479 // } else if (t2sp.isVisible()) {
480 // // If the summary panel is visible, close it
481 // t2sp.toggle();
482 }
483 }
484
Sean Condonb2c483c2019-01-16 20:28:55 +0000485 /**
486 * Updates the cache of preferences locally and onwards to the PrefsService
487 * @param what The attribute of the local topo2-prefs cache to update
488 * @param b the value to update it with
489 */
490 updatePrefsState(what: string, b: number) {
491 this.prefsState[what] = b;
492 this.ps.setPrefs(TOPO2_PREFS, this.prefsState);
Sean Condonf4f54a12018-10-10 23:25:46 +0100493 }
494
Sean Condonb2c483c2019-01-16 20:28:55 +0000495 /**
496 * When the button is clicked on the toolbar or the L key is pressed
497 * 1) cycle through options
498 * 2) flash up a message
499 * 3a) Update the local prefs cache
500 * 3b) And passes on to the global prefs service which sends back to the server
501 * 3c) It also has a knock on effect of passing it on to ForceSvgComponent
502 * because prefsState.dlbls is given as an input to it
503 * 3d) This will in turn pass it down to the DeviceSvgComponent which
504 * displays the label
505 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100506 protected cycleDeviceLabels() {
Sean Condonff85fbe2019-03-16 14:28:46 +0000507 const old: LabelToggle.Enum = this.prefsState.dlbls;
Sean Condon021f0fa2018-12-06 23:31:11 -0800508 const next = LabelToggle.next(old);
Sean Condon91481822019-01-01 13:56:14 +0000509 this.flashMsg = this.lionFn(TopologyComponent.deviceLabelFlashMessage(next));
Sean Condonb2c483c2019-01-16 20:28:55 +0000510 this.updatePrefsState(PREF_DLBLS, next);
Sean Condon021f0fa2018-12-06 23:31:11 -0800511 this.log.debug('Cycling device labels', old, next);
Sean Condonf4f54a12018-10-10 23:25:46 +0100512 }
513
514 protected cycleHostLabels() {
Sean Condonff85fbe2019-03-16 14:28:46 +0000515 const old: HostLabelToggle.Enum = this.prefsState.hlbls;
Sean Condon021f0fa2018-12-06 23:31:11 -0800516 const next = HostLabelToggle.next(old);
Sean Condon91481822019-01-01 13:56:14 +0000517 this.flashMsg = this.lionFn(TopologyComponent.hostLabelFlashMessage(next));
Sean Condonb2c483c2019-01-16 20:28:55 +0000518 this.updatePrefsState(PREF_HLBLS, next);
Sean Condon021f0fa2018-12-06 23:31:11 -0800519 this.log.debug('Cycling host labels', old, next);
Sean Condonf4f54a12018-10-10 23:25:46 +0100520 }
521
Sean Condon71910542019-02-16 18:16:42 +0000522 protected cycleGridDisplay() {
Sean Condonff85fbe2019-03-16 14:28:46 +0000523 const old: GridDisplayToggle.Enum = this.prefsState.grid;
Sean Condon71910542019-02-16 18:16:42 +0000524 const next = GridDisplayToggle.next(old);
525 this.flashMsg = this.lionFn(TopologyComponent.gridDisplayFlashMessage(next));
526 this.updatePrefsState(PREF_GRID, next);
527 this.log.debug('Cycling grid display', old, next);
528 }
529
Sean Condonb2c483c2019-01-16 20:28:55 +0000530 /**
531 * When the button is clicked on the toolbar or the B key is pressed
532 * 1) Find the inverse of the current state (held as 1 or 0)
533 * 2) Flash up a message on screen
534 * 3b) And passes on to the global prefs service which sends back to the server
535 * 3c) It also has a knock on effect of passing it on to ToolbarComponent
536 * because prefsState.bg is given as an input to it
Sean Condonff85fbe2019-03-16 14:28:46 +0000537 * @param token not currently used
Sean Condonb2c483c2019-01-16 20:28:55 +0000538 */
Sean Condon91481822019-01-01 13:56:14 +0000539 protected toggleBackground(token?: KeysToken) {
Sean Condonb2c483c2019-01-16 20:28:55 +0000540 const bg: boolean = !Boolean(this.prefsState.bg);
541 this.flashMsg = this.lionFn(bg ? 'show' : 'hide') +
Sean Condon91481822019-01-01 13:56:14 +0000542 ' ' + this.lionFn('fl_background_map');
Sean Condonb2c483c2019-01-16 20:28:55 +0000543 this.updatePrefsState(PREF_BG, bg ? 1 : 0);
544 this.log.debug('Toggling background', token, bg ? 'shown' : 'hidden');
Sean Condonf4f54a12018-10-10 23:25:46 +0100545 }
546
Sean Condon91481822019-01-01 13:56:14 +0000547 protected toggleDetails(token?: KeysToken) {
Sean Condonb2c483c2019-01-16 20:28:55 +0000548 const on: boolean = !Boolean(this.prefsState.detail);
549 this.flashMsg = this.lionFn(on ? 'show' : 'hide') +
550 ' ' + this.lionFn('fl_panel_details');
551 this.updatePrefsState(PREF_DETAIL, on ? 1 : 0);
552 this.log.debug('Toggling details', token);
Sean Condonf4f54a12018-10-10 23:25:46 +0100553 }
554
Sean Condon91481822019-01-01 13:56:14 +0000555 protected toggleInstancePanel(token?: KeysToken) {
Sean Condonb2c483c2019-01-16 20:28:55 +0000556 const on: boolean = !Boolean(this.prefsState.insts);
Sean Condon91481822019-01-01 13:56:14 +0000557 this.flashMsg = this.lionFn(on ? 'show' : 'hide') +
558 ' ' + this.lionFn('fl_panel_instances');
Sean Condonb2c483c2019-01-16 20:28:55 +0000559 this.updatePrefsState(PREF_INSTS, on ? 1 : 0);
Sean Condon91481822019-01-01 13:56:14 +0000560 this.log.debug('Toggling instances', token, on);
Sean Condonf4f54a12018-10-10 23:25:46 +0100561 }
562
563 protected toggleSummary() {
Sean Condonb2c483c2019-01-16 20:28:55 +0000564 const on: boolean = !Boolean(this.prefsState.summary);
Sean Condon91481822019-01-01 13:56:14 +0000565 this.flashMsg = this.lionFn(on ? 'show' : 'hide') +
566 ' ' + this.lionFn('fl_panel_summary');
Sean Condonb2c483c2019-01-16 20:28:55 +0000567 this.updatePrefsState(PREF_SUMMARY, on ? 1 : 0);
568 }
569
570 protected togglePorts(token?: KeysToken) {
571 const current: boolean = !Boolean(this.prefsState.porthl);
572 this.flashMsg = this.lionFn(current ? 'enable' : 'disable') +
573 ' ' + this.lionFn('fl_port_highlighting');
574 this.updatePrefsState(PREF_PORTHL, current ? 1 : 0);
575 this.log.debug(current ? 'Enable' : 'Disable', 'port highlighting');
576 }
577
578 protected toggleToolbar() {
579 const on: boolean = !Boolean(this.prefsState.toolbar);
580 this.updatePrefsState(PREF_TOOLBAR, on ? 1 : 0);
581 this.log.debug('toggling toolbar', on ? 'shown' : 'hidden');
582 }
583
584 protected toggleHosts() {
585 const current: boolean = !Boolean(this.prefsState.hosts);
586 this.flashMsg = this.lionFn('hosts') + ' ' +
587 this.lionFn(this.force.showHosts ? 'visible' : 'hidden');
588 this.updatePrefsState(PREF_HOSTS, current ? 1 : 0);
589 this.log.debug('toggling hosts: ', this.prefsState.hosts ? 'Show' : 'Hide');
590 }
591
592 protected toggleOfflineDevices() {
593 const on: boolean = !Boolean(this.prefsState.offdev);
594 this.flashMsg = this.lionFn(on ? 'show' : 'hide') +
595 ' ' + this.lionFn('fl_offline_devices');
596 this.updatePrefsState(PREF_OFFDEV, on ? 1 : 0);
597 this.log.debug('toggling offline devices', this.prefsState.offdev);
Sean Condonf4f54a12018-10-10 23:25:46 +0100598 }
599
600 protected resetZoom() {
Sean Condonff85fbe2019-03-16 14:28:46 +0000601 const zoomMapExtents = ZoomUtils.zoomToWindowSize(
602 this.bannerHeight, this.window.innerWidth, this.window.innerHeight);
603 this.zoomDirective.changeZoomLevel(zoomMapExtents, false);
Sean Condon91481822019-01-01 13:56:14 +0000604 this.flashMsg = this.lionFn('fl_pan_zoom_reset');
Sean Condonf4f54a12018-10-10 23:25:46 +0100605 }
606
Sean Condonf4f54a12018-10-10 23:25:46 +0100607 protected equalizeMasters() {
Sean Condon64ea7d22019-04-12 19:39:13 +0100608 this.wss.sendEvent('equalizeMasters', {});
Sean Condon91481822019-01-01 13:56:14 +0000609 this.flashMsg = this.lionFn('fl_eq_masters');
Sean Condonf4f54a12018-10-10 23:25:46 +0100610 this.log.debug('equalizing masters');
Sean Condonf4f54a12018-10-10 23:25:46 +0100611 }
612
Sean Condon9de21352019-04-06 19:22:27 +0100613 /**
614 * If any nodes with fixed positions had been dragged out of place
615 * then put back where they belong
616 * If there are some devices selected reset only these
617 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100618 protected resetNodeLocation() {
Sean Condon9de21352019-04-06 19:22:27 +0100619 const numNodes = this.force.resetNodeLocations();
620 this.flashMsg = this.lionFn('fl_reset_node_locations') +
621 '(' + String(numNodes) + ')';
622 this.log.debug('resetting ', numNodes, 'node(s) location');
Sean Condonf4f54a12018-10-10 23:25:46 +0100623 }
624
Sean Condon9de21352019-04-06 19:22:27 +0100625 /**
626 * Toggle floating nodes between pinned and frozen
627 * If there are floating nodes selected toggle only these
628 */
629 protected unpinOrFreezeNodes() {
630 const pinned: boolean = !Boolean(this.prefsState.pinned);
631 const numNodes = this.force.unpinOrFreezeNodes(pinned);
632 this.flashMsg = this.lionFn(pinned ?
633 'fl_pinned_floating_nodes' : 'fl_unpinned_floating_nodes') +
634 '(' + String(numNodes) + ')';
635 this.updatePrefsState(PREF_PINNED, pinned ? 1 : 0);
636 this.log.debug('Toggling pinning for floating ', numNodes, 'nodes', pinned);
Sean Condonf4f54a12018-10-10 23:25:46 +0100637 }
638
Sean Condon91481822019-01-01 13:56:14 +0000639 /**
640 * Check to see if this is needed anymore
Sean Condonff85fbe2019-03-16 14:28:46 +0000641 * @param what - a key stroke
Sean Condon91481822019-01-01 13:56:14 +0000642 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100643 protected notValid(what) {
644 this.log.warn('topo.js getActionEntry(): Not a valid ' + what);
645 }
646
Sean Condon91481822019-01-01 13:56:14 +0000647 /**
648 * Check to see if this is needed anymore
Sean Condonff85fbe2019-03-16 14:28:46 +0000649 * @param key - a key stroke
Sean Condon91481822019-01-01 13:56:14 +0000650 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100651 getActionEntry(key) {
652 let entry;
653
654 if (!key) {
655 this.notValid('key');
656 return null;
657 }
658
659 entry = this.actionMap()[key];
660
661 if (!entry) {
662 this.notValid('actionMap (' + key + ') entry');
663 return null;
664 }
665 return this.fs.isA(entry) || [entry, ''];
666 }
667
Sean Condon91481822019-01-01 13:56:14 +0000668 /**
669 * An event handler that updates the details panel as items are
670 * selected in the forcesvg layer
Sean Condond88f3662019-04-03 16:35:30 +0100671 *
672 * @param nodesOrLink the item(s) to display details of
Sean Condon91481822019-01-01 13:56:14 +0000673 */
Sean Condond88f3662019-04-03 16:35:30 +0100674 nodeSelected(nodesOrLink: UiElement[]) {
675 this.details.ngOnChanges({'selectedNodes':
676 new SimpleChange(undefined, nodesOrLink, true)});
Sean Condon0c577f62018-11-18 22:40:05 +0000677 }
678
Sean Condon50855cf2018-12-23 15:37:42 +0000679 /**
680 * Enable traffic monitoring
681 */
682 monitorAllTraffic() {
Sean Condon91481822019-01-01 13:56:14 +0000683 // TODO: Implement support for toggling between bits, packets and octets
684 this.flashMsg = this.lionFn('tr_fl_pstats_bits');
Sean Condon50855cf2018-12-23 15:37:42 +0000685 this.trs.init(this.force);
686 }
687
688 /**
689 * Cancel traffic monitoring
690 */
691 cancelTraffic() {
Sean Condon91481822019-01-01 13:56:14 +0000692 this.flashMsg = this.lionFn('fl_monitoring_canceled');
Sean Condon50855cf2018-12-23 15:37:42 +0000693 this.trs.destroy();
694 }
Sean Condon91481822019-01-01 13:56:14 +0000695
Sean Condon0d064ec2019-02-04 21:53:53 +0000696 changeMap(map: MapObject) {
697 this.mapSelShown = false; // Hide the MapSelector component
698 this.mapIdState = map;
699 this.ps.setPrefs(TOPO_MAPID_PREFS, this.mapIdState);
700 this.log.debug('Map has been changed to ', map);
701 }
702
Sean Condon1ae15802019-03-02 09:07:18 +0000703 mapExtentsZoom(zoomMapExtents: TopoZoomPrefs) {
704 // this.zoomDirective.updateZoomState(zoomPrefs.tx, zoomPrefs.ty, zoomPrefs.sc);
705 this.zoomDirective.changeZoomLevel(zoomMapExtents);
706 this.log.debug('Map zoom prefs updated', zoomMapExtents);
707 }
708
Sean Condon64ea7d22019-04-12 19:39:13 +0100709 backgroundClicked(event: MouseEvent) {
710 const elemTagName = event.target['tagName'] + ' ' + event.target['id'];
711 if (BACKGROUND_ELEMENTS.includes(elemTagName)) {
712 this.force.updateSelected(
713 <SelectedEvent>{
714 uiElement: undefined,
715 deselecting: true
716 }
717 );
718 }
719 }
720
Sean Condon91481822019-01-01 13:56:14 +0000721 /**
722 * Read the LION bundle for Toolbar and set up the lionFn
723 */
724 doLion() {
725 this.lionFn = this.lion.bundle('core.view.Topo');
726 }
727
728 /**
729 * A dummy implementation of the lionFn until the response is received and the LION
730 * bundle is received from the WebSocket
731 */
732 dummyLion(key: string): string {
733 return '%' + key + '%';
734 }
Sean Condonf4f54a12018-10-10 23:25:46 +0100735}