blob: 0080b373334badc2982365d736e5482aa258fde7 [file] [log] [blame]
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -08003 *
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 Hartf4bd0482017-01-27 15:11:18 -080016
Jonathan Hart41349e92015-02-09 14:14:02 -080017package org.onosproject.routing.cli;
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080018
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070023import org.apache.karaf.shell.api.action.Command;
24import org.apache.karaf.shell.api.action.Option;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cli.AbstractShellCommand;
Jonathan Hart41349e92015-02-09 14:14:02 -080026import org.onosproject.routing.bgp.BgpInfoService;
27import org.onosproject.routing.bgp.BgpSession;
28
29import java.util.Collection;
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080030
31/**
32 * Command to show the BGP neighbors.
33 */
34@Command(scope = "onos", name = "bgp-neighbors",
35 description = "Lists the BGP neighbors")
36public 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 Radoslavov278cdde2014-12-16 14:09:31 -080047 " Remote AFI/SAFI IPv4 Unicast %s Multicast %s, IPv6 Unicast %s Multicast %s";
48 private static final String FORMAT_NEIGHBOR_LINE4 =
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080049 " Local router ID %s, IP %s, BGP version %d, Hold time %d";
Pavlin Radoslavov278cdde2014-12-16 14:09:31 -080050 private static final String FORMAT_NEIGHBOR_LINE5 =
51 " Local AFI/SAFI IPv4 Unicast %s Multicast %s, IPv6 Unicast %s Multicast %s";
Kunihiro Ishiguro923d9d82014-12-21 15:47:28 +090052 private static final String FORMAT_NEIGHBOR_LINE6 =
53 " 4 Octet AS Capability: %s %s";
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080054
55 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070056 protected void doExecute() {
Jonathan Hart41349e92015-02-09 14:14:02 -080057 BgpInfoService service = AbstractShellCommand.get(BgpInfoService.class);
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080058 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 Radoslavov8a36ce32015-01-28 12:26:57 -080064 if (bgpSession.remoteInfo().bgpId().toString().equals(bgpNeighbor)) {
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080065 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 Radoslavov8a36ce32015-01-28 12:26:57 -0800103 bgpSession.remoteInfo().bgpId().toString(),
104 bgpSession.remoteInfo().asNumber(),
105 bgpSession.localInfo().asNumber());
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800106 print(FORMAT_NEIGHBOR_LINE2,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800107 bgpSession.remoteInfo().bgpId().toString(),
108 bgpSession.remoteInfo().address().toString(),
109 bgpSession.remoteInfo().bgpVersion(),
110 bgpSession.remoteInfo().holdtime());
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800111 print(FORMAT_NEIGHBOR_LINE3,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800112 bgpSession.remoteInfo().ipv4Unicast() ? "YES" : "NO",
113 bgpSession.remoteInfo().ipv4Multicast() ? "YES" : "NO",
114 bgpSession.remoteInfo().ipv6Unicast() ? "YES" : "NO",
115 bgpSession.remoteInfo().ipv6Multicast() ? "YES" : "NO");
Pavlin Radoslavov278cdde2014-12-16 14:09:31 -0800116 print(FORMAT_NEIGHBOR_LINE4,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800117 bgpSession.localInfo().bgpId().toString(),
118 bgpSession.localInfo().address().toString(),
119 bgpSession.localInfo().bgpVersion(),
120 bgpSession.localInfo().holdtime());
Pavlin Radoslavov278cdde2014-12-16 14:09:31 -0800121 print(FORMAT_NEIGHBOR_LINE5,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800122 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 Ishiguro923d9d82014-12-21 15:47:28 +0900128 print(FORMAT_NEIGHBOR_LINE6,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800129 bgpSession.localInfo().as4OctetCapability() ? "Advertised" : "",
130 bgpSession.remoteInfo().as4OctetCapability() ? "Received" : "");
Kunihiro Ishiguro923d9d82014-12-21 15:47:28 +0900131 }
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800132 }
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 Radoslavov8a36ce32015-01-28 12:26:57 -0800160 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 Radoslavov0c84da82014-11-07 17:53:34 -0800170 //
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800171 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 Radoslavov0c84da82014-11-07 17:53:34 -0800181
182 return result;
183 }
184}