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