blob: 89923ae1fa739aff74ae1d2590f72fc3b8f280a0 [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 {
17 ChangeDetectionStrategy,
18 ChangeDetectorRef,
19 Component,
20 EventEmitter,
21 HostListener,
22 Input,
23 OnChanges,
24 OnInit,
Sean Condon021f0fa2018-12-06 23:31:11 -080025 Output,
26 QueryList,
Sean Condon50855cf2018-12-23 15:37:42 +000027 SimpleChange,
Sean Condon021f0fa2018-12-06 23:31:11 -080028 SimpleChanges,
29 ViewChildren
Sean Condon0c577f62018-11-18 22:40:05 +000030} from '@angular/core';
Sean Condon5f7d3bc2019-04-15 11:18:34 +010031import {LocMeta, LogService, MetaUi, WebSocketService, ZoomUtils} from 'gui2-fw-lib';
Sean Condon1ae15802019-03-02 09:07:18 +000032import {
Sean Condon5f7d3bc2019-04-15 11:18:34 +010033 Device,
34 DeviceProps,
Sean Condon0c577f62018-11-18 22:40:05 +000035 ForceDirectedGraph,
Sean Condon50855cf2018-12-23 15:37:42 +000036 Host,
37 HostLabelToggle,
Sean Condon0c577f62018-11-18 22:40:05 +000038 LabelToggle,
39 LayerType,
Sean Condon50855cf2018-12-23 15:37:42 +000040 Link,
41 LinkHighlight,
Sean Condon1ae15802019-03-02 09:07:18 +000042 Location,
Sean Condon50855cf2018-12-23 15:37:42 +000043 ModelEventMemo,
44 ModelEventType,
Sean Condon28884332019-03-21 14:07:00 +000045 Node,
Sean Condond88f3662019-04-03 16:35:30 +010046 Options,
Sean Condon0c577f62018-11-18 22:40:05 +000047 Region,
48 RegionLink,
Sean Condon50855cf2018-12-23 15:37:42 +000049 SubRegion,
50 UiElement
Sean Condon0c577f62018-11-18 22:40:05 +000051} from './models';
Sean Condonee545762019-03-09 10:43:58 +000052import {LocationType} from '../backgroundsvg/backgroundsvg.component';
Sean Condonff85fbe2019-03-16 14:28:46 +000053import {DeviceNodeSvgComponent} from './visuals/devicenodesvg/devicenodesvg.component';
Sean Condon5f7d3bc2019-04-15 11:18:34 +010054import {HostNodeSvgComponent} from './visuals/hostnodesvg/hostnodesvg.component';
55import {LinkSvgComponent} from './visuals/linksvg/linksvg.component';
Sean Condond88f3662019-04-03 16:35:30 +010056import {SelectedEvent} from './visuals/nodevisual';
Sean Condon71910542019-02-16 18:16:42 +000057
58interface UpdateMeta {
59 id: string;
60 class: string;
61 memento: MetaUi;
62}
Sean Condonaa4366d2018-11-02 14:29:01 +000063
Sean Condon28884332019-03-21 14:07:00 +000064const SVGCANVAS = <Options>{
65 width: 1000,
66 height: 1000
67};
68
69interface ChangeSummary {
70 numChanges: number;
71 locationChanged: boolean;
72}
73
Sean Condonf4f54a12018-10-10 23:25:46 +010074/**
75 * ONOS GUI -- Topology Forces Graph Layer View.
Sean Condon0c577f62018-11-18 22:40:05 +000076 *
77 * The regionData is set by Topology Service on WebSocket topo2CurrentRegion callback
78 * This drives the whole Force graph
Sean Condonf4f54a12018-10-10 23:25:46 +010079 */
80@Component({
81 selector: '[onos-forcesvg]',
82 templateUrl: './forcesvg.component.html',
Sean Condon0c577f62018-11-18 22:40:05 +000083 styleUrls: ['./forcesvg.component.css'],
84 changeDetection: ChangeDetectionStrategy.OnPush,
Sean Condonf4f54a12018-10-10 23:25:46 +010085})
Sean Condon0c577f62018-11-18 22:40:05 +000086export class ForceSvgComponent implements OnInit, OnChanges {
Sean Condonff85fbe2019-03-16 14:28:46 +000087 @Input() deviceLabelToggle: LabelToggle.Enum = LabelToggle.Enum.NONE;
88 @Input() hostLabelToggle: HostLabelToggle.Enum = HostLabelToggle.Enum.NONE;
Sean Condonb2c483c2019-01-16 20:28:55 +000089 @Input() showHosts: boolean = false;
90 @Input() highlightPorts: boolean = true;
91 @Input() onosInstMastership: string = '';
92 @Input() visibleLayer: LayerType = LayerType.LAYER_DEFAULT;
93 @Input() selectedLink: RegionLink = null;
Sean Condon1ae15802019-03-02 09:07:18 +000094 @Input() scale: number = 1;
Sean Condon0c577f62018-11-18 22:40:05 +000095 @Input() regionData: Region = <Region>{devices: [ [], [], [] ], hosts: [ [], [], [] ], links: []};
Sean Condonb2c483c2019-01-16 20:28:55 +000096 @Output() linkSelected = new EventEmitter<RegionLink>();
Sean Condond88f3662019-04-03 16:35:30 +010097 @Output() selectedNodeEvent = new EventEmitter<UiElement[]>();
Sean Condonff85fbe2019-03-16 14:28:46 +000098 public graph: ForceDirectedGraph;
Sean Condond88f3662019-04-03 16:35:30 +010099 private selectedNodes: UiElement[] = [];
Sean Condonf4f54a12018-10-10 23:25:46 +0100100
Sean Condon021f0fa2018-12-06 23:31:11 -0800101 // References to the children of this component - these are created in the
102 // template view with the *ngFor and we get them by a query here
Sean Condon0c577f62018-11-18 22:40:05 +0000103 @ViewChildren(DeviceNodeSvgComponent) devices: QueryList<DeviceNodeSvgComponent>;
Sean Condon021f0fa2018-12-06 23:31:11 -0800104 @ViewChildren(HostNodeSvgComponent) hosts: QueryList<HostNodeSvgComponent>;
Sean Condon50855cf2018-12-23 15:37:42 +0000105 @ViewChildren(LinkSvgComponent) links: QueryList<LinkSvgComponent>;
Sean Condonf4f54a12018-10-10 23:25:46 +0100106
Sean Condon0c577f62018-11-18 22:40:05 +0000107 constructor(
108 protected log: LogService,
Sean Condon71910542019-02-16 18:16:42 +0000109 private ref: ChangeDetectorRef,
110 protected wss: WebSocketService
Sean Condon0c577f62018-11-18 22:40:05 +0000111 ) {
112 this.selectedLink = null;
113 this.log.debug('ForceSvgComponent constructed');
114 }
115
116 /**
Sean Condon021f0fa2018-12-06 23:31:11 -0800117 * Utility for extracting a node name from an endpoint string
118 * In some cases - have to remove the port number from the end of a device
119 * name
120 * @param endPtStr The end point name
121 */
122 private static extractNodeName(endPtStr: string): string {
123 const slash: number = endPtStr.indexOf('/');
124 if (slash === -1) {
125 return endPtStr;
126 } else {
127 const afterSlash = endPtStr.substr(slash + 1);
128 if (afterSlash === 'None') {
129 return endPtStr;
130 } else {
131 return endPtStr.substr(0, slash);
132 }
133 }
134 }
135
Sean Condonee545762019-03-09 10:43:58 +0000136 /**
137 * Recursive method to compare 2 objects attribute by attribute and update
138 * the first where a change is detected
139 * @param existingNode 1st object
140 * @param updatedNode 2nd object
141 */
Sean Condon28884332019-03-21 14:07:00 +0000142 private static updateObject(existingNode: Object, updatedNode: Object): ChangeSummary {
143 const changed = <ChangeSummary>{numChanges: 0, locationChanged: false};
Sean Condonee545762019-03-09 10:43:58 +0000144 for (const key of Object.keys(updatedNode)) {
145 const o = updatedNode[key];
Sean Condon28884332019-03-21 14:07:00 +0000146 if (['id', 'x', 'y', 'fx', 'fy', 'vx', 'vy', 'index'].some(k => k === key)) {
Sean Condonee545762019-03-09 10:43:58 +0000147 continue;
148 } else if (o && typeof o === 'object' && o.constructor === Object) {
Sean Condon28884332019-03-21 14:07:00 +0000149 const subChanged = ForceSvgComponent.updateObject(existingNode[key], updatedNode[key]);
150 changed.numChanges += subChanged.numChanges;
151 changed.locationChanged = subChanged.locationChanged ? true : changed.locationChanged;
152 } else if (existingNode === undefined) {
153 // Copy the whole object
154 existingNode = updatedNode;
155 changed.locationChanged = true;
156 changed.numChanges++;
Sean Condonee545762019-03-09 10:43:58 +0000157 } else if (existingNode[key] !== updatedNode[key]) {
Sean Condon28884332019-03-21 14:07:00 +0000158 if (['locType', 'latOrY', 'longOrX', 'latitude', 'longitude', 'gridX', 'gridY'].some(k => k === key)) {
159 changed.locationChanged = true;
160 }
161 changed.numChanges++;
Sean Condonee545762019-03-09 10:43:58 +0000162 existingNode[key] = updatedNode[key];
163 }
164 }
165 return changed;
166 }
167
Sean Condon021f0fa2018-12-06 23:31:11 -0800168 @HostListener('window:resize', ['$event'])
169 onResize(event) {
Sean Condon28884332019-03-21 14:07:00 +0000170 this.graph.restartSimulation();
171 this.log.debug('Simulation restart after resize', event);
Sean Condon021f0fa2018-12-06 23:31:11 -0800172 }
173
174 /**
Sean Condon0c577f62018-11-18 22:40:05 +0000175 * After the component is initialized create the Force simulation
Sean Condon021f0fa2018-12-06 23:31:11 -0800176 * The list of devices, hosts and links will not have been receieved back
177 * from the WebSocket yet as this time - they will be updated later through
178 * ngOnChanges()
Sean Condon0c577f62018-11-18 22:40:05 +0000179 */
180 ngOnInit() {
181 // Receiving an initialized simulated graph from our custom d3 service
Sean Condon28884332019-03-21 14:07:00 +0000182 this.graph = new ForceDirectedGraph(SVGCANVAS, this.log);
Sean Condon0c577f62018-11-18 22:40:05 +0000183
184 /** Binding change detection check on each tick
Sean Condon021f0fa2018-12-06 23:31:11 -0800185 * This along with an onPush change detection strategy should enforce
186 * checking only when relevant! This improves scripting computation
187 * duration in a couple of tests I've made, consistently. Also, it makes
188 * sense to avoid unnecessary checks when we are dealing only with
189 * simulations data binding.
Sean Condon0c577f62018-11-18 22:40:05 +0000190 */
191 this.graph.ticker.subscribe((simulation) => {
Sean Condon28884332019-03-21 14:07:00 +0000192 // this.log.debug("Force simulation has ticked. Alpha",
193 // Math.round(simulation.alpha() * 1000) / 1000);
Sean Condon0c577f62018-11-18 22:40:05 +0000194 this.ref.markForCheck();
195 });
Sean Condon28884332019-03-21 14:07:00 +0000196
Sean Condon0c577f62018-11-18 22:40:05 +0000197 this.log.debug('ForceSvgComponent initialized - waiting for nodes and links');
198
Sean Condon0c577f62018-11-18 22:40:05 +0000199 }
200
201 /**
Sean Condon021f0fa2018-12-06 23:31:11 -0800202 * When any one of the inputs get changed by a containing component, this
203 * gets called automatically. In addition this is called manually by
204 * topology.service when a response is received from the WebSocket from the
205 * server
Sean Condon0c577f62018-11-18 22:40:05 +0000206 *
207 * The Devices, Hosts and SubRegions are all added to the Node list for the simulation
208 * The Links are added to the Link list of the simulation.
209 * Before they are added the Links are associated with Nodes based on their endPt
210 *
211 * @param changes - a list of changed @Input(s)
212 */
213 ngOnChanges(changes: SimpleChanges) {
214 if (changes['regionData']) {
215 const devices: Device[] =
216 changes['regionData'].currentValue.devices[this.visibleLayerIdx()];
217 const hosts: Host[] =
218 changes['regionData'].currentValue.hosts[this.visibleLayerIdx()];
219 const subRegions: SubRegion[] = changes['regionData'].currentValue.subRegion;
220 this.graph.nodes = [];
221 if (devices) {
222 this.graph.nodes = devices;
223 }
224 if (hosts) {
225 this.graph.nodes = this.graph.nodes.concat(hosts);
226 }
227 if (subRegions) {
228 this.graph.nodes = this.graph.nodes.concat(subRegions);
229 }
230
Sean Condon28884332019-03-21 14:07:00 +0000231 this.graph.nodes.forEach((n) => this.fixPosition(n));
Sean Condon71910542019-02-16 18:16:42 +0000232
Sean Condon0c577f62018-11-18 22:40:05 +0000233 // Associate the endpoints of each link with a real node
234 this.graph.links = [];
235 for (const linkIdx of Object.keys(this.regionData.links)) {
Sean Condon021f0fa2018-12-06 23:31:11 -0800236 const epA = ForceSvgComponent.extractNodeName(
237 this.regionData.links[linkIdx].epA);
Sean Condon0c577f62018-11-18 22:40:05 +0000238 this.regionData.links[linkIdx].source =
239 this.graph.nodes.find((node) =>
Sean Condon021f0fa2018-12-06 23:31:11 -0800240 node.id === epA);
241 const epB = ForceSvgComponent.extractNodeName(
242 this.regionData.links[linkIdx].epB);
Sean Condon0c577f62018-11-18 22:40:05 +0000243 this.regionData.links[linkIdx].target =
244 this.graph.nodes.find((node) =>
Sean Condon021f0fa2018-12-06 23:31:11 -0800245 node.id === epB);
Sean Condon0c577f62018-11-18 22:40:05 +0000246 this.regionData.links[linkIdx].index = Number(linkIdx);
247 }
248
249 this.graph.links = this.regionData.links;
Sean Condon28884332019-03-21 14:07:00 +0000250 if (this.graph.nodes.length > 0) {
251 this.graph.reinitSimulation();
252 }
Sean Condon0c577f62018-11-18 22:40:05 +0000253 this.log.debug('ForceSvgComponent input changed',
254 this.graph.nodes.length, 'nodes,', this.graph.links.length, 'links');
255 }
Sean Condon28884332019-03-21 14:07:00 +0000256 }
Sean Condon021f0fa2018-12-06 23:31:11 -0800257
Sean Condon28884332019-03-21 14:07:00 +0000258 /**
259 * If a node has a fixed location then assign it to fx and fy so
260 * that it doesn't get affected by forces
261 * @param graphNode The node whose location should be processed
262 */
263 private fixPosition(graphNode: Node): void {
264 const loc: Location = <Location>graphNode['location'];
265 const props: DeviceProps = <DeviceProps>graphNode['props'];
266 const metaUi = <MetaUi>graphNode['metaUi'];
267 if (loc && loc.locType === LocationType.GEO) {
268 const position: MetaUi =
269 ZoomUtils.convertGeoToCanvas(
270 <LocMeta>{lng: loc.longOrX, lat: loc.latOrY});
271 graphNode.fx = position.x;
272 graphNode.fy = position.y;
273 this.log.debug('Found node', graphNode.id, 'with', loc.locType);
274 } else if (loc && loc.locType === LocationType.GRID) {
275 graphNode.fx = loc.longOrX;
276 graphNode.fy = loc.latOrY;
277 this.log.debug('Found node', graphNode.id, 'with', loc.locType);
278 } else if (props && props.locType === LocationType.NONE && metaUi) {
279 graphNode.fx = metaUi.x;
280 graphNode.fy = metaUi.y;
281 this.log.debug('Found node', graphNode.id, 'with locType=none and metaUi');
282 } else {
283 graphNode.fx = null;
284 graphNode.fy = null;
285 }
Sean Condon0c577f62018-11-18 22:40:05 +0000286 }
287
288 /**
289 * Get the index of LayerType so it can drive the visibility of nodes and
290 * hosts on layers
291 */
292 visibleLayerIdx(): number {
293 const layerKeys: string[] = Object.keys(LayerType);
294 for (const idx in layerKeys) {
295 if (LayerType[layerKeys[idx]] === this.visibleLayer) {
296 return Number(idx);
297 }
298 }
299 return -1;
300 }
301
302 selectLink(link: RegionLink): void {
303 this.selectedLink = link;
304 this.linkSelected.emit(link);
305 }
306
Sean Condon021f0fa2018-12-06 23:31:11 -0800307 /**
Sean Condond88f3662019-04-03 16:35:30 +0100308 * Iterate through all hosts and devices and links to deselect the previously selected
Sean Condon021f0fa2018-12-06 23:31:11 -0800309 * node. The emit an event to the parent that lets it know the selection has
310 * changed.
Sean Condond88f3662019-04-03 16:35:30 +0100311 *
312 * This function collates all of the nodes that have been selected and passes
313 * a collection of nodes up to the topology component
314 *
Sean Condon021f0fa2018-12-06 23:31:11 -0800315 * @param selectedNode the newly selected node
316 */
Sean Condond88f3662019-04-03 16:35:30 +0100317 updateSelected(selectedNode: SelectedEvent): void {
318 this.log.debug('Node or link ',
Sean Condon64ea7d22019-04-12 19:39:13 +0100319 selectedNode.uiElement ? selectedNode.uiElement.id : '--',
Sean Condond88f3662019-04-03 16:35:30 +0100320 selectedNode.deselecting ? 'deselected' : 'selected',
321 selectedNode.isShift ? 'Multiple' : '');
Sean Condon021f0fa2018-12-06 23:31:11 -0800322
Sean Condond88f3662019-04-03 16:35:30 +0100323 if (selectedNode.isShift && selectedNode.deselecting) {
324 const idx = this.selectedNodes.findIndex((n) =>
325 n.id === selectedNode.uiElement.id
326 );
327 this.selectedNodes.splice(idx, 1);
328 this.log.debug('Removed node', idx);
329
330 } else if (selectedNode.isShift) {
331 this.selectedNodes.push(selectedNode.uiElement);
332
333 } else if (selectedNode.deselecting) {
Sean Condon64ea7d22019-04-12 19:39:13 +0100334 this.devices
335 .forEach((d) => d.deselect());
336 this.hosts
337 .forEach((h) => h.deselect());
338 this.links
339 .forEach((l) => l.deselect());
Sean Condond88f3662019-04-03 16:35:30 +0100340 this.selectedNodes = [];
341
342 } else {
343 const selNodeId = selectedNode.uiElement.id;
344 // Otherwise if shift was not pressed deselect previous
345 this.devices
346 .filter((d) => d.device.id !== selNodeId)
347 .forEach((d) => d.deselect());
348 this.hosts
349 .filter((h) => h.host.id !== selNodeId)
350 .forEach((h) => h.deselect());
351
352 this.links
353 .filter((l) => l.link.id !== selNodeId)
354 .forEach((l) => l.deselect());
355
356 this.selectedNodes = [selectedNode.uiElement];
357 }
Sean Condon91481822019-01-01 13:56:14 +0000358 // Push the changes back up to parent (Topology Component)
Sean Condond88f3662019-04-03 16:35:30 +0100359 this.selectedNodeEvent.emit(this.selectedNodes);
Sean Condon0c577f62018-11-18 22:40:05 +0000360 }
361
Sean Condon021f0fa2018-12-06 23:31:11 -0800362 /**
363 * We want to filter links to show only those not related to hosts if the
Sean Condon50855cf2018-12-23 15:37:42 +0000364 * 'showHosts' flag has been switched off. If 'showHosts' is true, then
Sean Condon021f0fa2018-12-06 23:31:11 -0800365 * display all links.
366 */
Sean Condon50855cf2018-12-23 15:37:42 +0000367 filteredLinks(): Link[] {
Sean Condon021f0fa2018-12-06 23:31:11 -0800368 return this.regionData.links.filter((h) =>
369 this.showHosts ||
370 ((<Host>h.source).nodeType !== 'host' &&
371 (<Host>h.target).nodeType !== 'host'));
Sean Condon0c577f62018-11-18 22:40:05 +0000372 }
Sean Condon50855cf2018-12-23 15:37:42 +0000373
374 /**
375 * When changes happen in the model, then model events are sent up through the
376 * Web Socket
377 * @param type - the type of the change
378 * @param memo - a qualifier on the type
379 * @param subject - the item that the update is for
380 * @param data - the new definition of the item
381 */
382 handleModelEvent(type: ModelEventType, memo: ModelEventMemo, subject: string, data: UiElement): void {
383 switch (type) {
384 case ModelEventType.DEVICE_ADDED_OR_UPDATED:
385 if (memo === ModelEventMemo.ADDED) {
Sean Condon28884332019-03-21 14:07:00 +0000386 this.fixPosition(<Device>data);
Sean Condonee545762019-03-09 10:43:58 +0000387 this.graph.nodes.push(<Device>data);
Sean Condonee5d4b92019-03-11 19:57:34 +0000388 this.regionData.devices[this.visibleLayerIdx()].push(<Device>data);
Sean Condonee545762019-03-09 10:43:58 +0000389 this.log.debug('Device added', (<Device>data).id);
Sean Condon50855cf2018-12-23 15:37:42 +0000390 } else if (memo === ModelEventMemo.UPDATED) {
391 const oldDevice: Device =
392 this.regionData.devices[this.visibleLayerIdx()]
393 .find((d) => d.id === subject);
Sean Condonee545762019-03-09 10:43:58 +0000394 const changes = ForceSvgComponent.updateObject(oldDevice, <Device>data);
Sean Condon28884332019-03-21 14:07:00 +0000395 if (changes.numChanges > 0) {
Sean Condonee545762019-03-09 10:43:58 +0000396 this.log.debug('Device ', oldDevice.id, memo, ' - ', changes, 'changes');
Sean Condon28884332019-03-21 14:07:00 +0000397 if (changes.locationChanged) {
398 this.fixPosition(oldDevice);
399 }
Sean Condonee545762019-03-09 10:43:58 +0000400 }
Sean Condon50855cf2018-12-23 15:37:42 +0000401 } else {
402 this.log.warn('Device ', memo, ' - not yet implemented', data);
403 }
Sean Condon50855cf2018-12-23 15:37:42 +0000404 break;
405 case ModelEventType.HOST_ADDED_OR_UPDATED:
406 if (memo === ModelEventMemo.ADDED) {
Sean Condon28884332019-03-21 14:07:00 +0000407 this.fixPosition(<Host>data);
Sean Condonee545762019-03-09 10:43:58 +0000408 this.graph.nodes.push(<Host>data);
Sean Condon28884332019-03-21 14:07:00 +0000409 this.regionData.hosts[this.visibleLayerIdx()].push(<Host>data);
Sean Condonee545762019-03-09 10:43:58 +0000410 this.log.debug('Host added', (<Host>data).id);
Sean Condon50855cf2018-12-23 15:37:42 +0000411 } else if (memo === ModelEventMemo.UPDATED) {
412 const oldHost: Host = this.regionData.hosts[this.visibleLayerIdx()]
413 .find((h) => h.id === subject);
Sean Condonee545762019-03-09 10:43:58 +0000414 const changes = ForceSvgComponent.updateObject(oldHost, <Host>data);
Sean Condon28884332019-03-21 14:07:00 +0000415 if (changes.numChanges > 0) {
Sean Condonee545762019-03-09 10:43:58 +0000416 this.log.debug('Host ', oldHost.id, memo, ' - ', changes, 'changes');
Sean Condon28884332019-03-21 14:07:00 +0000417 if (changes.locationChanged) {
418 this.fixPosition(oldHost);
419 }
Sean Condonee545762019-03-09 10:43:58 +0000420 }
Sean Condon50855cf2018-12-23 15:37:42 +0000421 } else {
422 this.log.warn('Host change', memo, ' - unexpected');
423 }
424 break;
425 case ModelEventType.DEVICE_REMOVED:
426 if (memo === ModelEventMemo.REMOVED || memo === undefined) {
427 const removeIdx: number =
428 this.regionData.devices[this.visibleLayerIdx()]
429 .findIndex((d) => d.id === subject);
Sean Condonee545762019-03-09 10:43:58 +0000430 this.regionData.devices[this.visibleLayerIdx()].splice(removeIdx, 1);
431 this.removeRelatedLinks(subject);
432 this.log.debug('Device ', subject, 'removed. Links', this.regionData.links);
Sean Condon50855cf2018-12-23 15:37:42 +0000433 } else {
434 this.log.warn('Device removed - unexpected memo', memo);
435 }
436 break;
437 case ModelEventType.HOST_REMOVED:
438 if (memo === ModelEventMemo.REMOVED || memo === undefined) {
439 const removeIdx: number =
440 this.regionData.hosts[this.visibleLayerIdx()]
441 .findIndex((h) => h.id === subject);
Sean Condonee545762019-03-09 10:43:58 +0000442 this.regionData.hosts[this.visibleLayerIdx()].splice(removeIdx, 1);
443 this.removeRelatedLinks(subject);
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100444 this.log.debug('Host ', subject, 'removed');
Sean Condon50855cf2018-12-23 15:37:42 +0000445 } else {
446 this.log.warn('Host removed - unexpected memo', memo);
447 }
448 break;
449 case ModelEventType.LINK_ADDED_OR_UPDATED:
Sean Condonee545762019-03-09 10:43:58 +0000450 if (memo === ModelEventMemo.ADDED &&
451 this.regionData.links.findIndex((l) => l.id === subject) === -1) {
452 const listLen = this.regionData.links.push(<RegionLink>data);
453 const epA = ForceSvgComponent.extractNodeName(
454 this.regionData.links[listLen - 1].epA);
455 this.regionData.links[listLen - 1].source =
456 this.graph.nodes.find((node) =>
457 node.id === epA);
458 const epB = ForceSvgComponent.extractNodeName(
459 this.regionData.links[listLen - 1].epB);
460 this.regionData.links[listLen - 1].target =
461 this.graph.nodes.find((node) =>
462 node.id === epB);
463 this.log.debug('Link added', subject);
464 } else if (memo === ModelEventMemo.UPDATED) {
465 const oldLink = this.regionData.links.find((l) => l.id === subject);
466 const changes = ForceSvgComponent.updateObject(oldLink, <RegionLink>data);
467 this.log.debug('Link ', subject, '. Updated', changes, 'items');
468 } else {
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100469 this.log.warn('Link event ignored', subject, data);
470 }
471 break;
472 case ModelEventType.LINK_REMOVED:
473 if (memo === ModelEventMemo.REMOVED) {
474 const removeIdx = this.regionData.links.findIndex((l) => l.id === subject);
475 this.regionData.links.splice(removeIdx, 1);
476 this.log.debug('Link ', subject, 'removed');
Sean Condonee545762019-03-09 10:43:58 +0000477 }
Sean Condon50855cf2018-12-23 15:37:42 +0000478 break;
479 default:
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100480 this.log.error('Unexpected model event', type, 'for', subject, 'Data', data);
Sean Condon50855cf2018-12-23 15:37:42 +0000481 }
Sean Condon28884332019-03-21 14:07:00 +0000482 this.graph.links = this.regionData.links;
483 this.graph.reinitSimulation();
Sean Condon50855cf2018-12-23 15:37:42 +0000484 }
485
Sean Condonee545762019-03-09 10:43:58 +0000486 private removeRelatedLinks(subject: string) {
487 const len = this.regionData.links.length;
488 for (let i = 0; i < len; i++) {
489 const linkIdx = this.regionData.links.findIndex((l) =>
490 (ForceSvgComponent.extractNodeName(l.epA) === subject ||
491 ForceSvgComponent.extractNodeName(l.epB) === subject));
492 if (linkIdx >= 0) {
493 this.regionData.links.splice(linkIdx, 1);
494 this.log.debug('Link ', linkIdx, 'removed on attempt', i);
495 }
Sean Condon50855cf2018-12-23 15:37:42 +0000496 }
497 }
498
499 /**
500 * When traffic monitoring is turned on (A key) highlights will be sent back
501 * from the WebSocket through the Traffic Service
502 * @param devices - an array of device highlights
503 * @param hosts - an array of host highlights
504 * @param links - an array of link highlights
505 */
Sean Condon64ea7d22019-04-12 19:39:13 +0100506 handleHighlights(devices: Device[], hosts: Host[], links: LinkHighlight[], fadeMs: number = 0): void {
Sean Condon50855cf2018-12-23 15:37:42 +0000507
508 if (devices.length > 0) {
509 this.log.debug(devices.length, 'Devices highlighted');
510 devices.forEach((dh) => {
511 const deviceComponent: DeviceNodeSvgComponent = this.devices.find((d) => d.device.id === dh.id );
512 if (deviceComponent) {
513 deviceComponent.ngOnChanges(
514 {'deviceHighlight': new SimpleChange(<Device>{}, dh, true)}
515 );
516 this.log.debug('Highlighting device', deviceComponent.device.id);
517 } else {
518 this.log.warn('Device component not found', dh.id);
519 }
520 });
521 }
522 if (hosts.length > 0) {
523 this.log.debug(hosts.length, 'Hosts highlighted');
524 hosts.forEach((hh) => {
525 const hostComponent: HostNodeSvgComponent = this.hosts.find((h) => h.host.id === hh.id );
526 if (hostComponent) {
527 hostComponent.ngOnChanges(
528 {'hostHighlight': new SimpleChange(<Host>{}, hh, true)}
529 );
530 this.log.debug('Highlighting host', hostComponent.host.id);
531 }
532 });
533 }
534 if (links.length > 0) {
535 this.log.debug(links.length, 'Links highlighted');
536 links.forEach((lh) => {
Sean Condon64ea7d22019-04-12 19:39:13 +0100537 const linkComponent: LinkSvgComponent =
538 this.links.find((l) => l.link.id === Link.linkIdFromShowHighlights(lh.id) );
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100539 if (linkComponent) { // A link might not be present if hosts viewing is switched off
Sean Condon64ea7d22019-04-12 19:39:13 +0100540 if (fadeMs > 0) {
541 lh.fadems = fadeMs;
542 }
Sean Condon50855cf2018-12-23 15:37:42 +0000543 linkComponent.ngOnChanges(
544 {'linkHighlight': new SimpleChange(<LinkHighlight>{}, lh, true)}
545 );
Sean Condon50855cf2018-12-23 15:37:42 +0000546 }
547 });
548 }
549 }
Sean Condon71910542019-02-16 18:16:42 +0000550
551 /**
552 * As nodes are dragged around the graph, their new location should be sent
553 * back to server
554 * @param klass The class of node e.g. 'host' or 'device'
555 * @param id - the ID of the node
556 * @param newLocation - the new Location of the node
557 */
558 nodeMoved(klass: string, id: string, newLocation: MetaUi) {
Sean Condon28884332019-03-21 14:07:00 +0000559 this.wss.sendEvent('updateMeta2', <UpdateMeta>{
Sean Condon71910542019-02-16 18:16:42 +0000560 id: id,
561 class: klass,
562 memento: newLocation
563 });
564 this.log.debug(klass, id, 'has been moved to', newLocation);
565 }
Sean Condon1ae15802019-03-02 09:07:18 +0000566
Sean Condon9de21352019-04-06 19:22:27 +0100567 /**
568 * If any nodes with fixed positions had been dragged out of place
569 * then put back where they belong
570 * If there are some devices selected reset only these
571 */
572 resetNodeLocations(): number {
573 let numbernodes = 0;
574 if (this.selectedNodes.length > 0) {
575 this.devices
576 .filter((d) => this.selectedNodes.some((s) => s.id === d.device.id))
577 .forEach((dev) => {
578 Node.resetNodeLocation(<Node>dev.device);
579 numbernodes++;
580 });
581 this.hosts
582 .filter((h) => this.selectedNodes.some((s) => s.id === h.host.id))
583 .forEach((h) => {
584 Host.resetNodeLocation(<Host>h.host);
585 numbernodes++;
586 });
587 } else {
588 this.devices.forEach((dev) => {
589 Node.resetNodeLocation(<Node>dev.device);
590 numbernodes++;
591 });
592 this.hosts.forEach((h) => {
593 Host.resetNodeLocation(<Host>h.host);
594 numbernodes++;
595 });
596 }
597 this.graph.reinitSimulation();
598 return numbernodes;
Sean Condon1ae15802019-03-02 09:07:18 +0000599 }
Sean Condon9de21352019-04-06 19:22:27 +0100600
601 /**
602 * Toggle floating nodes between unpinned and frozen
603 * There may be frozen and unpinned in the selection
604 *
605 * If there are nodes selected toggle only these
606 */
607 unpinOrFreezeNodes(freeze: boolean): number {
608 let numbernodes = 0;
609 if (this.selectedNodes.length > 0) {
610 this.devices
611 .filter((d) => this.selectedNodes.some((s) => s.id === d.device.id))
612 .forEach((d) => {
613 Node.unpinOrFreezeNode(<Node>d.device, freeze);
614 numbernodes++;
615 });
616 this.hosts
617 .filter((h) => this.selectedNodes.some((s) => s.id === h.host.id))
618 .forEach((h) => {
619 Node.unpinOrFreezeNode(<Node>h.host, freeze);
620 numbernodes++;
621 });
622 } else {
623 this.devices.forEach((d) => {
624 Node.unpinOrFreezeNode(<Node>d.device, freeze);
625 numbernodes++;
626 });
627 this.hosts.forEach((h) => {
628 Node.unpinOrFreezeNode(<Node>h.host, freeze);
629 numbernodes++;
630 });
631 }
632 this.graph.reinitSimulation();
633 return numbernodes;
634 }
635
Sean Condonf4f54a12018-10-10 23:25:46 +0100636}
Sean Condon0c577f62018-11-18 22:40:05 +0000637