blob: 97b8764443c744fe6d44da8b8a9048ca2e61b113 [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 Condon19e83672019-04-13 16:21:52 +010068import {TrafficService, TrafficType} 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 Condon19e83672019-04-13 16:21:52 +010089const PREF_TRAFFIC = 'traffic';
Sean Condonb2c483c2019-01-16 20:28:55 +000090
Sean Condon64ea7d22019-04-12 19:39:13 +010091const BACKGROUND_ELEMENTS = [
92 'svg topo2',
93 'path bgmap'
94];
95
Sean Condonb2c483c2019-01-16 20:28:55 +000096/**
Sean Condon0d064ec2019-02-04 21:53:53 +000097 * Model of the topo2_prefs object - this is a subset of the overall Prefs returned
Sean Condonb2c483c2019-01-16 20:28:55 +000098 * by the server
99 */
100export interface Topo2Prefs {
101 bg: number;
102 detail: number;
103 dlbls: number;
104 hlbls: number;
105 hosts: number;
106 insts: number;
107 offdev: number;
108 porthl: number;
109 spr: number;
110 ovid: string;
111 summary: number;
112 toolbar: number;
Sean Condon71910542019-02-16 18:16:42 +0000113 grid: number;
Sean Condon9de21352019-04-06 19:22:27 +0100114 pinned: number;
Sean Condon19e83672019-04-13 16:21:52 +0100115 traffic: number;
Sean Condonb2c483c2019-01-16 20:28:55 +0000116}
117
Sean Condonf4f54a12018-10-10 23:25:46 +0100118/**
119 * ONOS GUI Topology View
120 *
121 * This Topology View component is the top level component in a hierarchy that
122 * comprises the whole Topology View
123 *
124 * There are three main parts (panels, graphical and breadcrumbs)
125 * The panel hierarchy
126 * |-- Instances Panel (shows ONOS instances)
127 * |-- Summary Panel (summary of ONOS)
128 * |-- Toolbar Panel (the toolbar)
129 * |-- Details Panel (when a node is selected in the Force graphical view (see below))
130 *
131 * The graphical hierarchy contains
132 * Topology (this)
133 * |-- No Devices Connected (only of there are no nodes to show)
134 * |-- Zoom Layer (everything beneath this can be zoomed and panned)
135 * |-- Background (container for any backgrounds - can be toggled on and off)
136 * |-- Map
137 * |-- Forces (all of the nodes and links laid out by a d3.force simulation)
138 *
139 * The breadcrumbs
140 * |-- Breadcrumb (in region view a way of navigating back up through regions)
141 */
142@Component({
143 selector: 'onos-topology',
144 templateUrl: './topology.component.html',
145 styleUrls: ['./topology.component.css']
146})
Sean Condonff85fbe2019-03-16 14:28:46 +0000147export class TopologyComponent implements AfterContentInit, OnInit, OnDestroy {
148 @Input() bannerHeight: number = 48;
Sean Condonaa4366d2018-11-02 14:29:01 +0000149 // These are references to the components inserted in the template
Sean Condonf4f54a12018-10-10 23:25:46 +0100150 @ViewChild(InstanceComponent) instance: InstanceComponent;
Sean Condonf4f54a12018-10-10 23:25:46 +0100151 @ViewChild(DetailsComponent) details: DetailsComponent;
Sean Condonaa4366d2018-11-02 14:29:01 +0000152 @ViewChild(BackgroundSvgComponent) background: BackgroundSvgComponent;
153 @ViewChild(ForceSvgComponent) force: ForceSvgComponent;
Sean Condon91481822019-01-01 13:56:14 +0000154 @ViewChild(ZoomableDirective) zoomDirective: ZoomableDirective;
Sean Condonf4f54a12018-10-10 23:25:46 +0100155
156 flashMsg: string = '';
Sean Condonb2c483c2019-01-16 20:28:55 +0000157 // These are used as defaults if nothing is set on the server
158 prefsState: Topo2Prefs = <Topo2Prefs>{
159 bg: 0,
160 detail: 1,
161 dlbls: 0,
162 hlbls: 2,
163 hosts: 0,
164 insts: 1,
165 offdev: 1,
166 ovid: 'traffic', // default to traffic overlay
167 porthl: 1,
168 spr: 0,
169 summary: 1,
170 toolbar: 0,
Sean Condon19e83672019-04-13 16:21:52 +0100171 grid: 0,
172 pinned: 0,
173 traffic: 2 // default to PORTSTATSPKTSEC, as it will iterate over to 0 on init
Sean Condonb2c483c2019-01-16 20:28:55 +0000174 };
Sean Condon0d064ec2019-02-04 21:53:53 +0000175
176 mapIdState: MapObject = <MapObject>{
177 id: undefined,
178 scale: 1.0
179 };
180 mapSelShown: boolean = false;
Sean Condon91481822019-01-01 13:56:14 +0000181 lionFn; // Function
Sean Condon55c30532018-10-29 12:26:57 +0000182
Sean Condon71910542019-02-16 18:16:42 +0000183 gridShown: boolean = true;
184 geoGridShown: boolean = true;
185
Sean Condonf4f54a12018-10-10 23:25:46 +0100186 constructor(
187 protected log: LogService,
188 protected fs: FnService,
189 protected ks: KeysService,
190 protected sus: SvgUtilService,
191 protected ps: PrefsService,
Sean Condon55c30532018-10-29 12:26:57 +0000192 protected wss: WebSocketService,
Sean Condonaa4366d2018-11-02 14:29:01 +0000193 protected ts: TopologyService,
Sean Condon91481822019-01-01 13:56:14 +0000194 protected trs: TrafficService,
195 protected is: IconService,
196 private lion: LionService,
Sean Condon28884332019-03-21 14:07:00 +0000197 private layout: LayoutService,
Sean Condonff85fbe2019-03-16 14:28:46 +0000198 @Inject('Window') public window: any,
Sean Condonf4f54a12018-10-10 23:25:46 +0100199 ) {
Sean Condon91481822019-01-01 13:56:14 +0000200 if (this.lion.ubercache.length === 0) {
201 this.lionFn = this.dummyLion;
202 this.lion.loadCbs.set('topo-toolbar', () => this.doLion());
203 } else {
204 this.doLion();
205 }
Sean Condonf4f54a12018-10-10 23:25:46 +0100206
Sean Condon91481822019-01-01 13:56:14 +0000207 this.is.loadIconDef('bird');
208 this.is.loadIconDef('active');
209 this.is.loadIconDef('uiAttached');
210 this.is.loadIconDef('m_switch');
211 this.is.loadIconDef('m_roadm');
212 this.is.loadIconDef('m_router');
213 this.is.loadIconDef('m_uiAttached');
214 this.is.loadIconDef('m_endstation');
215 this.is.loadIconDef('m_ports');
216 this.is.loadIconDef('m_summary');
217 this.is.loadIconDef('m_details');
218 this.is.loadIconDef('m_map');
Sean Condon0d064ec2019-02-04 21:53:53 +0000219 this.is.loadIconDef('m_selectMap');
Sean Condon91481822019-01-01 13:56:14 +0000220 this.is.loadIconDef('m_cycleLabels');
Sean Condon71910542019-02-16 18:16:42 +0000221 this.is.loadIconDef('m_cycleGridDisplay');
Sean Condon91481822019-01-01 13:56:14 +0000222 this.is.loadIconDef('m_resetZoom');
223 this.is.loadIconDef('m_eqMaster');
224 this.is.loadIconDef('m_unknown');
225 this.is.loadIconDef('m_allTraffic');
226 this.is.loadIconDef('deviceTable');
227 this.is.loadIconDef('flowTable');
228 this.is.loadIconDef('portTable');
229 this.is.loadIconDef('groupTable');
230 this.is.loadIconDef('meterTable');
231 this.is.loadIconDef('triangleUp');
Sean Condon28884332019-03-21 14:07:00 +0000232 this.is.loadIconDef('m_disjointPaths');
233 this.is.loadIconDef('m_fiberSwitch');
Sean Condonf4f54a12018-10-10 23:25:46 +0100234 this.log.debug('Topology component constructed');
235 }
236
Sean Condon91481822019-01-01 13:56:14 +0000237 /**
238 * Static functions must come before member variables
Sean Condonff85fbe2019-03-16 14:28:46 +0000239 * @param index Corresponds to LabelToggle.Enum index
Sean Condon91481822019-01-01 13:56:14 +0000240 */
Sean Condon021f0fa2018-12-06 23:31:11 -0800241 private static deviceLabelFlashMessage(index: number): string {
242 switch (index) {
Sean Condon91481822019-01-01 13:56:14 +0000243 case 0: return 'fl_device_labels_hide';
244 case 1: return 'fl_device_labels_show_friendly';
245 case 2: return 'fl_device_labels_show_id';
Sean Condon021f0fa2018-12-06 23:31:11 -0800246 }
247 }
248
249 private static hostLabelFlashMessage(index: number): string {
250 switch (index) {
Sean Condon91481822019-01-01 13:56:14 +0000251 case 0: return 'fl_host_labels_hide';
252 case 1: return 'fl_host_labels_show_friendly';
253 case 2: return 'fl_host_labels_show_ip';
254 case 3: return 'fl_host_labels_show_mac';
Sean Condon021f0fa2018-12-06 23:31:11 -0800255 }
256 }
257
Sean Condon71910542019-02-16 18:16:42 +0000258 private static gridDisplayFlashMessage(index: number): string {
259 switch (index) {
260 case 0: return 'fl_grid_display_hide';
261 case 1: return 'fl_grid_display_1000';
262 case 2: return 'fl_grid_display_geo';
263 case 3: return 'fl_grid_display_both';
264 }
265 }
266
Sean Condon19e83672019-04-13 16:21:52 +0100267 private static trafficTypeFlashMessage(index: number): string {
268 switch (index) {
269 case 0: return 'tr_fl_fstats_bytes';
270 case 1: return 'tr_fl_pstats_bits';
271 case 2: return 'tr_fl_pstats_pkts';
272 }
273 }
274
Sean Condon91481822019-01-01 13:56:14 +0000275 /**
276 * Pass the list of Key Commands to the KeyService, and initialize the Topology
277 * Service - which communicates with through the WebSocket to the ONOS server
278 * to get the nodes and links.
279 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100280 ngOnInit() {
281 this.bindCommands();
Sean Condonaa4366d2018-11-02 14:29:01 +0000282 // The components from the template are handed over to TopologyService here
283 // so that WebSocket responses can be passed back in to them
284 // The handling of the WebSocket call is delegated out to the Topology
285 // Service just to compartmentalize things a bit
286 this.ts.init(this.instance, this.background, this.force);
Sean Condonb2c483c2019-01-16 20:28:55 +0000287
288 this.ps.addListener((data) => this.prefsUpdateHandler(data));
289 this.prefsState = this.ps.getPrefs(TOPO2_PREFS, this.prefsState);
Sean Condon0d064ec2019-02-04 21:53:53 +0000290 this.mapIdState = this.ps.getPrefs(TOPO_MAPID_PREFS, this.mapIdState);
Sean Condon19e83672019-04-13 16:21:52 +0100291 this.trs.init(this.force);
Sean Condonff85fbe2019-03-16 14:28:46 +0000292
Sean Condonf4f54a12018-10-10 23:25:46 +0100293 this.log.debug('Topology component initialized');
294 }
295
Sean Condonff85fbe2019-03-16 14:28:46 +0000296 ngAfterContentInit(): void {
297 // Scale the window initially - then after resize
298 const zoomMapExtents = ZoomUtils.zoomToWindowSize(
299 this.bannerHeight, this.window.innerWidth, this.window.innerHeight);
300 this.zoomDirective.changeZoomLevel(zoomMapExtents, true);
301 this.log.debug('Topology zoom initialized',
302 this.bannerHeight, this.window.innerWidth, this.window.innerHeight,
303 zoomMapExtents);
304 }
305
Sean Condon91481822019-01-01 13:56:14 +0000306 /**
Sean Condonb2c483c2019-01-16 20:28:55 +0000307 * Callback function that's called whenever new Prefs are received from WebSocket
308 *
309 * Note: At present the backend server does not filter updated by logged in user,
310 * so you might get updates pertaining to a different user
311 */
312 prefsUpdateHandler(data: any): void {
313 // Extract the TOPO2 prefs from it
Sean Condon0d064ec2019-02-04 21:53:53 +0000314 if (data[TOPO2_PREFS]) {
315 this.prefsState = data[TOPO2_PREFS];
316 }
Sean Condon0d064ec2019-02-04 21:53:53 +0000317 this.log.debug('Updated topo2 prefs', this.prefsState, this.mapIdState);
Sean Condonb2c483c2019-01-16 20:28:55 +0000318 }
319
320 /**
Sean Condon91481822019-01-01 13:56:14 +0000321 * When this component is being stopped, disconnect the TopologyService from
322 * the WebSocket
323 */
Sean Condonaa4366d2018-11-02 14:29:01 +0000324 ngOnDestroy() {
325 this.ts.destroy();
Sean Condonb2c483c2019-01-16 20:28:55 +0000326 this.ps.removeListener((data) => this.prefsUpdateHandler(data));
Sean Condon19e83672019-04-13 16:21:52 +0100327 this.trs.destroy();
Sean Condonaa4366d2018-11-02 14:29:01 +0000328 this.log.debug('Topology component destroyed');
329 }
330
Sean Condonff85fbe2019-03-16 14:28:46 +0000331 @HostListener('window:resize', ['$event'])
332 onResize(event) {
333 const zoomMapExtents = ZoomUtils.zoomToWindowSize(
334 this.bannerHeight, event.target.innerWidth, event.target.innerHeight);
335 this.zoomDirective.changeZoomLevel(zoomMapExtents, true);
336 this.log.debug('Topology window resize',
337 event.target.innerWidth, event.target.innerHeight, this.bannerHeight, zoomMapExtents);
338 }
339
Sean Condon91481822019-01-01 13:56:14 +0000340 /**
341 * When ever a toolbar button is clicked, an event is sent up from toolbar
342 * component which is caught and passed on to here.
343 * @param name The name of the button that was clicked
344 */
345 toolbarButtonClicked(name: string) {
346 switch (name) {
Sean Condonb2c483c2019-01-16 20:28:55 +0000347 case INSTANCE_TOGGLE:
Sean Condon91481822019-01-01 13:56:14 +0000348 this.toggleInstancePanel();
349 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000350 case SUMMARY_TOGGLE:
Sean Condon91481822019-01-01 13:56:14 +0000351 this.toggleSummary();
352 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000353 case DETAILS_TOGGLE:
Sean Condon91481822019-01-01 13:56:14 +0000354 this.toggleDetails();
355 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000356 case HOSTS_TOGGLE:
Sean Condon91481822019-01-01 13:56:14 +0000357 this.toggleHosts();
358 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000359 case OFFLINE_TOGGLE:
Sean Condon91481822019-01-01 13:56:14 +0000360 this.toggleOfflineDevices();
361 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000362 case PORTS_TOGGLE:
Sean Condon91481822019-01-01 13:56:14 +0000363 this.togglePorts();
364 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000365 case BKGRND_TOGGLE:
Sean Condon91481822019-01-01 13:56:14 +0000366 this.toggleBackground();
367 break;
Sean Condon0d064ec2019-02-04 21:53:53 +0000368 case BKGRND_SELECT:
369 this.mapSelShown = !this.mapSelShown;
370 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000371 case CYCLELABELS_BTN:
Sean Condon91481822019-01-01 13:56:14 +0000372 this.cycleDeviceLabels();
373 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000374 case CYCLEHOSTLABEL_BTN:
Sean Condon91481822019-01-01 13:56:14 +0000375 this.cycleHostLabels();
376 break;
Sean Condon71910542019-02-16 18:16:42 +0000377 case CYCLEGRIDDISPLAY_BTN:
378 this.cycleGridDisplay();
379 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000380 case RESETZOOM_BTN:
Sean Condon91481822019-01-01 13:56:14 +0000381 this.resetZoom();
382 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000383 case EQMASTER_BTN:
Sean Condon91481822019-01-01 13:56:14 +0000384 this.equalizeMasters();
385 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000386 case CANCEL_TRAFFIC:
Sean Condon91481822019-01-01 13:56:14 +0000387 this.cancelTraffic();
388 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000389 case ALL_TRAFFIC:
Sean Condon19e83672019-04-13 16:21:52 +0100390 this.cycleTrafficTypeDisplay();
Sean Condon91481822019-01-01 13:56:14 +0000391 break;
Sean Condonb2c483c2019-01-16 20:28:55 +0000392 case QUICKHELP_BTN:
393 this.ks.quickHelpShown = true;
394 break;
Sean Condon28884332019-03-21 14:07:00 +0000395 case LAYOUT_DEFAULT_BTN:
396 this.layout.changeLayout(LayoutType.LAYOUT_DEFAULT);
397 break;
398 case LAYOUT_ACCESS_BTN:
399 this.layout.changeLayout(LayoutType.LAYOUT_ACCESS);
400 break;
Sean Condon91481822019-01-01 13:56:14 +0000401 default:
402 this.log.warn('Unhandled Toolbar action', name);
403 }
404 }
405
406 /**
407 * The list of key strokes that will be active in the Topology View.
408 *
409 * This action map is passed to the KeyService through the bindCommands()
410 * when this component is being initialized
411 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100412 actionMap() {
413 return {
Sean Condon19e83672019-04-13 16:21:52 +0100414 A: [() => {this.cycleTrafficTypeDisplay(); }, 'Monitor all traffic'],
Sean Condonf4f54a12018-10-10 23:25:46 +0100415 B: [(token) => {this.toggleBackground(token); }, 'Toggle background'],
416 D: [(token) => {this.toggleDetails(token); }, 'Toggle details panel'],
Sean Condon71910542019-02-16 18:16:42 +0000417 E: [() => {this.equalizeMasters(); }, 'Equalize mastership roles'],
418 H: [() => {this.toggleHosts(); }, 'Toggle host visibility'],
Sean Condonf4f54a12018-10-10 23:25:46 +0100419 I: [(token) => {this.toggleInstancePanel(token); }, 'Toggle ONOS Instance Panel'],
Sean Condon0d064ec2019-02-04 21:53:53 +0000420 G: [() => {this.mapSelShown = !this.mapSelShown; }, 'Show map selection dialog'],
Sean Condon71910542019-02-16 18:16:42 +0000421 L: [() => {this.cycleDeviceLabels(); }, 'Cycle device labels'],
Sean Condonf4f54a12018-10-10 23:25:46 +0100422 M: [() => {this.toggleOfflineDevices(); }, 'Toggle offline visibility'],
Sean Condon71910542019-02-16 18:16:42 +0000423 O: [() => {this.toggleSummary(); }, 'Toggle the Summary Panel'],
424 P: [(token) => {this.togglePorts(token); }, 'Toggle Port Highlighting'],
425 Q: [() => {this.cycleGridDisplay(); }, 'Cycle grid display'],
426 R: [() => {this.resetZoom(); }, 'Reset pan / zoom'],
Sean Condon9de21352019-04-06 19:22:27 +0100427 U: [() => {this.unpinOrFreezeNodes(); }, 'Unpin or freeze nodes'],
Sean Condon71910542019-02-16 18:16:42 +0000428 X: [() => {this.resetNodeLocation(); }, 'Reset Node Location'],
Sean Condonf4f54a12018-10-10 23:25:46 +0100429 dot: [() => {this.toggleToolbar(); }, 'Toggle Toolbar'],
Sean Condon50855cf2018-12-23 15:37:42 +0000430 0: [() => {this.cancelTraffic(); }, 'Cancel traffic monitoring'],
Sean Condonf4f54a12018-10-10 23:25:46 +0100431 'shift-L': [() => {this.cycleHostLabels(); }, 'Cycle host labels'],
432
433 // -- instance color palette debug
Sean Condon55c30532018-10-29 12:26:57 +0000434 9: () => {
435 this.sus.cat7().testCard(d3.select('svg#topo2'));
Sean Condonf4f54a12018-10-10 23:25:46 +0100436 },
437
Sean Condonb2c483c2019-01-16 20:28:55 +0000438 esc: [() => {this.handleEscape(); }, 'Cancel commands'],
Sean Condonf4f54a12018-10-10 23:25:46 +0100439
440 // TODO update after adding in Background Service
441 // topology overlay selections
442 // F1: function () { t2tbs.fnKey(0); },
443 // F2: function () { t2tbs.fnKey(1); },
444 // F3: function () { t2tbs.fnKey(2); },
445 // F4: function () { t2tbs.fnKey(3); },
446 // F5: function () { t2tbs.fnKey(4); },
447 //
448 // _keyListener: t2tbs.keyListener.bind(t2tbs),
449
450 _helpFormat: [
451 ['I', 'O', 'D', 'H', 'M', 'P', 'dash', 'B'],
452 ['X', 'Z', 'N', 'L', 'shift-L', 'U', 'R', 'E', 'dot'],
453 [], // this column reserved for overlay actions
454 ],
455 };
456 }
457
458
459 bindCommands(additional?: any) {
460
461 const am = this.actionMap();
462 const add = this.fs.isO(additional);
463
Sean Condonf4f54a12018-10-10 23:25:46 +0100464 this.ks.keyBindings(am);
465
466 this.ks.gestureNotes([
467 ['click', 'Select the item and show details'],
468 ['shift-click', 'Toggle selection state'],
469 ['drag', 'Reposition (and pin) device / host'],
470 ['cmd-scroll', 'Zoom in / out'],
471 ['cmd-drag', 'Pan'],
472 ]);
473 }
474
475 handleEscape() {
476
477 if (false) {
478 // TODO: Cancel show mastership
479 // TODO: Cancel Active overlay
480 // TODO: Reinstate with components
481 } else {
Sean Condonb2c483c2019-01-16 20:28:55 +0000482 this.nodeSelected(undefined);
Sean Condonf4f54a12018-10-10 23:25:46 +0100483 this.log.debug('Handling escape');
484 // } else if (t2rs.deselectAllNodes()) {
485 // // else if we have node selections, deselect them all
486 // // (work already done)
487 // } else if (t2rs.deselectLink()) {
488 // // else if we have a link selection, deselect it
489 // // (work already done)
490 // } else if (t2is.isVisible()) {
491 // // If the instance panel is visible, close it
492 // t2is.toggle();
493 // } else if (t2sp.isVisible()) {
494 // // If the summary panel is visible, close it
495 // t2sp.toggle();
496 }
497 }
498
Sean Condonb2c483c2019-01-16 20:28:55 +0000499 /**
500 * Updates the cache of preferences locally and onwards to the PrefsService
501 * @param what The attribute of the local topo2-prefs cache to update
502 * @param b the value to update it with
503 */
504 updatePrefsState(what: string, b: number) {
505 this.prefsState[what] = b;
506 this.ps.setPrefs(TOPO2_PREFS, this.prefsState);
Sean Condonf4f54a12018-10-10 23:25:46 +0100507 }
508
Sean Condonb2c483c2019-01-16 20:28:55 +0000509 /**
510 * When the button is clicked on the toolbar or the L key is pressed
511 * 1) cycle through options
512 * 2) flash up a message
513 * 3a) Update the local prefs cache
514 * 3b) And passes on to the global prefs service which sends back to the server
515 * 3c) It also has a knock on effect of passing it on to ForceSvgComponent
516 * because prefsState.dlbls is given as an input to it
517 * 3d) This will in turn pass it down to the DeviceSvgComponent which
518 * displays the label
519 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100520 protected cycleDeviceLabels() {
Sean Condonff85fbe2019-03-16 14:28:46 +0000521 const old: LabelToggle.Enum = this.prefsState.dlbls;
Sean Condon021f0fa2018-12-06 23:31:11 -0800522 const next = LabelToggle.next(old);
Sean Condon91481822019-01-01 13:56:14 +0000523 this.flashMsg = this.lionFn(TopologyComponent.deviceLabelFlashMessage(next));
Sean Condonb2c483c2019-01-16 20:28:55 +0000524 this.updatePrefsState(PREF_DLBLS, next);
Sean Condon021f0fa2018-12-06 23:31:11 -0800525 this.log.debug('Cycling device labels', old, next);
Sean Condonf4f54a12018-10-10 23:25:46 +0100526 }
527
528 protected cycleHostLabels() {
Sean Condonff85fbe2019-03-16 14:28:46 +0000529 const old: HostLabelToggle.Enum = this.prefsState.hlbls;
Sean Condon021f0fa2018-12-06 23:31:11 -0800530 const next = HostLabelToggle.next(old);
Sean Condon91481822019-01-01 13:56:14 +0000531 this.flashMsg = this.lionFn(TopologyComponent.hostLabelFlashMessage(next));
Sean Condonb2c483c2019-01-16 20:28:55 +0000532 this.updatePrefsState(PREF_HLBLS, next);
Sean Condon021f0fa2018-12-06 23:31:11 -0800533 this.log.debug('Cycling host labels', old, next);
Sean Condonf4f54a12018-10-10 23:25:46 +0100534 }
535
Sean Condon71910542019-02-16 18:16:42 +0000536 protected cycleGridDisplay() {
Sean Condonff85fbe2019-03-16 14:28:46 +0000537 const old: GridDisplayToggle.Enum = this.prefsState.grid;
Sean Condon71910542019-02-16 18:16:42 +0000538 const next = GridDisplayToggle.next(old);
539 this.flashMsg = this.lionFn(TopologyComponent.gridDisplayFlashMessage(next));
540 this.updatePrefsState(PREF_GRID, next);
541 this.log.debug('Cycling grid display', old, next);
542 }
543
Sean Condon19e83672019-04-13 16:21:52 +0100544 protected cycleTrafficTypeDisplay() {
545 const old: TrafficType.Enum = this.prefsState.traffic; // by number
546 const next = TrafficType.next(old);
547 this.flashMsg = this.lionFn(TopologyComponent.trafficTypeFlashMessage(next));
548 this.updatePrefsState(PREF_TRAFFIC, next);
549 this.trs.requestTraffic(next);
550 this.log.debug('Cycling traffic display', old, next);
551 }
552
Sean Condonb2c483c2019-01-16 20:28:55 +0000553 /**
554 * When the button is clicked on the toolbar or the B key is pressed
555 * 1) Find the inverse of the current state (held as 1 or 0)
556 * 2) Flash up a message on screen
557 * 3b) And passes on to the global prefs service which sends back to the server
558 * 3c) It also has a knock on effect of passing it on to ToolbarComponent
559 * because prefsState.bg is given as an input to it
Sean Condonff85fbe2019-03-16 14:28:46 +0000560 * @param token not currently used
Sean Condonb2c483c2019-01-16 20:28:55 +0000561 */
Sean Condon91481822019-01-01 13:56:14 +0000562 protected toggleBackground(token?: KeysToken) {
Sean Condonb2c483c2019-01-16 20:28:55 +0000563 const bg: boolean = !Boolean(this.prefsState.bg);
564 this.flashMsg = this.lionFn(bg ? 'show' : 'hide') +
Sean Condon91481822019-01-01 13:56:14 +0000565 ' ' + this.lionFn('fl_background_map');
Sean Condonb2c483c2019-01-16 20:28:55 +0000566 this.updatePrefsState(PREF_BG, bg ? 1 : 0);
567 this.log.debug('Toggling background', token, bg ? 'shown' : 'hidden');
Sean Condonf4f54a12018-10-10 23:25:46 +0100568 }
569
Sean Condon91481822019-01-01 13:56:14 +0000570 protected toggleDetails(token?: KeysToken) {
Sean Condonb2c483c2019-01-16 20:28:55 +0000571 const on: boolean = !Boolean(this.prefsState.detail);
572 this.flashMsg = this.lionFn(on ? 'show' : 'hide') +
573 ' ' + this.lionFn('fl_panel_details');
574 this.updatePrefsState(PREF_DETAIL, on ? 1 : 0);
575 this.log.debug('Toggling details', token);
Sean Condonf4f54a12018-10-10 23:25:46 +0100576 }
577
Sean Condon91481822019-01-01 13:56:14 +0000578 protected toggleInstancePanel(token?: KeysToken) {
Sean Condonb2c483c2019-01-16 20:28:55 +0000579 const on: boolean = !Boolean(this.prefsState.insts);
Sean Condon91481822019-01-01 13:56:14 +0000580 this.flashMsg = this.lionFn(on ? 'show' : 'hide') +
581 ' ' + this.lionFn('fl_panel_instances');
Sean Condonb2c483c2019-01-16 20:28:55 +0000582 this.updatePrefsState(PREF_INSTS, on ? 1 : 0);
Sean Condon91481822019-01-01 13:56:14 +0000583 this.log.debug('Toggling instances', token, on);
Sean Condonf4f54a12018-10-10 23:25:46 +0100584 }
585
586 protected toggleSummary() {
Sean Condonb2c483c2019-01-16 20:28:55 +0000587 const on: boolean = !Boolean(this.prefsState.summary);
Sean Condon91481822019-01-01 13:56:14 +0000588 this.flashMsg = this.lionFn(on ? 'show' : 'hide') +
589 ' ' + this.lionFn('fl_panel_summary');
Sean Condonb2c483c2019-01-16 20:28:55 +0000590 this.updatePrefsState(PREF_SUMMARY, on ? 1 : 0);
591 }
592
593 protected togglePorts(token?: KeysToken) {
594 const current: boolean = !Boolean(this.prefsState.porthl);
595 this.flashMsg = this.lionFn(current ? 'enable' : 'disable') +
596 ' ' + this.lionFn('fl_port_highlighting');
597 this.updatePrefsState(PREF_PORTHL, current ? 1 : 0);
598 this.log.debug(current ? 'Enable' : 'Disable', 'port highlighting');
599 }
600
601 protected toggleToolbar() {
602 const on: boolean = !Boolean(this.prefsState.toolbar);
603 this.updatePrefsState(PREF_TOOLBAR, on ? 1 : 0);
604 this.log.debug('toggling toolbar', on ? 'shown' : 'hidden');
605 }
606
607 protected toggleHosts() {
608 const current: boolean = !Boolean(this.prefsState.hosts);
609 this.flashMsg = this.lionFn('hosts') + ' ' +
610 this.lionFn(this.force.showHosts ? 'visible' : 'hidden');
611 this.updatePrefsState(PREF_HOSTS, current ? 1 : 0);
612 this.log.debug('toggling hosts: ', this.prefsState.hosts ? 'Show' : 'Hide');
613 }
614
615 protected toggleOfflineDevices() {
616 const on: boolean = !Boolean(this.prefsState.offdev);
617 this.flashMsg = this.lionFn(on ? 'show' : 'hide') +
618 ' ' + this.lionFn('fl_offline_devices');
619 this.updatePrefsState(PREF_OFFDEV, on ? 1 : 0);
620 this.log.debug('toggling offline devices', this.prefsState.offdev);
Sean Condonf4f54a12018-10-10 23:25:46 +0100621 }
622
623 protected resetZoom() {
Sean Condonff85fbe2019-03-16 14:28:46 +0000624 const zoomMapExtents = ZoomUtils.zoomToWindowSize(
625 this.bannerHeight, this.window.innerWidth, this.window.innerHeight);
626 this.zoomDirective.changeZoomLevel(zoomMapExtents, false);
Sean Condon91481822019-01-01 13:56:14 +0000627 this.flashMsg = this.lionFn('fl_pan_zoom_reset');
Sean Condonf4f54a12018-10-10 23:25:46 +0100628 }
629
Sean Condonf4f54a12018-10-10 23:25:46 +0100630 protected equalizeMasters() {
Sean Condon64ea7d22019-04-12 19:39:13 +0100631 this.wss.sendEvent('equalizeMasters', {});
Sean Condon91481822019-01-01 13:56:14 +0000632 this.flashMsg = this.lionFn('fl_eq_masters');
Sean Condonf4f54a12018-10-10 23:25:46 +0100633 this.log.debug('equalizing masters');
Sean Condonf4f54a12018-10-10 23:25:46 +0100634 }
635
Sean Condon9de21352019-04-06 19:22:27 +0100636 /**
637 * If any nodes with fixed positions had been dragged out of place
638 * then put back where they belong
639 * If there are some devices selected reset only these
640 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100641 protected resetNodeLocation() {
Sean Condon9de21352019-04-06 19:22:27 +0100642 const numNodes = this.force.resetNodeLocations();
643 this.flashMsg = this.lionFn('fl_reset_node_locations') +
644 '(' + String(numNodes) + ')';
645 this.log.debug('resetting ', numNodes, 'node(s) location');
Sean Condonf4f54a12018-10-10 23:25:46 +0100646 }
647
Sean Condon9de21352019-04-06 19:22:27 +0100648 /**
649 * Toggle floating nodes between pinned and frozen
650 * If there are floating nodes selected toggle only these
651 */
652 protected unpinOrFreezeNodes() {
653 const pinned: boolean = !Boolean(this.prefsState.pinned);
654 const numNodes = this.force.unpinOrFreezeNodes(pinned);
655 this.flashMsg = this.lionFn(pinned ?
656 'fl_pinned_floating_nodes' : 'fl_unpinned_floating_nodes') +
657 '(' + String(numNodes) + ')';
658 this.updatePrefsState(PREF_PINNED, pinned ? 1 : 0);
659 this.log.debug('Toggling pinning for floating ', numNodes, 'nodes', pinned);
Sean Condonf4f54a12018-10-10 23:25:46 +0100660 }
661
Sean Condon91481822019-01-01 13:56:14 +0000662 /**
663 * Check to see if this is needed anymore
Sean Condonff85fbe2019-03-16 14:28:46 +0000664 * @param what - a key stroke
Sean Condon91481822019-01-01 13:56:14 +0000665 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100666 protected notValid(what) {
667 this.log.warn('topo.js getActionEntry(): Not a valid ' + what);
668 }
669
Sean Condon91481822019-01-01 13:56:14 +0000670 /**
671 * Check to see if this is needed anymore
Sean Condonff85fbe2019-03-16 14:28:46 +0000672 * @param key - a key stroke
Sean Condon91481822019-01-01 13:56:14 +0000673 */
Sean Condonf4f54a12018-10-10 23:25:46 +0100674 getActionEntry(key) {
675 let entry;
676
677 if (!key) {
678 this.notValid('key');
679 return null;
680 }
681
682 entry = this.actionMap()[key];
683
684 if (!entry) {
685 this.notValid('actionMap (' + key + ') entry');
686 return null;
687 }
688 return this.fs.isA(entry) || [entry, ''];
689 }
690
Sean Condon91481822019-01-01 13:56:14 +0000691 /**
692 * An event handler that updates the details panel as items are
693 * selected in the forcesvg layer
Sean Condond88f3662019-04-03 16:35:30 +0100694 *
695 * @param nodesOrLink the item(s) to display details of
Sean Condon91481822019-01-01 13:56:14 +0000696 */
Sean Condond88f3662019-04-03 16:35:30 +0100697 nodeSelected(nodesOrLink: UiElement[]) {
698 this.details.ngOnChanges({'selectedNodes':
699 new SimpleChange(undefined, nodesOrLink, true)});
Sean Condon19e83672019-04-13 16:21:52 +0100700 this.trs.cancelTraffic();
Sean Condon50855cf2018-12-23 15:37:42 +0000701 }
702
703 /**
704 * Cancel traffic monitoring
705 */
706 cancelTraffic() {
Sean Condon91481822019-01-01 13:56:14 +0000707 this.flashMsg = this.lionFn('fl_monitoring_canceled');
Sean Condon19e83672019-04-13 16:21:52 +0100708 this.trs.cancelTraffic();
Sean Condon50855cf2018-12-23 15:37:42 +0000709 }
Sean Condon91481822019-01-01 13:56:14 +0000710
Sean Condon0d064ec2019-02-04 21:53:53 +0000711 changeMap(map: MapObject) {
712 this.mapSelShown = false; // Hide the MapSelector component
713 this.mapIdState = map;
714 this.ps.setPrefs(TOPO_MAPID_PREFS, this.mapIdState);
715 this.log.debug('Map has been changed to ', map);
716 }
717
Sean Condon1ae15802019-03-02 09:07:18 +0000718 mapExtentsZoom(zoomMapExtents: TopoZoomPrefs) {
719 // this.zoomDirective.updateZoomState(zoomPrefs.tx, zoomPrefs.ty, zoomPrefs.sc);
720 this.zoomDirective.changeZoomLevel(zoomMapExtents);
721 this.log.debug('Map zoom prefs updated', zoomMapExtents);
722 }
723
Sean Condon64ea7d22019-04-12 19:39:13 +0100724 backgroundClicked(event: MouseEvent) {
725 const elemTagName = event.target['tagName'] + ' ' + event.target['id'];
726 if (BACKGROUND_ELEMENTS.includes(elemTagName)) {
727 this.force.updateSelected(
728 <SelectedEvent>{
729 uiElement: undefined,
730 deselecting: true
731 }
732 );
733 }
734 }
735
Sean Condon91481822019-01-01 13:56:14 +0000736 /**
737 * Read the LION bundle for Toolbar and set up the lionFn
738 */
739 doLion() {
740 this.lionFn = this.lion.bundle('core.view.Topo');
741 }
742
743 /**
744 * A dummy implementation of the lionFn until the response is received and the LION
745 * bundle is received from the WebSocket
746 */
747 dummyLion(key: string): string {
748 return '%' + key + '%';
749 }
Sean Condonf4f54a12018-10-10 23:25:46 +0100750}