blob: c1a1bec7ee3e631c90c59b9c4c2d0eecb7f7db91 [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
Simon Hunt8a0429a2017-01-06 16:52:47 -080019import com.fasterxml.jackson.databind.node.ArrayNode;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053020import com.fasterxml.jackson.databind.node.ObjectNode;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053021import com.google.common.collect.ImmutableSet;
22import org.onlab.osgi.ServiceDirectory;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053023import org.onlab.packet.IpAddress;
24import org.onlab.util.DataRateUnit;
Simon Hunt8a0429a2017-01-06 16:52:47 -080025import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
26import org.onosproject.incubator.net.tunnel.Tunnel;
27import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
28import org.onosproject.incubator.net.tunnel.TunnelEvent;
29import org.onosproject.incubator.net.tunnel.TunnelId;
30import org.onosproject.incubator.net.tunnel.TunnelListener;
31import org.onosproject.incubator.net.tunnel.TunnelService;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053032import org.onosproject.net.Device;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053033import org.onosproject.net.DeviceId;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053034import org.onosproject.net.ElementId;
35import org.onosproject.net.HostId;
36import org.onosproject.net.Link;
37import org.onosproject.net.Path;
Simon Hunt8a0429a2017-01-06 16:52:47 -080038import org.onosproject.net.device.DeviceService;
39import org.onosproject.net.intent.Constraint;
40import org.onosproject.net.intent.constraint.BandwidthConstraint;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053041import org.onosproject.net.topology.TopologyService;
Simon Hunt8a0429a2017-01-06 16:52:47 -080042import org.onosproject.pce.pceservice.LspType;
43import org.onosproject.pce.pceservice.api.PceService;
44import org.onosproject.pce.pceservice.constraint.CostConstraint;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053045import org.onosproject.ui.RequestHandler;
46import org.onosproject.ui.UiConnection;
47import org.onosproject.ui.UiMessageHandler;
48import org.onosproject.ui.topo.DeviceHighlight;
49import org.onosproject.ui.topo.Highlights;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +053050import org.onosproject.ui.topo.LinkHighlight;
51import org.onosproject.ui.topo.Mod;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053052import org.onosproject.ui.topo.NodeBadge;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +053053import org.onosproject.ui.topo.TopoUtils;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053054import org.slf4j.Logger;
55import org.slf4j.LoggerFactory;
56
57import java.util.Collection;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +053058import java.util.LinkedList;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053059import java.util.List;
60import java.util.Set;
61
Simon Hunt8a0429a2017-01-06 16:52:47 -080062import static org.onosproject.incubator.net.tunnel.Tunnel.State.ACTIVE;
63import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
64import static org.onosproject.ui.topo.LinkHighlight.Flavor.PRIMARY_HIGHLIGHT;
65import static org.onosproject.ui.topo.TopoJson.highlightsMessage;
66
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053067/**
68 * ONOS UI PCE WEB Topology-Overlay message handler.
69 */
70public class PceWebTopovMessageHandler extends UiMessageHandler {
71
72 private static final String PCEWEB_CLEAR = "pceTopovClear";
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +053073 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-Huaweid8eaf972016-06-16 19:08:24 +053081 private static final String PCEWEB_TUNNEL_UPDATE_INFO = "updatePathmsgInfo";
82 private static final String PCEWEB_TUNNEL_UPDATE_INFO_REPLY = "pceTopovShowTunnelsUpdate";
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +053083 private static final String PCEWEB_TUNNEL_QUERY_INFO = "pceTopovShowTunnelsQuery";
84 private static final String PCEWEB_TUNNEL_QUERY_INFO_SHOW = "pceTopovshowTunnelHighlightMsg";
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +053085 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";
MaheshRaju-Huaweifc9f8bc2016-07-14 12:33:22 +053098 private static final String BANDWIDTH_TYPE_MBPS = "mbps";
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.
Simon Hunt8a0429a2017-01-06 16:52:47 -0800107 private static final int DELAY_MS = 1_100;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530108 private static final double BANDWIDTH_KBPS = 1_000;
109 private static final double BANDWIDTH_MBPS = 1_000_000;
Simon Hunt8a0429a2017-01-06 16:52:47 -0800110 private static final String[] LINK_COLOR = {
111 "pCol1", "pCol2", "pCol3", "pCol4", "pCol5",
112 "pCol6", "pCol7", "pCol8", "pCol9", "pCol10",
113 "pCol11", "pCol12", "pCol13", "pCol14", "pCol15"
114 };
115
116 private static final int LINK_COLOR_MAX = LINK_COLOR.length;
117
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530118 private Set<Link> allPathLinks;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530119 private ElementId src, dst;
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530120 private List<Path> paths = new LinkedList<>();
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530121 private int pathIndex;
122
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530123 private final Logger log = LoggerFactory.getLogger(getClass());
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530124 private final TunnelListener tunnelListener = new InnerPceWebTunnelListener();
125
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530126 protected TopologyService topologyService;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530127 protected TunnelService tunnelService;
128 protected PceService pceService;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530129 protected DeviceService deviceService;
Simon Hunt8a0429a2017-01-06 16:52:47 -0800130
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530131 @Override
132 public void init(UiConnection connection, ServiceDirectory directory) {
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530133 super.init(connection, directory);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530134 tunnelService = directory.get(TunnelService.class);
135 pceService = directory.get(PceService.class);
136 deviceService = directory.get(DeviceService.class);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530137 tunnelService.addListener(tunnelListener);
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530138 }
139
140 @Override
141 protected Collection<RequestHandler> createRequestHandlers() {
142 return ImmutableSet.of(
143 new ClearHandler(),
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530144 new SetPathHandler(),
145 new UpdatePathQueryHandler(),
146 new UpdatePathHandler(),
147 new RemovePathQueryHandler(),
148 new RemovePathHandler(),
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530149 new UpdatePathInfoHandler(),
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530150 new ShowTunnelHandler(),
151 new ShowTunnelHighlight());
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530152 }
153
154 @Override
155 public void destroy() {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530156 tunnelService.removeListener(tunnelListener);
157 super.destroy();
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530158 }
159
160 // Handler classes
Simon Hunt8a0429a2017-01-06 16:52:47 -0800161
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530162 /**
163 * Handles the 'clear' event received from the client.
164 */
165 private final class ClearHandler extends RequestHandler {
166
167 public ClearHandler() {
168 super(PCEWEB_CLEAR);
169 }
170
171 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800172 public void process(ObjectNode payload) {
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530173 src = null;
174 dst = null;
Simon Hunt8a0429a2017-01-06 16:52:47 -0800175 sendMessage(highlightsMessage(new Highlights()));
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530176 }
177 }
178
179 /**
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530180 * Handles the 'path calculation' event received from the client.
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530181 */
182 private final class SetPathHandler extends RequestHandler {
183
184 public SetPathHandler() {
185 super(PCEWEB_SET_PATH);
186 }
187
188 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800189 public void process(ObjectNode payload) {
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530190 String srcId = string(payload, SRCID);
191 src = elementId(srcId);
192 String dstId = string(payload, DSTID);
193 dst = elementId(dstId);
194 if (src.equals(dst)) {
195 src = null;
196 }
197
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530198 String bandWidth = string(payload, BANDWIDTH);
199 String bandWidthType = string(payload, BANDWIDTHTYPE);
200 String costType = string(payload, COSTTYPE);
201 String lspType = string(payload, LSPTYPE);
202 String tunnelName = string(payload, TUNNEL_NAME);
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530203
Priyanka Bf97e13d2016-07-28 10:57:15 +0530204 if (tunnelName == null || tunnelName.equals(STRING_NULL)) {
205 log.error("tunnel name should not be empty");
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530206 return;
207 }
Priyanka Bf97e13d2016-07-28 10:57:15 +0530208 //Validating tunnel name, duplicated tunnel names not allowed
209 Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS);
210 if (existingTunnels != null) {
211 for (Tunnel t : existingTunnels) {
212 if (t.tunnelName().toString().equals(tunnelName)) {
213 log.error("Path creation failed, Tunnel name already exists");
214 return;
215 }
216 }
217 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530218
219 if (pceService == null) {
220 log.error("PCE service is not active");
221 return;
222 }
223
MaheshRaju-Huaweiaec67f82016-07-18 16:39:25 +0530224 if (lspType == null || lspType.equals(STRING_NULL)) {
225 log.error("PCE setup path is failed as LSP type is mandatory");
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530226 }
227
228 if ((src != null) && (dst != null)) {
229 findAndSendPaths(src, dst, bandWidth, bandWidthType, costType, lspType, tunnelName);
230 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530231 }
232 }
233
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530234 /**
235 * Handles the 'update path query' event received from the client.
236 */
237 private final class UpdatePathQueryHandler extends RequestHandler {
238
239 public UpdatePathQueryHandler() {
240 super(PCEWEB_UPDATE_PATH_QUERY);
241 }
242
243 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800244 public void process(ObjectNode payload) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530245 String srcId = string(payload, SRCID);
246 ElementId src = elementId(srcId);
247 String dstId = string(payload, DSTID);
248 ElementId dst = elementId(dstId);
249 Device srcDevice = deviceService.getDevice((DeviceId) src);
250 Device dstDevice = deviceService.getDevice((DeviceId) dst);
251
252 TunnelEndPoint tunSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress
253 .valueOf(srcDevice.annotations().value("lsrId")));
254 TunnelEndPoint tunDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress
255 .valueOf(dstDevice.annotations().value("lsrId")));
256
257 Collection<Tunnel> tunnelSet = tunnelService.queryTunnel(tunSrc, tunDst);
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530258 ObjectNode result = objectNode();
259 ArrayNode arrayNode = arrayNode();
260 for (Tunnel tunnel : tunnelSet) {
261 if (tunnel.type() == MPLS) {
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530262 if (tunnel.state().equals(ACTIVE)) {
263 arrayNode.add(tunnel.tunnelId().toString());
264 arrayNode.add(tunnel.tunnelName().toString());
265 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530266 }
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530267 }
268
269 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800270 sendMessage(PCEWEB_SHOW_TUNNEL, result);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530271 }
272 }
273
274 /**
275 * Handles the 'update path' event received from the client.
276 */
277 private final class UpdatePathHandler extends RequestHandler {
278
279 public UpdatePathHandler() {
280 super(PCEWEB_UPDATE_PATH);
281 }
282
283 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800284 public void process(ObjectNode payload) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530285 String bandWidth = string(payload, BANDWIDTH);
286 String bandWidthType = string(payload, BANDWIDTHTYPE);
287 String costType = string(payload, COSTTYPE);
288 String tunnelId = string(payload, TUNNEL_ID);
289
290 if (tunnelId == null) {
291 log.error("PCE update path is failed.");
292 }
293
294 findAndSendPathsUpdate(bandWidth, bandWidthType, costType, tunnelId);
295 }
296 }
297
298 /**
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530299 * Handles the 'update path' event received from the client.
300 */
301 private final class UpdatePathInfoHandler extends RequestHandler {
302
303 public UpdatePathInfoHandler() {
304 super(PCEWEB_TUNNEL_UPDATE_INFO);
305 }
306
307 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800308 public void process(ObjectNode payload) {
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530309 String tunnelIdStr = string(payload, TUNNEL_ID);
310
311 if (tunnelIdStr == null) {
312 log.error("PCE update path is failed.");
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530313 return;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530314 }
315
316 if (tunnelIdStr.equals(STRING_NULL)) {
317 log.error("PCE update path is failed.");
318 return;
319 }
320
321 if (pceService == null) {
322 log.error("PCE service is not active");
323 return;
324 }
325
326 TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr);
327 Tunnel tunnel = tunnelService.queryTunnel(tunnelId);
328 ObjectNode result = objectNode();
329 ArrayNode arrayNode = arrayNode();
330
331 arrayNode.add("Tunnel");
332 arrayNode.add(tunnelIdStr);
333 arrayNode.add("BandWidth");
334 arrayNode.add(tunnel.annotations().value("bandwidth"));
335 arrayNode.add("CostType");
336 arrayNode.add(tunnel.annotations().value("costType"));
337
338 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800339 sendMessage(PCEWEB_TUNNEL_UPDATE_INFO_REPLY, result);
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530340 }
341 }
342
343 /**
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530344 * Handles the 'remove path query' event received from the client.
345 */
346 private final class RemovePathQueryHandler extends RequestHandler {
347
348 public RemovePathQueryHandler() {
349 super(PCEWEB_REMOVE_PATH_QUERY);
350 }
351
352 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800353 public void process(ObjectNode payload) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530354 String srcId = string(payload, SRCID);
355 ElementId src = elementId(srcId);
356 String dstId = string(payload, DSTID);
357 ElementId dst = elementId(dstId);
358
359 Device srcDevice = deviceService.getDevice((DeviceId) src);
360 Device dstDevice = deviceService.getDevice((DeviceId) dst);
361
362 TunnelEndPoint tunSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress
363 .valueOf(srcDevice.annotations().value("lsrId")));
364 TunnelEndPoint tunDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress
365 .valueOf(dstDevice.annotations().value("lsrId")));
366
367 Collection<Tunnel> tunnelSet = tunnelService.queryTunnel(tunSrc, tunDst);
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530368 ObjectNode result = objectNode();
369 ArrayNode arrayNode = arrayNode();
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530370
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530371 for (Tunnel tunnel : tunnelSet) {
372 if (tunnel.type() == MPLS) {
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530373 if (tunnel.state().equals(ACTIVE)) {
374 arrayNode.add(tunnel.tunnelId().toString());
375 arrayNode.add(tunnel.tunnelName().toString());
376 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530377 }
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530378 }
379
380 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800381 sendMessage(PCEWEB_SHOW_TUNNEL_REMOVE, result);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530382 }
383 }
384
385 /**
386 * Handles the 'remove path' event received from the client.
387 */
388 private final class RemovePathHandler extends RequestHandler {
389
390 public RemovePathHandler() {
391 super(PCEWEB_REMOVE_PATH);
392 }
393
394 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800395 public void process(ObjectNode payload) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530396 String tunnelId = string(payload, TUNNEL_ID);
397
398 if (tunnelId == null) {
399 log.error("PCE update path is failed.");
400 }
401
402 findAndSendPathsRemove(tunnelId);
403 }
404 }
405
406 /**
407 * Handles the 'show the existed tunnels' event received from the client.
408 */
409 private final class ShowTunnelHandler extends RequestHandler {
410
411 public ShowTunnelHandler() {
412 super(PCEWEB_QUERY_TUNNELS);
413 }
414
415 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800416 public void process(ObjectNode payload) {
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530417 ObjectNode result = objectNode();
418 ArrayNode arrayNode = arrayNode();
419 Collection<Tunnel> tunnelSet = null;
420
421 tunnelSet = tunnelService.queryTunnel(MPLS);
422 for (Tunnel tunnel : tunnelSet) {
423 if (tunnel.state().equals(ACTIVE)) {
424 arrayNode.add(tunnel.tunnelId().toString());
425 arrayNode.add(tunnel.tunnelName().toString());
426 }
427 }
428
429 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800430 sendMessage(PCEWEB_TUNNEL_QUERY_INFO, result);
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530431 }
432 }
433
434 /**
435 * Handles the 'show the existed tunnels' event received from the client.
436 */
437 private final class ShowTunnelHighlight extends RequestHandler {
438
439 public ShowTunnelHighlight() {
440 super(PCEWEB_TUNNEL_QUERY_INFO_SHOW);
441 }
442
443 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800444 public void process(ObjectNode payload) {
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530445 String tunnelIdStr = string(payload, TUNNEL_ID);
446
447 if (tunnelIdStr == null) {
448 log.error("Tunnel Id is NULL.");
449 return;
450 }
451
452 if (tunnelIdStr.equals(STRING_NULL)) {
453 log.error("Tunnel Id is NULL.");
454 return;
455 }
456
457 if (pceService == null) {
458 log.error("PCE service is not active");
459 return;
460 }
461
462 TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr);
463 Tunnel tunnel = tunnelService.queryTunnel(tunnelId);
464 if (tunnel != null) {
465 highlightsForTunnel(tunnel);
466 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530467 }
468 }
469
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530470 /**
471 * provides the element id.
472 */
473 private ElementId elementId(String id) {
474 try {
475 return DeviceId.deviceId(id);
476 } catch (IllegalArgumentException e) {
477 return HostId.hostId(id);
478 }
479 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530480
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530481 /**
482 * Handles the setup path and highlights the path.
483 *
Simon Hunt8a0429a2017-01-06 16:52:47 -0800484 * @param src ID of source
485 * @param dst ID of destination
486 * @param bandWidth bandwidth
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530487 * @param bandWidthType is the kbps or mbps
Simon Hunt8a0429a2017-01-06 16:52:47 -0800488 * @param costType is igp or te
489 * @param lspType is WITH_SIGNALLING, WITHOUT_SIGNALLING_AND_WITHOUT_SR
490 * or SR_WITHOUT_SIGNALLING
491 * @param tunnelName tunnel id
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530492 */
Simon Hunt8a0429a2017-01-06 16:52:47 -0800493 private void findAndSendPaths(ElementId src, ElementId dst, String bandWidth,
494 String bandWidthType, String costType,
495 String lspType, String tunnelName) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530496 log.debug("src={}; dst={};", src, dst);
497 boolean path;
498 List<Constraint> listConstrnt;
499
500 listConstrnt = addBandwidthCostTypeConstraints(bandWidth, bandWidthType, costType);
501
502 //LSP type
503 LspType lspTypeVal = null;
504 switch (lspType) {
505 case LSP_TYPE_CR:
506 lspTypeVal = LspType.WITH_SIGNALLING;
507 break;
508 case LSP_TYPE_SRBE:
509 lspTypeVal = LspType.WITHOUT_SIGNALLING_AND_WITHOUT_SR;
510 break;
511 case LSP_TYPE_SRTE:
512 lspTypeVal = LspType.SR_WITHOUT_SIGNALLING;
513 break;
514 default:
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530515 break;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530516 }
517
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530518 //TODO: need to get explicit paths [temporarily using null as the value]
Simon Hunt8a0429a2017-01-06 16:52:47 -0800519 path = pceService.setupPath((DeviceId) src, (DeviceId) dst,
520 tunnelName, listConstrnt, lspTypeVal, null);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530521
Simon Hunt8a0429a2017-01-06 16:52:47 -0800522 if (!path) {
523 log.error("setup path is failed");
524 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530525 }
526
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530527 /**
528 * Handles the update path and highlights the path.
529 *
Simon Hunt8a0429a2017-01-06 16:52:47 -0800530 * @param bandWidth bandWidth
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530531 * @param bandWidthType is the kbps or mbps
Simon Hunt8a0429a2017-01-06 16:52:47 -0800532 * @param costType is igp or te
533 * @param tunnelIdStr tunnel id
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530534 */
Simon Hunt8a0429a2017-01-06 16:52:47 -0800535 private void findAndSendPathsUpdate(String bandWidth, String bandWidthType,
536 String costType, String tunnelIdStr) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530537 if (tunnelIdStr != null) {
538 List<Constraint> listConstrnt;
539
540 if (tunnelIdStr.equals(STRING_NULL)) {
541 log.error("update path is failed");
542 return;
543 }
544
545 if (pceService == null) {
546 log.error("PCE service is not active");
547 return;
548 }
549
550 listConstrnt = addBandwidthCostTypeConstraints(bandWidth, bandWidthType, costType);
551 TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr);
552 boolean path = pceService.updatePath(tunnelId, listConstrnt);
553
554 if (!path) {
555 log.error("update path is failed");
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530556 }
557 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530558 }
559
560 /**
561 * Handles the remove path and highlights the paths if existed.
562 *
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530563 * @param tunnelIdStr tunnelId
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530564 */
565 private void findAndSendPathsRemove(String tunnelIdStr) {
566 if (tunnelIdStr != null) {
567 if (pceService == null) {
568 log.error("PCE service is not active");
569 return;
570 }
571
572 TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr);
573 boolean path = pceService.releasePath(tunnelId);
574 if (!path) {
575 log.error("remove path is failed");
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530576 }
577 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530578 }
579
580 private ImmutableSet.Builder<Link> buildPaths(ImmutableSet.Builder<Link> pathBuilder) {
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530581 paths.forEach(path -> path.links().forEach(pathBuilder::add));
582 return pathBuilder;
583 }
584
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530585 /**
586 * Handles the preparation of constraints list with given bandwidth and cost-type.
587 *
Simon Hunt8a0429a2017-01-06 16:52:47 -0800588 * @param bandWidth bandWidth
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530589 * @param bandWidthType is the kbps or mbps
Simon Hunt8a0429a2017-01-06 16:52:47 -0800590 * @param costType is igp or te
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530591 * @return
592 */
593 private List<Constraint> addBandwidthCostTypeConstraints(String bandWidth,
594 String bandWidthType,
595 String costType) {
596 List<Constraint> listConstrnt = new LinkedList<>();
597 //bandwidth
598 double bwValue = 0.0;
599 if (!bandWidth.equals(STRING_NULL)) {
600 bwValue = Double.parseDouble(bandWidth);
601 }
602 if (bandWidthType.equals(BANDWIDTH_TYPE_KBPS)) {
603 bwValue = bwValue * BANDWIDTH_KBPS;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530604 } else if (bandWidthType.equals(BANDWIDTH_TYPE_MBPS)) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530605 bwValue = bwValue * BANDWIDTH_MBPS;
606 }
607
608 //Cost type
609 CostConstraint.Type costTypeVal = null;
610 switch (costType) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800611 case COST_TYPE_IGP:
612 costTypeVal = CostConstraint.Type.COST;
613 break;
614 case COST_TYPE_TE:
615 costTypeVal = CostConstraint.Type.TE_COST;
616 break;
617 default:
618 break;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530619 }
620
621 if (bwValue != 0.0) {
622 listConstrnt.add(BandwidthConstraint.of(bwValue, DataRateUnit.valueOf(BANDWIDTH_BPS)));
623 }
624
625 if (costTypeVal != null) {
626 listConstrnt.add(CostConstraint.of(costTypeVal));
627 }
628
629 return listConstrnt;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530630 }
631
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530632 /**
633 * Handles the highlights of selected path.
634 */
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530635 private void hilightAndSendPaths(Highlights highlights) {
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530636 LinkHighlight lh;
637 int linkclr = 0;
638 for (Path path : paths) {
639 for (Link link : path.links()) {
640 lh = new LinkHighlight(TopoUtils.compactLinkString(link), PRIMARY_HIGHLIGHT)
Simon Hunt8a0429a2017-01-06 16:52:47 -0800641 .addMod(new Mod(LINK_COLOR[linkclr]));
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530642 highlights.add(lh);
643 }
644 linkclr = linkclr + 1;
645 if (linkclr == LINK_COLOR_MAX) {
646 linkclr = 0;
647 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530648 }
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530649
Simon Hunt8a0429a2017-01-06 16:52:47 -0800650 sendMessage(highlightsMessage(highlights));
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530651 }
652
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530653 /**
Simon Hunt8a0429a2017-01-06 16:52:47 -0800654 * Handles the addition of badge and highlights.
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530655 *
656 * @param highlights highlights
Simon Hunt8a0429a2017-01-06 16:52:47 -0800657 * @param elemId device to be add badge
658 * @param src device to be add badge
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530659 * @return
660 */
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530661 private Highlights addBadge(Highlights highlights,
Simon Hunt8a0429a2017-01-06 16:52:47 -0800662 String elemId, String src) {
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530663 highlights = addDeviceBadge(highlights, elemId, src);
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530664 return highlights;
665 }
666
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530667 /**
668 * Handles the badge add and highlights.
669 *
Simon Hunt8a0429a2017-01-06 16:52:47 -0800670 * @param h highlights
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530671 * @param elemId device to be add badge
Simon Hunt8a0429a2017-01-06 16:52:47 -0800672 * @param type device badge value
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530673 * @return highlights
674 */
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530675 private Highlights addDeviceBadge(Highlights h, String elemId, String type) {
676 DeviceHighlight dh = new DeviceHighlight(elemId);
677 dh.setBadge(createBadge(type));
678 h.add(dh);
679 return h;
680 }
681
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530682 /**
683 * Handles the node badge add and highlights.
684 *
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530685 * @param type device badge value
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530686 * @return badge of given node
687 */
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530688 private NodeBadge createBadge(String type) {
689 return NodeBadge.text(type);
690 }
691
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530692 /**
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530693 * Handles the event of tunnel listeners.
694 */
695 private class InnerPceWebTunnelListener implements TunnelListener {
696 @Override
697 public void event(TunnelEvent event) {
698 Tunnel tunnel = event.subject();
699 if (tunnel.type() == MPLS) {
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530700 highlightsForTunnel(tunnel);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530701 }
702 }
703 }
704
Simon Hunt8a0429a2017-01-06 16:52:47 -0800705 /**
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530706 * Handles the event of topology listeners.
Simon Hunt8a0429a2017-01-06 16:52:47 -0800707 */
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530708 private void findTunnelAndHighlights() {
709 Collection<Tunnel> tunnelSet = null;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530710 Highlights highlights = new Highlights();
711 paths.removeAll(paths);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530712 tunnelSet = tunnelService.queryTunnel(MPLS);
Jon Hallcbd1b392017-01-18 20:15:44 -0800713 if (tunnelSet.isEmpty()) {
Mahesh Raju-Huawei31d31c02016-06-04 17:20:57 +0530714 log.warn("Tunnel does not exist");
Simon Hunt8a0429a2017-01-06 16:52:47 -0800715 sendMessage(highlightsMessage(highlights));
Mahesh Raju-Huawei31d31c02016-06-04 17:20:57 +0530716 return;
717 }
718
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530719 for (Tunnel tunnel : tunnelSet) {
720 if (tunnel.path() == null) {
721 log.error("path does not exist");
Simon Hunt8a0429a2017-01-06 16:52:47 -0800722 sendMessage(highlightsMessage(highlights));
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530723 return;
724 }
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530725 if (!tunnel.state().equals(ACTIVE)) {
726 log.debug("Tunnel state is not active");
Simon Hunt8a0429a2017-01-06 16:52:47 -0800727 sendMessage(highlightsMessage(highlights));
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530728 return;
729 }
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530730 Link firstLink = tunnel.path().links().get(0);
731 if (firstLink != null) {
732 if (firstLink.src() != null) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800733 highlights = addBadge(highlights, firstLink.src().deviceId().toString(), SRC);
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530734 }
735 }
736 Link lastLink = tunnel.path().links().get(tunnel.path().links().size() - 1);
737 if (lastLink != null) {
738 if (lastLink.dst() != null) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800739 highlights = addBadge(highlights, lastLink.dst().deviceId().toString(), DST);
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530740 }
741 }
742 paths.add(tunnel.path());
743 }
744
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530745 ImmutableSet.Builder<Link> builder = ImmutableSet.builder();
746 allPathLinks = buildPaths(builder).build();
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530747 hilightAndSendPaths(highlights);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530748 }
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530749
750 /**
751 * Handles the event of topology listeners.
752 */
753 private void highlightsForTunnel(Tunnel tunnel) {
754 Highlights highlights = new Highlights();
755 paths.removeAll(paths);
756 if (tunnel.path() == null) {
757 log.error("path does not exist");
Simon Hunt8a0429a2017-01-06 16:52:47 -0800758 sendMessage(highlightsMessage(highlights));
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530759 return;
760 }
761 if (!tunnel.state().equals(ACTIVE)) {
762 log.debug("Tunnel state is not active");
Simon Hunt8a0429a2017-01-06 16:52:47 -0800763 sendMessage(highlightsMessage(highlights));
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530764 return;
765 }
766
767 Link firstLink = tunnel.path().links().get(0);
768 if (firstLink != null) {
769 if (firstLink.src() != null) {
770 highlights = addBadge(highlights, firstLink.src().deviceId().toString(), SRC);
771 }
772 }
773 Link lastLink = tunnel.path().links().get(tunnel.path().links().size() - 1);
774 if (lastLink != null) {
775 if (lastLink.dst() != null) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800776 highlights = addBadge(highlights, lastLink.dst().deviceId().toString(), DST);
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530777 }
778 }
779 paths.add(tunnel.path());
780
781 ImmutableSet.Builder<Link> builder = ImmutableSet.builder();
782 allPathLinks = buildPaths(builder).build();
783 hilightAndSendPaths(highlights);
784 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530785}