Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.sdnip.cli; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 17 | |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame^] | 18 | import java.util.ArrayList; |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 19 | import java.util.Collection; |
| 20 | |
| 21 | import com.fasterxml.jackson.databind.JsonNode; |
| 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 24 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 25 | import org.apache.karaf.shell.commands.Command; |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 26 | import org.apache.karaf.shell.commands.Option; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.cli.AbstractShellCommand; |
| 28 | import org.onosproject.sdnip.SdnIpService; |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame^] | 29 | import org.onosproject.sdnip.bgp.BgpConstants.Update; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.sdnip.bgp.BgpRouteEntry; |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame^] | 31 | import org.onosproject.sdnip.bgp.BgpSession; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Command to show the routes learned through BGP. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "bgp-routes", |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame^] | 37 | description = "Lists all BGP best routes") |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 38 | public class BgpRoutesListCommand extends AbstractShellCommand { |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 39 | @Option(name = "-s", aliases = "--summary", |
| 40 | description = "BGP routes summary", |
| 41 | required = false, multiValued = false) |
| 42 | private boolean routesSummary = false; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 43 | |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame^] | 44 | @Option(name = "-n", aliases = "--neighbor", |
| 45 | description = "Routes from a BGP neighbor", |
| 46 | required = false, multiValued = false) |
| 47 | private String bgpNeighbor; |
| 48 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 49 | private static final String FORMAT_SUMMARY = "Total BGP routes = %d"; |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame^] | 50 | private static final String FORMAT_HEADER = |
| 51 | " Network Next Hop Origin LocalPref MED BGP-ID"; |
| 52 | private static final String FORMAT_ROUTE_LINE1 = |
| 53 | " %-18s %-15s %6s %9s %9s %-15s"; |
| 54 | private static final String FORMAT_ROUTE_LINE2 = |
| 55 | " AsPath %s"; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 56 | |
| 57 | @Override |
| 58 | protected void execute() { |
| 59 | SdnIpService service = get(SdnIpService.class); |
| 60 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 61 | // Print summary of the routes |
| 62 | if (routesSummary) { |
| 63 | printSummary(service.getBgpRoutes()); |
| 64 | return; |
| 65 | } |
| 66 | |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame^] | 67 | BgpSession foundBgpSession = null; |
| 68 | if (bgpNeighbor != null) { |
| 69 | // Print the routes from a single neighbor (if found) |
| 70 | for (BgpSession bgpSession : service.getBgpSessions()) { |
| 71 | if (bgpSession.getRemoteBgpId().toString().equals(bgpNeighbor)) { |
| 72 | foundBgpSession = bgpSession; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | if (foundBgpSession == null) { |
| 77 | print("BGP neighbor %s not found", bgpNeighbor); |
| 78 | return; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Print the routes |
| 83 | if (foundBgpSession != null) { |
| 84 | printRoutes(foundBgpSession.getBgpRibIn()); |
| 85 | } else { |
| 86 | printRoutes(service.getBgpRoutes()); |
| 87 | } |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Prints summary of the routes. |
| 92 | * |
| 93 | * @param routes the routes |
| 94 | */ |
| 95 | private void printSummary(Collection<BgpRouteEntry> routes) { |
| 96 | if (outputJson()) { |
| 97 | ObjectMapper mapper = new ObjectMapper(); |
| 98 | ObjectNode result = mapper.createObjectNode(); |
| 99 | result.put("totalRoutes", routes.size()); |
| 100 | print("%s", result); |
| 101 | } else { |
| 102 | print(FORMAT_SUMMARY, routes.size()); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 106 | /** |
| 107 | * Prints all routes. |
| 108 | * |
| 109 | * @param routes the routes to print |
| 110 | */ |
| 111 | private void printRoutes(Collection<BgpRouteEntry> routes) { |
| 112 | if (outputJson()) { |
| 113 | print("%s", json(routes)); |
| 114 | } else { |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame^] | 115 | print(FORMAT_HEADER); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 116 | for (BgpRouteEntry route : routes) { |
| 117 | printRoute(route); |
| 118 | } |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame^] | 119 | print(FORMAT_SUMMARY, routes.size()); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Prints a BGP route. |
| 125 | * |
| 126 | * @param route the route to print |
| 127 | */ |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 128 | private void printRoute(BgpRouteEntry route) { |
| 129 | if (route != null) { |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame^] | 130 | print(FORMAT_ROUTE_LINE1, route.prefix(), route.nextHop(), |
| 131 | Update.Origin.typeToString(route.getOrigin()), |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 132 | route.getLocalPref(), route.getMultiExitDisc(), |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame^] | 133 | route.getBgpSession().getRemoteBgpId()); |
| 134 | print(FORMAT_ROUTE_LINE2, asPath4Cli(route.getAsPath())); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 138 | /** |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame^] | 139 | * Formats the AS Path as a string that can be shown on the CLI. |
| 140 | * |
| 141 | * @param asPath the AS Path to format |
| 142 | * @return the AS Path as a string |
| 143 | */ |
| 144 | private String asPath4Cli(BgpRouteEntry.AsPath asPath) { |
| 145 | ArrayList<BgpRouteEntry.PathSegment> pathSegments = |
| 146 | asPath.getPathSegments(); |
| 147 | |
| 148 | if (pathSegments.isEmpty()) { |
| 149 | return "[none]"; |
| 150 | } |
| 151 | |
| 152 | final StringBuilder builder = new StringBuilder(); |
| 153 | for (BgpRouteEntry.PathSegment pathSegment : pathSegments) { |
| 154 | String prefix = null; |
| 155 | String suffix = null; |
| 156 | switch (pathSegment.getType()) { |
| 157 | case Update.AsPath.AS_SET: |
| 158 | prefix = "[AS-Set"; |
| 159 | suffix = "]"; |
| 160 | break; |
| 161 | case Update.AsPath.AS_SEQUENCE: |
| 162 | break; |
| 163 | case Update.AsPath.AS_CONFED_SEQUENCE: |
| 164 | prefix = "[AS-Confed-Seq"; |
| 165 | suffix = "]"; |
| 166 | break; |
| 167 | case Update.AsPath.AS_CONFED_SET: |
| 168 | prefix = "[AS-Confed-Set"; |
| 169 | suffix = "]"; |
| 170 | break; |
| 171 | default: |
| 172 | builder.append(String.format("(type = %s)", |
| 173 | Update.AsPath.typeToString(pathSegment.getType()))); |
| 174 | break; |
| 175 | } |
| 176 | |
| 177 | if (prefix != null) { |
| 178 | if (builder.length() > 0) { |
| 179 | builder.append(" "); // Separator |
| 180 | } |
| 181 | builder.append(prefix); |
| 182 | } |
| 183 | // Print the AS numbers |
| 184 | for (Long asn : pathSegment.getSegmentAsNumbers()) { |
| 185 | if (builder.length() > 0) { |
| 186 | builder.append(" "); // Separator |
| 187 | } |
| 188 | builder.append(String.format("%d", asn)); |
| 189 | } |
| 190 | if (suffix != null) { |
| 191 | // No need for separator |
| 192 | builder.append(prefix); |
| 193 | } |
| 194 | } |
| 195 | return builder.toString(); |
| 196 | } |
| 197 | |
| 198 | /** |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 199 | * Produces a JSON array of routes. |
| 200 | * |
| 201 | * @param routes the routes with the data |
| 202 | * @return JSON array with the routes |
| 203 | */ |
| 204 | private JsonNode json(Collection<BgpRouteEntry> routes) { |
| 205 | ObjectMapper mapper = new ObjectMapper(); |
| 206 | ArrayNode result = mapper.createArrayNode(); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 207 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 208 | for (BgpRouteEntry route : routes) { |
| 209 | result.add(json(mapper, route)); |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 210 | } |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 211 | return result; |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 214 | /** |
| 215 | * Produces JSON object for a route. |
| 216 | * |
| 217 | * @param mapper the JSON object mapper to use |
| 218 | * @param route the route with the data |
| 219 | * @return JSON object for the route |
| 220 | */ |
| 221 | private ObjectNode json(ObjectMapper mapper, BgpRouteEntry route) { |
| 222 | ObjectNode result = mapper.createObjectNode(); |
| 223 | |
| 224 | result.put("prefix", route.prefix().toString()); |
| 225 | result.put("nextHop", route.nextHop().toString()); |
| 226 | result.put("bgpId", route.getBgpSession().getRemoteBgpId().toString()); |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame^] | 227 | result.put("origin", Update.Origin.typeToString(route.getOrigin())); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 228 | result.put("asPath", json(mapper, route.getAsPath())); |
| 229 | result.put("localPref", route.getLocalPref()); |
| 230 | result.put("multiExitDisc", route.getMultiExitDisc()); |
| 231 | |
| 232 | return result; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Produces JSON object for an AS path. |
| 237 | * |
| 238 | * @param mapper the JSON object mapper to use |
| 239 | * @param asPath the AS path with the data |
| 240 | * @return JSON object for the AS path |
| 241 | */ |
| 242 | private ObjectNode json(ObjectMapper mapper, BgpRouteEntry.AsPath asPath) { |
| 243 | ObjectNode result = mapper.createObjectNode(); |
| 244 | ArrayNode pathSegmentsJson = mapper.createArrayNode(); |
| 245 | for (BgpRouteEntry.PathSegment pathSegment : asPath.getPathSegments()) { |
| 246 | ObjectNode pathSegmentJson = mapper.createObjectNode(); |
| 247 | pathSegmentJson.put("type", |
Pavlin Radoslavov | dfde7ab | 2014-12-04 14:06:39 -0800 | [diff] [blame^] | 248 | Update.AsPath.typeToString(pathSegment.getType())); |
Pavlin Radoslavov | 2ce1c52 | 2014-11-07 10:32:37 -0800 | [diff] [blame] | 249 | ArrayNode segmentAsNumbersJson = mapper.createArrayNode(); |
| 250 | for (Long asNumber : pathSegment.getSegmentAsNumbers()) { |
| 251 | segmentAsNumbersJson.add(asNumber); |
| 252 | } |
| 253 | pathSegmentJson.put("segmentAsNumbers", segmentAsNumbersJson); |
| 254 | pathSegmentsJson.add(pathSegmentJson); |
| 255 | } |
| 256 | result.put("pathSegments", pathSegmentsJson); |
| 257 | |
| 258 | return result; |
| 259 | } |
Jonathan Hart | 0b04bed | 2014-10-16 16:39:19 -0700 | [diff] [blame] | 260 | } |