Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 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; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 22 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 23 | import org.onosproject.utils.Comparators; |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 26 | import org.onosproject.net.config.NetworkConfigService; |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 27 | import org.onosproject.routing.RoutingService; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 28 | import org.onosproject.routing.config.BgpConfig; |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 29 | |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 30 | import java.util.Collections; |
| 31 | import java.util.Comparator; |
| 32 | import java.util.List; |
| 33 | |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Lists the BGP speakers configured in the system. |
| 36 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 37 | @Service |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 38 | @Command(scope = "onos", name = "bgp-speakers", |
| 39 | description = "Lists all BGP speakers") |
| 40 | public class BgpSpeakersListCommand extends AbstractShellCommand { |
| 41 | |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 42 | private static final String FORMAT = "port=%s/%s, vlan=%s, peers=%s"; |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 43 | 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 Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 47 | |
| 48 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 49 | protected void doExecute() { |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 50 | NetworkConfigService configService = get(NetworkConfigService.class); |
| 51 | CoreService coreService = get(CoreService.class); |
| 52 | ApplicationId appId = coreService.getAppId(RoutingService.ROUTER_APP_ID); |
| 53 | |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 54 | BgpConfig config = configService.getConfig(appId, BgpConfig.class); |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 55 | if (config == null) { |
| 56 | print("No speakers configured"); |
| 57 | return; |
| 58 | } |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 59 | |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 60 | List<BgpConfig.BgpSpeakerConfig> bgpSpeakers = |
| 61 | Lists.newArrayList(config.bgpSpeakers()); |
| 62 | |
| 63 | Collections.sort(bgpSpeakers, SPEAKERS_COMPARATOR); |
| 64 | |
papazois | 7d39a74 | 2015-10-14 10:15:56 +0300 | [diff] [blame] | 65 | if (config.bgpSpeakers().isEmpty()) { |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 66 | print("No speakers configured"); |
| 67 | } else { |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 68 | bgpSpeakers.forEach( |
| 69 | s -> { |
| 70 | if (s.name().isPresent()) { |
| 71 | print(NAME_FORMAT, s.name().get(), s.connectPoint().deviceId(), |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 72 | s.connectPoint().port(), s.vlan(), s.peers()); |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 73 | } else { |
| 74 | print(FORMAT, s.connectPoint().deviceId(), |
Luca Prete | 8f2a3ce | 2016-03-29 16:13:01 -0700 | [diff] [blame] | 75 | s.connectPoint().port(), s.vlan(), s.peers()); |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 76 | } |
| 77 | }); |
Jonathan Hart | 6601899 | 2015-07-31 11:19:27 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |