2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onosproject.routing.cli;
19 import com.google.common.collect.Lists;
20 import org.apache.karaf.shell.commands.Command;
21 import org.onosproject.cli.AbstractShellCommand;
22 import org.onosproject.cli.Comparators;
23 import org.onosproject.core.ApplicationId;
24 import org.onosproject.core.CoreService;
25 import org.onosproject.net.config.NetworkConfigService;
26 import org.onosproject.routing.RoutingService;
27 import org.onosproject.routing.config.BgpConfig;
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.List;
34 * Lists the BGP speakers configured in the system.
36 @Command(scope = "onos", name = "bgp-speakers",
37 description = "Lists all BGP speakers")
38 public class BgpSpeakersListCommand extends AbstractShellCommand {
40 private static final String FORMAT = "port=%s/%s, peers=%s";
41 private static final String NAME_FORMAT = "%s: " + FORMAT;
43 private static final Comparator<BgpConfig.BgpSpeakerConfig> SPEAKERS_COMPARATOR = (s1, s2) ->
44 Comparators.CONNECT_POINT_COMPARATOR.compare(s1.connectPoint(), s2.connectPoint());
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);
52 BgpConfig config = configService.getConfig(appId, BgpConfig.class);
54 List<BgpConfig.BgpSpeakerConfig> bgpSpeakers =
55 Lists.newArrayList(config.bgpSpeakers());
57 Collections.sort(bgpSpeakers, SPEAKERS_COMPARATOR);
59 if (config == null || config.bgpSpeakers().isEmpty()) {
60 print("No speakers configured");
64 if (s.name().isPresent()) {
65 print(NAME_FORMAT, s.name().get(), s.connectPoint().deviceId(),
66 s.connectPoint().port(), s.peers());
68 print(FORMAT, s.connectPoint().deviceId(),
69 s.connectPoint().port(), s.peers());