blob: 95c113cb179fb2a7085183b0862770c0ea5faa40 [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");
Palash Kala2349ac92017-03-31 11:31:02 +0900226 return;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530227 }
228
229 if ((src != null) && (dst != null)) {
230 findAndSendPaths(src, dst, bandWidth, bandWidthType, costType, lspType, tunnelName);
231 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530232 }
233 }
234
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530235 /**
236 * Handles the 'update path query' event received from the client.
237 */
238 private final class UpdatePathQueryHandler extends RequestHandler {
239
240 public UpdatePathQueryHandler() {
241 super(PCEWEB_UPDATE_PATH_QUERY);
242 }
243
244 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800245 public void process(ObjectNode payload) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530246 String srcId = string(payload, SRCID);
247 ElementId src = elementId(srcId);
248 String dstId = string(payload, DSTID);
249 ElementId dst = elementId(dstId);
250 Device srcDevice = deviceService.getDevice((DeviceId) src);
251 Device dstDevice = deviceService.getDevice((DeviceId) dst);
252
253 TunnelEndPoint tunSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress
254 .valueOf(srcDevice.annotations().value("lsrId")));
255 TunnelEndPoint tunDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress
256 .valueOf(dstDevice.annotations().value("lsrId")));
257
258 Collection<Tunnel> tunnelSet = tunnelService.queryTunnel(tunSrc, tunDst);
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530259 ObjectNode result = objectNode();
260 ArrayNode arrayNode = arrayNode();
261 for (Tunnel tunnel : tunnelSet) {
262 if (tunnel.type() == MPLS) {
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530263 if (tunnel.state().equals(ACTIVE)) {
264 arrayNode.add(tunnel.tunnelId().toString());
265 arrayNode.add(tunnel.tunnelName().toString());
266 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530267 }
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530268 }
269
270 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800271 sendMessage(PCEWEB_SHOW_TUNNEL, result);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530272 }
273 }
274
275 /**
276 * Handles the 'update path' event received from the client.
277 */
278 private final class UpdatePathHandler extends RequestHandler {
279
280 public UpdatePathHandler() {
281 super(PCEWEB_UPDATE_PATH);
282 }
283
284 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800285 public void process(ObjectNode payload) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530286 String bandWidth = string(payload, BANDWIDTH);
287 String bandWidthType = string(payload, BANDWIDTHTYPE);
288 String costType = string(payload, COSTTYPE);
289 String tunnelId = string(payload, TUNNEL_ID);
290
291 if (tunnelId == null) {
292 log.error("PCE update path is failed.");
293 }
294
295 findAndSendPathsUpdate(bandWidth, bandWidthType, costType, tunnelId);
296 }
297 }
298
299 /**
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530300 * Handles the 'update path' event received from the client.
301 */
302 private final class UpdatePathInfoHandler extends RequestHandler {
303
304 public UpdatePathInfoHandler() {
305 super(PCEWEB_TUNNEL_UPDATE_INFO);
306 }
307
308 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800309 public void process(ObjectNode payload) {
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530310 String tunnelIdStr = string(payload, TUNNEL_ID);
311
312 if (tunnelIdStr == null) {
313 log.error("PCE update path is failed.");
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530314 return;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530315 }
316
317 if (tunnelIdStr.equals(STRING_NULL)) {
318 log.error("PCE update path is failed.");
319 return;
320 }
321
322 if (pceService == null) {
323 log.error("PCE service is not active");
324 return;
325 }
326
327 TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr);
328 Tunnel tunnel = tunnelService.queryTunnel(tunnelId);
329 ObjectNode result = objectNode();
330 ArrayNode arrayNode = arrayNode();
331
332 arrayNode.add("Tunnel");
333 arrayNode.add(tunnelIdStr);
334 arrayNode.add("BandWidth");
335 arrayNode.add(tunnel.annotations().value("bandwidth"));
336 arrayNode.add("CostType");
337 arrayNode.add(tunnel.annotations().value("costType"));
338
339 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800340 sendMessage(PCEWEB_TUNNEL_UPDATE_INFO_REPLY, result);
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530341 }
342 }
343
344 /**
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530345 * Handles the 'remove path query' event received from the client.
346 */
347 private final class RemovePathQueryHandler extends RequestHandler {
348
349 public RemovePathQueryHandler() {
350 super(PCEWEB_REMOVE_PATH_QUERY);
351 }
352
353 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800354 public void process(ObjectNode payload) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530355 String srcId = string(payload, SRCID);
356 ElementId src = elementId(srcId);
357 String dstId = string(payload, DSTID);
358 ElementId dst = elementId(dstId);
359
360 Device srcDevice = deviceService.getDevice((DeviceId) src);
361 Device dstDevice = deviceService.getDevice((DeviceId) dst);
362
363 TunnelEndPoint tunSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress
364 .valueOf(srcDevice.annotations().value("lsrId")));
365 TunnelEndPoint tunDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress
366 .valueOf(dstDevice.annotations().value("lsrId")));
367
368 Collection<Tunnel> tunnelSet = tunnelService.queryTunnel(tunSrc, tunDst);
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530369 ObjectNode result = objectNode();
370 ArrayNode arrayNode = arrayNode();
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530371
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530372 for (Tunnel tunnel : tunnelSet) {
373 if (tunnel.type() == MPLS) {
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530374 if (tunnel.state().equals(ACTIVE)) {
375 arrayNode.add(tunnel.tunnelId().toString());
376 arrayNode.add(tunnel.tunnelName().toString());
377 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530378 }
Mahesh Raju-Huawei2cfa5352016-05-27 20:09:51 +0530379 }
380
381 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800382 sendMessage(PCEWEB_SHOW_TUNNEL_REMOVE, result);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530383 }
384 }
385
386 /**
387 * Handles the 'remove path' event received from the client.
388 */
389 private final class RemovePathHandler extends RequestHandler {
390
391 public RemovePathHandler() {
392 super(PCEWEB_REMOVE_PATH);
393 }
394
395 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800396 public void process(ObjectNode payload) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530397 String tunnelId = string(payload, TUNNEL_ID);
398
399 if (tunnelId == null) {
400 log.error("PCE update path is failed.");
401 }
402
403 findAndSendPathsRemove(tunnelId);
404 }
405 }
406
407 /**
408 * Handles the 'show the existed tunnels' event received from the client.
409 */
410 private final class ShowTunnelHandler extends RequestHandler {
411
412 public ShowTunnelHandler() {
413 super(PCEWEB_QUERY_TUNNELS);
414 }
415
416 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800417 public void process(ObjectNode payload) {
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530418 ObjectNode result = objectNode();
419 ArrayNode arrayNode = arrayNode();
420 Collection<Tunnel> tunnelSet = null;
421
422 tunnelSet = tunnelService.queryTunnel(MPLS);
423 for (Tunnel tunnel : tunnelSet) {
424 if (tunnel.state().equals(ACTIVE)) {
425 arrayNode.add(tunnel.tunnelId().toString());
426 arrayNode.add(tunnel.tunnelName().toString());
427 }
428 }
429
430 result.putArray(BUFFER_ARRAY).addAll(arrayNode);
Simon Hunt8a0429a2017-01-06 16:52:47 -0800431 sendMessage(PCEWEB_TUNNEL_QUERY_INFO, result);
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530432 }
433 }
434
435 /**
436 * Handles the 'show the existed tunnels' event received from the client.
437 */
438 private final class ShowTunnelHighlight extends RequestHandler {
439
440 public ShowTunnelHighlight() {
441 super(PCEWEB_TUNNEL_QUERY_INFO_SHOW);
442 }
443
444 @Override
Simon Hunt8a0429a2017-01-06 16:52:47 -0800445 public void process(ObjectNode payload) {
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530446 String tunnelIdStr = string(payload, TUNNEL_ID);
447
448 if (tunnelIdStr == null) {
449 log.error("Tunnel Id is NULL.");
450 return;
451 }
452
453 if (tunnelIdStr.equals(STRING_NULL)) {
454 log.error("Tunnel Id is NULL.");
455 return;
456 }
457
458 if (pceService == null) {
459 log.error("PCE service is not active");
460 return;
461 }
462
463 TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr);
464 Tunnel tunnel = tunnelService.queryTunnel(tunnelId);
465 if (tunnel != null) {
466 highlightsForTunnel(tunnel);
467 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530468 }
469 }
470
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530471 /**
472 * provides the element id.
473 */
474 private ElementId elementId(String id) {
475 try {
476 return DeviceId.deviceId(id);
477 } catch (IllegalArgumentException e) {
478 return HostId.hostId(id);
479 }
480 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530481
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530482 /**
483 * Handles the setup path and highlights the path.
484 *
Simon Hunt8a0429a2017-01-06 16:52:47 -0800485 * @param src ID of source
486 * @param dst ID of destination
487 * @param bandWidth bandwidth
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530488 * @param bandWidthType is the kbps or mbps
Simon Hunt8a0429a2017-01-06 16:52:47 -0800489 * @param costType is igp or te
490 * @param lspType is WITH_SIGNALLING, WITHOUT_SIGNALLING_AND_WITHOUT_SR
491 * or SR_WITHOUT_SIGNALLING
492 * @param tunnelName tunnel id
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530493 */
Simon Hunt8a0429a2017-01-06 16:52:47 -0800494 private void findAndSendPaths(ElementId src, ElementId dst, String bandWidth,
495 String bandWidthType, String costType,
496 String lspType, String tunnelName) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530497 log.debug("src={}; dst={};", src, dst);
498 boolean path;
499 List<Constraint> listConstrnt;
500
501 listConstrnt = addBandwidthCostTypeConstraints(bandWidth, bandWidthType, costType);
502
503 //LSP type
504 LspType lspTypeVal = null;
505 switch (lspType) {
506 case LSP_TYPE_CR:
507 lspTypeVal = LspType.WITH_SIGNALLING;
508 break;
509 case LSP_TYPE_SRBE:
510 lspTypeVal = LspType.WITHOUT_SIGNALLING_AND_WITHOUT_SR;
511 break;
512 case LSP_TYPE_SRTE:
513 lspTypeVal = LspType.SR_WITHOUT_SIGNALLING;
514 break;
515 default:
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530516 break;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530517 }
518
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530519 //TODO: need to get explicit paths [temporarily using null as the value]
Simon Hunt8a0429a2017-01-06 16:52:47 -0800520 path = pceService.setupPath((DeviceId) src, (DeviceId) dst,
521 tunnelName, listConstrnt, lspTypeVal, null);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530522
Simon Hunt8a0429a2017-01-06 16:52:47 -0800523 if (!path) {
524 log.error("setup path is failed");
525 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530526 }
527
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530528 /**
529 * Handles the update path and highlights the path.
530 *
Simon Hunt8a0429a2017-01-06 16:52:47 -0800531 * @param bandWidth bandWidth
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530532 * @param bandWidthType is the kbps or mbps
Simon Hunt8a0429a2017-01-06 16:52:47 -0800533 * @param costType is igp or te
534 * @param tunnelIdStr tunnel id
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530535 */
Simon Hunt8a0429a2017-01-06 16:52:47 -0800536 private void findAndSendPathsUpdate(String bandWidth, String bandWidthType,
537 String costType, String tunnelIdStr) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530538 if (tunnelIdStr != null) {
539 List<Constraint> listConstrnt;
540
541 if (tunnelIdStr.equals(STRING_NULL)) {
542 log.error("update path is failed");
543 return;
544 }
545
546 if (pceService == null) {
547 log.error("PCE service is not active");
548 return;
549 }
550
551 listConstrnt = addBandwidthCostTypeConstraints(bandWidth, bandWidthType, costType);
552 TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr);
553 boolean path = pceService.updatePath(tunnelId, listConstrnt);
554
555 if (!path) {
556 log.error("update path is failed");
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530557 }
558 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530559 }
560
561 /**
562 * Handles the remove path and highlights the paths if existed.
563 *
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530564 * @param tunnelIdStr tunnelId
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530565 */
566 private void findAndSendPathsRemove(String tunnelIdStr) {
567 if (tunnelIdStr != null) {
568 if (pceService == null) {
569 log.error("PCE service is not active");
570 return;
571 }
572
573 TunnelId tunnelId = TunnelId.valueOf(tunnelIdStr);
574 boolean path = pceService.releasePath(tunnelId);
575 if (!path) {
576 log.error("remove path is failed");
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530577 }
578 }
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530579 }
580
581 private ImmutableSet.Builder<Link> buildPaths(ImmutableSet.Builder<Link> pathBuilder) {
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530582 paths.forEach(path -> path.links().forEach(pathBuilder::add));
583 return pathBuilder;
584 }
585
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530586 /**
587 * Handles the preparation of constraints list with given bandwidth and cost-type.
588 *
Simon Hunt8a0429a2017-01-06 16:52:47 -0800589 * @param bandWidth bandWidth
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530590 * @param bandWidthType is the kbps or mbps
Simon Hunt8a0429a2017-01-06 16:52:47 -0800591 * @param costType is igp or te
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530592 * @return
593 */
594 private List<Constraint> addBandwidthCostTypeConstraints(String bandWidth,
595 String bandWidthType,
596 String costType) {
597 List<Constraint> listConstrnt = new LinkedList<>();
598 //bandwidth
599 double bwValue = 0.0;
600 if (!bandWidth.equals(STRING_NULL)) {
601 bwValue = Double.parseDouble(bandWidth);
602 }
603 if (bandWidthType.equals(BANDWIDTH_TYPE_KBPS)) {
604 bwValue = bwValue * BANDWIDTH_KBPS;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530605 } else if (bandWidthType.equals(BANDWIDTH_TYPE_MBPS)) {
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530606 bwValue = bwValue * BANDWIDTH_MBPS;
607 }
608
609 //Cost type
610 CostConstraint.Type costTypeVal = null;
611 switch (costType) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800612 case COST_TYPE_IGP:
613 costTypeVal = CostConstraint.Type.COST;
614 break;
615 case COST_TYPE_TE:
616 costTypeVal = CostConstraint.Type.TE_COST;
617 break;
618 default:
619 break;
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530620 }
621
622 if (bwValue != 0.0) {
623 listConstrnt.add(BandwidthConstraint.of(bwValue, DataRateUnit.valueOf(BANDWIDTH_BPS)));
624 }
625
626 if (costTypeVal != null) {
627 listConstrnt.add(CostConstraint.of(costTypeVal));
628 }
629
630 return listConstrnt;
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530631 }
632
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530633 /**
634 * Handles the highlights of selected path.
635 */
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530636 private void hilightAndSendPaths(Highlights highlights) {
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530637 LinkHighlight lh;
638 int linkclr = 0;
639 for (Path path : paths) {
640 for (Link link : path.links()) {
641 lh = new LinkHighlight(TopoUtils.compactLinkString(link), PRIMARY_HIGHLIGHT)
Simon Hunt8a0429a2017-01-06 16:52:47 -0800642 .addMod(new Mod(LINK_COLOR[linkclr]));
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530643 highlights.add(lh);
644 }
645 linkclr = linkclr + 1;
646 if (linkclr == LINK_COLOR_MAX) {
647 linkclr = 0;
648 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530649 }
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530650
Simon Hunt8a0429a2017-01-06 16:52:47 -0800651 sendMessage(highlightsMessage(highlights));
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530652 }
653
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530654 /**
Simon Hunt8a0429a2017-01-06 16:52:47 -0800655 * Handles the addition of badge and highlights.
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530656 *
657 * @param highlights highlights
Simon Hunt8a0429a2017-01-06 16:52:47 -0800658 * @param elemId device to be add badge
659 * @param src device to be add badge
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530660 * @return
661 */
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530662 private Highlights addBadge(Highlights highlights,
Simon Hunt8a0429a2017-01-06 16:52:47 -0800663 String elemId, String src) {
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530664 highlights = addDeviceBadge(highlights, elemId, src);
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530665 return highlights;
666 }
667
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530668 /**
669 * Handles the badge add and highlights.
670 *
Simon Hunt8a0429a2017-01-06 16:52:47 -0800671 * @param h highlights
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530672 * @param elemId device to be add badge
Simon Hunt8a0429a2017-01-06 16:52:47 -0800673 * @param type device badge value
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530674 * @return highlights
675 */
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530676 private Highlights addDeviceBadge(Highlights h, String elemId, String type) {
677 DeviceHighlight dh = new DeviceHighlight(elemId);
678 dh.setBadge(createBadge(type));
679 h.add(dh);
680 return h;
681 }
682
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530683 /**
684 * Handles the node badge add and highlights.
685 *
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530686 * @param type device badge value
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530687 * @return badge of given node
688 */
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530689 private NodeBadge createBadge(String type) {
690 return NodeBadge.text(type);
691 }
692
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530693 /**
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530694 * Handles the event of tunnel listeners.
695 */
696 private class InnerPceWebTunnelListener implements TunnelListener {
697 @Override
698 public void event(TunnelEvent event) {
699 Tunnel tunnel = event.subject();
700 if (tunnel.type() == MPLS) {
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530701 highlightsForTunnel(tunnel);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530702 }
703 }
704 }
705
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530706 private void findTunnelAndHighlights() {
707 Collection<Tunnel> tunnelSet = null;
Mahesh Raju-Huaweid8eaf972016-06-16 19:08:24 +0530708 Highlights highlights = new Highlights();
Michal Mach71a0fa42017-01-23 10:21:28 +0100709 paths.clear();
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530710 tunnelSet = tunnelService.queryTunnel(MPLS);
Jon Hallcbd1b392017-01-18 20:15:44 -0800711 if (tunnelSet.isEmpty()) {
Mahesh Raju-Huawei31d31c02016-06-04 17:20:57 +0530712 log.warn("Tunnel does not exist");
Simon Hunt8a0429a2017-01-06 16:52:47 -0800713 sendMessage(highlightsMessage(highlights));
Mahesh Raju-Huawei31d31c02016-06-04 17:20:57 +0530714 return;
715 }
716
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530717 for (Tunnel tunnel : tunnelSet) {
718 if (tunnel.path() == null) {
719 log.error("path does not exist");
Simon Hunt8a0429a2017-01-06 16:52:47 -0800720 sendMessage(highlightsMessage(highlights));
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530721 return;
722 }
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530723 if (!tunnel.state().equals(ACTIVE)) {
724 log.debug("Tunnel state is not active");
Simon Hunt8a0429a2017-01-06 16:52:47 -0800725 sendMessage(highlightsMessage(highlights));
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530726 return;
727 }
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530728 Link firstLink = tunnel.path().links().get(0);
729 if (firstLink != null) {
730 if (firstLink.src() != null) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800731 highlights = addBadge(highlights, firstLink.src().deviceId().toString(), SRC);
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530732 }
733 }
734 Link lastLink = tunnel.path().links().get(tunnel.path().links().size() - 1);
735 if (lastLink != null) {
736 if (lastLink.dst() != null) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800737 highlights = addBadge(highlights, lastLink.dst().deviceId().toString(), DST);
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530738 }
739 }
740 paths.add(tunnel.path());
741 }
742
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530743 ImmutableSet.Builder<Link> builder = ImmutableSet.builder();
744 allPathLinks = buildPaths(builder).build();
Mahesh Raju-Huawei0f977ef2016-06-14 17:26:22 +0530745 hilightAndSendPaths(highlights);
Mahesh Raju-Huawei5b0453a2016-05-27 11:54:27 +0530746 }
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530747
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530748 private void highlightsForTunnel(Tunnel tunnel) {
749 Highlights highlights = new Highlights();
Michal Mach71a0fa42017-01-23 10:21:28 +0100750 paths.clear();
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530751 if (tunnel.path() == null) {
752 log.error("path does not exist");
Simon Hunt8a0429a2017-01-06 16:52:47 -0800753 sendMessage(highlightsMessage(highlights));
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530754 return;
755 }
756 if (!tunnel.state().equals(ACTIVE)) {
757 log.debug("Tunnel state is not active");
Simon Hunt8a0429a2017-01-06 16:52:47 -0800758 sendMessage(highlightsMessage(highlights));
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530759 return;
760 }
761
762 Link firstLink = tunnel.path().links().get(0);
763 if (firstLink != null) {
764 if (firstLink.src() != null) {
765 highlights = addBadge(highlights, firstLink.src().deviceId().toString(), SRC);
766 }
767 }
768 Link lastLink = tunnel.path().links().get(tunnel.path().links().size() - 1);
769 if (lastLink != null) {
770 if (lastLink.dst() != null) {
Simon Hunt8a0429a2017-01-06 16:52:47 -0800771 highlights = addBadge(highlights, lastLink.dst().deviceId().toString(), DST);
MaheshRaju-Huawei4bf9f2d2016-07-01 19:03:18 +0530772 }
773 }
774 paths.add(tunnel.path());
775
776 ImmutableSet.Builder<Link> builder = ImmutableSet.builder();
777 allPathLinks = buildPaths(builder).build();
778 hilightAndSendPaths(highlights);
779 }
Mahesh Raju-Huawei85930052016-04-26 21:09:57 +0530780}