Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 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 | |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 22 | import org.onosproject.utils.Comparators; |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.CoreService; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 25 | import org.onosproject.net.config.NetworkConfigService; |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 26 | import org.onosproject.routing.RoutingService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 27 | import org.onosproject.routing.config.BgpConfig; |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 28 | |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 29 | import java.util.Collections; |
| 30 | import java.util.Comparator; |
| 31 | import java.util.List; |
| 32 | |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 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 | |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 40 | private static final String FORMAT = "port=%s/%s, vlan=%s, peers=%s"; |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 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 Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 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 | |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 52 | BgpConfig config = configService.getConfig(appId, BgpConfig.class); |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 53 | if (config == null) { |
| 54 | print("No speakers configured"); |
| 55 | return; |
| 56 | } |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 57 | |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 58 | List<BgpConfig.BgpSpeakerConfig> bgpSpeakers = |
| 59 | Lists.newArrayList(config.bgpSpeakers()); |
| 60 | |
| 61 | Collections.sort(bgpSpeakers, SPEAKERS_COMPARATOR); |
| 62 | |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 63 | if (config.bgpSpeakers().isEmpty()) { |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 64 | print("No speakers configured"); |
| 65 | } else { |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 66 | bgpSpeakers.forEach( |
| 67 | s -> { |
| 68 | if (s.name().isPresent()) { |
| 69 | print(NAME_FORMAT, s.name().get(), s.connectPoint().deviceId(), |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 70 | s.connectPoint().port(), s.vlan(), s.peers()); |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 71 | } else { |
| 72 | print(FORMAT, s.connectPoint().deviceId(), |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 73 | s.connectPoint().port(), s.vlan(), s.peers()); |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 74 | } |
| 75 | }); |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | } |