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