Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 1 | /* |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 2 | * Copyright 2019-present Open Networking Foundation |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [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 | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 16 | import {Node, UiElement} from './node'; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 17 | import * as d3 from 'd3'; |
| 18 | |
| 19 | export enum LinkType { |
| 20 | UiRegionLink, |
| 21 | UiDeviceLink, |
| 22 | UiEdgeLink |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * model of the topo2CurrentRegion region rollup from Region below |
| 27 | * |
| 28 | */ |
| 29 | export interface RegionRollup { |
| 30 | id: string; |
| 31 | epA: string; |
| 32 | epB: string; |
| 33 | portA: string; |
| 34 | portB: string; |
| 35 | type: LinkType; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Implementing SimulationLinkDatum interface into our custom Link class |
| 40 | */ |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 41 | export class Link implements UiElement, d3.SimulationLinkDatum<Node> { |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 42 | // Optional - defining optional implementation properties - required for relevant typing assistance |
| 43 | index?: number; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 44 | id: string; // The id of the link in the format epA/portA~epB/portB |
| 45 | epA: string; // The name of the device or host at one end |
| 46 | epB: string; // The name of the device or host at the other end |
| 47 | portA: string; // The number of the port at one end |
| 48 | portB: string; // The number of the port at the other end |
| 49 | type: LinkType; |
Sean Condon | c13d956 | 2019-04-18 13:24:42 +0100 | [diff] [blame] | 50 | rollup: RegionRollup[]; // Links in sub regions represented by this one link |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 51 | |
| 52 | // Must - defining enforced implementation properties |
| 53 | source: Node; |
| 54 | target: Node; |
| 55 | |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 56 | public static deviceNameFromEp(ep: string): string { |
| 57 | if (ep !== undefined && ep.lastIndexOf('/') > 0) { |
pierventre | 178046b | 2021-10-20 17:37:52 +0200 | [diff] [blame] | 58 | // named port format is [name](number) |
| 59 | if (ep.includes('[')) { |
| 60 | return ep.substr(0, ep.lastIndexOf('[') - 1); |
| 61 | } else { |
| 62 | return ep.substr(0, ep.lastIndexOf('/')); |
| 63 | } |
Sean Condon | 9148182 | 2019-01-01 13:56:14 +0000 | [diff] [blame] | 64 | } |
| 65 | return ep; |
| 66 | } |
| 67 | |
Sean Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 68 | /** |
| 69 | * The WSS event showHighlights is sent up with a slightly different |
| 70 | * name format on the link id using the "-" separator rather than the "~" |
| 71 | * @param linkId The id of the link in either format |
| 72 | */ |
| 73 | public static linkIdFromShowHighlights(linkId: string) { |
pierventre | 178046b | 2021-10-20 17:37:52 +0200 | [diff] [blame] | 74 | // Already in the right format |
| 75 | if (linkId.includes('~')) { |
| 76 | const parts: string[] = linkId.split('~'); |
| 77 | // remove host part if needed |
Sean Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 78 | const part0 = Link.removeHostPortNum(parts[0]); |
| 79 | const part1 = Link.removeHostPortNum(parts[1]); |
| 80 | return part0 + '~' + part1; |
| 81 | } |
pierventre | 178046b | 2021-10-20 17:37:52 +0200 | [diff] [blame] | 82 | |
| 83 | // Custom traffic highlight |
| 84 | if (linkId.includes('-')) { |
| 85 | // "-" is used only as separator between the links |
| 86 | if (linkId.indexOf('-') === linkId.lastIndexOf('-')) { |
| 87 | const parts: string[] = linkId.split('-'); |
| 88 | const part0 = Link.removeHostPortNum(parts[0]); |
| 89 | const part1 = Link.removeHostPortNum(parts[1]); |
| 90 | return part0 + '~' + part1; |
| 91 | } else if (linkId.includes(')')) { |
| 92 | // "-" is used in the port name |
| 93 | var index = linkId.indexOf(')'); |
| 94 | // the format is [name](number) on both ends |
| 95 | if (linkId.charAt(index + 1) === '-') { |
| 96 | const part0 = Link.removeHostPortNum(linkId.substr(0, index + 1)); |
| 97 | const part1 = Link.removeHostPortNum(linkId.substr(index + 2, linkId.length)); |
| 98 | return part0 + '~' + part1; |
| 99 | } else { |
| 100 | index = linkId.indexOf('-'); |
| 101 | const part0 = Link.removeHostPortNum(linkId.substr(0, index)); |
| 102 | const part1 = Link.removeHostPortNum(linkId.substr(index + 1, linkId.length)); |
| 103 | return part0 + '~' + part1; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // unknown format |
Sean Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 109 | return linkId; |
| 110 | } |
| 111 | |
| 112 | private static removeHostPortNum(hostStr: string) { |
pierventre | 178046b | 2021-10-20 17:37:52 +0200 | [diff] [blame] | 113 | // Regex is for the tagged hosts |
| 114 | if (hostStr.includes('/None/') || hostStr.match('/[+-]?[0-9]+/')) { |
Sean Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 115 | const subparts = hostStr.split('/'); |
| 116 | return subparts[0] + '/' + subparts[1]; |
| 117 | } |
| 118 | return hostStr; |
| 119 | } |
| 120 | |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 121 | constructor(source, target) { |
| 122 | this.source = source; |
| 123 | this.target = target; |
| 124 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 125 | |
| 126 | linkTypeStr(): string { |
| 127 | return LinkType[this.type]; |
| 128 | } |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 132 | * model of the topo2CurrentRegion region link from Region |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 133 | */ |
| 134 | export class RegionLink extends Link { |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 135 | |
| 136 | constructor(type: LinkType, nodeA: Node, nodeB: Node) { |
| 137 | super(nodeA, nodeB); |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 138 | this.type = type; |
Sean Condon | 0c577f6 | 2018-11-18 22:40:05 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * model of the highlights that are sent back from WebSocket when traffic is shown |
| 144 | */ |
| 145 | export interface LinkHighlight { |
| 146 | id: string; |
| 147 | css: string; |
| 148 | label: string; |
Sean Condon | 64ea7d2 | 2019-04-12 19:39:13 +0100 | [diff] [blame] | 149 | fadems: number; |
Sean Condon | 50855cf | 2018-12-23 15:37:42 +0000 | [diff] [blame] | 150 | } |