23d7086c05c4356520185bf3cf089453c21791ec
[onosfw.git] /
1 /*
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
17 package org.onosproject.routing.cli;
18
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;
28
29 import java.util.Collections;
30 import java.util.Comparator;
31 import java.util.List;
32
33 /**
34  * Lists the BGP speakers configured in the system.
35  */
36 @Command(scope = "onos", name = "bgp-speakers",
37         description = "Lists all BGP speakers")
38 public class BgpSpeakersListCommand extends AbstractShellCommand {
39
40     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());
45
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
52         BgpConfig config = configService.getConfig(appId, BgpConfig.class);
53
54         List<BgpConfig.BgpSpeakerConfig> bgpSpeakers =
55                 Lists.newArrayList(config.bgpSpeakers());
56
57         Collections.sort(bgpSpeakers, SPEAKERS_COMPARATOR);
58
59         if (config == null || config.bgpSpeakers().isEmpty()) {
60             print("No speakers configured");
61         } else {
62             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                 });
72         }
73     }
74 }