Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 2 | * Copyright 2019-present Open Networking Foundation |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the 'License'); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an 'AS IS' BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 16 | import { |
| 17 | ChangeDetectionStrategy, |
| 18 | ChangeDetectorRef, |
| 19 | Component, |
| 20 | EventEmitter, |
| 21 | HostListener, |
| 22 | Input, |
| 23 | OnChanges, |
| 24 | OnInit, |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 25 | Output, |
| 26 | QueryList, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 27 | SimpleChange, |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 28 | SimpleChanges, |
| 29 | ViewChildren |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 30 | } from '@angular/core'; |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 31 | import { |
| 32 | LocMeta, |
| 33 | LogService, |
| 34 | MetaUi, |
| 35 | WebSocketService, |
| 36 | ZoomUtils |
| 37 | } from 'gui2-fw-lib'; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 38 | import { |
| 39 | Device, |
| 40 | ForceDirectedGraph, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 41 | Host, |
| 42 | HostLabelToggle, |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 43 | LabelToggle, |
| 44 | LayerType, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 45 | Link, |
| 46 | LinkHighlight, |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 47 | Location, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 48 | ModelEventMemo, |
| 49 | ModelEventType, |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 50 | Region, |
| 51 | RegionLink, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 52 | SubRegion, |
| 53 | UiElement |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 54 | } from './models'; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 55 | import { |
| 56 | DeviceNodeSvgComponent, |
| 57 | HostNodeSvgComponent, |
| 58 | LinkSvgComponent |
| 59 | } from './visuals'; |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 60 | import {LocationType} from '../backgroundsvg/backgroundsvg.component'; |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 61 | |
| 62 | interface UpdateMeta { |
| 63 | id: string; |
| 64 | class: string; |
| 65 | memento: MetaUi; |
| 66 | } |
Sean Condon | aa4366d | 2018-11-02 14:29:01 +0000 | [diff] [blame] | 67 | |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 68 | /** |
| 69 | * ONOS GUI -- Topology Forces Graph Layer View. |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 70 | * |
| 71 | * The regionData is set by Topology Service on WebSocket topo2CurrentRegion callback |
| 72 | * This drives the whole Force graph |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 73 | */ |
| 74 | @Component({ |
| 75 | selector: '[onos-forcesvg]', |
| 76 | templateUrl: './forcesvg.component.html', |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 77 | styleUrls: ['./forcesvg.component.css'], |
| 78 | changeDetection: ChangeDetectionStrategy.OnPush, |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 79 | }) |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 80 | export class ForceSvgComponent implements OnInit, OnChanges { |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 81 | @Input() deviceLabelToggle: LabelToggle = LabelToggle.NONE; |
| 82 | @Input() hostLabelToggle: HostLabelToggle = HostLabelToggle.NONE; |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 83 | @Input() showHosts: boolean = false; |
| 84 | @Input() highlightPorts: boolean = true; |
| 85 | @Input() onosInstMastership: string = ''; |
| 86 | @Input() visibleLayer: LayerType = LayerType.LAYER_DEFAULT; |
| 87 | @Input() selectedLink: RegionLink = null; |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 88 | @Input() scale: number = 1; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 89 | @Input() regionData: Region = <Region>{devices: [ [], [], [] ], hosts: [ [], [], [] ], links: []}; |
Sean Condon | b2c483c | 2019-01-16 20:28:55 +0000 | [diff] [blame] | 90 | @Output() linkSelected = new EventEmitter<RegionLink>(); |
| 91 | @Output() selectedNodeEvent = new EventEmitter<UiElement>(); |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 92 | private graph: ForceDirectedGraph; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 93 | private _options: { width, height } = { width: 800, height: 600 }; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 94 | |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 95 | // References to the children of this component - these are created in the |
| 96 | // template view with the *ngFor and we get them by a query here |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 97 | @ViewChildren(DeviceNodeSvgComponent) devices: QueryList<DeviceNodeSvgComponent>; |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 98 | @ViewChildren(HostNodeSvgComponent) hosts: QueryList<HostNodeSvgComponent>; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 99 | @ViewChildren(LinkSvgComponent) links: QueryList<LinkSvgComponent>; |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 100 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 101 | constructor( |
| 102 | protected log: LogService, |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 103 | private ref: ChangeDetectorRef, |
| 104 | protected wss: WebSocketService |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 105 | ) { |
| 106 | this.selectedLink = null; |
| 107 | this.log.debug('ForceSvgComponent constructed'); |
| 108 | } |
| 109 | |
| 110 | /** |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 111 | * Utility for extracting a node name from an endpoint string |
| 112 | * In some cases - have to remove the port number from the end of a device |
| 113 | * name |
| 114 | * @param endPtStr The end point name |
| 115 | */ |
| 116 | private static extractNodeName(endPtStr: string): string { |
| 117 | const slash: number = endPtStr.indexOf('/'); |
| 118 | if (slash === -1) { |
| 119 | return endPtStr; |
| 120 | } else { |
| 121 | const afterSlash = endPtStr.substr(slash + 1); |
| 122 | if (afterSlash === 'None') { |
| 123 | return endPtStr; |
| 124 | } else { |
| 125 | return endPtStr.substr(0, slash); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 130 | /** |
| 131 | * Recursive method to compare 2 objects attribute by attribute and update |
| 132 | * the first where a change is detected |
| 133 | * @param existingNode 1st object |
| 134 | * @param updatedNode 2nd object |
| 135 | */ |
| 136 | private static updateObject(existingNode: Object, updatedNode: Object): number { |
| 137 | let changed: number = 0; |
| 138 | for (const key of Object.keys(updatedNode)) { |
| 139 | const o = updatedNode[key]; |
| 140 | if (key === 'id') { |
| 141 | continue; |
| 142 | } else if (o && typeof o === 'object' && o.constructor === Object) { |
| 143 | changed += ForceSvgComponent.updateObject(existingNode[key], updatedNode[key]); |
| 144 | } else if (existingNode[key] !== updatedNode[key]) { |
| 145 | changed++; |
| 146 | existingNode[key] = updatedNode[key]; |
| 147 | } |
| 148 | } |
| 149 | return changed; |
| 150 | } |
| 151 | |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 152 | @HostListener('window:resize', ['$event']) |
| 153 | onResize(event) { |
| 154 | this.graph.initSimulation(this.options); |
| 155 | this.log.debug('Simulation reinit after resize', event); |
| 156 | } |
| 157 | |
| 158 | /** |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 159 | * After the component is initialized create the Force simulation |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 160 | * The list of devices, hosts and links will not have been receieved back |
| 161 | * from the WebSocket yet as this time - they will be updated later through |
| 162 | * ngOnChanges() |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 163 | */ |
| 164 | ngOnInit() { |
| 165 | // Receiving an initialized simulated graph from our custom d3 service |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 166 | this.graph = new ForceDirectedGraph(this.options, this.log); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 167 | |
| 168 | /** Binding change detection check on each tick |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 169 | * This along with an onPush change detection strategy should enforce |
| 170 | * checking only when relevant! This improves scripting computation |
| 171 | * duration in a couple of tests I've made, consistently. Also, it makes |
| 172 | * sense to avoid unnecessary checks when we are dealing only with |
| 173 | * simulations data binding. |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 174 | */ |
| 175 | this.graph.ticker.subscribe((simulation) => { |
| 176 | // this.log.debug("Force simulation has ticked", simulation); |
| 177 | this.ref.markForCheck(); |
| 178 | }); |
| 179 | this.log.debug('ForceSvgComponent initialized - waiting for nodes and links'); |
| 180 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /** |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 184 | * When any one of the inputs get changed by a containing component, this |
| 185 | * gets called automatically. In addition this is called manually by |
| 186 | * topology.service when a response is received from the WebSocket from the |
| 187 | * server |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 188 | * |
| 189 | * The Devices, Hosts and SubRegions are all added to the Node list for the simulation |
| 190 | * The Links are added to the Link list of the simulation. |
| 191 | * Before they are added the Links are associated with Nodes based on their endPt |
| 192 | * |
| 193 | * @param changes - a list of changed @Input(s) |
| 194 | */ |
| 195 | ngOnChanges(changes: SimpleChanges) { |
| 196 | if (changes['regionData']) { |
| 197 | const devices: Device[] = |
| 198 | changes['regionData'].currentValue.devices[this.visibleLayerIdx()]; |
| 199 | const hosts: Host[] = |
| 200 | changes['regionData'].currentValue.hosts[this.visibleLayerIdx()]; |
| 201 | const subRegions: SubRegion[] = changes['regionData'].currentValue.subRegion; |
| 202 | this.graph.nodes = []; |
| 203 | if (devices) { |
| 204 | this.graph.nodes = devices; |
| 205 | } |
| 206 | if (hosts) { |
| 207 | this.graph.nodes = this.graph.nodes.concat(hosts); |
| 208 | } |
| 209 | if (subRegions) { |
| 210 | this.graph.nodes = this.graph.nodes.concat(subRegions); |
| 211 | } |
| 212 | |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 213 | // If a node has a fixed location then assign it to fx and fy so |
| 214 | // that it doesn't get affected by forces |
| 215 | this.graph.nodes |
| 216 | .forEach((n) => { |
| 217 | const loc: Location = <Location>n['location']; |
| 218 | if (loc && loc.locType === LocationType.GEO) { |
| 219 | const position: MetaUi = |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 220 | ZoomUtils.convertGeoToCanvas( |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 221 | <LocMeta>{lng: loc.longOrX, lat: loc.latOrY}); |
| 222 | n.fx = position.x; |
| 223 | n.fy = position.y; |
| 224 | this.log.debug('Found node', n.id, 'with', loc.locType); |
| 225 | } |
| 226 | }); |
| 227 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 228 | // Associate the endpoints of each link with a real node |
| 229 | this.graph.links = []; |
| 230 | for (const linkIdx of Object.keys(this.regionData.links)) { |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 231 | const epA = ForceSvgComponent.extractNodeName( |
| 232 | this.regionData.links[linkIdx].epA); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 233 | this.regionData.links[linkIdx].source = |
| 234 | this.graph.nodes.find((node) => |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 235 | node.id === epA); |
| 236 | const epB = ForceSvgComponent.extractNodeName( |
| 237 | this.regionData.links[linkIdx].epB); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 238 | this.regionData.links[linkIdx].target = |
| 239 | this.graph.nodes.find((node) => |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 240 | node.id === epB); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 241 | this.regionData.links[linkIdx].index = Number(linkIdx); |
| 242 | } |
| 243 | |
| 244 | this.graph.links = this.regionData.links; |
| 245 | |
| 246 | this.graph.initSimulation(this.options); |
| 247 | this.graph.initNodes(); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 248 | this.graph.initLinks(); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 249 | this.log.debug('ForceSvgComponent input changed', |
| 250 | this.graph.nodes.length, 'nodes,', this.graph.links.length, 'links'); |
| 251 | } |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 252 | |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 253 | this.ref.markForCheck(); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Get the index of LayerType so it can drive the visibility of nodes and |
| 258 | * hosts on layers |
| 259 | */ |
| 260 | visibleLayerIdx(): number { |
| 261 | const layerKeys: string[] = Object.keys(LayerType); |
| 262 | for (const idx in layerKeys) { |
| 263 | if (LayerType[layerKeys[idx]] === this.visibleLayer) { |
| 264 | return Number(idx); |
| 265 | } |
| 266 | } |
| 267 | return -1; |
| 268 | } |
| 269 | |
| 270 | selectLink(link: RegionLink): void { |
| 271 | this.selectedLink = link; |
| 272 | this.linkSelected.emit(link); |
| 273 | } |
| 274 | |
| 275 | get options() { |
| 276 | return this._options = { |
| 277 | width: window.innerWidth, |
| 278 | height: window.innerHeight |
| 279 | }; |
| 280 | } |
| 281 | |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 282 | /** |
| 283 | * Iterate through all hosts and devices to deselect the previously selected |
| 284 | * node. The emit an event to the parent that lets it know the selection has |
| 285 | * changed. |
| 286 | * @param selectedNode the newly selected node |
| 287 | */ |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 288 | updateSelected(selectedNode: UiElement): void { |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 289 | this.log.debug('Node or link selected', selectedNode ? selectedNode.id : 'none'); |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 290 | this.devices |
| 291 | .filter((d) => |
| 292 | selectedNode === undefined || d.device.id !== selectedNode.id) |
| 293 | .forEach((d) => d.deselect()); |
| 294 | this.hosts |
| 295 | .filter((h) => |
| 296 | selectedNode === undefined || h.host.id !== selectedNode.id) |
| 297 | .forEach((h) => h.deselect()); |
| 298 | |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 299 | this.links |
| 300 | .filter((l) => |
| 301 | selectedNode === undefined || l.link.id !== selectedNode.id) |
| 302 | .forEach((l) => l.deselect()); |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 303 | // Push the changes back up to parent (Topology Component) |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 304 | this.selectedNodeEvent.emit(selectedNode); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 307 | /** |
| 308 | * We want to filter links to show only those not related to hosts if the |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 309 | * 'showHosts' flag has been switched off. If 'showHosts' is true, then |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 310 | * display all links. |
| 311 | */ |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 312 | filteredLinks(): Link[] { |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 313 | return this.regionData.links.filter((h) => |
| 314 | this.showHosts || |
| 315 | ((<Host>h.source).nodeType !== 'host' && |
| 316 | (<Host>h.target).nodeType !== 'host')); |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 317 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 318 | |
| 319 | /** |
| 320 | * When changes happen in the model, then model events are sent up through the |
| 321 | * Web Socket |
| 322 | * @param type - the type of the change |
| 323 | * @param memo - a qualifier on the type |
| 324 | * @param subject - the item that the update is for |
| 325 | * @param data - the new definition of the item |
| 326 | */ |
| 327 | handleModelEvent(type: ModelEventType, memo: ModelEventMemo, subject: string, data: UiElement): void { |
| 328 | switch (type) { |
| 329 | case ModelEventType.DEVICE_ADDED_OR_UPDATED: |
| 330 | if (memo === ModelEventMemo.ADDED) { |
Sean Condon | ee5d4b9 | 2019-03-11 19:57:34 +0000 | [diff] [blame^] | 331 | const loc = (<Device>data).location; |
| 332 | if (loc && loc.locType === LocationType.GEO) { |
| 333 | const position = |
| 334 | ZoomUtils.convertGeoToCanvas(<LocMeta>{ lng: loc.longOrX, lat: loc.latOrY}); |
| 335 | (<Device>data).fx = position.x; |
| 336 | (<Device>data).fy = position.y; |
| 337 | this.log.debug('Using long', loc.longOrX, 'lat', loc.latOrY, '(', position.x, position.y, ')'); |
| 338 | } else if (loc && loc.locType === LocationType.GRID) { |
| 339 | (<Device>data).fx = loc.longOrX; |
| 340 | (<Device>data).fy = loc.latOrY; |
| 341 | this.log.debug('Using grid', loc.longOrX, loc.latOrY); |
| 342 | } else { |
| 343 | (<Device>data).fx = null; |
| 344 | (<Device>data).fy = null; |
| 345 | // (<Device>data).x = 500; |
| 346 | // (<Device>data).y = 500; |
| 347 | } |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 348 | this.graph.nodes.push(<Device>data); |
Sean Condon | ee5d4b9 | 2019-03-11 19:57:34 +0000 | [diff] [blame^] | 349 | this.regionData.devices[this.visibleLayerIdx()].push(<Device>data); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 350 | this.log.debug('Device added', (<Device>data).id); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 351 | } else if (memo === ModelEventMemo.UPDATED) { |
| 352 | const oldDevice: Device = |
| 353 | this.regionData.devices[this.visibleLayerIdx()] |
| 354 | .find((d) => d.id === subject); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 355 | const changes = ForceSvgComponent.updateObject(oldDevice, <Device>data); |
| 356 | if (changes > 0) { |
| 357 | this.log.debug('Device ', oldDevice.id, memo, ' - ', changes, 'changes'); |
| 358 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 359 | } else { |
| 360 | this.log.warn('Device ', memo, ' - not yet implemented', data); |
| 361 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 362 | break; |
| 363 | case ModelEventType.HOST_ADDED_OR_UPDATED: |
| 364 | if (memo === ModelEventMemo.ADDED) { |
| 365 | this.regionData.hosts[this.visibleLayerIdx()].push(<Host>data); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 366 | this.graph.nodes.push(<Host>data); |
| 367 | this.log.debug('Host added', (<Host>data).id); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 368 | } else if (memo === ModelEventMemo.UPDATED) { |
| 369 | const oldHost: Host = this.regionData.hosts[this.visibleLayerIdx()] |
| 370 | .find((h) => h.id === subject); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 371 | const changes = ForceSvgComponent.updateObject(oldHost, <Host>data); |
| 372 | if (changes > 0) { |
| 373 | this.log.debug('Host ', oldHost.id, memo, ' - ', changes, 'changes'); |
| 374 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 375 | } else { |
| 376 | this.log.warn('Host change', memo, ' - unexpected'); |
| 377 | } |
| 378 | break; |
| 379 | case ModelEventType.DEVICE_REMOVED: |
| 380 | if (memo === ModelEventMemo.REMOVED || memo === undefined) { |
| 381 | const removeIdx: number = |
| 382 | this.regionData.devices[this.visibleLayerIdx()] |
| 383 | .findIndex((d) => d.id === subject); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 384 | this.regionData.devices[this.visibleLayerIdx()].splice(removeIdx, 1); |
| 385 | this.removeRelatedLinks(subject); |
| 386 | this.log.debug('Device ', subject, 'removed. Links', this.regionData.links); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 387 | } else { |
| 388 | this.log.warn('Device removed - unexpected memo', memo); |
| 389 | } |
| 390 | break; |
| 391 | case ModelEventType.HOST_REMOVED: |
| 392 | if (memo === ModelEventMemo.REMOVED || memo === undefined) { |
| 393 | const removeIdx: number = |
| 394 | this.regionData.hosts[this.visibleLayerIdx()] |
| 395 | .findIndex((h) => h.id === subject); |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 396 | this.regionData.hosts[this.visibleLayerIdx()].splice(removeIdx, 1); |
| 397 | this.removeRelatedLinks(subject); |
| 398 | this.log.warn('Host ', subject, 'removed'); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 399 | } else { |
| 400 | this.log.warn('Host removed - unexpected memo', memo); |
| 401 | } |
| 402 | break; |
| 403 | case ModelEventType.LINK_ADDED_OR_UPDATED: |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 404 | if (memo === ModelEventMemo.ADDED && |
| 405 | this.regionData.links.findIndex((l) => l.id === subject) === -1) { |
| 406 | const listLen = this.regionData.links.push(<RegionLink>data); |
| 407 | const epA = ForceSvgComponent.extractNodeName( |
| 408 | this.regionData.links[listLen - 1].epA); |
| 409 | this.regionData.links[listLen - 1].source = |
| 410 | this.graph.nodes.find((node) => |
| 411 | node.id === epA); |
| 412 | const epB = ForceSvgComponent.extractNodeName( |
| 413 | this.regionData.links[listLen - 1].epB); |
| 414 | this.regionData.links[listLen - 1].target = |
| 415 | this.graph.nodes.find((node) => |
| 416 | node.id === epB); |
| 417 | this.log.debug('Link added', subject); |
| 418 | } else if (memo === ModelEventMemo.UPDATED) { |
| 419 | const oldLink = this.regionData.links.find((l) => l.id === subject); |
| 420 | const changes = ForceSvgComponent.updateObject(oldLink, <RegionLink>data); |
| 421 | this.log.debug('Link ', subject, '. Updated', changes, 'items'); |
| 422 | } else { |
| 423 | this.log.warn('Link added or updated - unexpected memo', memo); |
| 424 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 425 | break; |
| 426 | default: |
| 427 | this.log.error('Unexpected model event', type, 'for', subject); |
| 428 | } |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 429 | this.ref.markForCheck(); |
| 430 | this.graph.initSimulation(this.options); |
Sean Condon | ee5d4b9 | 2019-03-11 19:57:34 +0000 | [diff] [blame^] | 431 | this.graph.initNodes(); |
| 432 | this.graph.initLinks(); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 435 | private removeRelatedLinks(subject: string) { |
| 436 | const len = this.regionData.links.length; |
| 437 | for (let i = 0; i < len; i++) { |
| 438 | const linkIdx = this.regionData.links.findIndex((l) => |
| 439 | (ForceSvgComponent.extractNodeName(l.epA) === subject || |
| 440 | ForceSvgComponent.extractNodeName(l.epB) === subject)); |
| 441 | if (linkIdx >= 0) { |
| 442 | this.regionData.links.splice(linkIdx, 1); |
| 443 | this.log.debug('Link ', linkIdx, 'removed on attempt', i); |
| 444 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * When traffic monitoring is turned on (A key) highlights will be sent back |
| 450 | * from the WebSocket through the Traffic Service |
| 451 | * @param devices - an array of device highlights |
| 452 | * @param hosts - an array of host highlights |
| 453 | * @param links - an array of link highlights |
| 454 | */ |
| 455 | handleHighlights(devices: Device[], hosts: Host[], links: LinkHighlight[]): void { |
| 456 | |
| 457 | if (devices.length > 0) { |
| 458 | this.log.debug(devices.length, 'Devices highlighted'); |
| 459 | devices.forEach((dh) => { |
| 460 | const deviceComponent: DeviceNodeSvgComponent = this.devices.find((d) => d.device.id === dh.id ); |
| 461 | if (deviceComponent) { |
| 462 | deviceComponent.ngOnChanges( |
| 463 | {'deviceHighlight': new SimpleChange(<Device>{}, dh, true)} |
| 464 | ); |
| 465 | this.log.debug('Highlighting device', deviceComponent.device.id); |
| 466 | } else { |
| 467 | this.log.warn('Device component not found', dh.id); |
| 468 | } |
| 469 | }); |
| 470 | } |
| 471 | if (hosts.length > 0) { |
| 472 | this.log.debug(hosts.length, 'Hosts highlighted'); |
| 473 | hosts.forEach((hh) => { |
| 474 | const hostComponent: HostNodeSvgComponent = this.hosts.find((h) => h.host.id === hh.id ); |
| 475 | if (hostComponent) { |
| 476 | hostComponent.ngOnChanges( |
| 477 | {'hostHighlight': new SimpleChange(<Host>{}, hh, true)} |
| 478 | ); |
| 479 | this.log.debug('Highlighting host', hostComponent.host.id); |
| 480 | } |
| 481 | }); |
| 482 | } |
| 483 | if (links.length > 0) { |
| 484 | this.log.debug(links.length, 'Links highlighted'); |
| 485 | links.forEach((lh) => { |
| 486 | const linkComponent: LinkSvgComponent = this.links.find((l) => l.link.id === lh.id ); |
| 487 | if (linkComponent) { // A link might not be present is hosts viewing is switched off |
| 488 | linkComponent.ngOnChanges( |
| 489 | {'linkHighlight': new SimpleChange(<LinkHighlight>{}, lh, true)} |
| 490 | ); |
| 491 | // this.log.debug('Highlighting link', linkComponent.link.id, lh.css, lh.label); |
| 492 | } |
| 493 | }); |
| 494 | } |
| 495 | } |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 496 | |
| 497 | /** |
| 498 | * As nodes are dragged around the graph, their new location should be sent |
| 499 | * back to server |
| 500 | * @param klass The class of node e.g. 'host' or 'device' |
| 501 | * @param id - the ID of the node |
| 502 | * @param newLocation - the new Location of the node |
| 503 | */ |
| 504 | nodeMoved(klass: string, id: string, newLocation: MetaUi) { |
| 505 | this.wss.sendEvent('updateMeta', <UpdateMeta>{ |
| 506 | id: id, |
| 507 | class: klass, |
| 508 | memento: newLocation |
| 509 | }); |
| 510 | this.log.debug(klass, id, 'has been moved to', newLocation); |
| 511 | } |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 512 | |
| 513 | resetNodeLocations() { |
| 514 | this.devices.forEach((d) => { |
| 515 | d.resetNodeLocation(); |
| 516 | }); |
| 517 | } |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 518 | } |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 519 | |