blob: 6a784cc74cf8d10c0140f54b907d59243256cd4f [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;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Jonathan Hart66018992015-07-31 11:19:27 -070022import org.onosproject.cli.AbstractShellCommand;
Ray Milkeyc7477292016-03-11 10:53:43 -080023import org.onosproject.utils.Comparators;
Jonathan Hart66018992015-07-31 11:19:27 -070024import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
Ray Milkeya4122362015-08-18 15:19:08 -070026import org.onosproject.net.config.NetworkConfigService;
Jonathan Hart66018992015-07-31 11:19:27 -070027import org.onosproject.routing.RoutingService;
Jonathan Hart4cb39882015-08-12 23:50:55 -040028import org.onosproject.routing.config.BgpConfig;
Jonathan Hart66018992015-07-31 11:19:27 -070029
Jonathan Harta8625482015-09-08 16:14:56 -070030import java.util.Collections;
31import java.util.Comparator;
32import java.util.List;
33
Jonathan Hart66018992015-07-31 11:19:27 -070034/**
35 * Lists the BGP speakers configured in the system.
36 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070037@Service
Jonathan Hart66018992015-07-31 11:19:27 -070038@Command(scope = "onos", name = "bgp-speakers",
39 description = "Lists all BGP speakers")
40public class BgpSpeakersListCommand extends AbstractShellCommand {
41
Luca Prete8f2a3ce2016-03-29 16:13:01 -070042 private static final String FORMAT = "port=%s/%s, vlan=%s, peers=%s";
Jonathan Harta8625482015-09-08 16:14:56 -070043 private static final String NAME_FORMAT = "%s: " + FORMAT;
44
45 private static final Comparator<BgpConfig.BgpSpeakerConfig> SPEAKERS_COMPARATOR = (s1, s2) ->
46 Comparators.CONNECT_POINT_COMPARATOR.compare(s1.connectPoint(), s2.connectPoint());
Jonathan Hart66018992015-07-31 11:19:27 -070047
48 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070049 protected void doExecute() {
Jonathan Hart66018992015-07-31 11:19:27 -070050 NetworkConfigService configService = get(NetworkConfigService.class);
51 CoreService coreService = get(CoreService.class);
52 ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
53
Jonathan Hart66018992015-07-31 11:19:27 -070054 BgpConfig config = configService.getConfig(appId, BgpConfig.class);
papazois7d39a742015-10-14 10:15:56 +030055 if (config == null) {
56 print("No speakers configured");
57 return;
58 }
Jonathan Hart66018992015-07-31 11:19:27 -070059
Jonathan Harta8625482015-09-08 16:14:56 -070060 List<BgpConfig.BgpSpeakerConfig> bgpSpeakers =
61 Lists.newArrayList(config.bgpSpeakers());
62
63 Collections.sort(bgpSpeakers, SPEAKERS_COMPARATOR);
64
papazois7d39a742015-10-14 10:15:56 +030065 if (config.bgpSpeakers().isEmpty()) {
Jonathan Hart66018992015-07-31 11:19:27 -070066 print("No speakers configured");
67 } else {
Jonathan Harta8625482015-09-08 16:14:56 -070068 bgpSpeakers.forEach(
69 s -> {
70 if (s.name().isPresent()) {
71 print(NAME_FORMAT, s.name().get(), s.connectPoint().deviceId(),
Luca Prete8f2a3ce2016-03-29 16:13:01 -070072 s.connectPoint().port(), s.vlan(), s.peers());
Jonathan Harta8625482015-09-08 16:14:56 -070073 } else {
74 print(FORMAT, s.connectPoint().deviceId(),
Luca Prete8f2a3ce2016-03-29 16:13:01 -070075 s.connectPoint().port(), s.vlan(), s.peers());
Jonathan Harta8625482015-09-08 16:14:56 -070076 }
77 });
Jonathan Hart66018992015-07-31 11:19:27 -070078 }
79 }
80}