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