blob: d797844a11143c834f6cd985954a183326f63dfa [file] [log] [blame]
Jonathan Hart6b045582016-02-03 10:00:08 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jonathan Hart6b045582016-02-03 10:00:08 -08003 *
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.routing.fpm.cli;
18
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Jonathan Hartdc7e76c2017-03-27 11:35:34 -070021import org.onlab.packet.IpAddress;
Jonathan Hart6b045582016-02-03 10:00:08 -080022import org.onlab.util.Tools;
23import org.onosproject.cli.AbstractShellCommand;
Jonathan Hartdc7e76c2017-03-27 11:35:34 -070024import org.onosproject.cluster.ClusterService;
Jonathan Hartf4b2ca12017-05-17 16:10:16 -070025import org.onosproject.routing.fpm.FpmPeerInfo;
Jonathan Hart6b045582016-02-03 10:00:08 -080026import org.onosproject.routing.fpm.FpmInfoService;
Jonathan Hartf4b2ca12017-05-17 16:10:16 -070027import org.onosproject.routing.fpm.FpmPeer;
Jonathan Hart6b045582016-02-03 10:00:08 -080028
Jonathan Hartdc7e76c2017-03-27 11:35:34 -070029import java.util.Comparator;
Jonathan Hartf4b2ca12017-05-17 16:10:16 -070030import java.util.Map;
Jonathan Hartdc7e76c2017-03-27 11:35:34 -070031
Jonathan Hart6b045582016-02-03 10:00:08 -080032/**
33 * Displays the current FPM connections.
34 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070035@Service
Jonathan Hart6b045582016-02-03 10:00:08 -080036@Command(scope = "onos", name = "fpm-connections",
37 description = "Displays the current FPM connections")
38public class FpmConnectionsList extends AbstractShellCommand {
39
Jonathan Hartf4b2ca12017-05-17 16:10:16 -070040 private static final String FORMAT = "peer %s:%s connected to %s since %s %s (%d routes locally)";
Jonathan Hart6b045582016-02-03 10:00:08 -080041
42 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070043 protected void doExecute() {
Jonathan Hartdc7e76c2017-03-27 11:35:34 -070044 FpmInfoService fpmInfo = get(FpmInfoService.class);
Jonathan Hartf4b2ca12017-05-17 16:10:16 -070045
Kalhee Kim40beb722018-01-16 20:32:04 +000046 print(String.format("PD Pushing is %s.", fpmInfo.isPdPushEnabled() ? "enabled" : "disabled"));
47
Jonathan Hartf4b2ca12017-05-17 16:10:16 -070048 fpmInfo.peers().entrySet().stream()
49 .sorted(Comparator.<Map.Entry<FpmPeer, FpmPeerInfo>, IpAddress>comparing(e -> e.getKey().address())
50 .thenComparing(e -> e.getKey().port()))
51 .map(Map.Entry::getValue)
52 .forEach(this::print);
53 }
54
55 private void print(FpmPeerInfo info) {
Jonathan Hartdc7e76c2017-03-27 11:35:34 -070056 ClusterService clusterService = get(ClusterService.class);
Jonathan Hart6b045582016-02-03 10:00:08 -080057
Jonathan Hartf4b2ca12017-05-17 16:10:16 -070058 info.connections().forEach(cinfo ->
59 print(FORMAT, cinfo.peer().address(), cinfo.peer().port(),
60 cinfo.connectedTo(), Tools.timeAgo(cinfo.connectTime()),
61 cinfo.connectedTo().equals(clusterService.getLocalNode().id()) ? "*" : "",
62 info.routes())
63 );
Jonathan Hart6b045582016-02-03 10:00:08 -080064 }
65}