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