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