blob: 8ead8be1e4839a5c51cfe5a468a93b168b55b98d [file] [log] [blame]
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -08001/*
2 * Copyright 2014,2015 Open Networking Laboratory
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 */
16package org.onosproject.pathpainter;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableSet;
Andrea Campanella8583e6b2015-12-01 21:24:45 -080021import com.google.common.collect.Sets;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080022import org.onlab.osgi.ServiceDirectory;
23import org.onosproject.net.DeviceId;
Thomas Vachuska08bef152015-12-02 17:08:59 -080024import org.onosproject.net.DisjointPath;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080025import org.onosproject.net.ElementId;
26import org.onosproject.net.HostId;
27import org.onosproject.net.Link;
28import org.onosproject.net.Path;
Andrea Campanellac87fba72015-12-04 11:30:59 -080029import org.onosproject.net.device.DeviceService;
30import org.onosproject.net.topology.GeoDistanceLinkWeight;
31import org.onosproject.net.topology.LinkWeight;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080032import org.onosproject.net.topology.PathService;
33import org.onosproject.ui.RequestHandler;
34import org.onosproject.ui.UiConnection;
35import org.onosproject.ui.UiMessageHandler;
Andrea Campanella490e8392015-12-03 12:18:11 -080036import org.onosproject.ui.topo.DeviceHighlight;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080037import org.onosproject.ui.topo.Highlights;
Andrea Campanella490e8392015-12-03 12:18:11 -080038import org.onosproject.ui.topo.HostHighlight;
39import org.onosproject.ui.topo.NodeBadge;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080040import org.onosproject.ui.topo.TopoJson;
41import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
43
44import java.util.Collection;
45import java.util.List;
46import java.util.Set;
47
48/**
49 * Skeletal ONOS UI Topology-Overlay message handler.
50 */
51public class PathPainterTopovMessageHandler extends UiMessageHandler {
52
53 private static final String PAINTER_SET_SRC = "ppTopovSetSrc";
54 private static final String PAINTER_SET_DST = "ppTopovSetDst";
55 private static final String PAINTER_SWAP_SRC_DST = "ppTopovSwapSrcDst";
56 private static final String PAINTER_SET_MODE = "ppTopovSetMode";
57
58 private static final String PAINTER_NEXT_PATH = "ppTopovNextPath";
59 private static final String PAINTER_PREV_PATH = "ppTopovPrevPath";
60
61 private static final String ID = "id";
62 private static final String MODE = "mode";
Andrea Campanella490e8392015-12-03 12:18:11 -080063 private static final String TYPE = "type";
64 private static final String SWITCH = "switch";
65 private static final String ENDSTATION = "endstation";
66 public static final String DST = "Dst";
67 public static final String SRC = "Src";
Andrea Campanellac87fba72015-12-04 11:30:59 -080068 private static LinkWeight linkData;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080069
70 private Set<Link> allPathLinks;
71
72 private enum Mode {
Andrea Campanellac87fba72015-12-04 11:30:59 -080073 SHORTEST, DISJOINT, GEODATA, SRLG, INVALID
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080074 }
75
76 private final Logger log = LoggerFactory.getLogger(getClass());
77
78 private PathService pathService;
79
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080080 private ElementId src, dst;
Andrea Campanella490e8392015-12-03 12:18:11 -080081 private String srcType, dstType;
Andrea Campanella8583e6b2015-12-01 21:24:45 -080082 private Mode currentMode = Mode.SHORTEST;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080083 private List<Path> paths;
84 private int pathIndex;
85
86
87 // ===============-=-=-=-=-=-======================-=-=-=-=-=-=-================================
88
89
90 @Override
91 public void init(UiConnection connection, ServiceDirectory directory) {
92 super.init(connection, directory);
93 pathService = directory.get(PathService.class);
Andrea Campanellac87fba72015-12-04 11:30:59 -080094 linkData = new GeoDistanceLinkWeight(directory.get(DeviceService.class));
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080095 }
96
97 @Override
98 protected Collection<RequestHandler> createRequestHandlers() {
99 return ImmutableSet.of(
100 new SetSrcHandler(),
101 new SetDstHandler(),
Andrea Campanella0c17a0a2015-12-01 09:53:51 -0800102 new SwapSrcDstHandler(),
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800103 new NextPathHandler(),
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800104 new PrevPathHandler(),
105 new SetModeHandler()
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800106 );
107 }
108
109 // === -------------------------
110 // === Handler classes
111
112 private final class SetSrcHandler extends RequestHandler {
Andrea Campanella490e8392015-12-03 12:18:11 -0800113
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800114 public SetSrcHandler() {
115 super(PAINTER_SET_SRC);
116 }
117
118 @Override
119 public void process(long sid, ObjectNode payload) {
120 String id = string(payload, ID);
121 src = elementId(id);
Andrea Campanella490e8392015-12-03 12:18:11 -0800122 srcType = string(payload, TYPE);
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800123 if (src.equals(dst)) {
124 dst = null;
125 }
Andrea Campanella490e8392015-12-03 12:18:11 -0800126 sendMessage(TopoJson.highlightsMessage(addBadge(new Highlights(),
127 srcType,
128 src.toString(),
129 SRC)));
130 findAndSendPaths(currentMode);
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800131 }
132 }
133
134 private final class SetDstHandler extends RequestHandler {
135 public SetDstHandler() {
136 super(PAINTER_SET_DST);
137 }
138
139 @Override
140 public void process(long sid, ObjectNode payload) {
141 String id = string(payload, ID);
142 dst = elementId(id);
Andrea Campanella490e8392015-12-03 12:18:11 -0800143 dstType = string(payload, TYPE);
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800144 if (src.equals(dst)) {
145 src = null;
146 }
Andrea Campanella490e8392015-12-03 12:18:11 -0800147
148 sendMessage(TopoJson.highlightsMessage(addBadge(new Highlights(),
149 dstType,
150 dst.toString(),
151 DST)));
152 findAndSendPaths(currentMode);
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800153 }
154 }
155
Andrea Campanella0c17a0a2015-12-01 09:53:51 -0800156 private final class SwapSrcDstHandler extends RequestHandler {
157 public SwapSrcDstHandler() {
158 super(PAINTER_SWAP_SRC_DST);
159 }
160
161 @Override
162 public void process(long sid, ObjectNode payload) {
163 ElementId temp = src;
164 src = dst;
165 dst = temp;
Andrea Campanella490e8392015-12-03 12:18:11 -0800166 String s = srcType;
167 srcType = dstType;
168 dstType = s;
169 findAndSendPaths(currentMode);
Andrea Campanella0c17a0a2015-12-01 09:53:51 -0800170 }
171 }
172
Andrea Campanellac87fba72015-12-04 11:30:59 -0800173
174
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800175 private final class NextPathHandler extends RequestHandler {
176 public NextPathHandler() {
177 super(PAINTER_NEXT_PATH);
178 }
179
180 @Override
181 public void process(long sid, ObjectNode payload) {
182 pathIndex = (pathIndex >= paths.size() - 1 ? 0 : pathIndex + 1);
183 hilightAndSendPaths();
184 }
185 }
186
187 private final class PrevPathHandler extends RequestHandler {
188 public PrevPathHandler() {
189 super(PAINTER_PREV_PATH);
190 }
191
192 @Override
193 public void process(long sid, ObjectNode payload) {
194 pathIndex = (pathIndex <= 0 ? paths.size() - 1 : pathIndex - 1);
195 hilightAndSendPaths();
196 }
197 }
198
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800199 private final class SetModeHandler extends RequestHandler {
200 public SetModeHandler() {
201 super(PAINTER_SET_MODE);
202 }
203
204 @Override
205 public void process(long sid, ObjectNode payload) {
206 String mode = string(payload, MODE);
Andrea Campanellac87fba72015-12-04 11:30:59 -0800207 switch (mode) {
208 case "shortest":
209 currentMode = Mode.SHORTEST;
210 break;
211 case "disjoint":
212 currentMode = Mode.DISJOINT;
213 break;
214 case "geodata":
215 currentMode = Mode.GEODATA;
216 break;
217 case "srlg":
218 currentMode = Mode.SRLG;
219 break;
220 default:
221 currentMode = Mode.INVALID;
222 break;
223 }
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800224 //TODO: add support for SRLG
Andrea Campanella490e8392015-12-03 12:18:11 -0800225 findAndSendPaths(currentMode);
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800226 }
227 }
228
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800229 // === ------------
230
231 private ElementId elementId(String id) {
232 try {
233 return DeviceId.deviceId(id);
234 } catch (IllegalArgumentException e) {
235 return HostId.hostId(id);
236 }
237 }
238
Andrea Campanella490e8392015-12-03 12:18:11 -0800239 private void findAndSendPaths(Mode mode) {
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800240 log.info("src={}; dst={}; mode={}", src, dst, currentMode);
241 if (src != null && dst != null) {
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800242 pathIndex = 0;
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800243 ImmutableSet.Builder<Link> builder = ImmutableSet.builder();
Andrea Campanella490e8392015-12-03 12:18:11 -0800244 if (mode.equals(Mode.SHORTEST)) {
245 paths = ImmutableList.copyOf(pathService.getPaths(src, dst));
246 allPathLinks = buildPaths(builder).build();
247 } else if (mode.equals(Mode.DISJOINT)) {
248 paths = ImmutableList.copyOf(pathService.getDisjointPaths(src, dst));
249 allPathLinks = buildDisjointPaths(builder).build();
Andrea Campanellac87fba72015-12-04 11:30:59 -0800250 } else if (mode.equals(Mode.GEODATA)) {
251 paths = ImmutableList.copyOf(pathService.getPaths(src, dst, linkData));
252 allPathLinks = buildPaths(builder).build();
Andrea Campanella490e8392015-12-03 12:18:11 -0800253 } else {
254 log.info("Unsupported MODE");
255 }
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800256 } else {
257 paths = ImmutableList.of();
258 allPathLinks = ImmutableSet.of();
259 }
260 hilightAndSendPaths();
Andrea Campanella490e8392015-12-03 12:18:11 -0800261
262 }
263
264 private ImmutableSet.Builder<Link> buildPaths(ImmutableSet.Builder<Link> pathBuilder) {
265 paths.forEach(path -> path.links().forEach(pathBuilder::add));
266 return pathBuilder;
267 }
268
269 private ImmutableSet.Builder<Link> buildDisjointPaths(ImmutableSet.Builder<Link> pathBuilder) {
270 paths.forEach(path -> {
271 DisjointPath dp = (DisjointPath) path;
272 pathBuilder.addAll(dp.primary().links());
273 pathBuilder.addAll(dp.backup().links());
274 });
275 return pathBuilder;
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800276 }
277
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800278 private void hilightAndSendPaths() {
279 PathLinkMap linkMap = new PathLinkMap();
280 allPathLinks.forEach(linkMap::add);
281
Thomas Vachuska08bef152015-12-02 17:08:59 -0800282 Set<Link> selectedPathLinks;
283
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800284 // Prepare two working sets; one containing selected path links and
285 // the other containing all paths links.
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800286 if (currentMode.equals(Mode.DISJOINT)) {
Andrea Campanella490e8392015-12-03 12:18:11 -0800287 DisjointPath dp = (DisjointPath) paths.get(pathIndex);
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800288 selectedPathLinks = paths.isEmpty() ?
Andrea Campanella490e8392015-12-03 12:18:11 -0800289 ImmutableSet.of() : Sets.newHashSet(dp.primary().links());
Thomas Vachuska08bef152015-12-02 17:08:59 -0800290 selectedPathLinks.addAll(dp.backup().links());
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800291 } else {
292 selectedPathLinks = paths.isEmpty() ?
Thomas Vachuska08bef152015-12-02 17:08:59 -0800293 ImmutableSet.of() : ImmutableSet.copyOf(paths.get(pathIndex).links());
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800294 }
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800295 Highlights highlights = new Highlights();
296 for (PathLink plink : linkMap.biLinks()) {
297 plink.computeHilight(selectedPathLinks, allPathLinks);
298 highlights.add(plink.highlight(null));
299 }
Andrea Campanella490e8392015-12-03 12:18:11 -0800300 if (src != null) {
301 highlights = addBadge(highlights, srcType, src.toString(), SRC);
302 }
303 if (dst != null) {
304 highlights = addBadge(highlights, dstType, dst.toString(), DST);
305 }
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800306 sendMessage(TopoJson.highlightsMessage(highlights));
307 }
308
Andrea Campanella490e8392015-12-03 12:18:11 -0800309 private Highlights addBadge(Highlights highlights, String type, String elemId, String src) {
310 if (SWITCH.equals(type)) {
311 highlights = addDeviceBadge(highlights, elemId, src);
312 } else if (ENDSTATION.equals(type)) {
313 highlights = addHostBadge(highlights, elemId, src);
314 }
315 return highlights;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800316 }
317
Andrea Campanella490e8392015-12-03 12:18:11 -0800318 private Highlights addDeviceBadge(Highlights h, String elemId, String type) {
319 DeviceHighlight dh = new DeviceHighlight(elemId);
320 dh.setBadge(createBadge(type));
321 h.add(dh);
322 return h;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800323 }
Andrea Campanella490e8392015-12-03 12:18:11 -0800324
325 private Highlights addHostBadge(Highlights h, String elemId, String type) {
326 HostHighlight hh = new HostHighlight(elemId);
327 hh.setBadge(createBadge(type));
328 h.add(hh);
329 return h;
330 }
331
332 private NodeBadge createBadge(String type) {
333 return NodeBadge.text(type);
334 }
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800335
336}