PathPainterTopovMessageHandler.java
13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* Copyright 2014,2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.pathpainter;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import org.onlab.osgi.ServiceDirectory;
import org.onosproject.net.DeviceId;
import org.onosproject.net.DisjointPath;
import org.onosproject.net.ElementId;
import org.onosproject.net.HostId;
import org.onosproject.net.Link;
import org.onosproject.net.Path;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.topology.GeoDistanceLinkWeight;
import org.onosproject.net.topology.LinkWeight;
import org.onosproject.net.topology.PathService;
import org.onosproject.net.topology.TopologyEvent;
import org.onosproject.net.topology.TopologyListener;
import org.onosproject.net.topology.TopologyService;
import org.onosproject.ui.RequestHandler;
import org.onosproject.ui.UiConnection;
import org.onosproject.ui.UiMessageHandler;
import org.onosproject.ui.topo.DeviceHighlight;
import org.onosproject.ui.topo.Highlights;
import org.onosproject.ui.topo.HostHighlight;
import org.onosproject.ui.topo.NodeBadge;
import org.onosproject.ui.topo.TopoJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
* ONOS UI PathPainter Topology-Overlay message handler.
*/
public class PathPainterTopovMessageHandler extends UiMessageHandler {
private static final String PAINTER_CLEAR = "ppTopovClear";
private static final String PAINTER_SET_SRC = "ppTopovSetSrc";
private static final String PAINTER_SET_DST = "ppTopovSetDst";
private static final String PAINTER_SWAP_SRC_DST = "ppTopovSwapSrcDst";
private static final String PAINTER_SET_MODE = "ppTopovSetMode";
private static final String PAINTER_NEXT_PATH = "ppTopovNextPath";
private static final String PAINTER_PREV_PATH = "ppTopovPrevPath";
private static final String ID = "id";
private static final String MODE = "mode";
private static final String TYPE = "type";
private static final String SWITCH = "switch";
private static final String ENDSTATION = "endstation";
public static final String DST = "Dst";
public static final String SRC = "Src";
// Delay for showHighlights event processing on GUI client side to
// account for addLink animation.
public static final int DELAY_MS = 1100;
private final TopologyListener topologyListener = new InternalTopologyListener();
private Set<Link> allPathLinks;
private boolean listenersRemoved;
private LinkWeight linkData;
private int highlightDelay;
private enum Mode {
SHORTEST, DISJOINT, GEODATA, SRLG, INVALID
}
private final Logger log = LoggerFactory.getLogger(getClass());
private PathService pathService;
private ElementId src, dst;
private String srcType, dstType;
private Mode currentMode = Mode.SHORTEST;
private List<Path> paths;
private int pathIndex;
protected TopologyService topologyService;
// ===============-=-=-=-=-=-======================-=-=-=-=-=-=-================================
@Override
public void init(UiConnection connection, ServiceDirectory directory) {
super.init(connection, directory);
pathService = directory.get(PathService.class);
topologyService = directory.get(TopologyService.class);
linkData = new GeoDistanceLinkWeight(directory.get(DeviceService.class));
addListeners();
}
@Override
public void destroy() {
removeListeners();
super.destroy();
}
@Override
protected Collection<RequestHandler> createRequestHandlers() {
return ImmutableSet.of(
new ClearHandler(),
new SetSrcHandler(),
new SetDstHandler(),
new SwapSrcDstHandler(),
new NextPathHandler(),
new PrevPathHandler(),
new SetModeHandler()
);
}
// === -------------------------
// === Handler classes
private final class ClearHandler extends RequestHandler {
public ClearHandler() {
super(PAINTER_CLEAR);
}
@Override
public void process(long sid, ObjectNode payload) {
src = null;
dst = null;
sendMessage(TopoJson.highlightsMessage(new Highlights()));
}
}
private final class SetSrcHandler extends RequestHandler {
public SetSrcHandler() {
super(PAINTER_SET_SRC);
}
@Override
public void process(long sid, ObjectNode payload) {
String id = string(payload, ID);
src = elementId(id);
srcType = string(payload, TYPE);
if (src.equals(dst)) {
dst = null;
}
sendMessage(TopoJson.highlightsMessage(addBadge(new Highlights(),
srcType,
src.toString(),
SRC)));
findAndSendPaths(currentMode);
}
}
private final class SetDstHandler extends RequestHandler {
public SetDstHandler() {
super(PAINTER_SET_DST);
}
@Override
public void process(long sid, ObjectNode payload) {
String id = string(payload, ID);
dst = elementId(id);
dstType = string(payload, TYPE);
if (src.equals(dst)) {
src = null;
}
sendMessage(TopoJson.highlightsMessage(addBadge(new Highlights(),
dstType,
dst.toString(),
DST)));
findAndSendPaths(currentMode);
}
}
private final class SwapSrcDstHandler extends RequestHandler {
public SwapSrcDstHandler() {
super(PAINTER_SWAP_SRC_DST);
}
@Override
public void process(long sid, ObjectNode payload) {
ElementId temp = src;
src = dst;
dst = temp;
String s = srcType;
srcType = dstType;
dstType = s;
findAndSendPaths(currentMode);
}
}
private final class NextPathHandler extends RequestHandler {
public NextPathHandler() {
super(PAINTER_NEXT_PATH);
}
@Override
public void process(long sid, ObjectNode payload) {
pathIndex = (pathIndex >= paths.size() - 1 ? 0 : pathIndex + 1);
hilightAndSendPaths();
}
}
private final class PrevPathHandler extends RequestHandler {
public PrevPathHandler() {
super(PAINTER_PREV_PATH);
}
@Override
public void process(long sid, ObjectNode payload) {
pathIndex = (pathIndex <= 0 ? paths.size() - 1 : pathIndex - 1);
hilightAndSendPaths();
}
}
private final class SetModeHandler extends RequestHandler {
public SetModeHandler() {
super(PAINTER_SET_MODE);
}
@Override
public void process(long sid, ObjectNode payload) {
String mode = string(payload, MODE);
switch (mode) {
case "shortest":
currentMode = Mode.SHORTEST;
break;
case "disjoint":
currentMode = Mode.DISJOINT;
break;
case "geodata":
currentMode = Mode.GEODATA;
break;
case "srlg":
currentMode = Mode.SRLG;
break;
default:
currentMode = Mode.INVALID;
break;
}
//TODO: add support for SRLG
findAndSendPaths(currentMode);
}
}
// === ------------
private ElementId elementId(String id) {
try {
return DeviceId.deviceId(id);
} catch (IllegalArgumentException e) {
return HostId.hostId(id);
}
}
private void findAndSendPaths(Mode mode) {
log.info("src={}; dst={}; mode={}", src, dst, currentMode);
if (src != null && dst != null) {
pathIndex = 0;
ImmutableSet.Builder<Link> builder = ImmutableSet.builder();
if (mode.equals(Mode.SHORTEST)) {
paths = ImmutableList.copyOf(pathService.getPaths(src, dst));
allPathLinks = buildPaths(builder).build();
} else if (mode.equals(Mode.DISJOINT)) {
paths = ImmutableList.copyOf(pathService.getDisjointPaths(src, dst));
allPathLinks = buildDisjointPaths(builder).build();
} else if (mode.equals(Mode.GEODATA)) {
paths = ImmutableList.copyOf(pathService.getPaths(src, dst, linkData));
allPathLinks = buildPaths(builder).build();
} else {
log.info("Unsupported MODE");
}
} else {
paths = ImmutableList.of();
allPathLinks = ImmutableSet.of();
}
hilightAndSendPaths();
}
private ImmutableSet.Builder<Link> buildPaths(ImmutableSet.Builder<Link> pathBuilder) {
paths.forEach(path -> path.links().forEach(pathBuilder::add));
return pathBuilder;
}
private ImmutableSet.Builder<Link> buildDisjointPaths(ImmutableSet.Builder<Link> pathBuilder) {
paths.forEach(path -> {
DisjointPath dp = (DisjointPath) path;
pathBuilder.addAll(dp.primary().links());
pathBuilder.addAll(dp.backup().links());
});
return pathBuilder;
}
private void hilightAndSendPaths() {
PathLinkMap linkMap = new PathLinkMap();
allPathLinks.forEach(linkMap::add);
Set<Link> selectedPathLinks;
// Prepare two working sets; one containing selected path links and
// the other containing all paths links.
if (currentMode.equals(Mode.DISJOINT)) {
DisjointPath dp = (DisjointPath) paths.get(pathIndex);
selectedPathLinks = paths.isEmpty() ?
ImmutableSet.of() : Sets.newHashSet(dp.primary().links());
selectedPathLinks.addAll(dp.backup().links());
} else {
selectedPathLinks = paths.isEmpty() ?
ImmutableSet.of() : ImmutableSet.copyOf(paths.get(pathIndex).links());
}
Highlights highlights = new Highlights();
if (highlightDelay > 0) {
highlights.delay(highlightDelay);
}
for (PathLink plink : linkMap.biLinks()) {
plink.computeHilight(selectedPathLinks, allPathLinks);
highlights.add(plink.highlight(null));
}
if (src != null) {
highlights = addBadge(highlights, srcType, src.toString(), SRC);
}
if (dst != null) {
highlights = addBadge(highlights, dstType, dst.toString(), DST);
}
sendMessage(TopoJson.highlightsMessage(highlights));
}
private Highlights addBadge(Highlights highlights, String type, String elemId, String src) {
if (SWITCH.equals(type)) {
highlights = addDeviceBadge(highlights, elemId, src);
} else if (ENDSTATION.equals(type)) {
highlights = addHostBadge(highlights, elemId, src);
}
return highlights;
}
private Highlights addDeviceBadge(Highlights h, String elemId, String type) {
DeviceHighlight dh = new DeviceHighlight(elemId);
dh.setBadge(createBadge(type));
h.add(dh);
return h;
}
private Highlights addHostBadge(Highlights h, String elemId, String type) {
HostHighlight hh = new HostHighlight(elemId);
hh.setBadge(createBadge(type));
h.add(hh);
return h;
}
private NodeBadge createBadge(String type) {
return NodeBadge.text(type);
}
private synchronized void addListeners() {
listenersRemoved = false;
topologyService.addListener(topologyListener);
}
private synchronized void removeListeners() {
if (!listenersRemoved) {
listenersRemoved = true;
topologyService.removeListener(topologyListener);
}
}
// Link event listener.
private class InternalTopologyListener implements TopologyListener {
@Override
public void event(TopologyEvent event) {
highlightDelay = DELAY_MS;
findAndSendPaths(currentMode);
highlightDelay = 0;
}
}
}