blob: d4decb06424008b79f1b165f00de1be0aad7dd58 [file] [log] [blame]
Sean Condonf4f54a12018-10-10 23:25:46 +01001/*
2 * Copyright 2018-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 Condon0c577f62018-11-18 22:40:05 +000016import {
17 Component,
18 OnDestroy,
19 OnInit,
20 ViewChild
21} from '@angular/core';
Sean Condon55c30532018-10-29 12:26:57 +000022import * as d3 from 'd3';
Sean Condonf4f54a12018-10-10 23:25:46 +010023import {
24 FnService,
Sean Condon0c577f62018-11-18 22:40:05 +000025 KeysService,
26 KeysToken,
27 LogService,
28 PrefsService,
29 SvgUtilService,
30 WebSocketService,
31 Zoomer,
32 ZoomOpts,
33 ZoomService
Sean Condonf4f54a12018-10-10 23:25:46 +010034} from 'gui2-fw-lib';
Sean Condon0c577f62018-11-18 22:40:05 +000035import {InstanceComponent} from '../panel/instance/instance.component';
36import {SummaryComponent} from '../panel/summary/summary.component';
37import {DetailsComponent} from '../panel/details/details.component';
38import {BackgroundSvgComponent} from '../layer/backgroundsvg/backgroundsvg.component';
39import {ForceSvgComponent} from '../layer/forcesvg/forcesvg.component';
40import {TopologyService} from '../topology.service';
41import {Node} from '../layer/forcesvg/models';
Sean Condonf4f54a12018-10-10 23:25:46 +010042
43/**
44 * ONOS GUI Topology View
45 *
46 * This Topology View component is the top level component in a hierarchy that
47 * comprises the whole Topology View
48 *
49 * There are three main parts (panels, graphical and breadcrumbs)
50 * The panel hierarchy
51 * |-- Instances Panel (shows ONOS instances)
52 * |-- Summary Panel (summary of ONOS)
53 * |-- Toolbar Panel (the toolbar)
54 * |-- Details Panel (when a node is selected in the Force graphical view (see below))
55 *
56 * The graphical hierarchy contains
57 * Topology (this)
58 * |-- No Devices Connected (only of there are no nodes to show)
59 * |-- Zoom Layer (everything beneath this can be zoomed and panned)
60 * |-- Background (container for any backgrounds - can be toggled on and off)
61 * |-- Map
62 * |-- Forces (all of the nodes and links laid out by a d3.force simulation)
63 *
64 * The breadcrumbs
65 * |-- Breadcrumb (in region view a way of navigating back up through regions)
66 */
67@Component({
68 selector: 'onos-topology',
69 templateUrl: './topology.component.html',
70 styleUrls: ['./topology.component.css']
71})
Sean Condonaa4366d2018-11-02 14:29:01 +000072export class TopologyComponent implements OnInit, OnDestroy {
73 // These are references to the components inserted in the template
Sean Condonf4f54a12018-10-10 23:25:46 +010074 @ViewChild(InstanceComponent) instance: InstanceComponent;
75 @ViewChild(SummaryComponent) summary: SummaryComponent;
76 @ViewChild(DetailsComponent) details: DetailsComponent;
Sean Condonaa4366d2018-11-02 14:29:01 +000077 @ViewChild(BackgroundSvgComponent) background: BackgroundSvgComponent;
78 @ViewChild(ForceSvgComponent) force: ForceSvgComponent;
Sean Condonf4f54a12018-10-10 23:25:46 +010079
80 flashMsg: string = '';
81 prefsState = {};
Sean Condonf4f54a12018-10-10 23:25:46 +010082 hostLabelIdx: number = 1;
83
Sean Condon55c30532018-10-29 12:26:57 +000084 zoomer: Zoomer;
85 zoomEventListeners: any[];
86
Sean Condonf4f54a12018-10-10 23:25:46 +010087 constructor(
88 protected log: LogService,
89 protected fs: FnService,
90 protected ks: KeysService,
91 protected sus: SvgUtilService,
92 protected ps: PrefsService,
Sean Condon55c30532018-10-29 12:26:57 +000093 protected wss: WebSocketService,
Sean Condonaa4366d2018-11-02 14:29:01 +000094 protected zs: ZoomService,
95 protected ts: TopologyService,
Sean Condonf4f54a12018-10-10 23:25:46 +010096 ) {
97
98 this.log.debug('Topology component constructed');
99 }
100
101 ngOnInit() {
102 this.bindCommands();
Sean Condon55c30532018-10-29 12:26:57 +0000103 this.zoomer = this.createZoomer(<ZoomOpts>{
104 svg: d3.select('svg#topo2'),
105 zoomLayer: d3.select('g#topo-zoomlayer'),
106 zoomEnabled: () => true,
107 zoomMin: 0.25,
108 zoomMax: 10.0,
109 zoomCallback: (() => { return; })
110 });
111 this.zoomEventListeners = [];
Sean Condonaa4366d2018-11-02 14:29:01 +0000112 // The components from the template are handed over to TopologyService here
113 // so that WebSocket responses can be passed back in to them
114 // The handling of the WebSocket call is delegated out to the Topology
115 // Service just to compartmentalize things a bit
116 this.ts.init(this.instance, this.background, this.force);
Sean Condonf4f54a12018-10-10 23:25:46 +0100117 this.log.debug('Topology component initialized');
118 }
119
Sean Condonaa4366d2018-11-02 14:29:01 +0000120 ngOnDestroy() {
121 this.ts.destroy();
122 this.log.debug('Topology component destroyed');
123 }
124
Sean Condonf4f54a12018-10-10 23:25:46 +0100125 actionMap() {
126 return {
127 L: [() => {this.cycleDeviceLabels(); }, 'Cycle device labels'],
128 B: [(token) => {this.toggleBackground(token); }, 'Toggle background'],
129 D: [(token) => {this.toggleDetails(token); }, 'Toggle details panel'],
130 I: [(token) => {this.toggleInstancePanel(token); }, 'Toggle ONOS Instance Panel'],
131 O: [() => {this.toggleSummary(); }, 'Toggle the Summary Panel'],
132 R: [() => {this.resetZoom(); }, 'Reset pan / zoom'],
Sean Condon55c30532018-10-29 12:26:57 +0000133 'shift-Z': [() => {this.panAndZoom([0, 0], this.zoomer.scale() * 2); }, 'Zoom x2'],
134 'alt-Z': [() => {this.panAndZoom([0, 0], this.zoomer.scale() / 2); }, 'Zoom x0.5'],
Sean Condonf4f54a12018-10-10 23:25:46 +0100135 P: [(token) => {this.togglePorts(token); }, 'Toggle Port Highlighting'],
136 E: [() => {this.equalizeMasters(); }, 'Equalize mastership roles'],
137 X: [() => {this.resetNodeLocation(); }, 'Reset Node Location'],
138 U: [() => {this.unpinNode(); }, 'Unpin node (mouse over)'],
139 H: [() => {this.toggleHosts(); }, 'Toggle host visibility'],
140 M: [() => {this.toggleOfflineDevices(); }, 'Toggle offline visibility'],
141 dot: [() => {this.toggleToolbar(); }, 'Toggle Toolbar'],
142 'shift-L': [() => {this.cycleHostLabels(); }, 'Cycle host labels'],
143
144 // -- instance color palette debug
Sean Condon55c30532018-10-29 12:26:57 +0000145 9: () => {
146 this.sus.cat7().testCard(d3.select('svg#topo2'));
Sean Condonf4f54a12018-10-10 23:25:46 +0100147 },
148
149 esc: this.handleEscape,
150
151 // TODO update after adding in Background Service
152 // topology overlay selections
153 // F1: function () { t2tbs.fnKey(0); },
154 // F2: function () { t2tbs.fnKey(1); },
155 // F3: function () { t2tbs.fnKey(2); },
156 // F4: function () { t2tbs.fnKey(3); },
157 // F5: function () { t2tbs.fnKey(4); },
158 //
159 // _keyListener: t2tbs.keyListener.bind(t2tbs),
160
161 _helpFormat: [
162 ['I', 'O', 'D', 'H', 'M', 'P', 'dash', 'B'],
163 ['X', 'Z', 'N', 'L', 'shift-L', 'U', 'R', 'E', 'dot'],
164 [], // this column reserved for overlay actions
165 ],
166 };
167 }
168
169
170 bindCommands(additional?: any) {
171
172 const am = this.actionMap();
173 const add = this.fs.isO(additional);
174
175 // TODO: Reimplement when we have a use case
176 // if (add) {
177 // _.each(add, function (value, key) {
178 // // filter out meta properties (e.g. _keyOrder)
179 // if (!(_.startsWith(key, '_'))) {
180 // // don't allow re-definition of existing key bindings
181 // if (am[key]) {
182 // this.log.warn('keybind: ' + key + ' already exists');
183 // } else {
184 // am[key] = [value.cb, value.tt];
185 // }
186 // }
187 // });
188 // }
189
190 this.ks.keyBindings(am);
191
192 this.ks.gestureNotes([
193 ['click', 'Select the item and show details'],
194 ['shift-click', 'Toggle selection state'],
195 ['drag', 'Reposition (and pin) device / host'],
196 ['cmd-scroll', 'Zoom in / out'],
197 ['cmd-drag', 'Pan'],
198 ]);
199 }
200
201 handleEscape() {
202
203 if (false) {
204 // TODO: Cancel show mastership
205 // TODO: Cancel Active overlay
206 // TODO: Reinstate with components
207 } else {
208 this.log.debug('Handling escape');
209 // } else if (t2rs.deselectAllNodes()) {
210 // // else if we have node selections, deselect them all
211 // // (work already done)
212 // } else if (t2rs.deselectLink()) {
213 // // else if we have a link selection, deselect it
214 // // (work already done)
215 // } else if (t2is.isVisible()) {
216 // // If the instance panel is visible, close it
217 // t2is.toggle();
218 // } else if (t2sp.isVisible()) {
219 // // If the summary panel is visible, close it
220 // t2sp.toggle();
221 }
222 }
223
224
225
226 updatePrefsState(what, b) {
227 this.prefsState[what] = b ? 1 : 0;
228 this.ps.setPrefs('topo2_prefs', this.prefsState);
229 }
230
231 deviceLabelFlashMessage(index) {
232 switch (index) {
233 case 0: return 'Hide device labels';
234 case 1: return 'Show friendly device labels';
235 case 2: return 'Show device ID labels';
236 }
237 }
238
239 hostLabelFlashMessage(index) {
240 switch (index) {
241 case 0: return 'Hide host labels';
242 case 1: return 'Show friendly host labels';
243 case 2: return 'Show host IP labels';
244 case 3: return 'Show host MAC Address labels';
245 }
246 }
247
248 protected cycleDeviceLabels() {
249 this.log.debug('Cycling device labels');
250 // TODO: Reinstate with components
Sean Condon0c577f62018-11-18 22:40:05 +0000251 this.force.updateDeviceLabelToggle();
Sean Condonf4f54a12018-10-10 23:25:46 +0100252 // let deviceLabelIndex = t2ps.get('dlbls') + 1;
253 // let newDeviceLabelIndex = deviceLabelIndex % 3;
254 //
255 // t2ps.set('dlbls', newDeviceLabelIndex);
256 // t2fs.updateNodes();
257 // flash.flash(deviceLabelFlashMessage(newDeviceLabelIndex));
258 }
259
260 protected cycleHostLabels() {
261 const hostLabelIndex = this.hostLabelIdx + 1;
262 this.hostLabelIdx = hostLabelIndex % 4;
263 this.flashMsg = this.hostLabelFlashMessage(this.hostLabelIdx);
264 this.log.debug('Cycling host labels');
265 // TODO: Reinstate with components
266 // t2ps.set('hlbls', newHostLabelIndex);
267 // t2fs.updateNodes();
268 }
269
270 protected toggleBackground(token: KeysToken) {
271 this.flashMsg = 'Toggling background';
272 this.log.debug('Toggling background', token);
273 // TODO: Reinstate with components
274 // t2bgs.toggle(x);
275 }
276
277 protected toggleDetails(token: KeysToken) {
Sean Condon0c577f62018-11-18 22:40:05 +0000278 if (this.details.selectedNode) {
279 this.flashMsg = 'Toggling details';
280 this.details.togglePanel(() => {
281 });
282 this.log.debug('Toggling details', token);
283 }
Sean Condonf4f54a12018-10-10 23:25:46 +0100284 }
285
286 protected toggleInstancePanel(token: KeysToken) {
287 this.flashMsg = 'Toggling instances';
288 this.instance.togglePanel(() => {});
289 this.log.debug('Toggling instances', token);
290 // TODO: Reinstate with components
291 // this.updatePrefsState('insts', t2is.toggle(x));
292 }
293
294 protected toggleSummary() {
295 this.flashMsg = 'Toggling summary';
296 this.summary.togglePanel(() => {});
297 }
298
299 protected resetZoom() {
Sean Condon55c30532018-10-29 12:26:57 +0000300 this.zoomer.reset();
Sean Condonf4f54a12018-10-10 23:25:46 +0100301 this.log.debug('resetting zoom');
302 // TODO: Reinstate with components
303 // t2bgs.resetZoom();
304 // flash.flash('Pan and zoom reset');
305 }
306
307 protected togglePorts(token: KeysToken) {
308 this.log.debug('Toggling ports');
309 // TODO: Reinstate with components
310 // this.updatePrefsState('porthl', t2vs.togglePortHighlights(x));
311 // t2fs.updateLinks();
312 }
313
314 protected equalizeMasters() {
315 this.wss.sendEvent('equalizeMasters', null);
316
317 this.log.debug('equalizing masters');
318 // TODO: Reinstate with components
319 // flash.flash('Equalizing master roles');
320 }
321
322 protected resetNodeLocation() {
323 this.log.debug('resetting node location');
324 // TODO: Reinstate with components
325 // t2fs.resetNodeLocation();
326 // flash.flash('Reset node locations');
327 }
328
329 protected unpinNode() {
330 this.log.debug('unpinning node');
331 // TODO: Reinstate with components
332 // t2fs.unpin();
333 // flash.flash('Unpin node');
334 }
335
336 protected toggleToolbar() {
337 this.log.debug('toggling toolbar');
338 // TODO: Reinstate with components
339 // t2tbs.toggle();
340 }
341
342 protected actionedFlashed(action, message) {
343 this.log.debug('action flashed');
344 // TODO: Reinstate with components
345 // this.flash.flash(action + ' ' + message);
346 }
347
348 protected toggleHosts() {
349 // this.flashMsg = on ? 'Show': 'Hide', 'Hosts';
350 this.log.debug('toggling hosts');
351 // TODO: Reinstate with components
352 // let on = t2rs.toggleHosts();
353 // this.actionedFlashed(on ? 'Show': 'Hide', 'Hosts');
354 }
355
356 protected toggleOfflineDevices() {
357 this.log.debug('toggling offline devices');
358 // TODO: Reinstate with components
359 // let on = t2rs.toggleOfflineDevices();
360 // this.actionedFlashed(on ? 'Show': 'Hide', 'offline devices');
361 }
362
363 protected notValid(what) {
364 this.log.warn('topo.js getActionEntry(): Not a valid ' + what);
365 }
366
367 getActionEntry(key) {
368 let entry;
369
370 if (!key) {
371 this.notValid('key');
372 return null;
373 }
374
375 entry = this.actionMap()[key];
376
377 if (!entry) {
378 this.notValid('actionMap (' + key + ') entry');
379 return null;
380 }
381 return this.fs.isA(entry) || [entry, ''];
382 }
383
Sean Condon55c30532018-10-29 12:26:57 +0000384
385
386 protected createZoomer(options: ZoomOpts) {
387 // need to wrap the original zoom callback to extend its behavior
388 const origCallback = this.fs.isF(options.zoomCallback) ? options.zoomCallback : () => {};
389
390 options.zoomCallback = () => {
391 origCallback([0, 0], 1);
392
393 this.zoomEventListeners.forEach((ev) => ev(this.zoomer));
394 };
395
396 return this.zs.createZoomer(options);
397 }
398
399 getZoomer() {
400 return this.zoomer;
401 }
402
403 findZoomEventListener(ev) {
404 for (let i = 0, len = this.zoomEventListeners.length; i < len; i++) {
405 if (this.zoomEventListeners[i] === ev) {
406 return i;
407 }
408 }
409 return -1;
410 }
411
412 addZoomEventListener(callback) {
413 this.zoomEventListeners.push(callback);
414 }
415
416 removeZoomEventListener(callback) {
417 const evIndex = this.findZoomEventListener(callback);
418
419 if (evIndex !== -1) {
420 this.zoomEventListeners.splice(evIndex);
421 }
422 }
423
424 adjustmentScale(min: number, max: number): number {
425 let _scale = 1;
426 const size = (min + max) / 2;
427
428 if (size * this.scale() < max) {
429 _scale = min / (size * this.scale());
430 } else if (size * this.scale() > max) {
431 _scale = min / (size * this.scale());
432 }
433
434 return _scale;
435 }
436
437 scale(): number {
438 return this.zoomer.scale();
439 }
440
441 panAndZoom(translate: number[], scale: number, transition?: number) {
442 this.zoomer.panZoom(translate, scale, transition);
443 }
444
Sean Condon0c577f62018-11-18 22:40:05 +0000445 nodeSelected(node: Node) {
446 this.details.selectedNode = node;
447 this.details.on = Boolean(node);
448 }
449
Sean Condonf4f54a12018-10-10 23:25:46 +0100450}