Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.sdnip.cli; |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 17 | |
| 18 | import java.util.Collection; |
| 19 | |
| 20 | import com.fasterxml.jackson.databind.JsonNode; |
| 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 23 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 24 | import org.apache.karaf.shell.commands.Command; |
| 25 | import org.apache.karaf.shell.commands.Option; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.cli.AbstractShellCommand; |
| 27 | import org.onosproject.sdnip.SdnIpService; |
| 28 | import org.onosproject.sdnip.bgp.BgpSession; |
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"; |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 51 | |
| 52 | @Override |
| 53 | protected void execute() { |
| 54 | SdnIpService service = get(SdnIpService.class); |
| 55 | Collection<BgpSession> bgpSessions = service.getBgpSessions(); |
| 56 | |
| 57 | if (bgpNeighbor != null) { |
| 58 | // Print a single neighbor (if found) |
| 59 | BgpSession foundBgpSession = null; |
| 60 | for (BgpSession bgpSession : bgpSessions) { |
| 61 | if (bgpSession.getRemoteBgpId().toString().equals(bgpNeighbor)) { |
| 62 | foundBgpSession = bgpSession; |
| 63 | break; |
| 64 | } |
| 65 | } |
| 66 | if (foundBgpSession != null) { |
| 67 | printNeighbor(foundBgpSession); |
| 68 | } else { |
| 69 | print("BGP neighbor %s not found", bgpNeighbor); |
| 70 | } |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | // Print all neighbors |
| 75 | printNeighbors(bgpSessions); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Prints all BGP neighbors. |
| 80 | * |
| 81 | * @param bgpSessions the BGP sessions for the neighbors to print |
| 82 | */ |
| 83 | private void printNeighbors(Collection<BgpSession> bgpSessions) { |
| 84 | if (outputJson()) { |
| 85 | print("%s", json(bgpSessions)); |
| 86 | } else { |
| 87 | for (BgpSession bgpSession : bgpSessions) { |
| 88 | printNeighbor(bgpSession); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Prints a BGP neighbor. |
| 95 | * |
| 96 | * @param bgpSession the BGP session for the neighbor to print |
| 97 | */ |
| 98 | private void printNeighbor(BgpSession bgpSession) { |
| 99 | print(FORMAT_NEIGHBOR_LINE1, |
| 100 | bgpSession.getRemoteBgpId().toString(), |
| 101 | bgpSession.getRemoteAs(), |
| 102 | bgpSession.getLocalAs()); |
| 103 | print(FORMAT_NEIGHBOR_LINE2, |
| 104 | bgpSession.getRemoteBgpId().toString(), |
| 105 | bgpSession.getRemoteAddress().toString(), |
| 106 | bgpSession.getRemoteBgpVersion(), |
| 107 | bgpSession.getRemoteHoldtime()); |
| 108 | print(FORMAT_NEIGHBOR_LINE3, |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame^] | 109 | bgpSession.getRemoteIpv4Unicast() ? "YES" : "NO", |
| 110 | bgpSession.getRemoteIpv4Multicast() ? "YES" : "NO", |
| 111 | bgpSession.getRemoteIpv6Unicast() ? "YES" : "NO", |
| 112 | bgpSession.getRemoteIpv6Multicast() ? "YES" : "NO"); |
| 113 | print(FORMAT_NEIGHBOR_LINE4, |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 114 | bgpSession.getLocalBgpId().toString(), |
| 115 | bgpSession.getLocalAddress().toString(), |
| 116 | bgpSession.getLocalBgpVersion(), |
| 117 | bgpSession.getLocalHoldtime()); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame^] | 118 | print(FORMAT_NEIGHBOR_LINE5, |
| 119 | bgpSession.getLocalIpv4Unicast() ? "YES" : "NO", |
| 120 | bgpSession.getLocalIpv4Multicast() ? "YES" : "NO", |
| 121 | bgpSession.getLocalIpv6Unicast() ? "YES" : "NO", |
| 122 | bgpSession.getLocalIpv6Multicast() ? "YES" : "NO"); |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Produces a JSON array of BGP neighbors. |
| 127 | * |
| 128 | * @param bgpSessions the BGP sessions with the data |
| 129 | * @return JSON array with the neighbors |
| 130 | */ |
| 131 | private JsonNode json(Collection<BgpSession> bgpSessions) { |
| 132 | ObjectMapper mapper = new ObjectMapper(); |
| 133 | ArrayNode result = mapper.createArrayNode(); |
| 134 | |
| 135 | for (BgpSession bgpSession : bgpSessions) { |
| 136 | result.add(json(mapper, bgpSession)); |
| 137 | } |
| 138 | return result; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Produces JSON object for a BGP neighbor. |
| 143 | * |
| 144 | * @param mapper the JSON object mapper to use |
| 145 | * @param bgpSession the BGP session with the data |
| 146 | * @return JSON object for the route |
| 147 | */ |
| 148 | private ObjectNode json(ObjectMapper mapper, BgpSession bgpSession) { |
| 149 | ObjectNode result = mapper.createObjectNode(); |
| 150 | |
| 151 | result.put("remoteAddress", bgpSession.getRemoteAddress().toString()); |
| 152 | result.put("remoteBgpVersion", bgpSession.getRemoteBgpVersion()); |
| 153 | result.put("remoteAs", bgpSession.getRemoteAs()); |
| 154 | result.put("remoteHoldtime", bgpSession.getRemoteHoldtime()); |
| 155 | result.put("remoteBgpId", bgpSession.getRemoteBgpId().toString()); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame^] | 156 | result.put("remoteIpv4Unicast", bgpSession.getRemoteIpv4Unicast()); |
| 157 | result.put("remoteIpv4Multicast", bgpSession.getRemoteIpv4Multicast()); |
| 158 | result.put("remoteIpv6Unicast", bgpSession.getRemoteIpv6Unicast()); |
| 159 | result.put("remoteIpv6Multicast", bgpSession.getRemoteIpv6Multicast()); |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 160 | // |
| 161 | result.put("localAddress", bgpSession.getLocalAddress().toString()); |
| 162 | result.put("localBgpVersion", bgpSession.getLocalBgpVersion()); |
| 163 | result.put("localAs", bgpSession.getLocalAs()); |
| 164 | result.put("localHoldtime", bgpSession.getLocalHoldtime()); |
| 165 | result.put("localBgpId", bgpSession.getLocalBgpId().toString()); |
Pavlin Radoslavov | 278cdde | 2014-12-16 14:09:31 -0800 | [diff] [blame^] | 166 | result.put("localIpv4Unicast", bgpSession.getLocalIpv4Unicast()); |
| 167 | result.put("localIpv4Multicast", bgpSession.getLocalIpv4Multicast()); |
| 168 | result.put("localIpv6Unicast", bgpSession.getLocalIpv6Unicast()); |
| 169 | result.put("localIpv6Multicast", bgpSession.getLocalIpv6Multicast()); |
Pavlin Radoslavov | 0c84da8 | 2014-11-07 17:53:34 -0800 | [diff] [blame] | 170 | |
| 171 | return result; |
| 172 | } |
| 173 | } |