Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 1 | /* |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [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.sdxl3.cli; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.cli.AbstractShellCommand; |
| 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 | import org.onosproject.sdxl3.SdxL3; |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 29 | import org.onosproject.sdxl3.SdxL3PeerService; |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 30 | import org.onosproject.sdxl3.config.SdxParticipantsConfig; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 31 | |
| 32 | import java.util.Comparator; |
| 33 | import java.util.List; |
| 34 | import java.util.Optional; |
| 35 | |
| 36 | /** |
| 37 | * Lists the BGP peers configured in the system. |
| 38 | */ |
| 39 | @Command(scope = "onos", name = "bgp-peers", |
| 40 | description = "Lists all BGP peers") |
| 41 | public class BgpPeersListCommand extends AbstractShellCommand { |
| 42 | |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 43 | private static final String BASIC_FORMAT = "ip=%s"; |
| 44 | private static final String DETAILS_FORMAT = |
| 45 | BASIC_FORMAT + ", port=%s/%s, intfName=%s"; |
| 46 | private static final String NAME_FORMAT = "%s: " + DETAILS_FORMAT; |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 47 | public static final String NO_PEERS = "No peers configured"; |
| 48 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 49 | private static final Comparator<SdxParticipantsConfig.PeerConfig> PEER_COMPARATOR = |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 50 | Comparator.comparing(p -> p.ip()); |
| 51 | public static final String EMPTY = ""; |
| 52 | |
| 53 | @Override |
| 54 | protected void execute() { |
| 55 | NetworkConfigService configService = get(NetworkConfigService.class); |
| 56 | CoreService coreService = get(CoreService.class); |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 57 | SdxL3PeerService peerService = get(SdxL3PeerService.class); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 58 | |
| 59 | ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID); |
| 60 | BgpConfig bgpConfig = configService.getConfig(routerAppId, RoutingService.CONFIG_CLASS); |
| 61 | |
| 62 | ApplicationId sdxL3AppId = coreService.getAppId(SdxL3.SDX_L3_APP); |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 63 | SdxParticipantsConfig peersConfig = configService. |
| 64 | getConfig(sdxL3AppId, SdxParticipantsConfig.class); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 65 | |
| 66 | if (bgpConfig == null && peersConfig == null) { |
| 67 | print(NO_PEERS); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | List<IpAddress> peeringAddresses = Lists.newArrayList(); |
| 72 | if (bgpConfig != null) { |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 73 | // Get all peering addresses from BGP configuration |
| 74 | peeringAddresses = peerService.getPeerAddresses(bgpConfig); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 77 | List<SdxParticipantsConfig.PeerConfig> bgpPeers = |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 78 | Lists.newArrayList(); |
| 79 | if (peersConfig != null) { |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 80 | // Get all peers having details specified |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 81 | bgpPeers.addAll(peersConfig.bgpPeers()); |
| 82 | } |
| 83 | |
| 84 | bgpPeers = mergePeers(peeringAddresses, bgpPeers); |
| 85 | |
| 86 | if (bgpPeers.isEmpty()) { |
| 87 | print(NO_PEERS); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | bgpPeers.sort(PEER_COMPARATOR); |
| 92 | bgpPeers.forEach(p -> { |
| 93 | if (p.name().isPresent()) { |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 94 | print(NAME_FORMAT, p.name().get(), p.ip(), |
| 95 | p.connectPoint().deviceId(), p.connectPoint().port(), |
| 96 | p.interfaceName()); |
| 97 | } else if (p.connectPoint() != null) { |
| 98 | print(DETAILS_FORMAT, p.ip(), p.connectPoint().deviceId(), |
| 99 | p.connectPoint().port(), p.interfaceName()); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 100 | } else { |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 101 | print(BASIC_FORMAT, p.ip()); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 102 | } |
| 103 | }); |
| 104 | } |
| 105 | |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 106 | private List<SdxParticipantsConfig.PeerConfig> mergePeers( |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 107 | List<IpAddress> peeringAddresses, |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 108 | List<SdxParticipantsConfig.PeerConfig> bgpPeers) { |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 109 | peeringAddresses.forEach(a -> { |
| 110 | boolean exists = bgpPeers.stream() |
| 111 | .filter(p -> p.ip().equals(a)) |
| 112 | .findAny().isPresent(); |
| 113 | if (!exists) { |
Andreas Papazois | e6aebaa | 2016-05-26 15:25:51 +0300 | [diff] [blame^] | 114 | bgpPeers.add(new SdxParticipantsConfig |
Andreas Papazois | c2c4501 | 2016-01-20 14:26:11 +0200 | [diff] [blame] | 115 | .PeerConfig(Optional.<String>empty(), a, null, EMPTY)); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 116 | } |
| 117 | }); |
Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 118 | return bgpPeers; |
| 119 | } |
| 120 | } |