blob: 8282d8c890be1d2810249ec9d5f182caea5fa1da [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-Huawei85930052016-04-26 21:09:57 +053031import org.onosproject.net.topology.TopologyService;
32import org.onosproject.ui.RequestHandler;
33import org.onosproject.ui.UiConnection;
34import org.onosproject.ui.UiMessageHandler;
35import org.onosproject.ui.topo.DeviceHighlight;
36import org.onosproject.ui.topo.Highlights;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +053037import org.onosproject.ui.topo.LinkHighlight;
38import org.onosproject.ui.topo.Mod;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053039import org.onosproject.ui.topo.NodeBadge;
40import org.onosproject.ui.topo.TopoJson;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +053041import org.onosproject.ui.topo.TopoUtils;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053042import org.onosproject.net.device.DeviceService;
43import org.onosproject.net.intent.Constraint;
44import org.onosproject.pce.pceservice.LspType;
45import org.onosproject.pce.pceservice.api.PceService;
46import org.onosproject.pce.pceservice.constraint.CostConstraint;
47import org.onosproject.net.intent.constraint.BandwidthConstraint;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053048import org.slf4j.Logger;
49import org.slf4j.LoggerFactory;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053050import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
51import org.onosproject.incubator.net.tunnel.Tunnel;
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +053052import static org.onosproject.incubator.net.tunnel.Tunnel.State.ACTIVE;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053053import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
54import org.onosproject.incubator.net.tunnel.TunnelEvent;
55import org.onosproject.incubator.net.tunnel.TunnelId;
56import org.onosproject.incubator.net.tunnel.TunnelListener;
57import org.onosproject.incubator.net.tunnel.TunnelService;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +053058import static org.onosproject.ui.topo.LinkHighlight.Flavor.*;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053059import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
60
61import com.fasterxml.jackson.databind.node.ArrayNode;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053062
63import java.util.Collection;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053064import java.util.LinkedList;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053065import java.util.List;
66import java.util.Set;
67
68/**
69 * ONOS UI PCE WEB Topology-Overlay message handler.
70 */
71public class PceWebTopovMessageHandler extends UiMessageHandler {
72
73 private static final String PCEWEB_CLEAR = "pceTopovClear";
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053074 private static final String PCEWEB_SET_PATH = "pceTopovSetMode";
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053075 private static final String PCEWEB_UPDATE_PATH_QUERY = "pceTopovUpdateQuery";
76 private static final String PCEWEB_UPDATE_PATH = "pceTopovUpdate";
77 private static final String PCEWEB_REMOVE_PATH_QUERY = "pceTopovRemQuery";
78 private static final String PCEWEB_REMOVE_PATH = "pceTopovRem";
79 private static final String PCEWEB_QUERY_TUNNELS = "pceTopovTunnelDisplay";
80 private static final String PCEWEB_SHOW_TUNNEL = "pceTopovShowTunnels";
81 private static final String PCEWEB_SHOW_TUNNEL_REMOVE = "pceTopovShowTunnelsRem";
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +053082 private static final String PCEWEB_TUNNEL_UPDATE_INFO = "updatePathmsgInfo";
83 private static final String PCEWEB_TUNNEL_UPDATE_INFO_REPLY = "pceTopovShowTunnelsUpdate";
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +053084 private static final String PCEWEB_TUNNEL_QUERY_INFO = "pceTopovShowTunnelsQuery";
85 private static final String PCEWEB_TUNNEL_QUERY_INFO_SHOW = "pceTopovshowTunnelHighlightMsg";
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +053086 private static final String DST = "DST";
87 private static final String SRC = "SRC";
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053088 private static final String BANDWIDTH = "bw";
89 private static final String BANDWIDTHTYPE = "bwtype";
90 private static final String COSTTYPE = "ctype";
91 private static final String LSPTYPE = "lsptype";
92 private static final String SRCID = "srid";
93 private static final String DSTID = "dsid";
94 private static final String TUNNEL_ID = "tunnelid";
95 private static final String TUNNEL_NAME = "tunnelname";
96 private static final String COST_TYPE_IGP = "igp";
97 private static final String COST_TYPE_TE = "te";
98 private static final String BANDWIDTH_TYPE_KBPS = "kbps";
MaheshRaju-Huaweifc9f8bc2016-07-14 12:33:22 +053099 private static final String BANDWIDTH_TYPE_MBPS = "mbps";
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530100 private static final String BUFFER_ARRAY = "a";
101 private static final String BANDWIDTH_BPS = "BPS";
102 private static final String LSP_TYPE_CR = "cr";
103 private static final String LSP_TYPE_SRBE = "srbe";
104 private static final String LSP_TYPE_SRTE = "srte";
105 private static final String STRING_NULL = "null";
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530106 // Delay for showHighlights event processing on GUI client side to
107 // account for addLink animation.
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530108 private static final int DELAY_MS = 1100;
109 private static final double BANDWIDTH_KBPS = 1_000;
110 private static final double BANDWIDTH_MBPS = 1_000_000;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530111 private static String[] linkColor = {"pCol1", "pCol2", "pCol3", "pCol4", "pCol5",
112 "pCol6", "pCol7", "pCol8", "pCol9", "pCol10",
113 "pCol11", "pCol12", "pCol13", "pCol14", "pCol15"};
114 private static final int LINK_COLOR_MAX = 15;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530115 private Set<Link> allPathLinks;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530116 private ElementId src, dst;
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530117 private List<Path> paths = new LinkedList<>();
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530118 private int pathIndex;
119
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530120 private final Logger log = LoggerFactory.getLogger(getClass());
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530121 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 tunnelService = directory.get(TunnelService.class);
132 pceService = directory.get(PceService.class);
133 deviceService = directory.get(DeviceService.class);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530134 tunnelService.addListener(tunnelListener);
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530135 }
136
137 @Override
138 protected Collection<RequestHandler> createRequestHandlers() {
139 return ImmutableSet.of(
140 new ClearHandler(),
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530141 new SetPathHandler(),
142 new UpdatePathQueryHandler(),
143 new UpdatePathHandler(),
144 new RemovePathQueryHandler(),
145 new RemovePathHandler(),
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530146 new UpdatePathInfoHandler(),
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530147 new ShowTunnelHandler(),
148 new ShowTunnelHighlight());
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530149 }
150
151 @Override
152 public void destroy() {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530153 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 /**
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530176 * Handles the 'path calculation' event received from the client.
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530177 */
178 private final class SetPathHandler extends RequestHandler {
179
180 public SetPathHandler() {
181 super(PCEWEB_SET_PATH);
182 }
183
184 @Override
185 public void process(long sid, ObjectNode payload) {
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530186 String srcId = string(payload, SRCID);
187 src = elementId(srcId);
188 String dstId = string(payload, DSTID);
189 dst = elementId(dstId);
190 if (src.equals(dst)) {
191 src = null;
192 }
193
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530194 String bandWidth = string(payload, BANDWIDTH);
195 String bandWidthType = string(payload, BANDWIDTHTYPE);
196 String costType = string(payload, COSTTYPE);
197 String lspType = string(payload, LSPTYPE);
198 String tunnelName = string(payload, TUNNEL_NAME);
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530199
Priyanka Bf97e13d2016-07-28 10:57:15 +0530200 if (tunnelName == null || tunnelName.equals(STRING_NULL)) {
201 log.error("tunnel name should not be empty");
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530202 return;
203 }
Priyanka Bf97e13d2016-07-28 10:57:15 +0530204 //Validating tunnel name, duplicated tunnel names not allowed
205 Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS);
206 if (existingTunnels != null) {
207 for (Tunnel t : existingTunnels) {
208 if (t.tunnelName().toString().equals(tunnelName)) {
209 log.error("Path creation failed, Tunnel name already exists");
210 return;
211 }
212 }
213 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530214
215 if (pceService == null) {
216 log.error("PCE service is not active");
217 return;
218 }
219
MaheshRaju-Huaweiaec67f82016-07-18 16:39:25 +0530220 if (lspType == null || lspType.equals(STRING_NULL)) {
221 log.error("PCE setup path is failed as LSP type is mandatory");
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530222 }
223
224 if ((src != null) && (dst != null)) {
225 findAndSendPaths(src, dst, bandWidth, bandWidthType, costType, lspType, tunnelName);
226 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530227 }
228 }
229
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530230 /**
231 * Handles the 'update path query' event received from the client.
232 */
233 private final class UpdatePathQueryHandler extends RequestHandler {
234
235 public UpdatePathQueryHandler() {
236 super(PCEWEB_UPDATE_PATH_QUERY);
237 }
238
239 @Override
240 public void process(long sid, ObjectNode payload) {
241 String srcId = string(payload, SRCID);
242 ElementId src = elementId(srcId);
243 String dstId = string(payload, DSTID);
244 ElementId dst = elementId(dstId);
245 Device srcDevice = deviceService.getDevice((DeviceId) src);
246 Device dstDevice = deviceService.getDevice((DeviceId) dst);
247
248 TunnelEndPoint tunSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress
249 .valueOf(srcDevice.annotations().value("lsrId")));
250 TunnelEndPoint tunDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress
251 .valueOf(dstDevice.annotations().value("lsrId")));
252
253 Collection<Tunnel> tunnelSet = tunnelService.queryTunnel(tunSrc, tunDst);
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530254 ObjectNode result = objectNode();
255 ArrayNode arrayNode = arrayNode();
256 for (Tunnel tunnel : tunnelSet) {
257 if (tunnel.type() == MPLS) {
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530258 if (tunnel.state().equals(ACTIVE)) {
259 arrayNode.add(tunnel.tunnelId().toString());
260 arrayNode.add(tunnel.tunnelName().toString());
261 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530262 }
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530263 }
264
265 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
266 sendMessage(PCEWEB_SHOW_TUNNEL, sid, result);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530267 }
268 }
269
270 /**
271 * Handles the 'update path' event received from the client.
272 */
273 private final class UpdatePathHandler extends RequestHandler {
274
275 public UpdatePathHandler() {
276 super(PCEWEB_UPDATE_PATH);
277 }
278
279 @Override
280 public void process(long sid, ObjectNode payload) {
281 String bandWidth = string(payload, BANDWIDTH);
282 String bandWidthType = string(payload, BANDWIDTHTYPE);
283 String costType = string(payload, COSTTYPE);
284 String tunnelId = string(payload, TUNNEL_ID);
285
286 if (tunnelId == null) {
287 log.error("PCE update path is failed.");
288 }
289
290 findAndSendPathsUpdate(bandWidth, bandWidthType, costType, tunnelId);
291 }
292 }
293
294 /**
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530295 * Handles the 'update path' event received from the client.
296 */
297 private final class UpdatePathInfoHandler extends RequestHandler {
298
299 public UpdatePathInfoHandler() {
300 super(PCEWEB_TUNNEL_UPDATE_INFO);
301 }
302
303 @Override
304 public void process(long sid, ObjectNode payload) {
305 String tunnelIdStr = string(payload, TUNNEL_ID);
306
307 if (tunnelIdStr == null) {
308 log.error("PCE update path is failed.");
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530309 return;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530310 }
311
312 if (tunnelIdStr.equals(STRING_NULL)) {
313 log.error("PCE update path is failed.");
314 return;
315 }
316
317 if (pceService == null) {
318 log.error("PCE service is not active");
319 return;
320 }
321
322 TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr);
323 Tunnel tunnel = tunnelService.queryTunnel(tunnelId);
324 ObjectNode result = objectNode();
325 ArrayNode arrayNode = arrayNode();
326
327 arrayNode.add("Tunnel");
328 arrayNode.add(tunnelIdStr);
329 arrayNode.add("BandWidth");
330 arrayNode.add(tunnel.annotations().value("bandwidth"));
331 arrayNode.add("CostType");
332 arrayNode.add(tunnel.annotations().value("costType"));
333
334 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
335 sendMessage(PCEWEB_TUNNEL_UPDATE_INFO_REPLY, sid, result);
336 }
337 }
338
339 /**
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530340 * Handles the 'remove path query' event received from the client.
341 */
342 private final class RemovePathQueryHandler extends RequestHandler {
343
344 public RemovePathQueryHandler() {
345 super(PCEWEB_REMOVE_PATH_QUERY);
346 }
347
348 @Override
349 public void process(long sid, ObjectNode payload) {
350 String srcId = string(payload, SRCID);
351 ElementId src = elementId(srcId);
352 String dstId = string(payload, DSTID);
353 ElementId dst = elementId(dstId);
354
355 Device srcDevice = deviceService.getDevice((DeviceId) src);
356 Device dstDevice = deviceService.getDevice((DeviceId) dst);
357
358 TunnelEndPoint tunSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress
359 .valueOf(srcDevice.annotations().value("lsrId")));
360 TunnelEndPoint tunDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress
361 .valueOf(dstDevice.annotations().value("lsrId")));
362
363 Collection<Tunnel> tunnelSet = tunnelService.queryTunnel(tunSrc, tunDst);
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530364 ObjectNode result = objectNode();
365 ArrayNode arrayNode = arrayNode();
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530366
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530367 for (Tunnel tunnel : tunnelSet) {
368 if (tunnel.type() == MPLS) {
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530369 if (tunnel.state().equals(ACTIVE)) {
370 arrayNode.add(tunnel.tunnelId().toString());
371 arrayNode.add(tunnel.tunnelName().toString());
372 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530373 }
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530374 }
375
376 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
377 sendMessage(PCEWEB_SHOW_TUNNEL_REMOVE, sid, result);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530378 }
379 }
380
381 /**
382 * Handles the 'remove path' event received from the client.
383 */
384 private final class RemovePathHandler extends RequestHandler {
385
386 public RemovePathHandler() {
387 super(PCEWEB_REMOVE_PATH);
388 }
389
390 @Override
391 public void process(long sid, ObjectNode payload) {
392 String tunnelId = string(payload, TUNNEL_ID);
393
394 if (tunnelId == null) {
395 log.error("PCE update path is failed.");
396 }
397
398 findAndSendPathsRemove(tunnelId);
399 }
400 }
401
402 /**
403 * Handles the 'show the existed tunnels' event received from the client.
404 */
405 private final class ShowTunnelHandler extends RequestHandler {
406
407 public ShowTunnelHandler() {
408 super(PCEWEB_QUERY_TUNNELS);
409 }
410
411 @Override
412 public void process(long sid, ObjectNode payload) {
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530413 ObjectNode result = objectNode();
414 ArrayNode arrayNode = arrayNode();
415 Collection<Tunnel> tunnelSet = null;
416
417 tunnelSet = tunnelService.queryTunnel(MPLS);
418 for (Tunnel tunnel : tunnelSet) {
419 if (tunnel.state().equals(ACTIVE)) {
420 arrayNode.add(tunnel.tunnelId().toString());
421 arrayNode.add(tunnel.tunnelName().toString());
422 }
423 }
424
425 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
426 sendMessage(PCEWEB_TUNNEL_QUERY_INFO, sid, result);
427 }
428 }
429
430 /**
431 * Handles the 'show the existed tunnels' event received from the client.
432 */
433 private final class ShowTunnelHighlight extends RequestHandler {
434
435 public ShowTunnelHighlight() {
436 super(PCEWEB_TUNNEL_QUERY_INFO_SHOW);
437 }
438
439 @Override
440 public void process(long sid, ObjectNode payload) {
441 String tunnelIdStr = string(payload, TUNNEL_ID);
442
443 if (tunnelIdStr == null) {
444 log.error("Tunnel Id is NULL.");
445 return;
446 }
447
448 if (tunnelIdStr.equals(STRING_NULL)) {
449 log.error("Tunnel Id is NULL.");
450 return;
451 }
452
453 if (pceService == null) {
454 log.error("PCE service is not active");
455 return;
456 }
457
458 TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr);
459 Tunnel tunnel = tunnelService.queryTunnel(tunnelId);
460 if (tunnel != null) {
461 highlightsForTunnel(tunnel);
462 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530463 }
464 }
465
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530466 /**
467 * provides the element id.
468 */
469 private ElementId elementId(String id) {
470 try {
471 return DeviceId.deviceId(id);
472 } catch (IllegalArgumentException e) {
473 return HostId.hostId(id);
474 }
475 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530476
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530477 /**
478 * Handles the setup path and highlights the path.
479 *
480 * @param bandWidth
481 * @param bandWidthType is the kbps or mbps
482 * @param costType is igp or te
483 * @param lspType is WITH_SIGNALLING,WITHOUT_SIGNALLING_AND_WITHOUT_SR or SR_WITHOUT_SIGNALLING
484 * @param tunnelName tunnel id
485 */
486 private void findAndSendPaths(ElementId src, ElementId dst, String bandWidth, String bandWidthType,
487 String costType, String lspType, String tunnelName) {
488 log.debug("src={}; dst={};", src, dst);
489 boolean path;
490 List<Constraint> listConstrnt;
491
492 listConstrnt = addBandwidthCostTypeConstraints(bandWidth, bandWidthType, costType);
493
494 //LSP type
495 LspType lspTypeVal = null;
496 switch (lspType) {
497 case LSP_TYPE_CR:
498 lspTypeVal = LspType.WITH_SIGNALLING;
499 break;
500 case LSP_TYPE_SRBE:
501 lspTypeVal = LspType.WITHOUT_SIGNALLING_AND_WITHOUT_SR;
502 break;
503 case LSP_TYPE_SRTE:
504 lspTypeVal = LspType.SR_WITHOUT_SIGNALLING;
505 break;
506 default:
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530507 break;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530508 }
509
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530510 //TODO: need to get explicit paths [temporarily using null as the value]
511 path = pceService.setupPath((DeviceId) src, (DeviceId) dst, tunnelName, listConstrnt, lspTypeVal,
512 null);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530513 if (!path) {
514 log.error("setup path is failed");
515 return;
516 }
517
518 return;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530519 }
520
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530521 /**
522 * Handles the update path and highlights the path.
523 *
524 * @param bandWidth bandWidth
525 * @param bandWidthType is the kbps or mbps
526 * @param costType is igp or te
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530527 * @param tunnelIdStr tunnel id
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530528 */
529 private void findAndSendPathsUpdate(String bandWidth, String bandWidthType, String costType, String tunnelIdStr) {
530 if (tunnelIdStr != null) {
531 List<Constraint> listConstrnt;
532
533 if (tunnelIdStr.equals(STRING_NULL)) {
534 log.error("update path is failed");
535 return;
536 }
537
538 if (pceService == null) {
539 log.error("PCE service is not active");
540 return;
541 }
542
543 listConstrnt = addBandwidthCostTypeConstraints(bandWidth, bandWidthType, costType);
544 TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr);
545 boolean path = pceService.updatePath(tunnelId, listConstrnt);
546
547 if (!path) {
548 log.error("update path is failed");
549 return;
550 }
551 }
552 return;
553 }
554
555 /**
556 * Handles the remove path and highlights the paths if existed.
557 *
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530558 * @param tunnelIdStr tunnelId
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530559 */
560 private void findAndSendPathsRemove(String tunnelIdStr) {
561 if (tunnelIdStr != null) {
562 if (pceService == null) {
563 log.error("PCE service is not active");
564 return;
565 }
566
567 TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr);
568 boolean path = pceService.releasePath(tunnelId);
569 if (!path) {
570 log.error("remove path is failed");
571 return;
572 }
573 }
574 return;
575 }
576
577 private ImmutableSet.Builder<Link> buildPaths(ImmutableSet.Builder<Link> pathBuilder) {
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530578 paths.forEach(path -> path.links().forEach(pathBuilder::add));
579 return pathBuilder;
580 }
581
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530582 /**
583 * Handles the preparation of constraints list with given bandwidth and cost-type.
584 *
585 * @param bandWidth bandWidth
586 * @param bandWidthType is the kbps or mbps
587 * @param costType is igp or te
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530588 * @return
589 */
590 private List<Constraint> addBandwidthCostTypeConstraints(String bandWidth,
591 String bandWidthType,
592 String costType) {
593 List<Constraint> listConstrnt = new LinkedList<>();
594 //bandwidth
595 double bwValue = 0.0;
596 if (!bandWidth.equals(STRING_NULL)) {
597 bwValue = Double.parseDouble(bandWidth);
598 }
599 if (bandWidthType.equals(BANDWIDTH_TYPE_KBPS)) {
600 bwValue = bwValue * BANDWIDTH_KBPS;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530601 } else if (bandWidthType.equals(BANDWIDTH_TYPE_MBPS)) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530602 bwValue = bwValue * BANDWIDTH_MBPS;
603 }
604
605 //Cost type
606 CostConstraint.Type costTypeVal = null;
607 switch (costType) {
608 case COST_TYPE_IGP:
609 costTypeVal = CostConstraint.Type.COST;
610 break;
611 case COST_TYPE_TE:
612 costTypeVal = CostConstraint.Type.TE_COST;
613 break;
614 default:
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530615 break;
616 }
617
618 if (bwValue != 0.0) {
619 listConstrnt.add(BandwidthConstraint.of(bwValue, DataRateUnit.valueOf(BANDWIDTH_BPS)));
620 }
621
622 if (costTypeVal != null) {
623 listConstrnt.add(CostConstraint.of(costTypeVal));
624 }
625
626 return listConstrnt;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530627 }
628
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530629 /**
630 * Handles the highlights of selected path.
631 */
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530632 private void hilightAndSendPaths(Highlights highlights) {
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530633 LinkHighlight lh;
634 int linkclr = 0;
635 for (Path path : paths) {
636 for (Link link : path.links()) {
637 lh = new LinkHighlight(TopoUtils.compactLinkString(link), PRIMARY_HIGHLIGHT)
638 .addMod(new Mod(linkColor[linkclr]));
639 highlights.add(lh);
640 }
641 linkclr = linkclr + 1;
642 if (linkclr == LINK_COLOR_MAX) {
643 linkclr = 0;
644 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530645 }
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530646
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530647 sendMessage(TopoJson.highlightsMessage(highlights));
648 }
649
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530650 /**
651 * Handles the addition of badge and highlights.
652 *
653 * @param highlights highlights
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530654 * @param elemId device to be add badge
655 * @param src device to be add badge
656 * @return
657 */
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530658 private Highlights addBadge(Highlights highlights,
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530659 String elemId, String src) {
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530660 highlights = addDeviceBadge(highlights, elemId, src);
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530661 return highlights;
662 }
663
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530664 /**
665 * Handles the badge add and highlights.
666 *
667 * @param h highlights
668 * @param elemId device to be add badge
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530669 * @param type device badge value
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530670 * @return highlights
671 */
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530672 private Highlights addDeviceBadge(Highlights h, String elemId, String type) {
673 DeviceHighlight dh = new DeviceHighlight(elemId);
674 dh.setBadge(createBadge(type));
675 h.add(dh);
676 return h;
677 }
678
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530679 /**
680 * Handles the node badge add and highlights.
681 *
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530682 * @param type device badge value
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530683 * @return badge of given node
684 */
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530685 private NodeBadge createBadge(String type) {
686 return NodeBadge.text(type);
687 }
688
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530689 /**
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530690 * Handles the event of tunnel listeners.
691 */
692 private class InnerPceWebTunnelListener implements TunnelListener {
693 @Override
694 public void event(TunnelEvent event) {
695 Tunnel tunnel = event.subject();
696 if (tunnel.type() == MPLS) {
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530697 highlightsForTunnel(tunnel);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530698 }
699 }
700 }
701
702 /**
703 * Handles the event of topology listeners.
704 */
705 private void findTunnelAndHighlights() {
706 Collection<Tunnel> tunnelSet = null;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530707 Highlights highlights = new Highlights();
708 paths.removeAll(paths);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530709 tunnelSet = tunnelService.queryTunnel(MPLS);
Mahesh Raju-Huawei31d31c02016-06-04 17:20:57 +0530710 if (tunnelSet.size() == 0) {
711 log.warn("Tunnel does not exist");
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530712 sendMessage(TopoJson.highlightsMessage(highlights));
Mahesh Raju-Huawei31d31c02016-06-04 17:20:57 +0530713 return;
714 }
715
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530716 for (Tunnel tunnel : tunnelSet) {
717 if (tunnel.path() == null) {
718 log.error("path does not exist");
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530719 sendMessage(TopoJson.highlightsMessage(highlights));
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530720 return;
721 }
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530722 if (!tunnel.state().equals(ACTIVE)) {
723 log.debug("Tunnel state is not active");
724 sendMessage(TopoJson.highlightsMessage(highlights));
725 return;
726 }
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530727 Link firstLink = tunnel.path().links().get(0);
728 if (firstLink != null) {
729 if (firstLink.src() != null) {
730 highlights = addBadge(highlights, firstLink.src().deviceId().toString(), SRC);
731 }
732 }
733 Link lastLink = tunnel.path().links().get(tunnel.path().links().size() - 1);
734 if (lastLink != null) {
735 if (lastLink.dst() != null) {
736 highlights = addBadge(highlights, lastLink.dst().deviceId().toString(), DST);
737 }
738 }
739 paths.add(tunnel.path());
740 }
741
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530742 ImmutableSet.Builder<Link> builder = ImmutableSet.builder();
743 allPathLinks = buildPaths(builder).build();
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530744 hilightAndSendPaths(highlights);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530745 }
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530746
747 /**
748 * Handles the event of topology listeners.
749 */
750 private void highlightsForTunnel(Tunnel tunnel) {
751 Highlights highlights = new Highlights();
752 paths.removeAll(paths);
753 if (tunnel.path() == null) {
754 log.error("path does not exist");
755 sendMessage(TopoJson.highlightsMessage(highlights));
756 return;
757 }
758 if (!tunnel.state().equals(ACTIVE)) {
759 log.debug("Tunnel state is not active");
760 sendMessage(TopoJson.highlightsMessage(highlights));
761 return;
762 }
763
764 Link firstLink = tunnel.path().links().get(0);
765 if (firstLink != null) {
766 if (firstLink.src() != null) {
767 highlights = addBadge(highlights, firstLink.src().deviceId().toString(), SRC);
768 }
769 }
770 Link lastLink = tunnel.path().links().get(tunnel.path().links().size() - 1);
771 if (lastLink != null) {
772 if (lastLink.dst() != null) {
773 highlights = addBadge(highlights, lastLink.dst().deviceId().toString(), DST);
774 }
775 }
776 paths.add(tunnel.path());
777
778 ImmutableSet.Builder<Link> builder = ImmutableSet.builder();
779 allPathLinks = buildPaths(builder).build();
780 hilightAndSendPaths(highlights);
781 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530782}