blob: 7192de0bd9e229f81c485300e82fa0e6930f6607 [file] [log] [blame]
Sean Condon0c577f62018-11-18 22:40:05 +00001/*
Sean Condon91481822019-01-01 13:56:14 +00002 * Copyright 2019-present Open Networking Foundation
Sean Condon0c577f62018-11-18 22:40:05 +00003 *
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 Condon50855cf2018-12-23 15:37:42 +000016import {Node, UiElement} from './node';
Sean Condon0c577f62018-11-18 22:40:05 +000017import * as d3 from 'd3';
18
19export enum LinkType {
20 UiRegionLink,
21 UiDeviceLink,
22 UiEdgeLink
23}
24
25/**
26 * model of the topo2CurrentRegion region rollup from Region below
27 *
28 */
29export 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 Condon50855cf2018-12-23 15:37:42 +000041export class Link implements UiElement, d3.SimulationLinkDatum<Node> {
Sean Condon0c577f62018-11-18 22:40:05 +000042 // Optional - defining optional implementation properties - required for relevant typing assistance
43 index?: number;
Sean Condon50855cf2018-12-23 15:37:42 +000044 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 Condon0c577f62018-11-18 22:40:05 +000050
51 // Must - defining enforced implementation properties
52 source: Node;
53 target: Node;
54
Sean Condon91481822019-01-01 13:56:14 +000055 public static deviceNameFromEp(ep: string): string {
56 if (ep !== undefined && ep.lastIndexOf('/') > 0) {
57 return ep.substr(0, ep.lastIndexOf('/'));
58 }
59 return ep;
60 }
61
Sean Condon64ea7d22019-04-12 19:39:13 +010062 /**
63 * The WSS event showHighlights is sent up with a slightly different
64 * name format on the link id using the "-" separator rather than the "~"
65 * @param linkId The id of the link in either format
66 */
67 public static linkIdFromShowHighlights(linkId: string) {
68 if (linkId.includes('-')) {
69 const parts: string[] = linkId.split('-');
70 const part0 = Link.removeHostPortNum(parts[0]);
71 const part1 = Link.removeHostPortNum(parts[1]);
72 return part0 + '~' + part1;
73 }
74 return linkId;
75 }
76
77 private static removeHostPortNum(hostStr: string) {
78 if (hostStr.includes('/None/')) {
79 const subparts = hostStr.split('/');
80 return subparts[0] + '/' + subparts[1];
81 }
82 return hostStr;
83 }
84
Sean Condon0c577f62018-11-18 22:40:05 +000085 constructor(source, target) {
86 this.source = source;
87 this.target = target;
88 }
Sean Condon50855cf2018-12-23 15:37:42 +000089
90 linkTypeStr(): string {
91 return LinkType[this.type];
92 }
Sean Condon0c577f62018-11-18 22:40:05 +000093}
94
95/**
Sean Condon50855cf2018-12-23 15:37:42 +000096 * model of the topo2CurrentRegion region link from Region
Sean Condon0c577f62018-11-18 22:40:05 +000097 */
98export class RegionLink extends Link {
Sean Condon0c577f62018-11-18 22:40:05 +000099 rollup: RegionRollup[]; // Links in sub regions represented by this one link
Sean Condon0c577f62018-11-18 22:40:05 +0000100
101 constructor(type: LinkType, nodeA: Node, nodeB: Node) {
102 super(nodeA, nodeB);
Sean Condon50855cf2018-12-23 15:37:42 +0000103 this.type = type;
Sean Condon0c577f62018-11-18 22:40:05 +0000104 }
105}
Sean Condon50855cf2018-12-23 15:37:42 +0000106
107/**
108 * model of the highlights that are sent back from WebSocket when traffic is shown
109 */
110export interface LinkHighlight {
111 id: string;
112 css: string;
113 label: string;
Sean Condon64ea7d22019-04-12 19:39:13 +0100114 fadems: number;
Sean Condon50855cf2018-12-23 15:37:42 +0000115}