blob: 2706e56d5650ea00d951eda5f084fd8c98ee949c [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
19import org.apache.karaf.shell.commands.Command;
Jonathan Hartdc7e76c2017-03-27 11:35:34 -070020import org.onlab.packet.IpAddress;
Jonathan Hart6b045582016-02-03 10:00:08 -080021import org.onlab.util.Tools;
22import org.onosproject.cli.AbstractShellCommand;
Jonathan Hartdc7e76c2017-03-27 11:35:34 -070023import org.onosproject.cluster.ClusterService;
Jonathan Hartf4b2ca12017-05-17 16:10:16 -070024import org.onosproject.routing.fpm.FpmPeerInfo;
Jonathan Hart6b045582016-02-03 10:00:08 -080025import org.onosproject.routing.fpm.FpmInfoService;
Jonathan Hartf4b2ca12017-05-17 16:10:16 -070026import org.onosproject.routing.fpm.FpmPeer;
Jonathan Hart6b045582016-02-03 10:00:08 -080027
Jonathan Hartdc7e76c2017-03-27 11:35:34 -070028import java.util.Comparator;
Jonathan Hartf4b2ca12017-05-17 16:10:16 -070029import java.util.Map;
Jonathan Hartdc7e76c2017-03-27 11:35:34 -070030
Jonathan Hart6b045582016-02-03 10:00:08 -080031/**
32 * Displays the current FPM connections.
33 */
34@Command(scope = "onos", name = "fpm-connections",
35 description = "Displays the current FPM connections")
36public class FpmConnectionsList extends AbstractShellCommand {
37
Jonathan Hartf4b2ca12017-05-17 16:10:16 -070038 private static final String FORMAT = "peer %s:%s connected to %s since %s %s (%d routes locally)";
Jonathan Hart6b045582016-02-03 10:00:08 -080039
40 @Override
41 protected void execute() {
Jonathan Hartdc7e76c2017-03-27 11:35:34 -070042 FpmInfoService fpmInfo = get(FpmInfoService.class);
Jonathan Hartf4b2ca12017-05-17 16:10:16 -070043
44 fpmInfo.peers().entrySet().stream()
45 .sorted(Comparator.<Map.Entry<FpmPeer, FpmPeerInfo>, IpAddress>comparing(e -> e.getKey().address())
46 .thenComparing(e -> e.getKey().port()))
47 .map(Map.Entry::getValue)
48 .forEach(this::print);
49 }
50
51 private void print(FpmPeerInfo info) {
Jonathan Hartdc7e76c2017-03-27 11:35:34 -070052 ClusterService clusterService = get(ClusterService.class);
Jonathan Hart6b045582016-02-03 10:00:08 -080053
Jonathan Hartf4b2ca12017-05-17 16:10:16 -070054 info.connections().forEach(cinfo ->
55 print(FORMAT, cinfo.peer().address(), cinfo.peer().port(),
56 cinfo.connectedTo(), Tools.timeAgo(cinfo.connectTime()),
57 cinfo.connectedTo().equals(clusterService.getLocalNode().id()) ? "*" : "",
58 info.routes())
59 );
Jonathan Hart6b045582016-02-03 10:00:08 -080060 }
61}