blob: 521507c5a86cbbf20c4061494aa6d7de4822b342 [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;
24import org.onosproject.net.ElementId;
25import org.onosproject.net.HostId;
26import org.onosproject.net.Link;
27import org.onosproject.net.Path;
28import org.onosproject.net.topology.PathService;
29import org.onosproject.ui.RequestHandler;
30import org.onosproject.ui.UiConnection;
31import org.onosproject.ui.UiMessageHandler;
32import org.onosproject.ui.topo.Highlights;
33import org.onosproject.ui.topo.TopoJson;
34import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
Andrea Campanella8583e6b2015-12-01 21:24:45 -080037import java.util.ArrayList;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080038import java.util.Collection;
39import java.util.List;
40import java.util.Set;
41
42/**
43 * Skeletal ONOS UI Topology-Overlay message handler.
44 */
45public class PathPainterTopovMessageHandler extends UiMessageHandler {
46
47 private static final String PAINTER_SET_SRC = "ppTopovSetSrc";
48 private static final String PAINTER_SET_DST = "ppTopovSetDst";
49 private static final String PAINTER_SWAP_SRC_DST = "ppTopovSwapSrcDst";
50 private static final String PAINTER_SET_MODE = "ppTopovSetMode";
51
52 private static final String PAINTER_NEXT_PATH = "ppTopovNextPath";
53 private static final String PAINTER_PREV_PATH = "ppTopovPrevPath";
54
55 private static final String ID = "id";
56 private static final String MODE = "mode";
57
58 private Set<Link> allPathLinks;
Andrea Campanella8583e6b2015-12-01 21:24:45 -080059 private Set<Link> selectedPathLinks;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080060
61 private enum Mode {
62 SHORTEST, DISJOINT, SRLG
63 }
64
65 private final Logger log = LoggerFactory.getLogger(getClass());
66
67 private PathService pathService;
68
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080069 private ElementId src, dst;
Andrea Campanella8583e6b2015-12-01 21:24:45 -080070 private Mode currentMode = Mode.SHORTEST;
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080071 private List<Path> paths;
72 private int pathIndex;
73
74
75 // ===============-=-=-=-=-=-======================-=-=-=-=-=-=-================================
76
77
78 @Override
79 public void init(UiConnection connection, ServiceDirectory directory) {
80 super.init(connection, directory);
81 pathService = directory.get(PathService.class);
82 }
83
84 @Override
85 protected Collection<RequestHandler> createRequestHandlers() {
86 return ImmutableSet.of(
87 new SetSrcHandler(),
88 new SetDstHandler(),
Andrea Campanella0c17a0a2015-12-01 09:53:51 -080089 new SwapSrcDstHandler(),
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080090 new NextPathHandler(),
Andrea Campanella8583e6b2015-12-01 21:24:45 -080091 new PrevPathHandler(),
92 new SetModeHandler()
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -080093 );
94 }
95
96 // === -------------------------
97 // === Handler classes
98
99 private final class SetSrcHandler extends RequestHandler {
100 public SetSrcHandler() {
101 super(PAINTER_SET_SRC);
102 }
103
104 @Override
105 public void process(long sid, ObjectNode payload) {
106 String id = string(payload, ID);
107 src = elementId(id);
108 if (src.equals(dst)) {
109 dst = null;
110 }
111 findAndSendPaths();
112 }
113 }
114
115 private final class SetDstHandler extends RequestHandler {
116 public SetDstHandler() {
117 super(PAINTER_SET_DST);
118 }
119
120 @Override
121 public void process(long sid, ObjectNode payload) {
122 String id = string(payload, ID);
123 dst = elementId(id);
124 if (src.equals(dst)) {
125 src = null;
126 }
127 findAndSendPaths();
128 }
129 }
130
Andrea Campanella0c17a0a2015-12-01 09:53:51 -0800131 private final class SwapSrcDstHandler extends RequestHandler {
132 public SwapSrcDstHandler() {
133 super(PAINTER_SWAP_SRC_DST);
134 }
135
136 @Override
137 public void process(long sid, ObjectNode payload) {
138 ElementId temp = src;
139 src = dst;
140 dst = temp;
141 findAndSendPaths();
142 }
143 }
144
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800145 private final class NextPathHandler extends RequestHandler {
146 public NextPathHandler() {
147 super(PAINTER_NEXT_PATH);
148 }
149
150 @Override
151 public void process(long sid, ObjectNode payload) {
152 pathIndex = (pathIndex >= paths.size() - 1 ? 0 : pathIndex + 1);
153 hilightAndSendPaths();
154 }
155 }
156
157 private final class PrevPathHandler extends RequestHandler {
158 public PrevPathHandler() {
159 super(PAINTER_PREV_PATH);
160 }
161
162 @Override
163 public void process(long sid, ObjectNode payload) {
164 pathIndex = (pathIndex <= 0 ? paths.size() - 1 : pathIndex - 1);
165 hilightAndSendPaths();
166 }
167 }
168
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800169 private final class SetModeHandler extends RequestHandler {
170 public SetModeHandler() {
171 super(PAINTER_SET_MODE);
172 }
173
174 @Override
175 public void process(long sid, ObjectNode payload) {
176 String mode = string(payload, MODE);
177 currentMode = (mode.equals("shortest") ?
178 Mode.SHORTEST : (mode.equals("disjoint") ?
179 Mode.DISJOINT : Mode.SRLG));
180 //TODO: add support for SRLG
181 if (currentMode.equals(Mode.SHORTEST)) {
182 findAndSendPaths();
183 } else {
184 findAndSendDisjointPaths();
185 }
186 }
187 }
188
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800189 // === ------------
190
191 private ElementId elementId(String id) {
192 try {
193 return DeviceId.deviceId(id);
194 } catch (IllegalArgumentException e) {
195 return HostId.hostId(id);
196 }
197 }
198
199 private void findAndSendPaths() {
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800200 if (src != null && dst != null) {
201 paths = ImmutableList.copyOf(pathService.getPaths(src, dst));
202 pathIndex = 0;
203
204 ImmutableSet.Builder<Link> builder = ImmutableSet.builder();
205 paths.forEach(path -> path.links().forEach(builder::add));
206 allPathLinks = builder.build();
207 } else {
208 paths = ImmutableList.of();
209 allPathLinks = ImmutableSet.of();
210 }
211 hilightAndSendPaths();
212 }
213
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800214 private void findAndSendDisjointPaths() {
215 log.info("src={}; dst={}; mode={}", src, dst, currentMode);
216 if (src != null && dst != null) {
217 log.info("test" + src + dst);
218 paths = null;
219 paths = new ArrayList<>();
220 pathService.getDisjointPaths(src, dst).forEach(djp -> {
221 paths.add(djp.primary());
222 paths.add(djp.backup());
223 });
224 pathIndex = 0;
225
226 ImmutableSet.Builder<Link> builder = ImmutableSet.builder();
227 paths.forEach(path -> path.links().forEach(builder::add));
228 allPathLinks = builder.build();
229 } else {
230 paths = ImmutableList.of();
231 allPathLinks = ImmutableSet.of();
232 }
233 hilightAndSendPaths();
234 }
235
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800236 private void hilightAndSendPaths() {
237 PathLinkMap linkMap = new PathLinkMap();
238 allPathLinks.forEach(linkMap::add);
239
240 // Prepare two working sets; one containing selected path links and
241 // the other containing all paths links.
Andrea Campanella8583e6b2015-12-01 21:24:45 -0800242 if (currentMode.equals(Mode.DISJOINT)) {
243 //FIXME: find a way to skip 2 paths for disjoint
244 selectedPathLinks = paths.isEmpty() ?
245 ImmutableSet.of() : Sets.newHashSet(paths.get(pathIndex * 2).links());
246 selectedPathLinks.addAll(Sets.newHashSet(paths.get(pathIndex * 2 + 1).links()));
247 } else {
248 selectedPathLinks = paths.isEmpty() ?
249 ImmutableSet.of() : Sets.newHashSet(paths.get(pathIndex).links());
250 }
Thomas Vachuskab4d3ff72015-12-01 09:53:51 -0800251 Highlights highlights = new Highlights();
252 for (PathLink plink : linkMap.biLinks()) {
253 plink.computeHilight(selectedPathLinks, allPathLinks);
254 highlights.add(plink.highlight(null));
255 }
256
257 sendMessage(TopoJson.highlightsMessage(highlights));
258 }
259
260 /*
261 private void addDeviceBadge(Highlights h, DeviceId devId, int n) {
262 DeviceHighlight dh = new DeviceHighlight(devId.toString());
263 dh.setBadge(createBadge(n));
264 h.add(dh);
265 }
266
267 private NodeBadge createBadge(int n) {
268 Status status = n > 3 ? Status.ERROR : Status.WARN;
269 String noun = n > 3 ? "(critical)" : "(problematic)";
270 String msg = "Egress links: " + n + " " + noun;
271 return NodeBadge.number(status, n, msg);
272 }
273 */
274
275}