blob: 23d7086c05c4356520185bf3cf089453c21791ec [file] [log] [blame]
Jonathan Hart66018992015-07-31 11:19:27 -07001/*
2 * Copyright 2015 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.routing.cli;
18
Jonathan Harta8625482015-09-08 16:14:56 -070019import com.google.common.collect.Lists;
Jonathan Hart66018992015-07-31 11:19:27 -070020import org.apache.karaf.shell.commands.Command;
21import org.onosproject.cli.AbstractShellCommand;
Jonathan Harta8625482015-09-08 16:14:56 -070022import org.onosproject.cli.Comparators;
Jonathan Hart66018992015-07-31 11:19:27 -070023import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreService;
Ray Milkeya4122362015-08-18 15:19:08 -070025import org.onosproject.net.config.NetworkConfigService;
Jonathan Hart66018992015-07-31 11:19:27 -070026import org.onosproject.routing.RoutingService;
Jonathan Hart4cb39882015-08-12 23:50:55 -040027import org.onosproject.routing.config.BgpConfig;
Jonathan Hart66018992015-07-31 11:19:27 -070028
Jonathan Harta8625482015-09-08 16:14:56 -070029import java.util.Collections;
30import java.util.Comparator;
31import java.util.List;
32
Jonathan Hart66018992015-07-31 11:19:27 -070033/**
34 * Lists the BGP speakers configured in the system.
35 */
36@Command(scope = "onos", name = "bgp-speakers",
37 description = "Lists all BGP speakers")
38public class BgpSpeakersListCommand extends AbstractShellCommand {
39
Jonathan Harta8625482015-09-08 16:14:56 -070040 private static final String FORMAT = "port=%s/%s, peers=%s";
41 private static final String NAME_FORMAT = "%s: " + FORMAT;
42
43 private static final Comparator<BgpConfig.BgpSpeakerConfig> SPEAKERS_COMPARATOR = (s1, s2) ->
44 Comparators.CONNECT_POINT_COMPARATOR.compare(s1.connectPoint(), s2.connectPoint());
Jonathan Hart66018992015-07-31 11:19:27 -070045
46 @Override
47 protected void execute() {
48 NetworkConfigService configService = get(NetworkConfigService.class);
49 CoreService coreService = get(CoreService.class);
50 ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
51
Jonathan Hart66018992015-07-31 11:19:27 -070052 BgpConfig config = configService.getConfig(appId, BgpConfig.class);
53
Jonathan Harta8625482015-09-08 16:14:56 -070054 List<BgpConfig.BgpSpeakerConfig> bgpSpeakers =
55 Lists.newArrayList(config.bgpSpeakers());
56
57 Collections.sort(bgpSpeakers, SPEAKERS_COMPARATOR);
58
Jonathan Hart66018992015-07-31 11:19:27 -070059 if (config == null || config.bgpSpeakers().isEmpty()) {
60 print("No speakers configured");
61 } else {
Jonathan Harta8625482015-09-08 16:14:56 -070062 bgpSpeakers.forEach(
63 s -> {
64 if (s.name().isPresent()) {
65 print(NAME_FORMAT, s.name().get(), s.connectPoint().deviceId(),
66 s.connectPoint().port(), s.peers());
67 } else {
68 print(FORMAT, s.connectPoint().deviceId(),
69 s.connectPoint().port(), s.peers());
70 }
71 });
Jonathan Hart66018992015-07-31 11:19:27 -070072 }
73 }
74}