blob: 95e9f1fcad4e0ac3cb52f2335088567bd2defec4 [file] [log] [blame]
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +05301/*
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
17package org.onosproject.pceweb;
18
19import com.fasterxml.jackson.databind.node.ObjectNode;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053020import com.google.common.collect.ImmutableSet;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053021
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053022import org.onlab.osgi.ServiceDirectory;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053023import org.onlab.packet.IpAddress;
24import org.onlab.util.DataRateUnit;
25import org.onosproject.net.Device;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053026import org.onosproject.net.DeviceId;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053027import org.onosproject.net.ElementId;
28import org.onosproject.net.HostId;
29import org.onosproject.net.Link;
30import org.onosproject.net.Path;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053031import org.onosproject.net.topology.TopologyEvent;
32import org.onosproject.net.topology.TopologyListener;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053033import org.onosproject.net.topology.TopologyService;
34import org.onosproject.ui.RequestHandler;
35import org.onosproject.ui.UiConnection;
36import org.onosproject.ui.UiMessageHandler;
37import org.onosproject.ui.topo.DeviceHighlight;
38import org.onosproject.ui.topo.Highlights;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053039import org.onosproject.ui.topo.NodeBadge;
40import org.onosproject.ui.topo.TopoJson;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053041import org.onosproject.net.device.DeviceService;
42import org.onosproject.net.intent.Constraint;
43import org.onosproject.pce.pceservice.LspType;
44import org.onosproject.pce.pceservice.api.PceService;
45import org.onosproject.pce.pceservice.constraint.CostConstraint;
46import org.onosproject.net.intent.constraint.BandwidthConstraint;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053047import org.slf4j.Logger;
48import org.slf4j.LoggerFactory;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053049import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
50import org.onosproject.incubator.net.tunnel.Tunnel;
51import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
52import org.onosproject.incubator.net.tunnel.TunnelEvent;
53import org.onosproject.incubator.net.tunnel.TunnelId;
54import org.onosproject.incubator.net.tunnel.TunnelListener;
55import org.onosproject.incubator.net.tunnel.TunnelService;
56import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
57
58import com.fasterxml.jackson.databind.node.ArrayNode;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053059
60import java.util.Collection;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053061import java.util.LinkedList;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053062import java.util.List;
63import java.util.Set;
64
65/**
66 * ONOS UI PCE WEB Topology-Overlay message handler.
67 */
68public 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-Huawei5b0453a2016-05-27 11:54:27 +053074 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-Huawei85930052016-04-26 21:09:57 +053081 private static final String ID = "id";
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053082 private static final String TYPE = "type";
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053083 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-Huawei85930052016-04-26 21:09:57 +0530103 // Delay for showHighlights event processing on GUI client side to
104 // account for addLink animation.
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530105 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-Huawei85930052016-04-26 21:09:57 +0530108
109 private Set<Link> allPathLinks;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530110 private int highlightDelay;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530111 private ElementId src, dst;
112 private String srcType, dstType;
113 private List<Path> paths;
114 private int pathIndex;
115
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530116 private final Logger log = LoggerFactory.getLogger(getClass());
117 private final TopologyListener topologyListener = new InternalTopologyListener();
118 private final TunnelListener tunnelListener = new InnerPceWebTunnelListener();
119
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530120 protected TopologyService topologyService;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530121 protected TunnelService tunnelService;
122 protected PceService pceService;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530123 protected DeviceService deviceService;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530124 @Override
125 public void init(UiConnection connection, ServiceDirectory directory) {
126
127 super.init(connection, directory);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530128 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-Huawei85930052016-04-26 21:09:57 +0530134 }
135
136 @Override
137 protected Collection<RequestHandler> createRequestHandlers() {
138 return ImmutableSet.of(
139 new ClearHandler(),
140 new SetSrcHandler(),
141 new SetDstHandler(),
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530142 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-Huawei85930052016-04-26 21:09:57 +0530155 }
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-Huawei85930052016-04-26 21:09:57 +0530186 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-Huawei85930052016-04-26 21:09:57 +0530194 }
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-Huawei85930052016-04-26 21:09:57 +0530214 sendMessage(TopoJson.highlightsMessage(addBadge(new Highlights(),
215 dstType, dst.toString(), DST)));
216
217 }
218 }
219
220 /**
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530221 * Handles the 'path calculation' event received from the client.
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530222 */
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-Huawei5b0453a2016-05-27 11:54:27 +0530231 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-Huawei85930052016-04-26 21:09:57 +0530236
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530237 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-Huawei85930052016-04-26 21:09:57 +0530254 }
255 }
256
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530257 /**
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-Huawei2cfa5352016-05-27 20:09:51 +0530281 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-Huawei5b0453a2016-05-27 11:54:27 +0530286 }
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530287 }
288
289 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
290 sendMessage(PCEWEB_SHOW_TUNNEL, sid, result);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530291 }
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-Huawei2cfa5352016-05-27 20:09:51 +0530343 ObjectNode result = objectNode();
344 ArrayNode arrayNode = arrayNode();
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530345
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530346 for (Tunnel tunnel : tunnelSet) {
347 if (tunnel.type() == MPLS) {
348 arrayNode.add(tunnel.tunnelId().toString());
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530349 }
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530350 }
351
352 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
353 sendMessage(PCEWEB_SHOW_TUNNEL_REMOVE, sid, result);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530354 }
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-Huawei85930052016-04-26 21:09:57 +0530393 /**
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-Huawei85930052016-04-26 21:09:57 +0530403
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530404 /**
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:
434 log.error("Invalid LSP type");
435 break;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530436 }
437
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530438 path = pceService.setupPath((DeviceId) src, (DeviceId) dst, tunnelName, listConstrnt, lspTypeVal);
439 if (!path) {
440 log.error("setup path is failed");
441 return;
442 }
443
444 return;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530445 }
446
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530447 /**
448 * Handles the update path and highlights the path.
449 *
450 * @param bandWidth bandWidth
451 * @param bandWidthType is the kbps or mbps
452 * @param costType is igp or te
453 * @param tunnelName tunnel id
454 */
455 private void findAndSendPathsUpdate(String bandWidth, String bandWidthType, String costType, String tunnelIdStr) {
456 if (tunnelIdStr != null) {
457 List<Constraint> listConstrnt;
458
459 if (tunnelIdStr.equals(STRING_NULL)) {
460 log.error("update path is failed");
461 return;
462 }
463
464 if (pceService == null) {
465 log.error("PCE service is not active");
466 return;
467 }
468
469 listConstrnt = addBandwidthCostTypeConstraints(bandWidth, bandWidthType, costType);
470 TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr);
471 boolean path = pceService.updatePath(tunnelId, listConstrnt);
472
473 if (!path) {
474 log.error("update path is failed");
475 return;
476 }
477 }
478 return;
479 }
480
481 /**
482 * Handles the remove path and highlights the paths if existed.
483 *
484 * @param tunnelIdName tunnelId
485 */
486 private void findAndSendPathsRemove(String tunnelIdStr) {
487 if (tunnelIdStr != null) {
488 if (pceService == null) {
489 log.error("PCE service is not active");
490 return;
491 }
492
493 TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr);
494 boolean path = pceService.releasePath(tunnelId);
495 if (!path) {
496 log.error("remove path is failed");
497 return;
498 }
499 }
500 return;
501 }
502
503 private ImmutableSet.Builder<Link> buildPaths(ImmutableSet.Builder<Link> pathBuilder) {
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530504 paths.forEach(path -> path.links().forEach(pathBuilder::add));
505 return pathBuilder;
506 }
507
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530508 /**
509 * Handles the preparation of constraints list with given bandwidth and cost-type.
510 *
511 * @param bandWidth bandWidth
512 * @param bandWidthType is the kbps or mbps
513 * @param costType is igp or te
514 * @param listConstrnt list of constraints
515 * @return
516 */
517 private List<Constraint> addBandwidthCostTypeConstraints(String bandWidth,
518 String bandWidthType,
519 String costType) {
520 List<Constraint> listConstrnt = new LinkedList<>();
521 //bandwidth
522 double bwValue = 0.0;
523 if (!bandWidth.equals(STRING_NULL)) {
524 bwValue = Double.parseDouble(bandWidth);
525 }
526 if (bandWidthType.equals(BANDWIDTH_TYPE_KBPS)) {
527 bwValue = bwValue * BANDWIDTH_KBPS;
528 } else {
529 bwValue = bwValue * BANDWIDTH_MBPS;
530 }
531
532 //Cost type
533 CostConstraint.Type costTypeVal = null;
534 switch (costType) {
535 case COST_TYPE_IGP:
536 costTypeVal = CostConstraint.Type.COST;
537 break;
538 case COST_TYPE_TE:
539 costTypeVal = CostConstraint.Type.TE_COST;
540 break;
541 default:
542 log.error("Invalid cost type");
543 break;
544 }
545
546 if (bwValue != 0.0) {
547 listConstrnt.add(BandwidthConstraint.of(bwValue, DataRateUnit.valueOf(BANDWIDTH_BPS)));
548 }
549
550 if (costTypeVal != null) {
551 listConstrnt.add(CostConstraint.of(costTypeVal));
552 }
553
554 return listConstrnt;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530555 }
556
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530557 /**
558 * Handles the highlights of selected path.
559 */
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530560 private void hilightAndSendPaths() {
561 PceWebLinkMap linkMap = new PceWebLinkMap();
562 allPathLinks.forEach(linkMap::add);
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530563 Set<Link> selectedPathLinks;
564
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530565 selectedPathLinks = paths.isEmpty() ?
566 ImmutableSet.of() : ImmutableSet.copyOf(paths.get(pathIndex).links());
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530567
568 Highlights highlights = new Highlights();
569 if (highlightDelay > 0) {
570 highlights.delay(highlightDelay);
571 }
572 for (PceWebLink plink : linkMap.biLinks()) {
573 plink.computeHilight(selectedPathLinks, allPathLinks);
574 highlights.add(plink.highlight(null));
575 }
576 if (src != null) {
577 highlights = addBadge(highlights, srcType, src.toString(), SRC);
578 }
579 if (dst != null) {
580 highlights = addBadge(highlights, dstType, dst.toString(), DST);
581 }
582 sendMessage(TopoJson.highlightsMessage(highlights));
583 }
584
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530585 /**
586 * Handles the addition of badge and highlights.
587 *
588 * @param highlights highlights
589 * @param type device type
590 * @param elemId device to be add badge
591 * @param src device to be add badge
592 * @return
593 */
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530594 private Highlights addBadge(Highlights highlights, String type,
595 String elemId, String src) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530596 if (ROUTER.equals(type)) {
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530597 highlights = addDeviceBadge(highlights, elemId, src);
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530598 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530599
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530600 return highlights;
601 }
602
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530603 /**
604 * Handles the badge add and highlights.
605 *
606 * @param h highlights
607 * @param elemId device to be add badge
608 * @param type device type
609 * @return highlights
610 */
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530611 private Highlights addDeviceBadge(Highlights h, String elemId, String type) {
612 DeviceHighlight dh = new DeviceHighlight(elemId);
613 dh.setBadge(createBadge(type));
614 h.add(dh);
615 return h;
616 }
617
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530618 /**
619 * Handles the node badge add and highlights.
620 *
621 * @param type device type
622 * @return badge of given node
623 */
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530624 private NodeBadge createBadge(String type) {
625 return NodeBadge.text(type);
626 }
627
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530628 /**
629 * Handles the event of topology listeners.
630 */
631 private class InternalTopologyListener implements TopologyListener {
632 @Override
633 public void event(TopologyEvent event) {
634 highlightDelay = DELAY_MS;
635 findTunnelAndHighlights();
636 highlightDelay = 0;
637 }
638 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530639
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530640 /**
641 * Handles the event of tunnel listeners.
642 */
643 private class InnerPceWebTunnelListener implements TunnelListener {
644 @Override
645 public void event(TunnelEvent event) {
646 Tunnel tunnel = event.subject();
647 if (tunnel.type() == MPLS) {
648 highlightDelay = DELAY_MS;
649 findTunnelAndHighlights();
650 highlightDelay = 0;
651 }
652 }
653 }
654
655 /**
656 * Handles the event of topology listeners.
657 */
658 private void findTunnelAndHighlights() {
659 Collection<Tunnel> tunnelSet = null;
660 tunnelSet = tunnelService.queryTunnel(MPLS);
661 for (Tunnel tunnel : tunnelSet) {
662 if (tunnel.path() == null) {
663 log.info("path does not exist");
664 return;
665 }
666 paths.add(tunnel.path());
667 }
668
669 ImmutableSet.Builder<Link> builder = ImmutableSet.builder();
670 allPathLinks = buildPaths(builder).build();
671 hilightAndSendPaths();
672 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530673}