Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | */ |
| 16 | import * as d3 from 'd3'; |
| 17 | import {LocationType} from '../../backgroundsvg/backgroundsvg.component'; |
Sean Condon | 1ae1580 | 2019-03-02 09:07:18 +0000 | [diff] [blame] | 18 | import {LayerType, Location, NodeType, RegionProps} from './regions'; |
Sean Condon | 3b3fdab | 2020-04-14 09:25:00 +0100 | [diff] [blame] | 19 | import {LocMeta, MetaUi, ZoomUtils} from 'org_onosproject_onos/web/gui2-fw-lib/public_api'; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 20 | |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 21 | export interface UiElement { |
| 22 | index?: number; |
| 23 | id: string; |
| 24 | } |
| 25 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 26 | export namespace LabelToggle { |
Sean Condon | ff85fbe | 2019-03-16 14:28:46 +0000 | [diff] [blame] | 27 | /** |
| 28 | * Toggle state for how device labels should be displayed |
| 29 | */ |
| 30 | export enum Enum { |
| 31 | NONE, |
| 32 | ID, |
| 33 | NAME |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Add the method 'next()' to the LabelToggle enum above |
| 38 | */ |
| 39 | export function next(current: Enum) { |
| 40 | if (current === Enum.NONE) { |
| 41 | return Enum.ID; |
| 42 | } else if (current === Enum.ID) { |
| 43 | return Enum.NAME; |
| 44 | } else if (current === Enum.NAME) { |
| 45 | return Enum.NONE; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 50 | export namespace HostLabelToggle { |
Sean Condon | ff85fbe | 2019-03-16 14:28:46 +0000 | [diff] [blame] | 51 | /** |
| 52 | * Toggle state for how host labels should be displayed |
| 53 | */ |
| 54 | export enum Enum { |
| 55 | NONE, |
| 56 | NAME, |
| 57 | IP, |
| 58 | MAC |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Add the method 'next()' to the HostLabelToggle enum above |
| 63 | */ |
| 64 | export function next(current: Enum) { |
| 65 | if (current === Enum.NONE) { |
| 66 | return Enum.NAME; |
| 67 | } else if (current === Enum.NAME) { |
| 68 | return Enum.IP; |
| 69 | } else if (current === Enum.IP) { |
| 70 | return Enum.MAC; |
| 71 | } else if (current === Enum.MAC) { |
| 72 | return Enum.NONE; |
Sean Condon | 021f0fa | 2018-12-06 23:31:11 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 77 | export namespace GridDisplayToggle { |
Sean Condon | ff85fbe | 2019-03-16 14:28:46 +0000 | [diff] [blame] | 78 | /** |
| 79 | * Toggle state for how the grid should be displayed |
| 80 | */ |
| 81 | export enum Enum { |
| 82 | GRIDNONE, |
| 83 | GRID1000, |
| 84 | GRIDGEO, |
| 85 | GRIDBOTH |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Add the method 'next()' to the GridDisplayToggle enum above |
| 90 | */ |
| 91 | export function next(current: Enum) { |
| 92 | if (current === Enum.GRIDNONE) { |
| 93 | return Enum.GRID1000; |
| 94 | } else if (current === Enum.GRID1000) { |
| 95 | return Enum.GRIDGEO; |
| 96 | } else if (current === Enum.GRIDGEO) { |
| 97 | return Enum.GRIDBOTH; |
| 98 | } else if (current === Enum.GRIDBOTH) { |
| 99 | return Enum.GRIDNONE; |
Sean Condon | 7191054 | 2019-02-16 18:16:42 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 105 | * model of the topo2CurrentRegion device props from Device below |
| 106 | */ |
| 107 | export interface DeviceProps { |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 108 | latitude: string; |
| 109 | longitude: string; |
Sean Condon | 2888433 | 2019-03-21 14:07:00 +0000 | [diff] [blame] | 110 | gridX: number; |
| 111 | gridY: number; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 112 | name: string; |
| 113 | locType: LocationType; |
Sean Condon | 59d3137 | 2019-02-02 20:07:00 +0000 | [diff] [blame] | 114 | uiType: string; |
Sean Condon | ee54576 | 2019-03-09 10:43:58 +0000 | [diff] [blame] | 115 | channelId: string; |
| 116 | managementAddress: string; |
| 117 | protocol: string; |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 118 | driver: string; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 121 | export interface HostProps { |
| 122 | gridX: number; |
| 123 | gridY: number; |
| 124 | latitude: number; |
| 125 | longitude: number; |
| 126 | locType: LocationType; |
| 127 | name: string; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Implementing SimulationNodeDatum interface into our custom Node class |
| 132 | */ |
Sean Condon | 9de2135 | 2019-04-06 19:22:27 +0100 | [diff] [blame] | 133 | export class Node implements UiElement, d3.SimulationNodeDatum { |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 134 | // Optional - defining optional implementation properties - required for relevant typing assistance |
| 135 | index?: number; |
| 136 | x: number; |
| 137 | y: number; |
| 138 | vx?: number; |
| 139 | vy?: number; |
| 140 | fx?: number | null; |
| 141 | fy?: number | null; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 142 | nodeType: NodeType; |
Sean Condon | 9de2135 | 2019-04-06 19:22:27 +0100 | [diff] [blame] | 143 | location: Location; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 144 | id: string; |
| 145 | |
| 146 | protected constructor(id) { |
| 147 | this.id = id; |
| 148 | this.x = 0; |
| 149 | this.y = 0; |
| 150 | } |
Sean Condon | 9de2135 | 2019-04-06 19:22:27 +0100 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * Static method to reset the node's position to that specified in its |
| 154 | * coordinates |
| 155 | * This is overridden for host |
| 156 | * @param node The node to reset |
| 157 | */ |
| 158 | static resetNodeLocation(node: Node): void { |
| 159 | let origLoc: MetaUi; |
| 160 | |
| 161 | if (!node.location || node.location.locType === LocationType.NONE) { |
| 162 | // No location - nothing to do |
| 163 | return; |
| 164 | } else if (node.location.locType === LocationType.GEO) { |
| 165 | origLoc = ZoomUtils.convertGeoToCanvas(<LocMeta>{ |
| 166 | lng: node.location.longOrX, |
| 167 | lat: node.location.latOrY |
| 168 | }); |
| 169 | } else if (node.location.locType === LocationType.GRID) { |
| 170 | origLoc = ZoomUtils.convertXYtoGeo( |
| 171 | node.location.longOrX, node.location.latOrY); |
| 172 | } |
| 173 | Node.moveNodeTo(node, origLoc); |
| 174 | } |
| 175 | |
| 176 | protected static moveNodeTo(node: Node, origLoc: MetaUi) { |
| 177 | const currentX = node.fx; |
| 178 | const currentY = node.fy; |
| 179 | const distX = origLoc.x - node.fx; |
| 180 | const distY = origLoc.y - node.fy; |
| 181 | let count = 0; |
| 182 | const task = setInterval(() => { |
| 183 | count++; |
| 184 | if (count >= 10) { |
| 185 | clearInterval(task); |
| 186 | } |
| 187 | node.fx = currentX + count * distX / 10; |
| 188 | node.fy = currentY + count * distY / 10; |
| 189 | }, 50); |
| 190 | } |
| 191 | |
| 192 | static unpinOrFreezeNode(node: Node, freeze: boolean): void { |
| 193 | if (!node.location || node.location.locType === LocationType.NONE) { |
| 194 | if (freeze) { |
| 195 | node.fx = node.x; |
| 196 | node.fy = node.y; |
| 197 | } else { |
| 198 | node.fx = null; |
| 199 | node.fy = null; |
| 200 | } |
| 201 | } |
| 202 | } |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 205 | export interface Badge { |
| 206 | status: string; |
| 207 | isGlyph: boolean; |
| 208 | txt: string; |
| 209 | msg: string; |
| 210 | } |
| 211 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 212 | /** |
| 213 | * model of the topo2CurrentRegion device from Region below |
| 214 | */ |
| 215 | export class Device extends Node { |
| 216 | id: string; |
| 217 | layer: LayerType; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 218 | metaUi: MetaUi; |
| 219 | master: string; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 220 | online: boolean; |
| 221 | props: DeviceProps; |
| 222 | type: string; |
| 223 | |
| 224 | constructor(id: string) { |
| 225 | super(id); |
| 226 | } |
| 227 | } |
| 228 | |
Sean Condon | 590b34b | 2019-12-04 18:44:37 +0000 | [diff] [blame] | 229 | export interface DeviceHighlight { |
| 230 | id: string; |
| 231 | badge: Badge; |
| 232 | } |
| 233 | |
| 234 | export interface HostHighlight { |
| 235 | id: string; |
| 236 | badge: Badge; |
| 237 | } |
| 238 | |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 239 | /** |
| 240 | * Model of the ONOS Host element in the topology |
| 241 | */ |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 242 | export class Host extends Node { |
| 243 | configured: boolean; |
| 244 | id: string; |
| 245 | ips: string[]; |
| 246 | layer: LayerType; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 247 | props: HostProps; |
| 248 | |
| 249 | constructor(id: string) { |
| 250 | super(id); |
| 251 | } |
Sean Condon | 9de2135 | 2019-04-06 19:22:27 +0100 | [diff] [blame] | 252 | |
| 253 | static resetNodeLocation(host: Host): void { |
| 254 | let origLoc: MetaUi; |
| 255 | |
| 256 | if (!host.props || host.props.locType === LocationType.NONE) { |
| 257 | // No location - nothing to do |
| 258 | return; |
| 259 | } else if (host.props.locType === LocationType.GEO) { |
| 260 | origLoc = ZoomUtils.convertGeoToCanvas(<LocMeta>{ |
| 261 | lng: host.props.longitude, |
| 262 | lat: host.props.latitude |
| 263 | }); |
| 264 | } else if (host.props.locType === LocationType.GRID) { |
| 265 | origLoc = ZoomUtils.convertXYtoGeo( |
| 266 | host.props.gridX, host.props.gridY); |
| 267 | } |
| 268 | Node.moveNodeTo(host, origLoc); |
| 269 | } |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | |
| 273 | /** |
| 274 | * model of the topo2CurrentRegion subregion from Region below |
| 275 | */ |
| 276 | export class SubRegion extends Node { |
| 277 | id: string; |
| 278 | location: Location; |
| 279 | nDevs: number; |
| 280 | nHosts: number; |
| 281 | name: string; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 282 | props: RegionProps; |
| 283 | |
| 284 | constructor(id: string) { |
| 285 | super(id); |
| 286 | } |
| 287 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * Enumerated values for topology update event types |
| 291 | */ |
| 292 | export enum ModelEventType { |
| 293 | HOST_ADDED_OR_UPDATED, |
| 294 | LINK_ADDED_OR_UPDATED, |
| 295 | DEVICE_ADDED_OR_UPDATED, |
| 296 | DEVICE_REMOVED, |
Sean Condon | 5f7d3bc | 2019-04-15 11:18:34 +0100 | [diff] [blame] | 297 | HOST_REMOVED, |
| 298 | LINK_REMOVED, |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Enumerated values for topology update event memo field |
| 303 | */ |
| 304 | export enum ModelEventMemo { |
| 305 | ADDED = 'added', |
| 306 | REMOVED = 'removed', |
| 307 | UPDATED = 'updated' |
| 308 | } |
| 309 | |