blob: 6f85c5e70f96b612bb17c1c463dc97ac9f093094 [file] [log] [blame]
Jonathan Hart66018992015-07-31 11:19:27 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jonathan Hart66018992015-07-31 11:19:27 -07003 *
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;
Ray Milkeyc7477292016-03-11 10:53:43 -080022import org.onosproject.utils.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
Luca Prete8f2a3ce2016-03-29 16:13:01 -070040 private static final String FORMAT = "port=%s/%s, vlan=%s, peers=%s";
Jonathan Harta8625482015-09-08 16:14:56 -070041 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);
papazois7d39a742015-10-14 10:15:56 +030053 if (config == null) {
54 print("No speakers configured");
55 return;
56 }
Jonathan Hart66018992015-07-31 11:19:27 -070057
Jonathan Harta8625482015-09-08 16:14:56 -070058 List<BgpConfig.BgpSpeakerConfig> bgpSpeakers =
59 Lists.newArrayList(config.bgpSpeakers());
60
61 Collections.sort(bgpSpeakers, SPEAKERS_COMPARATOR);
62
papazois7d39a742015-10-14 10:15:56 +030063 if (config.bgpSpeakers().isEmpty()) {
Jonathan Hart66018992015-07-31 11:19:27 -070064 print("No speakers configured");
65 } else {
Jonathan Harta8625482015-09-08 16:14:56 -070066 bgpSpeakers.forEach(
67 s -> {
68 if (s.name().isPresent()) {
69 print(NAME_FORMAT, s.name().get(), s.connectPoint().deviceId(),
Luca Prete8f2a3ce2016-03-29 16:13:01 -070070 s.connectPoint().port(), s.vlan(), s.peers());
Jonathan Harta8625482015-09-08 16:14:56 -070071 } else {
72 print(FORMAT, s.connectPoint().deviceId(),
Luca Prete8f2a3ce2016-03-29 16:13:01 -070073 s.connectPoint().port(), s.vlan(), s.peers());
Jonathan Harta8625482015-09-08 16:14:56 -070074 }
75 });
Jonathan Hart66018992015-07-31 11:19:27 -070076 }
77 }
78}