blob: ccd92508d1cd9f351a605abbc6da828f783e15da [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 {
Sean Condonc13d9562019-04-18 13:24:42 +0100108 latitude: string;
109 longitude: string;
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 Condonc13d9562019-04-18 13:24:42 +0100118 driver: string;
Sean Condon0c577f62018-11-18 22:40:05 +0000119}
120
Sean Condon0c577f62018-11-18 22:40:05 +0000121export 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 Condon9de21352019-04-06 19:22:27 +0100133export class Node implements UiElement, d3.SimulationNodeDatum {
Sean Condon0c577f62018-11-18 22:40:05 +0000134 // 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 Condon50855cf2018-12-23 15:37:42 +0000142 nodeType: NodeType;
Sean Condon9de21352019-04-06 19:22:27 +0100143 location: Location;
Sean Condon0c577f62018-11-18 22:40:05 +0000144 id: string;
145
146 protected constructor(id) {
147 this.id = id;
148 this.x = 0;
149 this.y = 0;
150 }
Sean Condon9de21352019-04-06 19:22:27 +0100151
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 Condon0c577f62018-11-18 22:40:05 +0000203}
204
205/**
206 * model of the topo2CurrentRegion device from Region below
207 */
208export class Device extends Node {
209 id: string;
210 layer: LayerType;
Sean Condon0c577f62018-11-18 22:40:05 +0000211 metaUi: MetaUi;
212 master: string;
Sean Condon0c577f62018-11-18 22:40:05 +0000213 online: boolean;
214 props: DeviceProps;
215 type: string;
216
217 constructor(id: string) {
218 super(id);
219 }
220}
221
Sean Condon50855cf2018-12-23 15:37:42 +0000222/**
223 * Model of the ONOS Host element in the topology
224 */
Sean Condon0c577f62018-11-18 22:40:05 +0000225export class Host extends Node {
226 configured: boolean;
227 id: string;
228 ips: string[];
229 layer: LayerType;
Sean Condon0c577f62018-11-18 22:40:05 +0000230 props: HostProps;
231
232 constructor(id: string) {
233 super(id);
234 }
Sean Condon9de21352019-04-06 19:22:27 +0100235
236 static resetNodeLocation(host: Host): void {
237 let origLoc: MetaUi;
238
239 if (!host.props || host.props.locType === LocationType.NONE) {
240 // No location - nothing to do
241 return;
242 } else if (host.props.locType === LocationType.GEO) {
243 origLoc = ZoomUtils.convertGeoToCanvas(<LocMeta>{
244 lng: host.props.longitude,
245 lat: host.props.latitude
246 });
247 } else if (host.props.locType === LocationType.GRID) {
248 origLoc = ZoomUtils.convertXYtoGeo(
249 host.props.gridX, host.props.gridY);
250 }
251 Node.moveNodeTo(host, origLoc);
252 }
Sean Condon0c577f62018-11-18 22:40:05 +0000253}
254
255
256/**
257 * model of the topo2CurrentRegion subregion from Region below
258 */
259export class SubRegion extends Node {
260 id: string;
261 location: Location;
262 nDevs: number;
263 nHosts: number;
264 name: string;
Sean Condon0c577f62018-11-18 22:40:05 +0000265 props: RegionProps;
266
267 constructor(id: string) {
268 super(id);
269 }
270}
Sean Condon50855cf2018-12-23 15:37:42 +0000271
272/**
273 * Enumerated values for topology update event types
274 */
275export enum ModelEventType {
276 HOST_ADDED_OR_UPDATED,
277 LINK_ADDED_OR_UPDATED,
278 DEVICE_ADDED_OR_UPDATED,
279 DEVICE_REMOVED,
Sean Condon5f7d3bc2019-04-15 11:18:34 +0100280 HOST_REMOVED,
281 LINK_REMOVED,
Sean Condon50855cf2018-12-23 15:37:42 +0000282}
283
284/**
285 * Enumerated values for topology update event memo field
286 */
287export enum ModelEventMemo {
288 ADDED = 'added',
289 REMOVED = 'removed',
290 UPDATED = 'updated'
291}
292