blob: 1402f81eacdacca8282c9c64041c9a13bf69f7bd [file] [log] [blame]
Sean Condon0c577f62018-11-18 22:40:05 +00001/*
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 */
16import * as d3 from 'd3';
17import {LocationType} from '../../backgroundsvg/backgroundsvg.component';
Sean Condon1ae15802019-03-02 09:07:18 +000018import {LayerType, Location, NodeType, RegionProps} from './regions';
Sean Condon9de21352019-04-06 19:22:27 +010019import {LocMeta, MetaUi, ZoomUtils} from 'gui2-fw-lib';
Sean Condon0c577f62018-11-18 22:40:05 +000020
Sean Condon50855cf2018-12-23 15:37:42 +000021export interface UiElement {
22 index?: number;
23 id: string;
24}
25
Sean Condon0c577f62018-11-18 22:40:05 +000026export namespace LabelToggle {
Sean Condonff85fbe2019-03-16 14:28:46 +000027 /**
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 Condon0c577f62018-11-18 22:40:05 +000046 }
47 }
48}
49
Sean Condon021f0fa2018-12-06 23:31:11 -080050export namespace HostLabelToggle {
Sean Condonff85fbe2019-03-16 14:28:46 +000051 /**
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 Condon021f0fa2018-12-06 23:31:11 -080073 }
74 }
75}
76
Sean Condon71910542019-02-16 18:16:42 +000077export namespace GridDisplayToggle {
Sean Condonff85fbe2019-03-16 14:28:46 +000078 /**
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 Condon71910542019-02-16 18:16:42 +0000100 }
101 }
102}
103
104/**
Sean Condon0c577f62018-11-18 22:40:05 +0000105 * model of the topo2CurrentRegion device props from Device below
106 */
107export interface DeviceProps {
108 latitude: number;
109 longitude: number;
Sean Condon28884332019-03-21 14:07:00 +0000110 gridX: number;
111 gridY: number;
Sean Condon0c577f62018-11-18 22:40:05 +0000112 name: string;
113 locType: LocationType;
Sean Condon59d31372019-02-02 20:07:00 +0000114 uiType: string;
Sean Condonee545762019-03-09 10:43:58 +0000115 channelId: string;
116 managementAddress: string;
117 protocol: string;
Sean Condon0c577f62018-11-18 22:40:05 +0000118}
119
Sean Condon0c577f62018-11-18 22:40:05 +0000120export interface HostProps {
121 gridX: number;
122 gridY: number;
123 latitude: number;
124 longitude: number;
125 locType: LocationType;
126 name: string;
127}
128
129/**
130 * Implementing SimulationNodeDatum interface into our custom Node class
131 */
Sean Condon9de21352019-04-06 19:22:27 +0100132export class Node implements UiElement, d3.SimulationNodeDatum {
Sean Condon0c577f62018-11-18 22:40:05 +0000133 // Optional - defining optional implementation properties - required for relevant typing assistance
134 index?: number;
135 x: number;
136 y: number;
137 vx?: number;
138 vy?: number;
139 fx?: number | null;
140 fy?: number | null;
Sean Condon50855cf2018-12-23 15:37:42 +0000141 nodeType: NodeType;
Sean Condon9de21352019-04-06 19:22:27 +0100142 location: Location;
Sean Condon0c577f62018-11-18 22:40:05 +0000143 id: string;
144
145 protected constructor(id) {
146 this.id = id;
147 this.x = 0;
148 this.y = 0;
149 }
Sean Condon9de21352019-04-06 19:22:27 +0100150
151 /**
152 * Static method to reset the node's position to that specified in its
153 * coordinates
154 * This is overridden for host
155 * @param node The node to reset
156 */
157 static resetNodeLocation(node: Node): void {
158 let origLoc: MetaUi;
159
160 if (!node.location || node.location.locType === LocationType.NONE) {
161 // No location - nothing to do
162 return;
163 } else if (node.location.locType === LocationType.GEO) {
164 origLoc = ZoomUtils.convertGeoToCanvas(<LocMeta>{
165 lng: node.location.longOrX,
166 lat: node.location.latOrY
167 });
168 } else if (node.location.locType === LocationType.GRID) {
169 origLoc = ZoomUtils.convertXYtoGeo(
170 node.location.longOrX, node.location.latOrY);
171 }
172 Node.moveNodeTo(node, origLoc);
173 }
174
175 protected static moveNodeTo(node: Node, origLoc: MetaUi) {
176 const currentX = node.fx;
177 const currentY = node.fy;
178 const distX = origLoc.x - node.fx;
179 const distY = origLoc.y - node.fy;
180 let count = 0;
181 const task = setInterval(() => {
182 count++;
183 if (count >= 10) {
184 clearInterval(task);
185 }
186 node.fx = currentX + count * distX / 10;
187 node.fy = currentY + count * distY / 10;
188 }, 50);
189 }
190
191 static unpinOrFreezeNode(node: Node, freeze: boolean): void {
192 if (!node.location || node.location.locType === LocationType.NONE) {
193 if (freeze) {
194 node.fx = node.x;
195 node.fy = node.y;
196 } else {
197 node.fx = null;
198 node.fy = null;
199 }
200 }
201 }
Sean Condon0c577f62018-11-18 22:40:05 +0000202}
203
204/**
205 * model of the topo2CurrentRegion device from Region below
206 */
207export class Device extends Node {
208 id: string;
209 layer: LayerType;
Sean Condon0c577f62018-11-18 22:40:05 +0000210 metaUi: MetaUi;
211 master: string;
Sean Condon0c577f62018-11-18 22:40:05 +0000212 online: boolean;
213 props: DeviceProps;
214 type: string;
215
216 constructor(id: string) {
217 super(id);
218 }
219}
220
Sean Condon50855cf2018-12-23 15:37:42 +0000221/**
222 * Model of the ONOS Host element in the topology
223 */
Sean Condon0c577f62018-11-18 22:40:05 +0000224export class Host extends Node {
225 configured: boolean;
226 id: string;
227 ips: string[];
228 layer: LayerType;
Sean Condon0c577f62018-11-18 22:40:05 +0000229 props: HostProps;
230
231 constructor(id: string) {
232 super(id);
233 }
Sean Condon9de21352019-04-06 19:22:27 +0100234
235 static resetNodeLocation(host: Host): void {
236 let origLoc: MetaUi;
237
238 if (!host.props || host.props.locType === LocationType.NONE) {
239 // No location - nothing to do
240 return;
241 } else if (host.props.locType === LocationType.GEO) {
242 origLoc = ZoomUtils.convertGeoToCanvas(<LocMeta>{
243 lng: host.props.longitude,
244 lat: host.props.latitude
245 });
246 } else if (host.props.locType === LocationType.GRID) {
247 origLoc = ZoomUtils.convertXYtoGeo(
248 host.props.gridX, host.props.gridY);
249 }
250 Node.moveNodeTo(host, origLoc);
251 }
Sean Condon0c577f62018-11-18 22:40:05 +0000252}
253
254
255/**
256 * model of the topo2CurrentRegion subregion from Region below
257 */
258export class SubRegion extends Node {
259 id: string;
260 location: Location;
261 nDevs: number;
262 nHosts: number;
263 name: string;
Sean Condon0c577f62018-11-18 22:40:05 +0000264 props: RegionProps;
265
266 constructor(id: string) {
267 super(id);
268 }
269}
Sean Condon50855cf2018-12-23 15:37:42 +0000270
271/**
272 * Enumerated values for topology update event types
273 */
274export enum ModelEventType {
275 HOST_ADDED_OR_UPDATED,
276 LINK_ADDED_OR_UPDATED,
277 DEVICE_ADDED_OR_UPDATED,
278 DEVICE_REMOVED,
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100279 HOST_REMOVED,
280 LINK_REMOVED,
Sean Condon50855cf2018-12-23 15:37:42 +0000281}
282
283/**
284 * Enumerated values for topology update event memo field
285 */
286export enum ModelEventMemo {
287 ADDED = 'added',
288 REMOVED = 'removed',
289 UPDATED = 'updated'
290}
291