blob: dc67bc242d75641a4b65516be659494c159630a2 [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 Condon3dd062f2020-04-14 09:25:00 +010031import {LocMeta, LogService, MetaUi, WebSocketService, ZoomUtils} from 'org_onosproject_onos/web/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 Condon00e56d02020-02-28 09:50:04 +0000103 linksHighlighted: Map<string, LinkHighlight>;
Sean Condonf4f54a12018-10-10 23:25:46 +0100104
Sean Condon021f0fa2018-12-06 23:31:11 -0800105 // References to the children of this component - these are created in the
106 // template view with the *ngFor and we get them by a query here
Sean Condon0c577f62018-11-18 22:40:05 +0000107 @ViewChildren(DeviceNodeSvgComponent) devices: QueryList<DeviceNodeSvgComponent>;
Sean Condon021f0fa2018-12-06 23:31:11 -0800108 @ViewChildren(HostNodeSvgComponent) hosts: QueryList<HostNodeSvgComponent>;
Sean Condon50855cf2018-12-23 15:37:42 +0000109 @ViewChildren(LinkSvgComponent) links: QueryList<LinkSvgComponent>;
Sean Condonf4f54a12018-10-10 23:25:46 +0100110
Sean Condon0c577f62018-11-18 22:40:05 +0000111 constructor(
112 protected log: LogService,
Sean Condon71910542019-02-16 18:16:42 +0000113 private ref: ChangeDetectorRef,
114 protected wss: WebSocketService
Sean Condon0c577f62018-11-18 22:40:05 +0000115 ) {
116 this.selectedLink = null;
117 this.log.debug('ForceSvgComponent constructed');
Sean Condon00e56d02020-02-28 09:50:04 +0000118 this.linksHighlighted = new Map<string, LinkHighlight>();
Sean Condon0c577f62018-11-18 22:40:05 +0000119 }
120
121 /**
Sean Condon021f0fa2018-12-06 23:31:11 -0800122 * Utility for extracting a node name from an endpoint string
123 * In some cases - have to remove the port number from the end of a device
124 * name
125 * @param endPtStr The end point name
126 */
Sean Condonb77768e2019-05-04 20:23:42 +0100127 static extractNodeName(endPtStr: string, portStr: string): string {
128 if (portStr === undefined || endPtStr === undefined) {
Sean Condon021f0fa2018-12-06 23:31:11 -0800129 return endPtStr;
Sean Condonb77768e2019-05-04 20:23:42 +0100130 } else if (endPtStr.includes('/')) {
131 return endPtStr.substr(0, endPtStr.length - portStr.length - 1);
Sean Condon021f0fa2018-12-06 23:31:11 -0800132 }
Sean Condonb77768e2019-05-04 20:23:42 +0100133 return endPtStr;
Sean Condon021f0fa2018-12-06 23:31:11 -0800134 }
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 Condonb77768e2019-05-04 20:23:42 +0100236 const link = this.regionData.links[linkIdx];
237 const epA = ForceSvgComponent.extractNodeName(link.epA, link.portA);
238 if (!this.graph.nodes.find((node) => node.id === epA)) {
239 this.log.error('ngOnChange Could not find endpoint A', epA, 'for', link);
240 continue;
241 }
242 const epB = ForceSvgComponent.extractNodeName(
243 link.epB, link.portB);
244 if (!this.graph.nodes.find((node) => node.id === epB)) {
245 this.log.error('ngOnChange Could not find endpoint B', epB, 'for', link);
246 continue;
247 }
Sean Condon0c577f62018-11-18 22:40:05 +0000248 this.regionData.links[linkIdx].source =
249 this.graph.nodes.find((node) =>
Sean Condon021f0fa2018-12-06 23:31:11 -0800250 node.id === epA);
Sean Condon0c577f62018-11-18 22:40:05 +0000251 this.regionData.links[linkIdx].target =
252 this.graph.nodes.find((node) =>
Sean Condon021f0fa2018-12-06 23:31:11 -0800253 node.id === epB);
Sean Condon0c577f62018-11-18 22:40:05 +0000254 this.regionData.links[linkIdx].index = Number(linkIdx);
255 }
256
257 this.graph.links = this.regionData.links;
Sean Condon28884332019-03-21 14:07:00 +0000258 if (this.graph.nodes.length > 0) {
259 this.graph.reinitSimulation();
260 }
Sean Condon0c577f62018-11-18 22:40:05 +0000261 this.log.debug('ForceSvgComponent input changed',
262 this.graph.nodes.length, 'nodes,', this.graph.links.length, 'links');
Sean Condon590b34b2019-12-04 18:44:37 +0000263 if (!this.viewInitialized) {
264 this.viewInitialized = true;
265 if (this.showAlarms) {
266 this.wss.sendEvent('alarmTopovDisplayStart', {});
267 }
268 }
Sean Condon0c577f62018-11-18 22:40:05 +0000269 }
Sean Condon590b34b2019-12-04 18:44:37 +0000270
271 if (changes['showAlarms'] && this.viewInitialized) {
272 if (this.showAlarms) {
273 this.wss.sendEvent('alarmTopovDisplayStart', {});
274 } else {
275 this.wss.sendEvent('alarmTopovDisplayStop', {});
276 this.cancelAllDeviceHighlightsNow();
277 }
278 }
279 }
280
281 ngOnDestroy(): void {
282 if (this.showAlarms) {
283 this.wss.sendEvent('alarmTopovDisplayStop', {});
284 this.cancelAllDeviceHighlightsNow();
285 }
286 this.viewInitialized = false;
Sean Condon28884332019-03-21 14:07:00 +0000287 }
Sean Condon021f0fa2018-12-06 23:31:11 -0800288
Sean Condon28884332019-03-21 14:07:00 +0000289 /**
Sean Condon058804c2019-04-16 09:41:52 +0100290 * If instance has a value then mute colors of devices not connected to it
291 * Otherwise if instance does not have a value unmute all
292 * @param instanceName name of the selected instance
293 */
294 changeInstSelection(instanceName: string) {
295 this.log.debug('Mastership changed', instanceName);
296 this.devices.filter((d) => d.device.master !== instanceName)
297 .forEach((d) => {
298 const isMuted = Boolean(instanceName);
299 d.ngOnChanges({'colorMuted': new SimpleChange(!isMuted, isMuted, true)});
300 }
301 );
302 }
303
304 /**
Sean Condon28884332019-03-21 14:07:00 +0000305 * If a node has a fixed location then assign it to fx and fy so
306 * that it doesn't get affected by forces
307 * @param graphNode The node whose location should be processed
308 */
309 private fixPosition(graphNode: Node): void {
310 const loc: Location = <Location>graphNode['location'];
311 const props: DeviceProps = <DeviceProps>graphNode['props'];
312 const metaUi = <MetaUi>graphNode['metaUi'];
313 if (loc && loc.locType === LocationType.GEO) {
314 const position: MetaUi =
315 ZoomUtils.convertGeoToCanvas(
316 <LocMeta>{lng: loc.longOrX, lat: loc.latOrY});
317 graphNode.fx = position.x;
318 graphNode.fy = position.y;
319 this.log.debug('Found node', graphNode.id, 'with', loc.locType);
320 } else if (loc && loc.locType === LocationType.GRID) {
321 graphNode.fx = loc.longOrX;
322 graphNode.fy = loc.latOrY;
323 this.log.debug('Found node', graphNode.id, 'with', loc.locType);
324 } else if (props && props.locType === LocationType.NONE && metaUi) {
325 graphNode.fx = metaUi.x;
326 graphNode.fy = metaUi.y;
327 this.log.debug('Found node', graphNode.id, 'with locType=none and metaUi');
328 } else {
329 graphNode.fx = null;
330 graphNode.fy = null;
331 }
Sean Condon0c577f62018-11-18 22:40:05 +0000332 }
333
334 /**
335 * Get the index of LayerType so it can drive the visibility of nodes and
336 * hosts on layers
337 */
338 visibleLayerIdx(): number {
339 const layerKeys: string[] = Object.keys(LayerType);
340 for (const idx in layerKeys) {
341 if (LayerType[layerKeys[idx]] === this.visibleLayer) {
342 return Number(idx);
343 }
344 }
345 return -1;
346 }
347
348 selectLink(link: RegionLink): void {
349 this.selectedLink = link;
350 this.linkSelected.emit(link);
351 }
352
Sean Condon021f0fa2018-12-06 23:31:11 -0800353 /**
Sean Condond88f3662019-04-03 16:35:30 +0100354 * Iterate through all hosts and devices and links to deselect the previously selected
Sean Condon021f0fa2018-12-06 23:31:11 -0800355 * node. The emit an event to the parent that lets it know the selection has
356 * changed.
Sean Condond88f3662019-04-03 16:35:30 +0100357 *
358 * This function collates all of the nodes that have been selected and passes
359 * a collection of nodes up to the topology component
360 *
Sean Condon021f0fa2018-12-06 23:31:11 -0800361 * @param selectedNode the newly selected node
362 */
Sean Condond88f3662019-04-03 16:35:30 +0100363 updateSelected(selectedNode: SelectedEvent): void {
364 this.log.debug('Node or link ',
Sean Condon64ea7d22019-04-12 19:39:13 +0100365 selectedNode.uiElement ? selectedNode.uiElement.id : '--',
Sean Condond88f3662019-04-03 16:35:30 +0100366 selectedNode.deselecting ? 'deselected' : 'selected',
367 selectedNode.isShift ? 'Multiple' : '');
Sean Condon021f0fa2018-12-06 23:31:11 -0800368
Sean Condond88f3662019-04-03 16:35:30 +0100369 if (selectedNode.isShift && selectedNode.deselecting) {
370 const idx = this.selectedNodes.findIndex((n) =>
371 n.id === selectedNode.uiElement.id
372 );
373 this.selectedNodes.splice(idx, 1);
374 this.log.debug('Removed node', idx);
375
376 } else if (selectedNode.isShift) {
377 this.selectedNodes.push(selectedNode.uiElement);
378
379 } else if (selectedNode.deselecting) {
Sean Condon64ea7d22019-04-12 19:39:13 +0100380 this.devices
381 .forEach((d) => d.deselect());
382 this.hosts
383 .forEach((h) => h.deselect());
384 this.links
385 .forEach((l) => l.deselect());
Sean Condond88f3662019-04-03 16:35:30 +0100386 this.selectedNodes = [];
387
388 } else {
389 const selNodeId = selectedNode.uiElement.id;
390 // Otherwise if shift was not pressed deselect previous
391 this.devices
392 .filter((d) => d.device.id !== selNodeId)
393 .forEach((d) => d.deselect());
394 this.hosts
395 .filter((h) => h.host.id !== selNodeId)
396 .forEach((h) => h.deselect());
397
398 this.links
399 .filter((l) => l.link.id !== selNodeId)
400 .forEach((l) => l.deselect());
401
402 this.selectedNodes = [selectedNode.uiElement];
403 }
Sean Condon91481822019-01-01 13:56:14 +0000404 // Push the changes back up to parent (Topology Component)
Sean Condond88f3662019-04-03 16:35:30 +0100405 this.selectedNodeEvent.emit(this.selectedNodes);
Sean Condon0c577f62018-11-18 22:40:05 +0000406 }
407
Sean Condon021f0fa2018-12-06 23:31:11 -0800408 /**
409 * We want to filter links to show only those not related to hosts if the
Sean Condon50855cf2018-12-23 15:37:42 +0000410 * 'showHosts' flag has been switched off. If 'showHosts' is true, then
Sean Condon021f0fa2018-12-06 23:31:11 -0800411 * display all links.
412 */
Sean Condon50855cf2018-12-23 15:37:42 +0000413 filteredLinks(): Link[] {
Sean Condon021f0fa2018-12-06 23:31:11 -0800414 return this.regionData.links.filter((h) =>
415 this.showHosts ||
416 ((<Host>h.source).nodeType !== 'host' &&
417 (<Host>h.target).nodeType !== 'host'));
Sean Condon0c577f62018-11-18 22:40:05 +0000418 }
Sean Condon50855cf2018-12-23 15:37:42 +0000419
420 /**
421 * When changes happen in the model, then model events are sent up through the
422 * Web Socket
423 * @param type - the type of the change
424 * @param memo - a qualifier on the type
425 * @param subject - the item that the update is for
426 * @param data - the new definition of the item
427 */
428 handleModelEvent(type: ModelEventType, memo: ModelEventMemo, subject: string, data: UiElement): void {
429 switch (type) {
430 case ModelEventType.DEVICE_ADDED_OR_UPDATED:
431 if (memo === ModelEventMemo.ADDED) {
Sean Condon28884332019-03-21 14:07:00 +0000432 this.fixPosition(<Device>data);
Sean Condonee545762019-03-09 10:43:58 +0000433 this.graph.nodes.push(<Device>data);
Sean Condonee5d4b92019-03-11 19:57:34 +0000434 this.regionData.devices[this.visibleLayerIdx()].push(<Device>data);
Sean Condonee545762019-03-09 10:43:58 +0000435 this.log.debug('Device added', (<Device>data).id);
Sean Condon50855cf2018-12-23 15:37:42 +0000436 } else if (memo === ModelEventMemo.UPDATED) {
437 const oldDevice: Device =
438 this.regionData.devices[this.visibleLayerIdx()]
439 .find((d) => d.id === subject);
Sean Condonee545762019-03-09 10:43:58 +0000440 const changes = ForceSvgComponent.updateObject(oldDevice, <Device>data);
Sean Condon28884332019-03-21 14:07:00 +0000441 if (changes.numChanges > 0) {
Sean Condonee545762019-03-09 10:43:58 +0000442 this.log.debug('Device ', oldDevice.id, memo, ' - ', changes, 'changes');
Sean Condon28884332019-03-21 14:07:00 +0000443 if (changes.locationChanged) {
444 this.fixPosition(oldDevice);
445 }
Sean Condon058804c2019-04-16 09:41:52 +0100446 const svgDevice: DeviceNodeSvgComponent =
447 this.devices.find((svgdevice) => svgdevice.device.id === subject);
448 svgDevice.ngOnChanges({'device':
449 new SimpleChange(<Device>{}, oldDevice, true)
450 });
Sean Condonee545762019-03-09 10:43:58 +0000451 }
Sean Condon50855cf2018-12-23 15:37:42 +0000452 } else {
453 this.log.warn('Device ', memo, ' - not yet implemented', data);
454 }
Sean Condon50855cf2018-12-23 15:37:42 +0000455 break;
456 case ModelEventType.HOST_ADDED_OR_UPDATED:
457 if (memo === ModelEventMemo.ADDED) {
Sean Condon28884332019-03-21 14:07:00 +0000458 this.fixPosition(<Host>data);
Sean Condonee545762019-03-09 10:43:58 +0000459 this.graph.nodes.push(<Host>data);
Sean Condon28884332019-03-21 14:07:00 +0000460 this.regionData.hosts[this.visibleLayerIdx()].push(<Host>data);
Sean Condonee545762019-03-09 10:43:58 +0000461 this.log.debug('Host added', (<Host>data).id);
Sean Condon50855cf2018-12-23 15:37:42 +0000462 } else if (memo === ModelEventMemo.UPDATED) {
463 const oldHost: Host = this.regionData.hosts[this.visibleLayerIdx()]
464 .find((h) => h.id === subject);
Sean Condonee545762019-03-09 10:43:58 +0000465 const changes = ForceSvgComponent.updateObject(oldHost, <Host>data);
Sean Condon28884332019-03-21 14:07:00 +0000466 if (changes.numChanges > 0) {
Sean Condonee545762019-03-09 10:43:58 +0000467 this.log.debug('Host ', oldHost.id, memo, ' - ', changes, 'changes');
Sean Condon28884332019-03-21 14:07:00 +0000468 if (changes.locationChanged) {
469 this.fixPosition(oldHost);
470 }
Sean Condonee545762019-03-09 10:43:58 +0000471 }
Sean Condon50855cf2018-12-23 15:37:42 +0000472 } else {
473 this.log.warn('Host change', memo, ' - unexpected');
474 }
475 break;
476 case ModelEventType.DEVICE_REMOVED:
477 if (memo === ModelEventMemo.REMOVED || memo === undefined) {
478 const removeIdx: number =
479 this.regionData.devices[this.visibleLayerIdx()]
480 .findIndex((d) => d.id === subject);
Sean Condonee545762019-03-09 10:43:58 +0000481 this.regionData.devices[this.visibleLayerIdx()].splice(removeIdx, 1);
482 this.removeRelatedLinks(subject);
483 this.log.debug('Device ', subject, 'removed. Links', this.regionData.links);
Sean Condon50855cf2018-12-23 15:37:42 +0000484 } else {
485 this.log.warn('Device removed - unexpected memo', memo);
486 }
487 break;
488 case ModelEventType.HOST_REMOVED:
489 if (memo === ModelEventMemo.REMOVED || memo === undefined) {
490 const removeIdx: number =
491 this.regionData.hosts[this.visibleLayerIdx()]
492 .findIndex((h) => h.id === subject);
Sean Condonee545762019-03-09 10:43:58 +0000493 this.regionData.hosts[this.visibleLayerIdx()].splice(removeIdx, 1);
494 this.removeRelatedLinks(subject);
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100495 this.log.debug('Host ', subject, 'removed');
Sean Condon50855cf2018-12-23 15:37:42 +0000496 } else {
497 this.log.warn('Host removed - unexpected memo', memo);
498 }
499 break;
500 case ModelEventType.LINK_ADDED_OR_UPDATED:
Sean Condonee545762019-03-09 10:43:58 +0000501 if (memo === ModelEventMemo.ADDED &&
502 this.regionData.links.findIndex((l) => l.id === subject) === -1) {
Sean Condonb77768e2019-05-04 20:23:42 +0100503 const newLink = <RegionLink>data;
504
505
Sean Condonee545762019-03-09 10:43:58 +0000506 const epA = ForceSvgComponent.extractNodeName(
Sean Condonb77768e2019-05-04 20:23:42 +0100507 newLink.epA, newLink.portA);
508 if (!this.graph.nodes.find((node) => node.id === epA)) {
509 this.log.error('Could not find endpoint A', epA, 'of', newLink);
510 break;
511 }
Sean Condonee545762019-03-09 10:43:58 +0000512 const epB = ForceSvgComponent.extractNodeName(
Sean Condonb77768e2019-05-04 20:23:42 +0100513 newLink.epB, newLink.portB);
514 if (!this.graph.nodes.find((node) => node.id === epB)) {
515 this.log.error('Could not find endpoint B', epB, 'of link', newLink);
516 break;
517 }
518
519 const listLen = this.regionData.links.push(<RegionLink>data);
520 this.regionData.links[listLen - 1].source =
521 this.graph.nodes.find((node) => node.id === epA);
Sean Condonee545762019-03-09 10:43:58 +0000522 this.regionData.links[listLen - 1].target =
Sean Condonb77768e2019-05-04 20:23:42 +0100523 this.graph.nodes.find((node) => node.id === epB);
Sean Condonee545762019-03-09 10:43:58 +0000524 this.log.debug('Link added', subject);
525 } else if (memo === ModelEventMemo.UPDATED) {
526 const oldLink = this.regionData.links.find((l) => l.id === subject);
527 const changes = ForceSvgComponent.updateObject(oldLink, <RegionLink>data);
528 this.log.debug('Link ', subject, '. Updated', changes, 'items');
529 } else {
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100530 this.log.warn('Link event ignored', subject, data);
531 }
532 break;
533 case ModelEventType.LINK_REMOVED:
534 if (memo === ModelEventMemo.REMOVED) {
535 const removeIdx = this.regionData.links.findIndex((l) => l.id === subject);
536 this.regionData.links.splice(removeIdx, 1);
537 this.log.debug('Link ', subject, 'removed');
Sean Condonee545762019-03-09 10:43:58 +0000538 }
Sean Condon50855cf2018-12-23 15:37:42 +0000539 break;
540 default:
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100541 this.log.error('Unexpected model event', type, 'for', subject, 'Data', data);
Sean Condon50855cf2018-12-23 15:37:42 +0000542 }
Sean Condon28884332019-03-21 14:07:00 +0000543 this.graph.links = this.regionData.links;
544 this.graph.reinitSimulation();
Sean Condon50855cf2018-12-23 15:37:42 +0000545 }
546
Sean Condonee545762019-03-09 10:43:58 +0000547 private removeRelatedLinks(subject: string) {
548 const len = this.regionData.links.length;
549 for (let i = 0; i < len; i++) {
550 const linkIdx = this.regionData.links.findIndex((l) =>
Sean Condonb77768e2019-05-04 20:23:42 +0100551 (ForceSvgComponent.extractNodeName(l.epA, l.portA) === subject ||
552 ForceSvgComponent.extractNodeName(l.epB, l.portB) === subject));
Sean Condonee545762019-03-09 10:43:58 +0000553 if (linkIdx >= 0) {
554 this.regionData.links.splice(linkIdx, 1);
555 this.log.debug('Link ', linkIdx, 'removed on attempt', i);
556 }
Sean Condon50855cf2018-12-23 15:37:42 +0000557 }
558 }
559
560 /**
561 * When traffic monitoring is turned on (A key) highlights will be sent back
562 * from the WebSocket through the Traffic Service
Sean Condon4e55c802019-12-03 22:13:34 +0000563 * Also handles Intent highlights in case one is selected
Sean Condon50855cf2018-12-23 15:37:42 +0000564 * @param devices - an array of device highlights
565 * @param hosts - an array of host highlights
566 * @param links - an array of link highlights
567 */
Sean Condon590b34b2019-12-04 18:44:37 +0000568 handleHighlights(devices: DeviceHighlight[], hosts: HostHighlight[], links: LinkHighlight[], fadeMs: number = 0): void {
Sean Condon50855cf2018-12-23 15:37:42 +0000569
570 if (devices.length > 0) {
571 this.log.debug(devices.length, 'Devices highlighted');
Sean Condon590b34b2019-12-04 18:44:37 +0000572 devices.forEach((dh: DeviceHighlight) => {
573 this.devices.forEach((d: DeviceNodeSvgComponent) => {
574 if (d.device.id === dh.id) {
575 d.badge = dh.badge;
576 this.ref.markForCheck(); // Forces ngOnChange in the DeviceSvgComponent
577 this.log.debug('Highlighting device', dh.id);
578 }
579 });
Sean Condon50855cf2018-12-23 15:37:42 +0000580 });
581 }
582 if (hosts.length > 0) {
583 this.log.debug(hosts.length, 'Hosts highlighted');
Sean Condon590b34b2019-12-04 18:44:37 +0000584 hosts.forEach((hh: HostHighlight) => {
585 this.hosts.forEach((h) => {
586 if (h.host.id === hh.id) {
587 h.badge = hh.badge;
588 this.ref.markForCheck(); // Forces ngOnChange in the HostSvgComponent
589 this.log.debug('Highlighting host', hh.id);
590 }
591 });
Sean Condon50855cf2018-12-23 15:37:42 +0000592 });
593 }
594 if (links.length > 0) {
595 this.log.debug(links.length, 'Links highlighted');
Sean Condon4e55c802019-12-03 22:13:34 +0000596 links.forEach((lh: LinkHighlight) => {
597 if (fadeMs > 0) {
598 lh.fadems = fadeMs;
Sean Condon50855cf2018-12-23 15:37:42 +0000599 }
Sean Condon00e56d02020-02-28 09:50:04 +0000600 this.linksHighlighted.set(Link.linkIdFromShowHighlights(lh.id), lh);
Sean Condon50855cf2018-12-23 15:37:42 +0000601 });
Sean Condon00e56d02020-02-28 09:50:04 +0000602 this.ref.detectChanges(); // Forces ngOnChange in the LinkSvgComponent
Sean Condon50855cf2018-12-23 15:37:42 +0000603 }
604 }
Sean Condon71910542019-02-16 18:16:42 +0000605
Sean Condon590b34b2019-12-04 18:44:37 +0000606 cancelAllHostHighlightsNow() {
607 this.hosts.forEach((host: HostNodeSvgComponent) => {
608 host.badge = undefined;
609 this.ref.markForCheck(); // Forces ngOnChange in the HostSvgComponent
610 });
611 }
612
613 cancelAllDeviceHighlightsNow() {
614 this.devices.forEach((device: DeviceNodeSvgComponent) => {
615 device.badge = undefined;
616 this.ref.markForCheck(); // Forces ngOnChange in the DeviceSvgComponent
617 });
618 }
619
Sean Condon4e55c802019-12-03 22:13:34 +0000620 cancelAllLinkHighlightsNow() {
621 this.links.forEach((link: LinkSvgComponent) => {
622 link.linkHighlight = <LinkHighlight>{};
Sean Condon590b34b2019-12-04 18:44:37 +0000623 this.ref.markForCheck(); // Forces ngOnChange in the LinkSvgComponent
Sean Condon4e55c802019-12-03 22:13:34 +0000624 });
625 }
626
Sean Condon71910542019-02-16 18:16:42 +0000627 /**
628 * As nodes are dragged around the graph, their new location should be sent
629 * back to server
630 * @param klass The class of node e.g. 'host' or 'device'
631 * @param id - the ID of the node
632 * @param newLocation - the new Location of the node
633 */
634 nodeMoved(klass: string, id: string, newLocation: MetaUi) {
Sean Condon28884332019-03-21 14:07:00 +0000635 this.wss.sendEvent('updateMeta2', <UpdateMeta>{
Sean Condon71910542019-02-16 18:16:42 +0000636 id: id,
637 class: klass,
638 memento: newLocation
639 });
640 this.log.debug(klass, id, 'has been moved to', newLocation);
641 }
Sean Condon1ae15802019-03-02 09:07:18 +0000642
Sean Condon9de21352019-04-06 19:22:27 +0100643 /**
644 * If any nodes with fixed positions had been dragged out of place
645 * then put back where they belong
646 * If there are some devices selected reset only these
647 */
648 resetNodeLocations(): number {
649 let numbernodes = 0;
650 if (this.selectedNodes.length > 0) {
651 this.devices
652 .filter((d) => this.selectedNodes.some((s) => s.id === d.device.id))
653 .forEach((dev) => {
654 Node.resetNodeLocation(<Node>dev.device);
655 numbernodes++;
656 });
657 this.hosts
658 .filter((h) => this.selectedNodes.some((s) => s.id === h.host.id))
659 .forEach((h) => {
660 Host.resetNodeLocation(<Host>h.host);
661 numbernodes++;
662 });
663 } else {
664 this.devices.forEach((dev) => {
665 Node.resetNodeLocation(<Node>dev.device);
666 numbernodes++;
667 });
668 this.hosts.forEach((h) => {
669 Host.resetNodeLocation(<Host>h.host);
670 numbernodes++;
671 });
672 }
673 this.graph.reinitSimulation();
674 return numbernodes;
Sean Condon1ae15802019-03-02 09:07:18 +0000675 }
Sean Condon9de21352019-04-06 19:22:27 +0100676
677 /**
678 * Toggle floating nodes between unpinned and frozen
679 * There may be frozen and unpinned in the selection
680 *
681 * If there are nodes selected toggle only these
682 */
683 unpinOrFreezeNodes(freeze: boolean): number {
684 let numbernodes = 0;
685 if (this.selectedNodes.length > 0) {
686 this.devices
687 .filter((d) => this.selectedNodes.some((s) => s.id === d.device.id))
688 .forEach((d) => {
689 Node.unpinOrFreezeNode(<Node>d.device, freeze);
690 numbernodes++;
691 });
692 this.hosts
693 .filter((h) => this.selectedNodes.some((s) => s.id === h.host.id))
694 .forEach((h) => {
695 Node.unpinOrFreezeNode(<Node>h.host, freeze);
696 numbernodes++;
697 });
698 } else {
699 this.devices.forEach((d) => {
700 Node.unpinOrFreezeNode(<Node>d.device, freeze);
701 numbernodes++;
702 });
703 this.hosts.forEach((h) => {
704 Node.unpinOrFreezeNode(<Node>h.host, freeze);
705 numbernodes++;
706 });
707 }
708 this.graph.reinitSimulation();
709 return numbernodes;
710 }
Sean Condonf4f54a12018-10-10 23:25:46 +0100711}
Sean Condon0c577f62018-11-18 22:40:05 +0000712