Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.pceweb; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableSet; |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 21 | |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 22 | import org.onlab.osgi.ServiceDirectory; |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onlab.util.DataRateUnit; |
| 25 | import org.onosproject.net.Device; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 27 | import org.onosproject.net.ElementId; |
| 28 | import org.onosproject.net.HostId; |
| 29 | import org.onosproject.net.Link; |
| 30 | import org.onosproject.net.Path; |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 31 | import org.onosproject.net.topology.TopologyEvent; |
| 32 | import org.onosproject.net.topology.TopologyListener; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 33 | import org.onosproject.net.topology.TopologyService; |
| 34 | import org.onosproject.ui.RequestHandler; |
| 35 | import org.onosproject.ui.UiConnection; |
| 36 | import org.onosproject.ui.UiMessageHandler; |
| 37 | import org.onosproject.ui.topo.DeviceHighlight; |
| 38 | import org.onosproject.ui.topo.Highlights; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 39 | import org.onosproject.ui.topo.NodeBadge; |
| 40 | import org.onosproject.ui.topo.TopoJson; |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 41 | import org.onosproject.net.device.DeviceService; |
| 42 | import org.onosproject.net.intent.Constraint; |
| 43 | import org.onosproject.pce.pceservice.LspType; |
| 44 | import org.onosproject.pce.pceservice.api.PceService; |
| 45 | import org.onosproject.pce.pceservice.constraint.CostConstraint; |
| 46 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | import org.slf4j.LoggerFactory; |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 49 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 50 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 51 | import org.onosproject.incubator.net.tunnel.TunnelEndPoint; |
| 52 | import org.onosproject.incubator.net.tunnel.TunnelEvent; |
| 53 | import org.onosproject.incubator.net.tunnel.TunnelId; |
| 54 | import org.onosproject.incubator.net.tunnel.TunnelListener; |
| 55 | import org.onosproject.incubator.net.tunnel.TunnelService; |
| 56 | import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS; |
| 57 | |
| 58 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 59 | |
| 60 | import java.util.Collection; |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 61 | import java.util.LinkedList; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 62 | import java.util.List; |
| 63 | import java.util.Set; |
| 64 | |
| 65 | /** |
| 66 | * ONOS UI PCE WEB Topology-Overlay message handler. |
| 67 | */ |
| 68 | public class PceWebTopovMessageHandler extends UiMessageHandler { |
| 69 | |
| 70 | private static final String PCEWEB_CLEAR = "pceTopovClear"; |
| 71 | private static final String PCEWEB_SET_SRC = "pceTopovSetSrc"; |
| 72 | private static final String PCEWEB_SET_DST = "pceTopovSetDst"; |
| 73 | private static final String PCEWEB_SET_PATH = "pceTopovSetMode"; |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 74 | private static final String PCEWEB_UPDATE_PATH_QUERY = "pceTopovUpdateQuery"; |
| 75 | private static final String PCEWEB_UPDATE_PATH = "pceTopovUpdate"; |
| 76 | private static final String PCEWEB_REMOVE_PATH_QUERY = "pceTopovRemQuery"; |
| 77 | private static final String PCEWEB_REMOVE_PATH = "pceTopovRem"; |
| 78 | private static final String PCEWEB_QUERY_TUNNELS = "pceTopovTunnelDisplay"; |
| 79 | private static final String PCEWEB_SHOW_TUNNEL = "pceTopovShowTunnels"; |
| 80 | private static final String PCEWEB_SHOW_TUNNEL_REMOVE = "pceTopovShowTunnelsRem"; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 81 | private static final String ID = "id"; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 82 | private static final String TYPE = "type"; |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 83 | private static final String ROUTER = "router"; |
| 84 | private static final String DST = "Egress"; |
| 85 | private static final String SRC = "Ingress"; |
| 86 | private static final String BANDWIDTH = "bw"; |
| 87 | private static final String BANDWIDTHTYPE = "bwtype"; |
| 88 | private static final String COSTTYPE = "ctype"; |
| 89 | private static final String LSPTYPE = "lsptype"; |
| 90 | private static final String SRCID = "srid"; |
| 91 | private static final String DSTID = "dsid"; |
| 92 | private static final String TUNNEL_ID = "tunnelid"; |
| 93 | private static final String TUNNEL_NAME = "tunnelname"; |
| 94 | private static final String COST_TYPE_IGP = "igp"; |
| 95 | private static final String COST_TYPE_TE = "te"; |
| 96 | private static final String BANDWIDTH_TYPE_KBPS = "kbps"; |
| 97 | private static final String BUFFER_ARRAY = "a"; |
| 98 | private static final String BANDWIDTH_BPS = "BPS"; |
| 99 | private static final String LSP_TYPE_CR = "cr"; |
| 100 | private static final String LSP_TYPE_SRBE = "srbe"; |
| 101 | private static final String LSP_TYPE_SRTE = "srte"; |
| 102 | private static final String STRING_NULL = "null"; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 103 | // Delay for showHighlights event processing on GUI client side to |
| 104 | // account for addLink animation. |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 105 | private static final int DELAY_MS = 1100; |
| 106 | private static final double BANDWIDTH_KBPS = 1_000; |
| 107 | private static final double BANDWIDTH_MBPS = 1_000_000; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 108 | |
| 109 | private Set<Link> allPathLinks; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 110 | private int highlightDelay; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 111 | private ElementId src, dst; |
| 112 | private String srcType, dstType; |
| 113 | private List<Path> paths; |
| 114 | private int pathIndex; |
| 115 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 116 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 117 | private final TopologyListener topologyListener = new InternalTopologyListener(); |
| 118 | private final TunnelListener tunnelListener = new InnerPceWebTunnelListener(); |
| 119 | |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 120 | protected TopologyService topologyService; |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 121 | protected TunnelService tunnelService; |
| 122 | protected PceService pceService; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 123 | protected DeviceService deviceService; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 124 | @Override |
| 125 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 126 | |
| 127 | super.init(connection, directory); |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 128 | topologyService = directory.get(TopologyService.class); |
| 129 | tunnelService = directory.get(TunnelService.class); |
| 130 | pceService = directory.get(PceService.class); |
| 131 | deviceService = directory.get(DeviceService.class); |
| 132 | topologyService.addListener(topologyListener); |
| 133 | tunnelService.addListener(tunnelListener); |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | @Override |
| 137 | protected Collection<RequestHandler> createRequestHandlers() { |
| 138 | return ImmutableSet.of( |
| 139 | new ClearHandler(), |
| 140 | new SetSrcHandler(), |
| 141 | new SetDstHandler(), |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 142 | new SetPathHandler(), |
| 143 | new UpdatePathQueryHandler(), |
| 144 | new UpdatePathHandler(), |
| 145 | new RemovePathQueryHandler(), |
| 146 | new RemovePathHandler(), |
| 147 | new ShowTunnelHandler()); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void destroy() { |
| 152 | topologyService.removeListener(topologyListener); |
| 153 | tunnelService.removeListener(tunnelListener); |
| 154 | super.destroy(); |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Handler classes |
| 158 | /** |
| 159 | * Handles the 'clear' event received from the client. |
| 160 | */ |
| 161 | private final class ClearHandler extends RequestHandler { |
| 162 | |
| 163 | public ClearHandler() { |
| 164 | super(PCEWEB_CLEAR); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void process(long sid, ObjectNode payload) { |
| 169 | src = null; |
| 170 | dst = null; |
| 171 | sendMessage(TopoJson.highlightsMessage(new Highlights())); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Handles the 'set source' event received from the client. |
| 177 | */ |
| 178 | private final class SetSrcHandler extends RequestHandler { |
| 179 | |
| 180 | public SetSrcHandler() { |
| 181 | super(PCEWEB_SET_SRC); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void process(long sid, ObjectNode payload) { |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 186 | String id = string(payload, ID); |
| 187 | src = elementId(id); |
| 188 | srcType = string(payload, TYPE); |
| 189 | if (src.equals(dst)) { |
| 190 | dst = null; |
| 191 | } |
| 192 | sendMessage(TopoJson.highlightsMessage(addBadge(new Highlights(), |
| 193 | srcType, src.toString(), SRC))); |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Handles the 'set destination' event received from the client. |
| 199 | */ |
| 200 | private final class SetDstHandler extends RequestHandler { |
| 201 | |
| 202 | public SetDstHandler() { |
| 203 | super(PCEWEB_SET_DST); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void process(long sid, ObjectNode payload) { |
| 208 | String id = string(payload, ID); |
| 209 | dst = elementId(id); |
| 210 | dstType = string(payload, TYPE); |
| 211 | if (src.equals(dst)) { |
| 212 | src = null; |
| 213 | } |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 214 | sendMessage(TopoJson.highlightsMessage(addBadge(new Highlights(), |
| 215 | dstType, dst.toString(), DST))); |
| 216 | |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | /** |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 221 | * Handles the 'path calculation' event received from the client. |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 222 | */ |
| 223 | private final class SetPathHandler extends RequestHandler { |
| 224 | |
| 225 | public SetPathHandler() { |
| 226 | super(PCEWEB_SET_PATH); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public void process(long sid, ObjectNode payload) { |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 231 | String bandWidth = string(payload, BANDWIDTH); |
| 232 | String bandWidthType = string(payload, BANDWIDTHTYPE); |
| 233 | String costType = string(payload, COSTTYPE); |
| 234 | String lspType = string(payload, LSPTYPE); |
| 235 | String tunnelName = string(payload, TUNNEL_NAME); |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 236 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 237 | if (tunnelName.equals(STRING_NULL)) { |
| 238 | log.error("tunnel name should not be empty"); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | if (pceService == null) { |
| 243 | log.error("PCE service is not active"); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | if (lspType == null) { |
| 248 | log.error("PCE setup path is failed."); |
| 249 | } |
| 250 | |
| 251 | if ((src != null) && (dst != null)) { |
| 252 | findAndSendPaths(src, dst, bandWidth, bandWidthType, costType, lspType, tunnelName); |
| 253 | } |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 257 | /** |
| 258 | * Handles the 'update path query' event received from the client. |
| 259 | */ |
| 260 | private final class UpdatePathQueryHandler extends RequestHandler { |
| 261 | |
| 262 | public UpdatePathQueryHandler() { |
| 263 | super(PCEWEB_UPDATE_PATH_QUERY); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void process(long sid, ObjectNode payload) { |
| 268 | String srcId = string(payload, SRCID); |
| 269 | ElementId src = elementId(srcId); |
| 270 | String dstId = string(payload, DSTID); |
| 271 | ElementId dst = elementId(dstId); |
| 272 | Device srcDevice = deviceService.getDevice((DeviceId) src); |
| 273 | Device dstDevice = deviceService.getDevice((DeviceId) dst); |
| 274 | |
| 275 | TunnelEndPoint tunSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 276 | .valueOf(srcDevice.annotations().value("lsrId"))); |
| 277 | TunnelEndPoint tunDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 278 | .valueOf(dstDevice.annotations().value("lsrId"))); |
| 279 | |
| 280 | Collection<Tunnel> tunnelSet = tunnelService.queryTunnel(tunSrc, tunDst); |
Mahesh Raju-Huawei | 2cfa535 | 2016-05-27 20:09:51 +0530 | [diff] [blame] | 281 | ObjectNode result = objectNode(); |
| 282 | ArrayNode arrayNode = arrayNode(); |
| 283 | for (Tunnel tunnel : tunnelSet) { |
| 284 | if (tunnel.type() == MPLS) { |
| 285 | arrayNode.add(tunnel.tunnelId().toString()); |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 286 | } |
Mahesh Raju-Huawei | 2cfa535 | 2016-05-27 20:09:51 +0530 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | result.putArray(BUFFER_ARRAY).addAll(arrayNode); |
| 290 | sendMessage(PCEWEB_SHOW_TUNNEL, sid, result); |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Handles the 'update path' event received from the client. |
| 296 | */ |
| 297 | private final class UpdatePathHandler extends RequestHandler { |
| 298 | |
| 299 | public UpdatePathHandler() { |
| 300 | super(PCEWEB_UPDATE_PATH); |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void process(long sid, ObjectNode payload) { |
| 305 | String bandWidth = string(payload, BANDWIDTH); |
| 306 | String bandWidthType = string(payload, BANDWIDTHTYPE); |
| 307 | String costType = string(payload, COSTTYPE); |
| 308 | String tunnelId = string(payload, TUNNEL_ID); |
| 309 | |
| 310 | if (tunnelId == null) { |
| 311 | log.error("PCE update path is failed."); |
| 312 | } |
| 313 | |
| 314 | findAndSendPathsUpdate(bandWidth, bandWidthType, costType, tunnelId); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Handles the 'remove path query' event received from the client. |
| 320 | */ |
| 321 | private final class RemovePathQueryHandler extends RequestHandler { |
| 322 | |
| 323 | public RemovePathQueryHandler() { |
| 324 | super(PCEWEB_REMOVE_PATH_QUERY); |
| 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public void process(long sid, ObjectNode payload) { |
| 329 | String srcId = string(payload, SRCID); |
| 330 | ElementId src = elementId(srcId); |
| 331 | String dstId = string(payload, DSTID); |
| 332 | ElementId dst = elementId(dstId); |
| 333 | |
| 334 | Device srcDevice = deviceService.getDevice((DeviceId) src); |
| 335 | Device dstDevice = deviceService.getDevice((DeviceId) dst); |
| 336 | |
| 337 | TunnelEndPoint tunSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 338 | .valueOf(srcDevice.annotations().value("lsrId"))); |
| 339 | TunnelEndPoint tunDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress |
| 340 | .valueOf(dstDevice.annotations().value("lsrId"))); |
| 341 | |
| 342 | Collection<Tunnel> tunnelSet = tunnelService.queryTunnel(tunSrc, tunDst); |
Mahesh Raju-Huawei | 2cfa535 | 2016-05-27 20:09:51 +0530 | [diff] [blame] | 343 | ObjectNode result = objectNode(); |
| 344 | ArrayNode arrayNode = arrayNode(); |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 345 | |
Mahesh Raju-Huawei | 2cfa535 | 2016-05-27 20:09:51 +0530 | [diff] [blame] | 346 | for (Tunnel tunnel : tunnelSet) { |
| 347 | if (tunnel.type() == MPLS) { |
| 348 | arrayNode.add(tunnel.tunnelId().toString()); |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 349 | } |
Mahesh Raju-Huawei | 2cfa535 | 2016-05-27 20:09:51 +0530 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | result.putArray(BUFFER_ARRAY).addAll(arrayNode); |
| 353 | sendMessage(PCEWEB_SHOW_TUNNEL_REMOVE, sid, result); |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Handles the 'remove path' event received from the client. |
| 359 | */ |
| 360 | private final class RemovePathHandler extends RequestHandler { |
| 361 | |
| 362 | public RemovePathHandler() { |
| 363 | super(PCEWEB_REMOVE_PATH); |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public void process(long sid, ObjectNode payload) { |
| 368 | String tunnelId = string(payload, TUNNEL_ID); |
| 369 | |
| 370 | if (tunnelId == null) { |
| 371 | log.error("PCE update path is failed."); |
| 372 | } |
| 373 | |
| 374 | findAndSendPathsRemove(tunnelId); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Handles the 'show the existed tunnels' event received from the client. |
| 380 | */ |
| 381 | private final class ShowTunnelHandler extends RequestHandler { |
| 382 | |
| 383 | public ShowTunnelHandler() { |
| 384 | super(PCEWEB_QUERY_TUNNELS); |
| 385 | } |
| 386 | |
| 387 | @Override |
| 388 | public void process(long sid, ObjectNode payload) { |
| 389 | findTunnelAndHighlights(); |
| 390 | } |
| 391 | } |
| 392 | |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 393 | /** |
| 394 | * provides the element id. |
| 395 | */ |
| 396 | private ElementId elementId(String id) { |
| 397 | try { |
| 398 | return DeviceId.deviceId(id); |
| 399 | } catch (IllegalArgumentException e) { |
| 400 | return HostId.hostId(id); |
| 401 | } |
| 402 | } |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 403 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 404 | /** |
| 405 | * Handles the setup path and highlights the path. |
| 406 | * |
| 407 | * @param bandWidth |
| 408 | * @param bandWidthType is the kbps or mbps |
| 409 | * @param costType is igp or te |
| 410 | * @param lspType is WITH_SIGNALLING,WITHOUT_SIGNALLING_AND_WITHOUT_SR or SR_WITHOUT_SIGNALLING |
| 411 | * @param tunnelName tunnel id |
| 412 | */ |
| 413 | private void findAndSendPaths(ElementId src, ElementId dst, String bandWidth, String bandWidthType, |
| 414 | String costType, String lspType, String tunnelName) { |
| 415 | log.debug("src={}; dst={};", src, dst); |
| 416 | boolean path; |
| 417 | List<Constraint> listConstrnt; |
| 418 | |
| 419 | listConstrnt = addBandwidthCostTypeConstraints(bandWidth, bandWidthType, costType); |
| 420 | |
| 421 | //LSP type |
| 422 | LspType lspTypeVal = null; |
| 423 | switch (lspType) { |
| 424 | case LSP_TYPE_CR: |
| 425 | lspTypeVal = LspType.WITH_SIGNALLING; |
| 426 | break; |
| 427 | case LSP_TYPE_SRBE: |
| 428 | lspTypeVal = LspType.WITHOUT_SIGNALLING_AND_WITHOUT_SR; |
| 429 | break; |
| 430 | case LSP_TYPE_SRTE: |
| 431 | lspTypeVal = LspType.SR_WITHOUT_SIGNALLING; |
| 432 | break; |
| 433 | default: |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 434 | break; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 435 | } |
| 436 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 437 | path = pceService.setupPath((DeviceId) src, (DeviceId) dst, tunnelName, listConstrnt, lspTypeVal); |
| 438 | if (!path) { |
| 439 | log.error("setup path is failed"); |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | return; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 444 | } |
| 445 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 446 | /** |
| 447 | * Handles the update path and highlights the path. |
| 448 | * |
| 449 | * @param bandWidth bandWidth |
| 450 | * @param bandWidthType is the kbps or mbps |
| 451 | * @param costType is igp or te |
| 452 | * @param tunnelName tunnel id |
| 453 | */ |
| 454 | private void findAndSendPathsUpdate(String bandWidth, String bandWidthType, String costType, String tunnelIdStr) { |
| 455 | if (tunnelIdStr != null) { |
| 456 | List<Constraint> listConstrnt; |
| 457 | |
| 458 | if (tunnelIdStr.equals(STRING_NULL)) { |
| 459 | log.error("update path is failed"); |
| 460 | return; |
| 461 | } |
| 462 | |
| 463 | if (pceService == null) { |
| 464 | log.error("PCE service is not active"); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | listConstrnt = addBandwidthCostTypeConstraints(bandWidth, bandWidthType, costType); |
| 469 | TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr); |
| 470 | boolean path = pceService.updatePath(tunnelId, listConstrnt); |
| 471 | |
| 472 | if (!path) { |
| 473 | log.error("update path is failed"); |
| 474 | return; |
| 475 | } |
| 476 | } |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Handles the remove path and highlights the paths if existed. |
| 482 | * |
| 483 | * @param tunnelIdName tunnelId |
| 484 | */ |
| 485 | private void findAndSendPathsRemove(String tunnelIdStr) { |
| 486 | if (tunnelIdStr != null) { |
| 487 | if (pceService == null) { |
| 488 | log.error("PCE service is not active"); |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr); |
| 493 | boolean path = pceService.releasePath(tunnelId); |
| 494 | if (!path) { |
| 495 | log.error("remove path is failed"); |
| 496 | return; |
| 497 | } |
| 498 | } |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | private ImmutableSet.Builder<Link> buildPaths(ImmutableSet.Builder<Link> pathBuilder) { |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 503 | paths.forEach(path -> path.links().forEach(pathBuilder::add)); |
| 504 | return pathBuilder; |
| 505 | } |
| 506 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 507 | /** |
| 508 | * Handles the preparation of constraints list with given bandwidth and cost-type. |
| 509 | * |
| 510 | * @param bandWidth bandWidth |
| 511 | * @param bandWidthType is the kbps or mbps |
| 512 | * @param costType is igp or te |
| 513 | * @param listConstrnt list of constraints |
| 514 | * @return |
| 515 | */ |
| 516 | private List<Constraint> addBandwidthCostTypeConstraints(String bandWidth, |
| 517 | String bandWidthType, |
| 518 | String costType) { |
| 519 | List<Constraint> listConstrnt = new LinkedList<>(); |
| 520 | //bandwidth |
| 521 | double bwValue = 0.0; |
| 522 | if (!bandWidth.equals(STRING_NULL)) { |
| 523 | bwValue = Double.parseDouble(bandWidth); |
| 524 | } |
| 525 | if (bandWidthType.equals(BANDWIDTH_TYPE_KBPS)) { |
| 526 | bwValue = bwValue * BANDWIDTH_KBPS; |
| 527 | } else { |
| 528 | bwValue = bwValue * BANDWIDTH_MBPS; |
| 529 | } |
| 530 | |
| 531 | //Cost type |
| 532 | CostConstraint.Type costTypeVal = null; |
| 533 | switch (costType) { |
| 534 | case COST_TYPE_IGP: |
| 535 | costTypeVal = CostConstraint.Type.COST; |
| 536 | break; |
| 537 | case COST_TYPE_TE: |
| 538 | costTypeVal = CostConstraint.Type.TE_COST; |
| 539 | break; |
| 540 | default: |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 541 | break; |
| 542 | } |
| 543 | |
| 544 | if (bwValue != 0.0) { |
| 545 | listConstrnt.add(BandwidthConstraint.of(bwValue, DataRateUnit.valueOf(BANDWIDTH_BPS))); |
| 546 | } |
| 547 | |
| 548 | if (costTypeVal != null) { |
| 549 | listConstrnt.add(CostConstraint.of(costTypeVal)); |
| 550 | } |
| 551 | |
| 552 | return listConstrnt; |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 553 | } |
| 554 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 555 | /** |
| 556 | * Handles the highlights of selected path. |
| 557 | */ |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 558 | private void hilightAndSendPaths() { |
| 559 | PceWebLinkMap linkMap = new PceWebLinkMap(); |
| 560 | allPathLinks.forEach(linkMap::add); |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 561 | Set<Link> selectedPathLinks; |
| 562 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 563 | selectedPathLinks = paths.isEmpty() ? |
| 564 | ImmutableSet.of() : ImmutableSet.copyOf(paths.get(pathIndex).links()); |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 565 | |
| 566 | Highlights highlights = new Highlights(); |
| 567 | if (highlightDelay > 0) { |
| 568 | highlights.delay(highlightDelay); |
| 569 | } |
| 570 | for (PceWebLink plink : linkMap.biLinks()) { |
| 571 | plink.computeHilight(selectedPathLinks, allPathLinks); |
| 572 | highlights.add(plink.highlight(null)); |
| 573 | } |
| 574 | if (src != null) { |
| 575 | highlights = addBadge(highlights, srcType, src.toString(), SRC); |
| 576 | } |
| 577 | if (dst != null) { |
| 578 | highlights = addBadge(highlights, dstType, dst.toString(), DST); |
| 579 | } |
| 580 | sendMessage(TopoJson.highlightsMessage(highlights)); |
| 581 | } |
| 582 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 583 | /** |
| 584 | * Handles the addition of badge and highlights. |
| 585 | * |
| 586 | * @param highlights highlights |
| 587 | * @param type device type |
| 588 | * @param elemId device to be add badge |
| 589 | * @param src device to be add badge |
| 590 | * @return |
| 591 | */ |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 592 | private Highlights addBadge(Highlights highlights, String type, |
| 593 | String elemId, String src) { |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 594 | if (ROUTER.equals(type)) { |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 595 | highlights = addDeviceBadge(highlights, elemId, src); |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 596 | } |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 597 | |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 598 | return highlights; |
| 599 | } |
| 600 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 601 | /** |
| 602 | * Handles the badge add and highlights. |
| 603 | * |
| 604 | * @param h highlights |
| 605 | * @param elemId device to be add badge |
| 606 | * @param type device type |
| 607 | * @return highlights |
| 608 | */ |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 609 | private Highlights addDeviceBadge(Highlights h, String elemId, String type) { |
| 610 | DeviceHighlight dh = new DeviceHighlight(elemId); |
| 611 | dh.setBadge(createBadge(type)); |
| 612 | h.add(dh); |
| 613 | return h; |
| 614 | } |
| 615 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 616 | /** |
| 617 | * Handles the node badge add and highlights. |
| 618 | * |
| 619 | * @param type device type |
| 620 | * @return badge of given node |
| 621 | */ |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 622 | private NodeBadge createBadge(String type) { |
| 623 | return NodeBadge.text(type); |
| 624 | } |
| 625 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 626 | /** |
| 627 | * Handles the event of topology listeners. |
| 628 | */ |
| 629 | private class InternalTopologyListener implements TopologyListener { |
| 630 | @Override |
| 631 | public void event(TopologyEvent event) { |
| 632 | highlightDelay = DELAY_MS; |
| 633 | findTunnelAndHighlights(); |
| 634 | highlightDelay = 0; |
| 635 | } |
| 636 | } |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 637 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 638 | /** |
| 639 | * Handles the event of tunnel listeners. |
| 640 | */ |
| 641 | private class InnerPceWebTunnelListener implements TunnelListener { |
| 642 | @Override |
| 643 | public void event(TunnelEvent event) { |
| 644 | Tunnel tunnel = event.subject(); |
| 645 | if (tunnel.type() == MPLS) { |
| 646 | highlightDelay = DELAY_MS; |
| 647 | findTunnelAndHighlights(); |
| 648 | highlightDelay = 0; |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Handles the event of topology listeners. |
| 655 | */ |
| 656 | private void findTunnelAndHighlights() { |
| 657 | Collection<Tunnel> tunnelSet = null; |
| 658 | tunnelSet = tunnelService.queryTunnel(MPLS); |
| 659 | for (Tunnel tunnel : tunnelSet) { |
| 660 | if (tunnel.path() == null) { |
Mahesh Raju-Huawei | 31d31c0 | 2016-06-04 17:20:57 +0530 | [diff] [blame^] | 661 | log.error("path does not exist"); |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 662 | return; |
| 663 | } |
| 664 | paths.add(tunnel.path()); |
| 665 | } |
| 666 | |
Mahesh Raju-Huawei | 31d31c0 | 2016-06-04 17:20:57 +0530 | [diff] [blame^] | 667 | if (tunnelSet.size() == 0) { |
| 668 | log.warn("Tunnel does not exist"); |
| 669 | return; |
| 670 | } |
| 671 | |
Mahesh Raju-Huawei | 5b0453a | 2016-05-27 11:54:27 +0530 | [diff] [blame] | 672 | ImmutableSet.Builder<Link> builder = ImmutableSet.builder(); |
| 673 | allPathLinks = buildPaths(builder).build(); |
| 674 | hilightAndSendPaths(); |
| 675 | } |
Mahesh Raju-Huawei | 8593005 | 2016-04-26 21:09:57 +0530 | [diff] [blame] | 676 | } |