Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.pathpainter; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.onlab.osgi.ServiceDirectory; |
| 22 | import org.onosproject.net.DeviceId; |
| 23 | import org.onosproject.net.ElementId; |
| 24 | import org.onosproject.net.HostId; |
| 25 | import org.onosproject.net.Link; |
| 26 | import org.onosproject.net.Path; |
| 27 | import org.onosproject.net.topology.PathService; |
| 28 | import org.onosproject.ui.RequestHandler; |
| 29 | import org.onosproject.ui.UiConnection; |
| 30 | import org.onosproject.ui.UiMessageHandler; |
| 31 | import org.onosproject.ui.topo.Highlights; |
| 32 | import org.onosproject.ui.topo.TopoJson; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.util.Collection; |
| 37 | import java.util.List; |
| 38 | import java.util.Set; |
| 39 | |
| 40 | /** |
| 41 | * Skeletal ONOS UI Topology-Overlay message handler. |
| 42 | */ |
| 43 | public class PathPainterTopovMessageHandler extends UiMessageHandler { |
| 44 | |
| 45 | private static final String PAINTER_SET_SRC = "ppTopovSetSrc"; |
| 46 | private static final String PAINTER_SET_DST = "ppTopovSetDst"; |
| 47 | private static final String PAINTER_SWAP_SRC_DST = "ppTopovSwapSrcDst"; |
| 48 | private static final String PAINTER_SET_MODE = "ppTopovSetMode"; |
| 49 | |
| 50 | private static final String PAINTER_NEXT_PATH = "ppTopovNextPath"; |
| 51 | private static final String PAINTER_PREV_PATH = "ppTopovPrevPath"; |
| 52 | |
| 53 | private static final String ID = "id"; |
| 54 | private static final String MODE = "mode"; |
| 55 | |
| 56 | private Set<Link> allPathLinks; |
| 57 | |
| 58 | private enum Mode { |
| 59 | SHORTEST, DISJOINT, SRLG |
| 60 | } |
| 61 | |
| 62 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 63 | |
| 64 | private PathService pathService; |
| 65 | |
| 66 | private Mode currentMode = Mode.SHORTEST; |
| 67 | private ElementId src, dst; |
| 68 | private Mode mode = Mode.SHORTEST; |
| 69 | private List<Path> paths; |
| 70 | private int pathIndex; |
| 71 | |
| 72 | |
| 73 | // ===============-=-=-=-=-=-======================-=-=-=-=-=-=-================================ |
| 74 | |
| 75 | |
| 76 | @Override |
| 77 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 78 | super.init(connection, directory); |
| 79 | pathService = directory.get(PathService.class); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected Collection<RequestHandler> createRequestHandlers() { |
| 84 | return ImmutableSet.of( |
| 85 | new SetSrcHandler(), |
| 86 | new SetDstHandler(), |
Andrea Campanella | 0c17a0a | 2015-12-01 09:53:51 -0800 | [diff] [blame^] | 87 | new SwapSrcDstHandler(), |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 88 | new NextPathHandler(), |
| 89 | new PrevPathHandler() |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | // === ------------------------- |
| 94 | // === Handler classes |
| 95 | |
| 96 | private final class SetSrcHandler extends RequestHandler { |
| 97 | public SetSrcHandler() { |
| 98 | super(PAINTER_SET_SRC); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public void process(long sid, ObjectNode payload) { |
| 103 | String id = string(payload, ID); |
| 104 | src = elementId(id); |
| 105 | if (src.equals(dst)) { |
| 106 | dst = null; |
| 107 | } |
| 108 | findAndSendPaths(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | private final class SetDstHandler extends RequestHandler { |
| 113 | public SetDstHandler() { |
| 114 | super(PAINTER_SET_DST); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void process(long sid, ObjectNode payload) { |
| 119 | String id = string(payload, ID); |
| 120 | dst = elementId(id); |
| 121 | if (src.equals(dst)) { |
| 122 | src = null; |
| 123 | } |
| 124 | findAndSendPaths(); |
| 125 | } |
| 126 | } |
| 127 | |
Andrea Campanella | 0c17a0a | 2015-12-01 09:53:51 -0800 | [diff] [blame^] | 128 | private final class SwapSrcDstHandler extends RequestHandler { |
| 129 | public SwapSrcDstHandler() { |
| 130 | super(PAINTER_SWAP_SRC_DST); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void process(long sid, ObjectNode payload) { |
| 135 | ElementId temp = src; |
| 136 | src = dst; |
| 137 | dst = temp; |
| 138 | findAndSendPaths(); |
| 139 | } |
| 140 | } |
| 141 | |
Thomas Vachuska | b4d3ff7 | 2015-12-01 09:53:51 -0800 | [diff] [blame] | 142 | private final class NextPathHandler extends RequestHandler { |
| 143 | public NextPathHandler() { |
| 144 | super(PAINTER_NEXT_PATH); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void process(long sid, ObjectNode payload) { |
| 149 | pathIndex = (pathIndex >= paths.size() - 1 ? 0 : pathIndex + 1); |
| 150 | hilightAndSendPaths(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | private final class PrevPathHandler extends RequestHandler { |
| 155 | public PrevPathHandler() { |
| 156 | super(PAINTER_PREV_PATH); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void process(long sid, ObjectNode payload) { |
| 161 | pathIndex = (pathIndex <= 0 ? paths.size() - 1 : pathIndex - 1); |
| 162 | hilightAndSendPaths(); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // === ------------ |
| 167 | |
| 168 | private ElementId elementId(String id) { |
| 169 | try { |
| 170 | return DeviceId.deviceId(id); |
| 171 | } catch (IllegalArgumentException e) { |
| 172 | return HostId.hostId(id); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | private void findAndSendPaths() { |
| 177 | log.info("src={}; dst={}; mode={}", src, dst, mode); |
| 178 | if (src != null && dst != null) { |
| 179 | paths = ImmutableList.copyOf(pathService.getPaths(src, dst)); |
| 180 | pathIndex = 0; |
| 181 | |
| 182 | ImmutableSet.Builder<Link> builder = ImmutableSet.builder(); |
| 183 | paths.forEach(path -> path.links().forEach(builder::add)); |
| 184 | allPathLinks = builder.build(); |
| 185 | } else { |
| 186 | paths = ImmutableList.of(); |
| 187 | allPathLinks = ImmutableSet.of(); |
| 188 | } |
| 189 | hilightAndSendPaths(); |
| 190 | } |
| 191 | |
| 192 | private void hilightAndSendPaths() { |
| 193 | PathLinkMap linkMap = new PathLinkMap(); |
| 194 | allPathLinks.forEach(linkMap::add); |
| 195 | |
| 196 | // Prepare two working sets; one containing selected path links and |
| 197 | // the other containing all paths links. |
| 198 | Set<Link> selectedPathLinks = paths.isEmpty() ? |
| 199 | ImmutableSet.of() : ImmutableSet.copyOf(paths.get(pathIndex).links()); |
| 200 | |
| 201 | Highlights highlights = new Highlights(); |
| 202 | for (PathLink plink : linkMap.biLinks()) { |
| 203 | plink.computeHilight(selectedPathLinks, allPathLinks); |
| 204 | highlights.add(plink.highlight(null)); |
| 205 | } |
| 206 | |
| 207 | sendMessage(TopoJson.highlightsMessage(highlights)); |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | private void addDeviceBadge(Highlights h, DeviceId devId, int n) { |
| 212 | DeviceHighlight dh = new DeviceHighlight(devId.toString()); |
| 213 | dh.setBadge(createBadge(n)); |
| 214 | h.add(dh); |
| 215 | } |
| 216 | |
| 217 | private NodeBadge createBadge(int n) { |
| 218 | Status status = n > 3 ? Status.ERROR : Status.WARN; |
| 219 | String noun = n > 3 ? "(critical)" : "(problematic)"; |
| 220 | String msg = "Egress links: " + n + " " + noun; |
| 221 | return NodeBadge.number(status, n, msg); |
| 222 | } |
| 223 | */ |
| 224 | |
| 225 | } |