blob: 72f0118676a8fdf5706a984dfa085d9d403fe3b6 [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:
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530434 break;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530435 }
436
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530437 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-Huawei85930052016-04-26 21:09:57 +0530444 }
445
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530446 /**
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-Huawei85930052016-04-26 21:09:57 +0530503 paths.forEach(path -> path.links().forEach(pathBuilder::add));
504 return pathBuilder;
505 }
506
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530507 /**
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-Huawei5b0453a2016-05-27 11:54:27 +0530541 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-Huawei85930052016-04-26 21:09:57 +0530553 }
554
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530555 /**
556 * Handles the highlights of selected path.
557 */
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530558 private void hilightAndSendPaths() {
559 PceWebLinkMap linkMap = new PceWebLinkMap();
560 allPathLinks.forEach(linkMap::add);
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530561 Set<Link> selectedPathLinks;
562
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530563 selectedPathLinks = paths.isEmpty() ?
564 ImmutableSet.of() : ImmutableSet.copyOf(paths.get(pathIndex).links());
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530565
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-Huawei5b0453a2016-05-27 11:54:27 +0530583 /**
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-Huawei85930052016-04-26 21:09:57 +0530592 private Highlights addBadge(Highlights highlights, String type,
593 String elemId, String src) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530594 if (ROUTER.equals(type)) {
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530595 highlights = addDeviceBadge(highlights, elemId, src);
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530596 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530597
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530598 return highlights;
599 }
600
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530601 /**
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-Huawei85930052016-04-26 21:09:57 +0530609 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-Huawei5b0453a2016-05-27 11:54:27 +0530616 /**
617 * Handles the node badge add and highlights.
618 *
619 * @param type device type
620 * @return badge of given node
621 */
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530622 private NodeBadge createBadge(String type) {
623 return NodeBadge.text(type);
624 }
625
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530626 /**
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-Huawei85930052016-04-26 21:09:57 +0530637
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530638 /**
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-Huawei31d31c02016-06-04 17:20:57 +0530661 log.error("path does not exist");
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530662 return;
663 }
664 paths.add(tunnel.path());
665 }
666
Mahesh Raju-Huawei31d31c02016-06-04 17:20:57 +0530667 if (tunnelSet.size() == 0) {
668 log.warn("Tunnel does not exist");
669 return;
670 }
671
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530672 ImmutableSet.Builder<Link> builder = ImmutableSet.builder();
673 allPathLinks = buildPaths(builder).build();
674 hilightAndSendPaths();
675 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530676}