blob: 05f04040f5dba5d739f65cccdaf51364e40a0f42 [file] [log] [blame]
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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 Hart41349e92015-02-09 14:14:02 -080016package org.onosproject.routing.cli;
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080017
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.apache.karaf.shell.commands.Command;
23import org.apache.karaf.shell.commands.Option;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.cli.AbstractShellCommand;
Jonathan Hart41349e92015-02-09 14:14:02 -080025import org.onosproject.routing.bgp.BgpInfoService;
26import org.onosproject.routing.bgp.BgpSession;
27
28import java.util.Collection;
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080029
30/**
31 * Command to show the BGP neighbors.
32 */
33@Command(scope = "onos", name = "bgp-neighbors",
34 description = "Lists the BGP neighbors")
35public 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 Radoslavov278cdde2014-12-16 14:09:31 -080046 " Remote AFI/SAFI IPv4 Unicast %s Multicast %s, IPv6 Unicast %s Multicast %s";
47 private static final String FORMAT_NEIGHBOR_LINE4 =
Pavlin Radoslavovdfde7ab2014-12-04 14:06:39 -080048 " Local router ID %s, IP %s, BGP version %d, Hold time %d";
Pavlin Radoslavov278cdde2014-12-16 14:09:31 -080049 private static final String FORMAT_NEIGHBOR_LINE5 =
50 " Local AFI/SAFI IPv4 Unicast %s Multicast %s, IPv6 Unicast %s Multicast %s";
Kunihiro Ishiguro923d9d82014-12-21 15:47:28 +090051 private static final String FORMAT_NEIGHBOR_LINE6 =
52 " 4 Octet AS Capability: %s %s";
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080053
54 @Override
55 protected void execute() {
Jonathan Hart41349e92015-02-09 14:14:02 -080056 BgpInfoService service = AbstractShellCommand.get(BgpInfoService.class);
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080057 Collection<BgpSession> bgpSessions = service.getBgpSessions();
58
59 if (bgpNeighbor != null) {
60 // Print a single neighbor (if found)
61 BgpSession foundBgpSession = null;
62 for (BgpSession bgpSession : bgpSessions) {
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -080063 if (bgpSession.remoteInfo().bgpId().toString().equals(bgpNeighbor)) {
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080064 foundBgpSession = bgpSession;
65 break;
66 }
67 }
68 if (foundBgpSession != null) {
69 printNeighbor(foundBgpSession);
70 } else {
71 print("BGP neighbor %s not found", bgpNeighbor);
72 }
73 return;
74 }
75
76 // Print all neighbors
77 printNeighbors(bgpSessions);
78 }
79
80 /**
81 * Prints all BGP neighbors.
82 *
83 * @param bgpSessions the BGP sessions for the neighbors to print
84 */
85 private void printNeighbors(Collection<BgpSession> bgpSessions) {
86 if (outputJson()) {
87 print("%s", json(bgpSessions));
88 } else {
89 for (BgpSession bgpSession : bgpSessions) {
90 printNeighbor(bgpSession);
91 }
92 }
93 }
94
95 /**
96 * Prints a BGP neighbor.
97 *
98 * @param bgpSession the BGP session for the neighbor to print
99 */
100 private void printNeighbor(BgpSession bgpSession) {
101 print(FORMAT_NEIGHBOR_LINE1,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800102 bgpSession.remoteInfo().bgpId().toString(),
103 bgpSession.remoteInfo().asNumber(),
104 bgpSession.localInfo().asNumber());
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800105 print(FORMAT_NEIGHBOR_LINE2,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800106 bgpSession.remoteInfo().bgpId().toString(),
107 bgpSession.remoteInfo().address().toString(),
108 bgpSession.remoteInfo().bgpVersion(),
109 bgpSession.remoteInfo().holdtime());
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800110 print(FORMAT_NEIGHBOR_LINE3,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800111 bgpSession.remoteInfo().ipv4Unicast() ? "YES" : "NO",
112 bgpSession.remoteInfo().ipv4Multicast() ? "YES" : "NO",
113 bgpSession.remoteInfo().ipv6Unicast() ? "YES" : "NO",
114 bgpSession.remoteInfo().ipv6Multicast() ? "YES" : "NO");
Pavlin Radoslavov278cdde2014-12-16 14:09:31 -0800115 print(FORMAT_NEIGHBOR_LINE4,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800116 bgpSession.localInfo().bgpId().toString(),
117 bgpSession.localInfo().address().toString(),
118 bgpSession.localInfo().bgpVersion(),
119 bgpSession.localInfo().holdtime());
Pavlin Radoslavov278cdde2014-12-16 14:09:31 -0800120 print(FORMAT_NEIGHBOR_LINE5,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800121 bgpSession.localInfo().ipv4Unicast() ? "YES" : "NO",
122 bgpSession.localInfo().ipv4Multicast() ? "YES" : "NO",
123 bgpSession.localInfo().ipv6Unicast() ? "YES" : "NO",
124 bgpSession.localInfo().ipv6Multicast() ? "YES" : "NO");
125 if (bgpSession.localInfo().as4OctetCapability() ||
126 bgpSession.remoteInfo().as4OctetCapability()) {
Kunihiro Ishiguro923d9d82014-12-21 15:47:28 +0900127 print(FORMAT_NEIGHBOR_LINE6,
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800128 bgpSession.localInfo().as4OctetCapability() ? "Advertised" : "",
129 bgpSession.remoteInfo().as4OctetCapability() ? "Received" : "");
Kunihiro Ishiguro923d9d82014-12-21 15:47:28 +0900130 }
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800131 }
132
133 /**
134 * Produces a JSON array of BGP neighbors.
135 *
136 * @param bgpSessions the BGP sessions with the data
137 * @return JSON array with the neighbors
138 */
139 private JsonNode json(Collection<BgpSession> bgpSessions) {
140 ObjectMapper mapper = new ObjectMapper();
141 ArrayNode result = mapper.createArrayNode();
142
143 for (BgpSession bgpSession : bgpSessions) {
144 result.add(json(mapper, bgpSession));
145 }
146 return result;
147 }
148
149 /**
150 * Produces JSON object for a BGP neighbor.
151 *
152 * @param mapper the JSON object mapper to use
153 * @param bgpSession the BGP session with the data
154 * @return JSON object for the route
155 */
156 private ObjectNode json(ObjectMapper mapper, BgpSession bgpSession) {
157 ObjectNode result = mapper.createObjectNode();
158
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800159 result.put("remoteAddress", bgpSession.remoteInfo().address().toString());
160 result.put("remoteBgpVersion", bgpSession.remoteInfo().bgpVersion());
161 result.put("remoteAs", bgpSession.remoteInfo().asNumber());
162 result.put("remoteAs4", bgpSession.remoteInfo().as4Number());
163 result.put("remoteHoldtime", bgpSession.remoteInfo().holdtime());
164 result.put("remoteBgpId", bgpSession.remoteInfo().bgpId().toString());
165 result.put("remoteIpv4Unicast", bgpSession.remoteInfo().ipv4Unicast());
166 result.put("remoteIpv4Multicast", bgpSession.remoteInfo().ipv4Multicast());
167 result.put("remoteIpv6Unicast", bgpSession.remoteInfo().ipv6Unicast());
168 result.put("remoteIpv6Multicast", bgpSession.remoteInfo().ipv6Multicast());
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800169 //
Pavlin Radoslavov8a36ce32015-01-28 12:26:57 -0800170 result.put("localAddress", bgpSession.localInfo().address().toString());
171 result.put("localBgpVersion", bgpSession.localInfo().bgpVersion());
172 result.put("localAs", bgpSession.localInfo().asNumber());
173 result.put("localAs4", bgpSession.localInfo().as4Number());
174 result.put("localHoldtime", bgpSession.localInfo().holdtime());
175 result.put("localBgpId", bgpSession.localInfo().bgpId().toString());
176 result.put("localIpv4Unicast", bgpSession.localInfo().ipv4Unicast());
177 result.put("localIpv4Multicast", bgpSession.localInfo().ipv4Multicast());
178 result.put("localIpv6Unicast", bgpSession.localInfo().ipv6Unicast());
179 result.put("localIpv6Multicast", bgpSession.localInfo().ipv6Multicast());
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800180
181 return result;
182 }
183}