blob: b220fd5b0558e4b267d3a58d655c95028b0f6670 [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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070025import org.apache.karaf.shell.api.action.lifecycle.Service;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cli.AbstractShellCommand;
Jonathan Hart41349e92015-02-09 14:14:02 -080027import org.onosproject.routing.bgp.BgpInfoService;
28import org.onosproject.routing.bgp.BgpSession;
29
30import java.util.Collection;
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080031
32/**
33 * Command to show the BGP neighbors.
34 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070035@Service
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080036@Command(scope = "onos", name = "bgp-neighbors",
37 description = "Lists the BGP neighbors")
38public 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 Radoslavov278cdde2014-12-16 14:09:31 -080049 " Remote AFI/SAFI IPv4 Unicast %s Multicast %s, IPv6 Unicast %s Multicast %s";
50 private static final String FORMAT_NEIGHBOR_LINE4 =
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080051 " Local router ID %s, IP %s, BGP version %d, Hold time %d";
Pavlin Radoslavov278cdde2014-12-16 14:09:31 -080052 private static final String FORMAT_NEIGHBOR_LINE5 =
53 " Local AFI/SAFI IPv4 Unicast %s Multicast %s, IPv6 Unicast %s Multicast %s";
Kunihiro Ishiguro923d9d82014-12-21 15:47:28 +090054 private static final String FORMAT_NEIGHBOR_LINE6 =
55 " 4 Octet AS Capability: %s %s";
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080056
57 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070058 protected void doExecute() {
Jonathan Hart41349e92015-02-09 14:14:02 -080059 BgpInfoService service = AbstractShellCommand.get(BgpInfoService.class);
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080060 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 Radoslavov8a36ce32015-01-28 12:26:57 -080066 if (bgpSession.remoteInfo().bgpId().toString().equals(bgpNeighbor)) {
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080067 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 Radoslavov8a36ce32015-01-28 12:26:57 -0800105 bgpSession.remoteInfo().bgpId().toString(),
106 bgpSession.remoteInfo().asNumber(),
107 bgpSession.localInfo().asNumber());
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800108 print(FORMAT_NEIGHBOR_LINE2,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800109 bgpSession.remoteInfo().bgpId().toString(),
110 bgpSession.remoteInfo().address().toString(),
111 bgpSession.remoteInfo().bgpVersion(),
112 bgpSession.remoteInfo().holdtime());
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800113 print(FORMAT_NEIGHBOR_LINE3,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800114 bgpSession.remoteInfo().ipv4Unicast() ? "YES" : "NO",
115 bgpSession.remoteInfo().ipv4Multicast() ? "YES" : "NO",
116 bgpSession.remoteInfo().ipv6Unicast() ? "YES" : "NO",
117 bgpSession.remoteInfo().ipv6Multicast() ? "YES" : "NO");
Pavlin Radoslavov278cdde2014-12-16 14:09:31 -0800118 print(FORMAT_NEIGHBOR_LINE4,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800119 bgpSession.localInfo().bgpId().toString(),
120 bgpSession.localInfo().address().toString(),
121 bgpSession.localInfo().bgpVersion(),
122 bgpSession.localInfo().holdtime());
Pavlin Radoslavov278cdde2014-12-16 14:09:31 -0800123 print(FORMAT_NEIGHBOR_LINE5,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800124 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 Ishiguro923d9d82014-12-21 15:47:28 +0900130 print(FORMAT_NEIGHBOR_LINE6,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800131 bgpSession.localInfo().as4OctetCapability() ? "Advertised" : "",
132 bgpSession.remoteInfo().as4OctetCapability() ? "Received" : "");
Kunihiro Ishiguro923d9d82014-12-21 15:47:28 +0900133 }
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800134 }
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 Radoslavov8a36ce32015-01-28 12:26:57 -0800162 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 Radoslavov0c84da82014-11-07 17:53:34 -0800172 //
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800173 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 Radoslavov0c84da82014-11-07 17:53:34 -0800183
184 return result;
185 }
186}