Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [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 | */ |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 16 | package org.onosproject.routing.cli; |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.apache.karaf.shell.commands.Option; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractShellCommand; |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 25 | import org.onosproject.routing.bgp.BgpInfoService; |
| 26 | import org.onosproject.routing.bgp.BgpSession; |
| 27 | |
| 28 | import java.util.Collection; |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Command to show the BGP neighbors. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "bgp-neighbors", |
| 34 | description = "Lists the BGP neighbors") |
| 35 | public class BgpNeighborsListCommand extends AbstractShellCommand { |
| 36 | @Option(name = "-n", aliases = "--neighbor", |
| 37 | description = "BGP neighbor to display information about", |
| 38 | required = false, multiValued = false) |
| 39 | private String bgpNeighbor; |
| 40 | |
| 41 | private static final String FORMAT_NEIGHBOR_LINE1 = |
| 42 | "BGP neighbor is %s, remote AS %d, local AS %d"; |
| 43 | private static final String FORMAT_NEIGHBOR_LINE2 = |
| 44 | " Remote router ID %s, IP %s, BGP version %d, Hold time %d"; |
| 45 | private static final String FORMAT_NEIGHBOR_LINE3 = |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 46 | " Remote AFI/SAFI IPv4 Unicast %s Multicast %s, IPv6 Unicast %s Multicast %s"; |
| 47 | private static final String FORMAT_NEIGHBOR_LINE4 = |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame] | 48 | " Local router ID %s, IP %s, BGP version %d, Hold time %d"; |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 49 | private static final String FORMAT_NEIGHBOR_LINE5 = |
| 50 | " Local AFI/SAFI IPv4 Unicast %s Multicast %s, IPv6 Unicast %s Multicast %s"; |
Kunihiro Ishiguro | 923d9d8 | 2014-12-21 15:47:28 +0900 | [diff] [blame] | 51 | private static final String FORMAT_NEIGHBOR_LINE6 = |
| 52 | " 4 Octet AS Capability: %s %s"; |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 53 | |
| 54 | @Override |
| 55 | protected void execute() { |
Jonathan Hart | 41349e9 | 2015-02-09 14:14:02 -0800 | [diff] [blame] | 56 | BgpInfoService service = AbstractShellCommand.get(BgpInfoService.class); |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 57 | Collection<BgpSession> bgpSessions = service.getBgpSessions(); |
| 58 | |
| 59 | if (bgpNeighbor != null) { |
| 60 | // Print a single neighbor (if found) |
| 61 | BgpSession foundBgpSession = null; |
| 62 | for (BgpSession bgpSession : bgpSessions) { |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 63 | if (bgpSession.remoteInfo().bgpId().toString().equals(bgpNeighbor)) { |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 64 | foundBgpSession = bgpSession; |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | if (foundBgpSession != null) { |
| 69 | printNeighbor(foundBgpSession); |
| 70 | } else { |
| 71 | print("BGP neighbor %s not found", bgpNeighbor); |
| 72 | } |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // Print all neighbors |
| 77 | printNeighbors(bgpSessions); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Prints all BGP neighbors. |
| 82 | * |
| 83 | * @param bgpSessions the BGP sessions for the neighbors to print |
| 84 | */ |
| 85 | private void printNeighbors(Collection<BgpSession> bgpSessions) { |
| 86 | if (outputJson()) { |
| 87 | print("%s", json(bgpSessions)); |
| 88 | } else { |
| 89 | for (BgpSession bgpSession : bgpSessions) { |
| 90 | printNeighbor(bgpSession); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Prints a BGP neighbor. |
| 97 | * |
| 98 | * @param bgpSession the BGP session for the neighbor to print |
| 99 | */ |
| 100 | private void printNeighbor(BgpSession bgpSession) { |
| 101 | print(FORMAT_NEIGHBOR_LINE1, |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 102 | bgpSession.remoteInfo().bgpId().toString(), |
| 103 | bgpSession.remoteInfo().asNumber(), |
| 104 | bgpSession.localInfo().asNumber()); |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 105 | print(FORMAT_NEIGHBOR_LINE2, |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 106 | bgpSession.remoteInfo().bgpId().toString(), |
| 107 | bgpSession.remoteInfo().address().toString(), |
| 108 | bgpSession.remoteInfo().bgpVersion(), |
| 109 | bgpSession.remoteInfo().holdtime()); |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 110 | print(FORMAT_NEIGHBOR_LINE3, |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 111 | bgpSession.remoteInfo().ipv4Unicast() ? "YES" : "NO", |
| 112 | bgpSession.remoteInfo().ipv4Multicast() ? "YES" : "NO", |
| 113 | bgpSession.remoteInfo().ipv6Unicast() ? "YES" : "NO", |
| 114 | bgpSession.remoteInfo().ipv6Multicast() ? "YES" : "NO"); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 115 | print(FORMAT_NEIGHBOR_LINE4, |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 116 | bgpSession.localInfo().bgpId().toString(), |
| 117 | bgpSession.localInfo().address().toString(), |
| 118 | bgpSession.localInfo().bgpVersion(), |
| 119 | bgpSession.localInfo().holdtime()); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame] | 120 | print(FORMAT_NEIGHBOR_LINE5, |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 121 | bgpSession.localInfo().ipv4Unicast() ? "YES" : "NO", |
| 122 | bgpSession.localInfo().ipv4Multicast() ? "YES" : "NO", |
| 123 | bgpSession.localInfo().ipv6Unicast() ? "YES" : "NO", |
| 124 | bgpSession.localInfo().ipv6Multicast() ? "YES" : "NO"); |
| 125 | if (bgpSession.localInfo().as4OctetCapability() || |
| 126 | bgpSession.remoteInfo().as4OctetCapability()) { |
Kunihiro Ishiguro | 923d9d8 | 2014-12-21 15:47:28 +0900 | [diff] [blame] | 127 | print(FORMAT_NEIGHBOR_LINE6, |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 128 | bgpSession.localInfo().as4OctetCapability() ? "Advertised" : "", |
| 129 | bgpSession.remoteInfo().as4OctetCapability() ? "Received" : ""); |
Kunihiro Ishiguro | 923d9d8 | 2014-12-21 15:47:28 +0900 | [diff] [blame] | 130 | } |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Produces a JSON array of BGP neighbors. |
| 135 | * |
| 136 | * @param bgpSessions the BGP sessions with the data |
| 137 | * @return JSON array with the neighbors |
| 138 | */ |
| 139 | private JsonNode json(Collection<BgpSession> bgpSessions) { |
| 140 | ObjectMapper mapper = new ObjectMapper(); |
| 141 | ArrayNode result = mapper.createArrayNode(); |
| 142 | |
| 143 | for (BgpSession bgpSession : bgpSessions) { |
| 144 | result.add(json(mapper, bgpSession)); |
| 145 | } |
| 146 | return result; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Produces JSON object for a BGP neighbor. |
| 151 | * |
| 152 | * @param mapper the JSON object mapper to use |
| 153 | * @param bgpSession the BGP session with the data |
| 154 | * @return JSON object for the route |
| 155 | */ |
| 156 | private ObjectNode json(ObjectMapper mapper, BgpSession bgpSession) { |
| 157 | ObjectNode result = mapper.createObjectNode(); |
| 158 | |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 159 | result.put("remoteAddress", bgpSession.remoteInfo().address().toString()); |
| 160 | result.put("remoteBgpVersion", bgpSession.remoteInfo().bgpVersion()); |
| 161 | result.put("remoteAs", bgpSession.remoteInfo().asNumber()); |
| 162 | result.put("remoteAs4", bgpSession.remoteInfo().as4Number()); |
| 163 | result.put("remoteHoldtime", bgpSession.remoteInfo().holdtime()); |
| 164 | result.put("remoteBgpId", bgpSession.remoteInfo().bgpId().toString()); |
| 165 | result.put("remoteIpv4Unicast", bgpSession.remoteInfo().ipv4Unicast()); |
| 166 | result.put("remoteIpv4Multicast", bgpSession.remoteInfo().ipv4Multicast()); |
| 167 | result.put("remoteIpv6Unicast", bgpSession.remoteInfo().ipv6Unicast()); |
| 168 | result.put("remoteIpv6Multicast", bgpSession.remoteInfo().ipv6Multicast()); |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 169 | // |
Pavlin Radoslavov | 8a36ce3 | 2015-01-28 12:26:57 -0800 | [diff] [blame] | 170 | result.put("localAddress", bgpSession.localInfo().address().toString()); |
| 171 | result.put("localBgpVersion", bgpSession.localInfo().bgpVersion()); |
| 172 | result.put("localAs", bgpSession.localInfo().asNumber()); |
| 173 | result.put("localAs4", bgpSession.localInfo().as4Number()); |
| 174 | result.put("localHoldtime", bgpSession.localInfo().holdtime()); |
| 175 | result.put("localBgpId", bgpSession.localInfo().bgpId().toString()); |
| 176 | result.put("localIpv4Unicast", bgpSession.localInfo().ipv4Unicast()); |
| 177 | result.put("localIpv4Multicast", bgpSession.localInfo().ipv4Multicast()); |
| 178 | result.put("localIpv6Unicast", bgpSession.localInfo().ipv6Unicast()); |
| 179 | result.put("localIpv6Multicast", bgpSession.localInfo().ipv6Multicast()); |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 180 | |
| 181 | return result; |
| 182 | } |
| 183 | } |