blob: 1aa0f1da56df1da77671bfaa4fc70a40978859a4 [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-Huaweid8eaf972016-06-16 19:08:24 +053039import org.onosproject.ui.topo.LinkHighlight;
40import org.onosproject.ui.topo.Mod;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053041import org.onosproject.ui.topo.NodeBadge;
42import org.onosproject.ui.topo.TopoJson;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +053043import org.onosproject.ui.topo.TopoUtils;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053044import org.onosproject.net.device.DeviceService;
45import org.onosproject.net.intent.Constraint;
46import org.onosproject.pce.pceservice.LspType;
47import org.onosproject.pce.pceservice.api.PceService;
48import org.onosproject.pce.pceservice.constraint.CostConstraint;
49import org.onosproject.net.intent.constraint.BandwidthConstraint;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053050import org.slf4j.Logger;
51import org.slf4j.LoggerFactory;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053052import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
53import org.onosproject.incubator.net.tunnel.Tunnel;
54import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
55import org.onosproject.incubator.net.tunnel.TunnelEvent;
56import org.onosproject.incubator.net.tunnel.TunnelId;
57import org.onosproject.incubator.net.tunnel.TunnelListener;
58import org.onosproject.incubator.net.tunnel.TunnelService;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +053059import static org.onosproject.ui.topo.LinkHighlight.Flavor.*;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053060import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
61
62import com.fasterxml.jackson.databind.node.ArrayNode;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053063
64import java.util.Collection;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053065import java.util.LinkedList;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053066import java.util.List;
67import java.util.Set;
68
69/**
70 * ONOS UI PCE WEB Topology-Overlay message handler.
71 */
72public class PceWebTopovMessageHandler extends UiMessageHandler {
73
74 private static final String PCEWEB_CLEAR = "pceTopovClear";
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053075 private static final String PCEWEB_SET_PATH = "pceTopovSetMode";
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053076 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-Huaweid8eaf972016-06-16 19:08:24 +053083 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-Huawei5b0453a2016-05-27 11:54:27 +053087 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-Huaweid8eaf972016-06-16 19:08:24 +053098 private static final String BANDWIDTH_TYPE_MBPS = "kbps";
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053099 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-Huawei85930052016-04-26 21:09:57 +0530105 // Delay for showHighlights event processing on GUI client side to
106 // account for addLink animation.
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530107 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-Huaweid8eaf972016-06-16 19:08:24 +0530110 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-Huawei85930052016-04-26 21:09:57 +0530114 private Set<Link> allPathLinks;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530115 private ElementId src, dst;
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530116 private List<Path> paths = new LinkedList<>();
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530117 private int pathIndex;
118
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530119 private final Logger log = LoggerFactory.getLogger(getClass());
120 private final TopologyListener topologyListener = new InternalTopologyListener();
121 private final TunnelListener tunnelListener = new InnerPceWebTunnelListener();
122
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530123 protected TopologyService topologyService;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530124 protected TunnelService tunnelService;
125 protected PceService pceService;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530126 protected DeviceService deviceService;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530127 @Override
128 public void init(UiConnection connection, ServiceDirectory directory) {
129
130 super.init(connection, directory);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530131 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-Huawei85930052016-04-26 21:09:57 +0530137 }
138
139 @Override
140 protected Collection<RequestHandler> createRequestHandlers() {
141 return ImmutableSet.of(
142 new ClearHandler(),
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530143 new SetPathHandler(),
144 new UpdatePathQueryHandler(),
145 new UpdatePathHandler(),
146 new RemovePathQueryHandler(),
147 new RemovePathHandler(),
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530148 new UpdatePathInfoHandler(),
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530149 new ShowTunnelHandler());
150 }
151
152 @Override
153 public void destroy() {
154 topologyService.removeListener(topologyListener);
155 tunnelService.removeListener(tunnelListener);
156 super.destroy();
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530157 }
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-Huawei5b0453a2016-05-27 11:54:27 +0530178 * Handles the 'path calculation' event received from the client.
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530179 */
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-Huawei0f977ef2016-06-14 17:26:22 +0530188 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-Huawei5b0453a2016-05-27 11:54:27 +0530196 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-Huawei85930052016-04-26 21:09:57 +0530201
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530202 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-Huawei85930052016-04-26 21:09:57 +0530219 }
220 }
221
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530222 /**
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-Huawei2cfa5352016-05-27 20:09:51 +0530246 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-Huawei5b0453a2016-05-27 11:54:27 +0530251 }
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530252 }
253
254 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
255 sendMessage(PCEWEB_SHOW_TUNNEL, sid, result);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530256 }
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-Huaweid8eaf972016-06-16 19:08:24 +0530284 * 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-Huawei5b0453a2016-05-27 11:54:27 +0530328 * 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-Huawei2cfa5352016-05-27 20:09:51 +0530352 ObjectNode result = objectNode();
353 ArrayNode arrayNode = arrayNode();
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530354
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530355 for (Tunnel tunnel : tunnelSet) {
356 if (tunnel.type() == MPLS) {
357 arrayNode.add(tunnel.tunnelId().toString());
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530358 }
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530359 }
360
361 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
362 sendMessage(PCEWEB_SHOW_TUNNEL_REMOVE, sid, result);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530363 }
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-Huawei85930052016-04-26 21:09:57 +0530402 /**
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-Huawei85930052016-04-26 21:09:57 +0530412
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530413 /**
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-Huawei5b0453a2016-05-27 11:54:27 +0530443 break;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530444 }
445
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530446 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-Huawei85930052016-04-26 21:09:57 +0530453 }
454
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530455 /**
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-Huawei85930052016-04-26 21:09:57 +0530512 paths.forEach(path -> path.links().forEach(pathBuilder::add));
513 return pathBuilder;
514 }
515
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530516 /**
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-Huaweid8eaf972016-06-16 19:08:24 +0530536 } else if (bandWidthType.equals(BANDWIDTH_TYPE_MBPS)) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530537 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-Huawei5b0453a2016-05-27 11:54:27 +0530550 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-Huawei85930052016-04-26 21:09:57 +0530562 }
563
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530564 /**
565 * Handles the highlights of selected path.
566 */
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530567 private void hilightAndSendPaths(Highlights highlights) {
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530568 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-Huawei85930052016-04-26 21:09:57 +0530580 }
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530581
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530582 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
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530589 * @param elemId device to be add badge
590 * @param src device to be add badge
591 * @return
592 */
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530593 private Highlights addBadge(Highlights highlights,
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530594 String elemId, String src) {
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530595 highlights = addDeviceBadge(highlights, elemId, src);
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530596 return highlights;
597 }
598
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530599 /**
600 * Handles the badge add and highlights.
601 *
602 * @param h highlights
603 * @param elemId device to be add badge
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530604 * @param type device badge value
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530605 * @return highlights
606 */
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530607 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-Huawei5b0453a2016-05-27 11:54:27 +0530614 /**
615 * Handles the node badge add and highlights.
616 *
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530617 * @param type device badge value
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530618 * @return badge of given node
619 */
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530620 private NodeBadge createBadge(String type) {
621 return NodeBadge.text(type);
622 }
623
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530624 /**
625 * Handles the event of topology listeners.
626 */
627 private class InternalTopologyListener implements TopologyListener {
628 @Override
629 public void event(TopologyEvent event) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530630 findTunnelAndHighlights();
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530631 }
632 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530633
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530634 /**
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-Huawei5b0453a2016-05-27 11:54:27 +0530642 findTunnelAndHighlights();
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530643 }
644 }
645 }
646
647 /**
648 * Handles the event of topology listeners.
649 */
650 private void findTunnelAndHighlights() {
651 Collection<Tunnel> tunnelSet = null;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530652 Highlights highlights = new Highlights();
653 paths.removeAll(paths);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530654 tunnelSet = tunnelService.queryTunnel(MPLS);
Mahesh Raju-Huawei31d31c02016-06-04 17:20:57 +0530655 if (tunnelSet.size() == 0) {
656 log.warn("Tunnel does not exist");
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530657 sendMessage(TopoJson.highlightsMessage(highlights));
Mahesh Raju-Huawei31d31c02016-06-04 17:20:57 +0530658 return;
659 }
660
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530661 for (Tunnel tunnel : tunnelSet) {
662 if (tunnel.path() == null) {
663 log.error("path does not exist");
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530664 sendMessage(TopoJson.highlightsMessage(highlights));
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530665 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-Huawei5b0453a2016-05-27 11:54:27 +0530682 ImmutableSet.Builder<Link> builder = ImmutableSet.builder();
683 allPathLinks = buildPaths(builder).build();
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530684 hilightAndSendPaths(highlights);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530685 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530686}