blob: 43673c36f9a86899a5af7cb20a1d65aa1b819271 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip.cli;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070017
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080018import java.util.ArrayList;
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080019import java.util.Collection;
20
21import com.fasterxml.jackson.databind.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.node.ArrayNode;
24import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070025import org.apache.karaf.shell.commands.Command;
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080026import org.apache.karaf.shell.commands.Option;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.cli.AbstractShellCommand;
28import org.onosproject.sdnip.SdnIpService;
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080029import org.onosproject.sdnip.bgp.BgpConstants.Update;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.sdnip.bgp.BgpRouteEntry;
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080031import org.onosproject.sdnip.bgp.BgpSession;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070032
33/**
34 * Command to show the routes learned through BGP.
35 */
36@Command(scope = "onos", name = "bgp-routes",
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080037 description = "Lists all BGP best routes")
Jonathan Hart0b04bed2014-10-16 16:39:19 -070038public class BgpRoutesListCommand extends AbstractShellCommand {
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080039 @Option(name = "-s", aliases = "--summary",
40 description = "BGP routes summary",
41 required = false, multiValued = false)
42 private boolean routesSummary = false;
Jonathan Hart0b04bed2014-10-16 16:39:19 -070043
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080044 @Option(name = "-n", aliases = "--neighbor",
45 description = "Routes from a BGP neighbor",
46 required = false, multiValued = false)
47 private String bgpNeighbor;
48
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080049 private static final String FORMAT_SUMMARY = "Total BGP routes = %d";
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080050 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 Hart0b04bed2014-10-16 16:39:19 -070056
57 @Override
58 protected void execute() {
59 SdnIpService service = get(SdnIpService.class);
60
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -080061 // Print summary of the routes
62 if (routesSummary) {
63 printSummary(service.getBgpRoutes());
64 return;
65 }
66
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080067 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 Radoslavov2ce1c522014-11-07 10:32:37 -080088 }
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 Hart0b04bed2014-10-16 16:39:19 -0700103 }
104 }
105
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800106 /**
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 Radoslavovdfde7ab2014-12-04 14:06:39 -0800115 print(FORMAT_HEADER);
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800116 for (BgpRouteEntry route : routes) {
117 printRoute(route);
118 }
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800119 print(FORMAT_SUMMARY, routes.size());
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800120 }
121 }
122
123 /**
124 * Prints a BGP route.
125 *
126 * @param route the route to print
127 */
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700128 private void printRoute(BgpRouteEntry route) {
129 if (route != null) {
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800130 print(FORMAT_ROUTE_LINE1, route.prefix(), route.nextHop(),
131 Update.Origin.typeToString(route.getOrigin()),
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800132 route.getLocalPref(), route.getMultiExitDisc(),
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800133 route.getBgpSession().getRemoteBgpId());
134 print(FORMAT_ROUTE_LINE2, asPath4Cli(route.getAsPath()));
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700135 }
136 }
137
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800138 /**
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -0800139 * 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 Radoslavov2ce1c522014-11-07 10:32:37 -0800199 * 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 Hart0b04bed2014-10-16 16:39:19 -0700207
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800208 for (BgpRouteEntry route : routes) {
209 result.add(json(mapper, route));
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700210 }
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800211 return result;
Jonathan Hart0b04bed2014-10-16 16:39:19 -0700212 }
213
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800214 /**
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 Radoslavovdfde7ab2014-12-04 14:06:39 -0800227 result.put("origin", Update.Origin.typeToString(route.getOrigin()));
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800228 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 Radoslavovdfde7ab2014-12-04 14:06:39 -0800248 Update.AsPath.typeToString(pathSegment.getType()));
Pavlin Radoslavov2ce1c522014-11-07 10:32:37 -0800249 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 Hart0b04bed2014-10-16 16:39:19 -0700260}