blob: ec3e57decd39551389ce20eefd8b920fcfd942b9 [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,
Sean Condon590b34b2019-12-04 18:44:37 +000023 OnChanges, OnDestroy,
Sean Condon0c577f62018-11-18 22:40:05 +000024 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 Condon98b6ddb2019-12-24 08:07:40 +000031import {LocMeta, LogService, MetaUi, WebSocketService, ZoomUtils} from '../../../../gui2-fw-lib/public_api';
Sean Condon1ae15802019-03-02 09:07:18 +000032import {
Sean Condon590b34b2019-12-04 18:44:37 +000033 Badge,
34 Device, DeviceHighlight,
Sean Condon5f7d3bc2019-04-15 11:18:34 +010035 DeviceProps,
Sean Condon0c577f62018-11-18 22:40:05 +000036 ForceDirectedGraph,
Sean Condon590b34b2019-12-04 18:44:37 +000037 Host, HostHighlight,
Sean Condon50855cf2018-12-23 15:37:42 +000038 HostLabelToggle,
Sean Condon0c577f62018-11-18 22:40:05 +000039 LabelToggle,
40 LayerType,
Sean Condon50855cf2018-12-23 15:37:42 +000041 Link,
42 LinkHighlight,
Sean Condon1ae15802019-03-02 09:07:18 +000043 Location,
Sean Condon50855cf2018-12-23 15:37:42 +000044 ModelEventMemo,
45 ModelEventType,
Sean Condon28884332019-03-21 14:07:00 +000046 Node,
Sean Condond88f3662019-04-03 16:35:30 +010047 Options,
Sean Condon0c577f62018-11-18 22:40:05 +000048 Region,
49 RegionLink,
Sean Condon50855cf2018-12-23 15:37:42 +000050 SubRegion,
51 UiElement
Sean Condon0c577f62018-11-18 22:40:05 +000052} from './models';
Sean Condonee545762019-03-09 10:43:58 +000053import {LocationType} from '../backgroundsvg/backgroundsvg.component';
Sean Condonff85fbe2019-03-16 14:28:46 +000054import {DeviceNodeSvgComponent} from './visuals/devicenodesvg/devicenodesvg.component';
Sean Condon5f7d3bc2019-04-15 11:18:34 +010055import {HostNodeSvgComponent} from './visuals/hostnodesvg/hostnodesvg.component';
56import {LinkSvgComponent} from './visuals/linksvg/linksvg.component';
Sean Condond88f3662019-04-03 16:35:30 +010057import {SelectedEvent} from './visuals/nodevisual';
Sean Condon71910542019-02-16 18:16:42 +000058
59interface UpdateMeta {
60 id: string;
61 class: string;
62 memento: MetaUi;
63}
Sean Condonaa4366d2018-11-02 14:29:01 +000064
Sean Condon28884332019-03-21 14:07:00 +000065const SVGCANVAS = <Options>{
66 width: 1000,
67 height: 1000
68};
69
70interface ChangeSummary {
71 numChanges: number;
72 locationChanged: boolean;
73}
74
Sean Condonf4f54a12018-10-10 23:25:46 +010075/**
76 * ONOS GUI -- Topology Forces Graph Layer View.
Sean Condon0c577f62018-11-18 22:40:05 +000077 *
78 * The regionData is set by Topology Service on WebSocket topo2CurrentRegion callback
79 * This drives the whole Force graph
Sean Condonf4f54a12018-10-10 23:25:46 +010080 */
81@Component({
82 selector: '[onos-forcesvg]',
83 templateUrl: './forcesvg.component.html',
Sean Condon0c577f62018-11-18 22:40:05 +000084 styleUrls: ['./forcesvg.component.css'],
85 changeDetection: ChangeDetectionStrategy.OnPush,
Sean Condonf4f54a12018-10-10 23:25:46 +010086})
Sean Condon590b34b2019-12-04 18:44:37 +000087export class ForceSvgComponent implements OnInit, OnDestroy, OnChanges {
Sean Condonff85fbe2019-03-16 14:28:46 +000088 @Input() deviceLabelToggle: LabelToggle.Enum = LabelToggle.Enum.NONE;
89 @Input() hostLabelToggle: HostLabelToggle.Enum = HostLabelToggle.Enum.NONE;
Sean Condonb2c483c2019-01-16 20:28:55 +000090 @Input() showHosts: boolean = false;
Sean Condon590b34b2019-12-04 18:44:37 +000091 @Input() showAlarms: boolean = false;
Sean Condonb2c483c2019-01-16 20:28:55 +000092 @Input() highlightPorts: boolean = true;
93 @Input() onosInstMastership: string = '';
94 @Input() visibleLayer: LayerType = LayerType.LAYER_DEFAULT;
95 @Input() selectedLink: RegionLink = null;
Sean Condon1ae15802019-03-02 09:07:18 +000096 @Input() scale: number = 1;
Sean Condon0c577f62018-11-18 22:40:05 +000097 @Input() regionData: Region = <Region>{devices: [ [], [], [] ], hosts: [ [], [], [] ], links: []};
Sean Condonb2c483c2019-01-16 20:28:55 +000098 @Output() linkSelected = new EventEmitter<RegionLink>();
Sean Condond88f3662019-04-03 16:35:30 +010099 @Output() selectedNodeEvent = new EventEmitter<UiElement[]>();
Sean Condonff85fbe2019-03-16 14:28:46 +0000100 public graph: ForceDirectedGraph;
Sean Condond88f3662019-04-03 16:35:30 +0100101 private selectedNodes: UiElement[] = [];
Sean Condon590b34b2019-12-04 18:44:37 +0000102 viewInitialized: boolean = false;
Sean Condonf4f54a12018-10-10 23:25:46 +0100103
Sean Condon021f0fa2018-12-06 23:31:11 -0800104 // References to the children of this component - these are created in the
105 // template view with the *ngFor and we get them by a query here
Sean Condon0c577f62018-11-18 22:40:05 +0000106 @ViewChildren(DeviceNodeSvgComponent) devices: QueryList<DeviceNodeSvgComponent>;
Sean Condon021f0fa2018-12-06 23:31:11 -0800107 @ViewChildren(HostNodeSvgComponent) hosts: QueryList<HostNodeSvgComponent>;
Sean Condon50855cf2018-12-23 15:37:42 +0000108 @ViewChildren(LinkSvgComponent) links: QueryList<LinkSvgComponent>;
Sean Condonf4f54a12018-10-10 23:25:46 +0100109
Sean Condon0c577f62018-11-18 22:40:05 +0000110 constructor(
111 protected log: LogService,
Sean Condon71910542019-02-16 18:16:42 +0000112 private ref: ChangeDetectorRef,
113 protected wss: WebSocketService
Sean Condon0c577f62018-11-18 22:40:05 +0000114 ) {
115 this.selectedLink = null;
116 this.log.debug('ForceSvgComponent constructed');
117 }
118
119 /**
Sean Condon021f0fa2018-12-06 23:31:11 -0800120 * Utility for extracting a node name from an endpoint string
121 * In some cases - have to remove the port number from the end of a device
122 * name
123 * @param endPtStr The end point name
124 */
Sean Condonb77768e2019-05-04 20:23:42 +0100125 static extractNodeName(endPtStr: string, portStr: string): string {
126 if (portStr === undefined || endPtStr === undefined) {
Sean Condon021f0fa2018-12-06 23:31:11 -0800127 return endPtStr;
Sean Condonb77768e2019-05-04 20:23:42 +0100128 } else if (endPtStr.includes('/')) {
129 return endPtStr.substr(0, endPtStr.length - portStr.length - 1);
Sean Condon021f0fa2018-12-06 23:31:11 -0800130 }
Sean Condonb77768e2019-05-04 20:23:42 +0100131 return endPtStr;
Sean Condon021f0fa2018-12-06 23:31:11 -0800132 }
133
Sean Condonee545762019-03-09 10:43:58 +0000134 /**
135 * Recursive method to compare 2 objects attribute by attribute and update
136 * the first where a change is detected
137 * @param existingNode 1st object
138 * @param updatedNode 2nd object
139 */
Sean Condon28884332019-03-21 14:07:00 +0000140 private static updateObject(existingNode: Object, updatedNode: Object): ChangeSummary {
141 const changed = <ChangeSummary>{numChanges: 0, locationChanged: false};
Sean Condonee545762019-03-09 10:43:58 +0000142 for (const key of Object.keys(updatedNode)) {
143 const o = updatedNode[key];
Sean Condon28884332019-03-21 14:07:00 +0000144 if (['id', 'x', 'y', 'fx', 'fy', 'vx', 'vy', 'index'].some(k => k === key)) {
Sean Condonee545762019-03-09 10:43:58 +0000145 continue;
146 } else if (o && typeof o === 'object' && o.constructor === Object) {
Sean Condon28884332019-03-21 14:07:00 +0000147 const subChanged = ForceSvgComponent.updateObject(existingNode[key], updatedNode[key]);
148 changed.numChanges += subChanged.numChanges;
149 changed.locationChanged = subChanged.locationChanged ? true : changed.locationChanged;
150 } else if (existingNode === undefined) {
151 // Copy the whole object
152 existingNode = updatedNode;
153 changed.locationChanged = true;
154 changed.numChanges++;
Sean Condonee545762019-03-09 10:43:58 +0000155 } else if (existingNode[key] !== updatedNode[key]) {
Sean Condon28884332019-03-21 14:07:00 +0000156 if (['locType', 'latOrY', 'longOrX', 'latitude', 'longitude', 'gridX', 'gridY'].some(k => k === key)) {
157 changed.locationChanged = true;
158 }
159 changed.numChanges++;
Sean Condonee545762019-03-09 10:43:58 +0000160 existingNode[key] = updatedNode[key];
161 }
162 }
163 return changed;
164 }
165
Sean Condon021f0fa2018-12-06 23:31:11 -0800166 @HostListener('window:resize', ['$event'])
167 onResize(event) {
Sean Condon28884332019-03-21 14:07:00 +0000168 this.graph.restartSimulation();
169 this.log.debug('Simulation restart after resize', event);
Sean Condon021f0fa2018-12-06 23:31:11 -0800170 }
171
172 /**
Sean Condon0c577f62018-11-18 22:40:05 +0000173 * After the component is initialized create the Force simulation
Sean Condon021f0fa2018-12-06 23:31:11 -0800174 * The list of devices, hosts and links will not have been receieved back
175 * from the WebSocket yet as this time - they will be updated later through
176 * ngOnChanges()
Sean Condon0c577f62018-11-18 22:40:05 +0000177 */
178 ngOnInit() {
179 // Receiving an initialized simulated graph from our custom d3 service
Sean Condon28884332019-03-21 14:07:00 +0000180 this.graph = new ForceDirectedGraph(SVGCANVAS, this.log);
Sean Condon0c577f62018-11-18 22:40:05 +0000181
182 /** Binding change detection check on each tick
Sean Condon021f0fa2018-12-06 23:31:11 -0800183 * This along with an onPush change detection strategy should enforce
184 * checking only when relevant! This improves scripting computation
185 * duration in a couple of tests I've made, consistently. Also, it makes
186 * sense to avoid unnecessary checks when we are dealing only with
187 * simulations data binding.
Sean Condon0c577f62018-11-18 22:40:05 +0000188 */
189 this.graph.ticker.subscribe((simulation) => {
Sean Condon28884332019-03-21 14:07:00 +0000190 // this.log.debug("Force simulation has ticked. Alpha",
191 // Math.round(simulation.alpha() * 1000) / 1000);
Sean Condon0c577f62018-11-18 22:40:05 +0000192 this.ref.markForCheck();
193 });
Sean Condon28884332019-03-21 14:07:00 +0000194
Sean Condon0c577f62018-11-18 22:40:05 +0000195 this.log.debug('ForceSvgComponent initialized - waiting for nodes and links');
196
Sean Condon0c577f62018-11-18 22:40:05 +0000197 }
198
199 /**
Sean Condon021f0fa2018-12-06 23:31:11 -0800200 * When any one of the inputs get changed by a containing component, this
201 * gets called automatically. In addition this is called manually by
202 * topology.service when a response is received from the WebSocket from the
203 * server
Sean Condon0c577f62018-11-18 22:40:05 +0000204 *
205 * The Devices, Hosts and SubRegions are all added to the Node list for the simulation
206 * The Links are added to the Link list of the simulation.
207 * Before they are added the Links are associated with Nodes based on their endPt
208 *
209 * @param changes - a list of changed @Input(s)
210 */
211 ngOnChanges(changes: SimpleChanges) {
212 if (changes['regionData']) {
213 const devices: Device[] =
214 changes['regionData'].currentValue.devices[this.visibleLayerIdx()];
215 const hosts: Host[] =
216 changes['regionData'].currentValue.hosts[this.visibleLayerIdx()];
217 const subRegions: SubRegion[] = changes['regionData'].currentValue.subRegion;
218 this.graph.nodes = [];
219 if (devices) {
220 this.graph.nodes = devices;
221 }
222 if (hosts) {
223 this.graph.nodes = this.graph.nodes.concat(hosts);
224 }
225 if (subRegions) {
226 this.graph.nodes = this.graph.nodes.concat(subRegions);
227 }
228
Sean Condon28884332019-03-21 14:07:00 +0000229 this.graph.nodes.forEach((n) => this.fixPosition(n));
Sean Condon71910542019-02-16 18:16:42 +0000230
Sean Condon0c577f62018-11-18 22:40:05 +0000231 // Associate the endpoints of each link with a real node
232 this.graph.links = [];
233 for (const linkIdx of Object.keys(this.regionData.links)) {
Sean Condonb77768e2019-05-04 20:23:42 +0100234 const link = this.regionData.links[linkIdx];
235 const epA = ForceSvgComponent.extractNodeName(link.epA, link.portA);
236 if (!this.graph.nodes.find((node) => node.id === epA)) {
237 this.log.error('ngOnChange Could not find endpoint A', epA, 'for', link);
238 continue;
239 }
240 const epB = ForceSvgComponent.extractNodeName(
241 link.epB, link.portB);
242 if (!this.graph.nodes.find((node) => node.id === epB)) {
243 this.log.error('ngOnChange Could not find endpoint B', epB, 'for', link);
244 continue;
245 }
Sean Condon0c577f62018-11-18 22:40:05 +0000246 this.regionData.links[linkIdx].source =
247 this.graph.nodes.find((node) =>
Sean Condon021f0fa2018-12-06 23:31:11 -0800248 node.id === epA);
Sean Condon0c577f62018-11-18 22:40:05 +0000249 this.regionData.links[linkIdx].target =
250 this.graph.nodes.find((node) =>
Sean Condon021f0fa2018-12-06 23:31:11 -0800251 node.id === epB);
Sean Condon0c577f62018-11-18 22:40:05 +0000252 this.regionData.links[linkIdx].index = Number(linkIdx);
253 }
254
255 this.graph.links = this.regionData.links;
Sean Condon28884332019-03-21 14:07:00 +0000256 if (this.graph.nodes.length > 0) {
257 this.graph.reinitSimulation();
258 }
Sean Condon0c577f62018-11-18 22:40:05 +0000259 this.log.debug('ForceSvgComponent input changed',
260 this.graph.nodes.length, 'nodes,', this.graph.links.length, 'links');
Sean Condon590b34b2019-12-04 18:44:37 +0000261 if (!this.viewInitialized) {
262 this.viewInitialized = true;
263 if (this.showAlarms) {
264 this.wss.sendEvent('alarmTopovDisplayStart', {});
265 }
266 }
Sean Condon0c577f62018-11-18 22:40:05 +0000267 }
Sean Condon590b34b2019-12-04 18:44:37 +0000268
269 if (changes['showAlarms'] && this.viewInitialized) {
270 if (this.showAlarms) {
271 this.wss.sendEvent('alarmTopovDisplayStart', {});
272 } else {
273 this.wss.sendEvent('alarmTopovDisplayStop', {});
274 this.cancelAllDeviceHighlightsNow();
275 }
276 }
277 }
278
279 ngOnDestroy(): void {
280 if (this.showAlarms) {
281 this.wss.sendEvent('alarmTopovDisplayStop', {});
282 this.cancelAllDeviceHighlightsNow();
283 }
284 this.viewInitialized = false;
Sean Condon28884332019-03-21 14:07:00 +0000285 }
Sean Condon021f0fa2018-12-06 23:31:11 -0800286
Sean Condon28884332019-03-21 14:07:00 +0000287 /**
Sean Condon058804c2019-04-16 09:41:52 +0100288 * If instance has a value then mute colors of devices not connected to it
289 * Otherwise if instance does not have a value unmute all
290 * @param instanceName name of the selected instance
291 */
292 changeInstSelection(instanceName: string) {
293 this.log.debug('Mastership changed', instanceName);
294 this.devices.filter((d) => d.device.master !== instanceName)
295 .forEach((d) => {
296 const isMuted = Boolean(instanceName);
297 d.ngOnChanges({'colorMuted': new SimpleChange(!isMuted, isMuted, true)});
298 }
299 );
300 }
301
302 /**
Sean Condon28884332019-03-21 14:07:00 +0000303 * If a node has a fixed location then assign it to fx and fy so
304 * that it doesn't get affected by forces
305 * @param graphNode The node whose location should be processed
306 */
307 private fixPosition(graphNode: Node): void {
308 const loc: Location = <Location>graphNode['location'];
309 const props: DeviceProps = <DeviceProps>graphNode['props'];
310 const metaUi = <MetaUi>graphNode['metaUi'];
311 if (loc && loc.locType === LocationType.GEO) {
312 const position: MetaUi =
313 ZoomUtils.convertGeoToCanvas(
314 <LocMeta>{lng: loc.longOrX, lat: loc.latOrY});
315 graphNode.fx = position.x;
316 graphNode.fy = position.y;
317 this.log.debug('Found node', graphNode.id, 'with', loc.locType);
318 } else if (loc && loc.locType === LocationType.GRID) {
319 graphNode.fx = loc.longOrX;
320 graphNode.fy = loc.latOrY;
321 this.log.debug('Found node', graphNode.id, 'with', loc.locType);
322 } else if (props && props.locType === LocationType.NONE && metaUi) {
323 graphNode.fx = metaUi.x;
324 graphNode.fy = metaUi.y;
325 this.log.debug('Found node', graphNode.id, 'with locType=none and metaUi');
326 } else {
327 graphNode.fx = null;
328 graphNode.fy = null;
329 }
Sean Condon0c577f62018-11-18 22:40:05 +0000330 }
331
332 /**
333 * Get the index of LayerType so it can drive the visibility of nodes and
334 * hosts on layers
335 */
336 visibleLayerIdx(): number {
337 const layerKeys: string[] = Object.keys(LayerType);
338 for (const idx in layerKeys) {
339 if (LayerType[layerKeys[idx]] === this.visibleLayer) {
340 return Number(idx);
341 }
342 }
343 return -1;
344 }
345
346 selectLink(link: RegionLink): void {
347 this.selectedLink = link;
348 this.linkSelected.emit(link);
349 }
350
Sean Condon021f0fa2018-12-06 23:31:11 -0800351 /**
Sean Condond88f3662019-04-03 16:35:30 +0100352 * Iterate through all hosts and devices and links to deselect the previously selected
Sean Condon021f0fa2018-12-06 23:31:11 -0800353 * node. The emit an event to the parent that lets it know the selection has
354 * changed.
Sean Condond88f3662019-04-03 16:35:30 +0100355 *
356 * This function collates all of the nodes that have been selected and passes
357 * a collection of nodes up to the topology component
358 *
Sean Condon021f0fa2018-12-06 23:31:11 -0800359 * @param selectedNode the newly selected node
360 */
Sean Condond88f3662019-04-03 16:35:30 +0100361 updateSelected(selectedNode: SelectedEvent): void {
362 this.log.debug('Node or link ',
Sean Condon64ea7d22019-04-12 19:39:13 +0100363 selectedNode.uiElement ? selectedNode.uiElement.id : '--',
Sean Condond88f3662019-04-03 16:35:30 +0100364 selectedNode.deselecting ? 'deselected' : 'selected',
365 selectedNode.isShift ? 'Multiple' : '');
Sean Condon021f0fa2018-12-06 23:31:11 -0800366
Sean Condond88f3662019-04-03 16:35:30 +0100367 if (selectedNode.isShift && selectedNode.deselecting) {
368 const idx = this.selectedNodes.findIndex((n) =>
369 n.id === selectedNode.uiElement.id
370 );
371 this.selectedNodes.splice(idx, 1);
372 this.log.debug('Removed node', idx);
373
374 } else if (selectedNode.isShift) {
375 this.selectedNodes.push(selectedNode.uiElement);
376
377 } else if (selectedNode.deselecting) {
Sean Condon64ea7d22019-04-12 19:39:13 +0100378 this.devices
379 .forEach((d) => d.deselect());
380 this.hosts
381 .forEach((h) => h.deselect());
382 this.links
383 .forEach((l) => l.deselect());
Sean Condond88f3662019-04-03 16:35:30 +0100384 this.selectedNodes = [];
385
386 } else {
387 const selNodeId = selectedNode.uiElement.id;
388 // Otherwise if shift was not pressed deselect previous
389 this.devices
390 .filter((d) => d.device.id !== selNodeId)
391 .forEach((d) => d.deselect());
392 this.hosts
393 .filter((h) => h.host.id !== selNodeId)
394 .forEach((h) => h.deselect());
395
396 this.links
397 .filter((l) => l.link.id !== selNodeId)
398 .forEach((l) => l.deselect());
399
400 this.selectedNodes = [selectedNode.uiElement];
401 }
Sean Condon91481822019-01-01 13:56:14 +0000402 // Push the changes back up to parent (Topology Component)
Sean Condond88f3662019-04-03 16:35:30 +0100403 this.selectedNodeEvent.emit(this.selectedNodes);
Sean Condon0c577f62018-11-18 22:40:05 +0000404 }
405
Sean Condon021f0fa2018-12-06 23:31:11 -0800406 /**
407 * We want to filter links to show only those not related to hosts if the
Sean Condon50855cf2018-12-23 15:37:42 +0000408 * 'showHosts' flag has been switched off. If 'showHosts' is true, then
Sean Condon021f0fa2018-12-06 23:31:11 -0800409 * display all links.
410 */
Sean Condon50855cf2018-12-23 15:37:42 +0000411 filteredLinks(): Link[] {
Sean Condon021f0fa2018-12-06 23:31:11 -0800412 return this.regionData.links.filter((h) =>
413 this.showHosts ||
414 ((<Host>h.source).nodeType !== 'host' &&
415 (<Host>h.target).nodeType !== 'host'));
Sean Condon0c577f62018-11-18 22:40:05 +0000416 }
Sean Condon50855cf2018-12-23 15:37:42 +0000417
418 /**
419 * When changes happen in the model, then model events are sent up through the
420 * Web Socket
421 * @param type - the type of the change
422 * @param memo - a qualifier on the type
423 * @param subject - the item that the update is for
424 * @param data - the new definition of the item
425 */
426 handleModelEvent(type: ModelEventType, memo: ModelEventMemo, subject: string, data: UiElement): void {
427 switch (type) {
428 case ModelEventType.DEVICE_ADDED_OR_UPDATED:
429 if (memo === ModelEventMemo.ADDED) {
Sean Condon28884332019-03-21 14:07:00 +0000430 this.fixPosition(<Device>data);
Sean Condonee545762019-03-09 10:43:58 +0000431 this.graph.nodes.push(<Device>data);
Sean Condonee5d4b92019-03-11 19:57:34 +0000432 this.regionData.devices[this.visibleLayerIdx()].push(<Device>data);
Sean Condonee545762019-03-09 10:43:58 +0000433 this.log.debug('Device added', (<Device>data).id);
Sean Condon50855cf2018-12-23 15:37:42 +0000434 } else if (memo === ModelEventMemo.UPDATED) {
435 const oldDevice: Device =
436 this.regionData.devices[this.visibleLayerIdx()]
437 .find((d) => d.id === subject);
Sean Condonee545762019-03-09 10:43:58 +0000438 const changes = ForceSvgComponent.updateObject(oldDevice, <Device>data);
Sean Condon28884332019-03-21 14:07:00 +0000439 if (changes.numChanges > 0) {
Sean Condonee545762019-03-09 10:43:58 +0000440 this.log.debug('Device ', oldDevice.id, memo, ' - ', changes, 'changes');
Sean Condon28884332019-03-21 14:07:00 +0000441 if (changes.locationChanged) {
442 this.fixPosition(oldDevice);
443 }
Sean Condon058804c2019-04-16 09:41:52 +0100444 const svgDevice: DeviceNodeSvgComponent =
445 this.devices.find((svgdevice) => svgdevice.device.id === subject);
446 svgDevice.ngOnChanges({'device':
447 new SimpleChange(<Device>{}, oldDevice, true)
448 });
Sean Condonee545762019-03-09 10:43:58 +0000449 }
Sean Condon50855cf2018-12-23 15:37:42 +0000450 } else {
451 this.log.warn('Device ', memo, ' - not yet implemented', data);
452 }
Sean Condon50855cf2018-12-23 15:37:42 +0000453 break;
454 case ModelEventType.HOST_ADDED_OR_UPDATED:
455 if (memo === ModelEventMemo.ADDED) {
Sean Condon28884332019-03-21 14:07:00 +0000456 this.fixPosition(<Host>data);
Sean Condonee545762019-03-09 10:43:58 +0000457 this.graph.nodes.push(<Host>data);
Sean Condon28884332019-03-21 14:07:00 +0000458 this.regionData.hosts[this.visibleLayerIdx()].push(<Host>data);
Sean Condonee545762019-03-09 10:43:58 +0000459 this.log.debug('Host added', (<Host>data).id);
Sean Condon50855cf2018-12-23 15:37:42 +0000460 } else if (memo === ModelEventMemo.UPDATED) {
461 const oldHost: Host = this.regionData.hosts[this.visibleLayerIdx()]
462 .find((h) => h.id === subject);
Sean Condonee545762019-03-09 10:43:58 +0000463 const changes = ForceSvgComponent.updateObject(oldHost, <Host>data);
Sean Condon28884332019-03-21 14:07:00 +0000464 if (changes.numChanges > 0) {
Sean Condonee545762019-03-09 10:43:58 +0000465 this.log.debug('Host ', oldHost.id, memo, ' - ', changes, 'changes');
Sean Condon28884332019-03-21 14:07:00 +0000466 if (changes.locationChanged) {
467 this.fixPosition(oldHost);
468 }
Sean Condonee545762019-03-09 10:43:58 +0000469 }
Sean Condon50855cf2018-12-23 15:37:42 +0000470 } else {
471 this.log.warn('Host change', memo, ' - unexpected');
472 }
473 break;
474 case ModelEventType.DEVICE_REMOVED:
475 if (memo === ModelEventMemo.REMOVED || memo === undefined) {
476 const removeIdx: number =
477 this.regionData.devices[this.visibleLayerIdx()]
478 .findIndex((d) => d.id === subject);
Sean Condonee545762019-03-09 10:43:58 +0000479 this.regionData.devices[this.visibleLayerIdx()].splice(removeIdx, 1);
480 this.removeRelatedLinks(subject);
481 this.log.debug('Device ', subject, 'removed. Links', this.regionData.links);
Sean Condon50855cf2018-12-23 15:37:42 +0000482 } else {
483 this.log.warn('Device removed - unexpected memo', memo);
484 }
485 break;
486 case ModelEventType.HOST_REMOVED:
487 if (memo === ModelEventMemo.REMOVED || memo === undefined) {
488 const removeIdx: number =
489 this.regionData.hosts[this.visibleLayerIdx()]
490 .findIndex((h) => h.id === subject);
Sean Condonee545762019-03-09 10:43:58 +0000491 this.regionData.hosts[this.visibleLayerIdx()].splice(removeIdx, 1);
492 this.removeRelatedLinks(subject);
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100493 this.log.debug('Host ', subject, 'removed');
Sean Condon50855cf2018-12-23 15:37:42 +0000494 } else {
495 this.log.warn('Host removed - unexpected memo', memo);
496 }
497 break;
498 case ModelEventType.LINK_ADDED_OR_UPDATED:
Sean Condonee545762019-03-09 10:43:58 +0000499 if (memo === ModelEventMemo.ADDED &&
500 this.regionData.links.findIndex((l) => l.id === subject) === -1) {
Sean Condonb77768e2019-05-04 20:23:42 +0100501 const newLink = <RegionLink>data;
502
503
Sean Condonee545762019-03-09 10:43:58 +0000504 const epA = ForceSvgComponent.extractNodeName(
Sean Condonb77768e2019-05-04 20:23:42 +0100505 newLink.epA, newLink.portA);
506 if (!this.graph.nodes.find((node) => node.id === epA)) {
507 this.log.error('Could not find endpoint A', epA, 'of', newLink);
508 break;
509 }
Sean Condonee545762019-03-09 10:43:58 +0000510 const epB = ForceSvgComponent.extractNodeName(
Sean Condonb77768e2019-05-04 20:23:42 +0100511 newLink.epB, newLink.portB);
512 if (!this.graph.nodes.find((node) => node.id === epB)) {
513 this.log.error('Could not find endpoint B', epB, 'of link', newLink);
514 break;
515 }
516
517 const listLen = this.regionData.links.push(<RegionLink>data);
518 this.regionData.links[listLen - 1].source =
519 this.graph.nodes.find((node) => node.id === epA);
Sean Condonee545762019-03-09 10:43:58 +0000520 this.regionData.links[listLen - 1].target =
Sean Condonb77768e2019-05-04 20:23:42 +0100521 this.graph.nodes.find((node) => node.id === epB);
Sean Condonee545762019-03-09 10:43:58 +0000522 this.log.debug('Link added', subject);
523 } else if (memo === ModelEventMemo.UPDATED) {
524 const oldLink = this.regionData.links.find((l) => l.id === subject);
525 const changes = ForceSvgComponent.updateObject(oldLink, <RegionLink>data);
526 this.log.debug('Link ', subject, '. Updated', changes, 'items');
527 } else {
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100528 this.log.warn('Link event ignored', subject, data);
529 }
530 break;
531 case ModelEventType.LINK_REMOVED:
532 if (memo === ModelEventMemo.REMOVED) {
533 const removeIdx = this.regionData.links.findIndex((l) => l.id === subject);
534 this.regionData.links.splice(removeIdx, 1);
535 this.log.debug('Link ', subject, 'removed');
Sean Condonee545762019-03-09 10:43:58 +0000536 }
Sean Condon50855cf2018-12-23 15:37:42 +0000537 break;
538 default:
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100539 this.log.error('Unexpected model event', type, 'for', subject, 'Data', data);
Sean Condon50855cf2018-12-23 15:37:42 +0000540 }
Sean Condon28884332019-03-21 14:07:00 +0000541 this.graph.links = this.regionData.links;
542 this.graph.reinitSimulation();
Sean Condon50855cf2018-12-23 15:37:42 +0000543 }
544
Sean Condonee545762019-03-09 10:43:58 +0000545 private removeRelatedLinks(subject: string) {
546 const len = this.regionData.links.length;
547 for (let i = 0; i < len; i++) {
548 const linkIdx = this.regionData.links.findIndex((l) =>
Sean Condonb77768e2019-05-04 20:23:42 +0100549 (ForceSvgComponent.extractNodeName(l.epA, l.portA) === subject ||
550 ForceSvgComponent.extractNodeName(l.epB, l.portB) === subject));
Sean Condonee545762019-03-09 10:43:58 +0000551 if (linkIdx >= 0) {
552 this.regionData.links.splice(linkIdx, 1);
553 this.log.debug('Link ', linkIdx, 'removed on attempt', i);
554 }
Sean Condon50855cf2018-12-23 15:37:42 +0000555 }
556 }
557
558 /**
559 * When traffic monitoring is turned on (A key) highlights will be sent back
560 * from the WebSocket through the Traffic Service
Sean Condon4e55c802019-12-03 22:13:34 +0000561 * Also handles Intent highlights in case one is selected
Sean Condon50855cf2018-12-23 15:37:42 +0000562 * @param devices - an array of device highlights
563 * @param hosts - an array of host highlights
564 * @param links - an array of link highlights
565 */
Sean Condon590b34b2019-12-04 18:44:37 +0000566 handleHighlights(devices: DeviceHighlight[], hosts: HostHighlight[], links: LinkHighlight[], fadeMs: number = 0): void {
Sean Condon50855cf2018-12-23 15:37:42 +0000567
568 if (devices.length > 0) {
569 this.log.debug(devices.length, 'Devices highlighted');
Sean Condon590b34b2019-12-04 18:44:37 +0000570 devices.forEach((dh: DeviceHighlight) => {
571 this.devices.forEach((d: DeviceNodeSvgComponent) => {
572 if (d.device.id === dh.id) {
573 d.badge = dh.badge;
574 this.ref.markForCheck(); // Forces ngOnChange in the DeviceSvgComponent
575 this.log.debug('Highlighting device', dh.id);
576 }
577 });
Sean Condon50855cf2018-12-23 15:37:42 +0000578 });
579 }
580 if (hosts.length > 0) {
581 this.log.debug(hosts.length, 'Hosts highlighted');
Sean Condon590b34b2019-12-04 18:44:37 +0000582 hosts.forEach((hh: HostHighlight) => {
583 this.hosts.forEach((h) => {
584 if (h.host.id === hh.id) {
585 h.badge = hh.badge;
586 this.ref.markForCheck(); // Forces ngOnChange in the HostSvgComponent
587 this.log.debug('Highlighting host', hh.id);
588 }
589 });
Sean Condon50855cf2018-12-23 15:37:42 +0000590 });
591 }
592 if (links.length > 0) {
593 this.log.debug(links.length, 'Links highlighted');
Sean Condon4e55c802019-12-03 22:13:34 +0000594 links.forEach((lh: LinkHighlight) => {
595 if (fadeMs > 0) {
596 lh.fadems = fadeMs;
Sean Condon50855cf2018-12-23 15:37:42 +0000597 }
Sean Condon4e55c802019-12-03 22:13:34 +0000598 // Don't user .filter() above as it will create a copy of the component which will be discarded
599 this.links.forEach((l) => {
600 if (l.link.id === Link.linkIdFromShowHighlights(lh.id)) {
601 l.linkHighlight = lh;
Sean Condon590b34b2019-12-04 18:44:37 +0000602 this.ref.markForCheck(); // Forces ngOnChange in the LinkSvgComponent
Sean Condon4e55c802019-12-03 22:13:34 +0000603 }
604 });
Sean Condon50855cf2018-12-23 15:37:42 +0000605 });
606 }
607 }
Sean Condon71910542019-02-16 18:16:42 +0000608
Sean Condon590b34b2019-12-04 18:44:37 +0000609 cancelAllHostHighlightsNow() {
610 this.hosts.forEach((host: HostNodeSvgComponent) => {
611 host.badge = undefined;
612 this.ref.markForCheck(); // Forces ngOnChange in the HostSvgComponent
613 });
614 }
615
616 cancelAllDeviceHighlightsNow() {
617 this.devices.forEach((device: DeviceNodeSvgComponent) => {
618 device.badge = undefined;
619 this.ref.markForCheck(); // Forces ngOnChange in the DeviceSvgComponent
620 });
621 }
622
Sean Condon4e55c802019-12-03 22:13:34 +0000623 cancelAllLinkHighlightsNow() {
624 this.links.forEach((link: LinkSvgComponent) => {
625 link.linkHighlight = <LinkHighlight>{};
Sean Condon590b34b2019-12-04 18:44:37 +0000626 this.ref.markForCheck(); // Forces ngOnChange in the LinkSvgComponent
Sean Condon4e55c802019-12-03 22:13:34 +0000627 });
628 }
629
Sean Condon71910542019-02-16 18:16:42 +0000630 /**
631 * As nodes are dragged around the graph, their new location should be sent
632 * back to server
633 * @param klass The class of node e.g. 'host' or 'device'
634 * @param id - the ID of the node
635 * @param newLocation - the new Location of the node
636 */
637 nodeMoved(klass: string, id: string, newLocation: MetaUi) {
Sean Condon28884332019-03-21 14:07:00 +0000638 this.wss.sendEvent('updateMeta2', <UpdateMeta>{
Sean Condon71910542019-02-16 18:16:42 +0000639 id: id,
640 class: klass,
641 memento: newLocation
642 });
643 this.log.debug(klass, id, 'has been moved to', newLocation);
644 }
Sean Condon1ae15802019-03-02 09:07:18 +0000645
Sean Condon9de21352019-04-06 19:22:27 +0100646 /**
647 * If any nodes with fixed positions had been dragged out of place
648 * then put back where they belong
649 * If there are some devices selected reset only these
650 */
651 resetNodeLocations(): number {
652 let numbernodes = 0;
653 if (this.selectedNodes.length > 0) {
654 this.devices
655 .filter((d) => this.selectedNodes.some((s) => s.id === d.device.id))
656 .forEach((dev) => {
657 Node.resetNodeLocation(<Node>dev.device);
658 numbernodes++;
659 });
660 this.hosts
661 .filter((h) => this.selectedNodes.some((s) => s.id === h.host.id))
662 .forEach((h) => {
663 Host.resetNodeLocation(<Host>h.host);
664 numbernodes++;
665 });
666 } else {
667 this.devices.forEach((dev) => {
668 Node.resetNodeLocation(<Node>dev.device);
669 numbernodes++;
670 });
671 this.hosts.forEach((h) => {
672 Host.resetNodeLocation(<Host>h.host);
673 numbernodes++;
674 });
675 }
676 this.graph.reinitSimulation();
677 return numbernodes;
Sean Condon1ae15802019-03-02 09:07:18 +0000678 }
Sean Condon9de21352019-04-06 19:22:27 +0100679
680 /**
681 * Toggle floating nodes between unpinned and frozen
682 * There may be frozen and unpinned in the selection
683 *
684 * If there are nodes selected toggle only these
685 */
686 unpinOrFreezeNodes(freeze: boolean): number {
687 let numbernodes = 0;
688 if (this.selectedNodes.length > 0) {
689 this.devices
690 .filter((d) => this.selectedNodes.some((s) => s.id === d.device.id))
691 .forEach((d) => {
692 Node.unpinOrFreezeNode(<Node>d.device, freeze);
693 numbernodes++;
694 });
695 this.hosts
696 .filter((h) => this.selectedNodes.some((s) => s.id === h.host.id))
697 .forEach((h) => {
698 Node.unpinOrFreezeNode(<Node>h.host, freeze);
699 numbernodes++;
700 });
701 } else {
702 this.devices.forEach((d) => {
703 Node.unpinOrFreezeNode(<Node>d.device, freeze);
704 numbernodes++;
705 });
706 this.hosts.forEach((h) => {
707 Node.unpinOrFreezeNode(<Node>h.host, freeze);
708 numbernodes++;
709 });
710 }
711 this.graph.reinitSimulation();
712 return numbernodes;
713 }
Sean Condonf4f54a12018-10-10 23:25:46 +0100714}
Sean Condon0c577f62018-11-18 22:40:05 +0000715