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