blob: 7fc5278fc86243b9937cb70790a92390f64d79ce [file] [log] [blame]
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.sdnip.cli;
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -080017
18import java.util.Collection;
19
20import com.fasterxml.jackson.databind.JsonNode;
21import com.fasterxml.jackson.databind.ObjectMapper;
22import com.fasterxml.jackson.databind.node.ArrayNode;
23import com.fasterxml.jackson.databind.node.ObjectNode;
24import org.apache.karaf.shell.commands.Command;
25import org.apache.karaf.shell.commands.Option;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.sdnip.SdnIpService;
28import org.onosproject.sdnip.bgp.BgpSession;
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() {
56 SdnIpService service = get(SdnIpService.class);
57 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) {
63 if (bgpSession.getRemoteBgpId().toString().equals(bgpNeighbor)) {
64 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,
102 bgpSession.getRemoteBgpId().toString(),
103 bgpSession.getRemoteAs(),
104 bgpSession.getLocalAs());
105 print(FORMAT_NEIGHBOR_LINE2,
106 bgpSession.getRemoteBgpId().toString(),
107 bgpSession.getRemoteAddress().toString(),
108 bgpSession.getRemoteBgpVersion(),
109 bgpSession.getRemoteHoldtime());
110 print(FORMAT_NEIGHBOR_LINE3,
Pavlin Radoslavov278cdde2014-12-16 14:09:31 -0800111 bgpSession.getRemoteIpv4Unicast() ? "YES" : "NO",
112 bgpSession.getRemoteIpv4Multicast() ? "YES" : "NO",
113 bgpSession.getRemoteIpv6Unicast() ? "YES" : "NO",
114 bgpSession.getRemoteIpv6Multicast() ? "YES" : "NO");
115 print(FORMAT_NEIGHBOR_LINE4,
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800116 bgpSession.getLocalBgpId().toString(),
117 bgpSession.getLocalAddress().toString(),
118 bgpSession.getLocalBgpVersion(),
119 bgpSession.getLocalHoldtime());
Pavlin Radoslavov278cdde2014-12-16 14:09:31 -0800120 print(FORMAT_NEIGHBOR_LINE5,
121 bgpSession.getLocalIpv4Unicast() ? "YES" : "NO",
122 bgpSession.getLocalIpv4Multicast() ? "YES" : "NO",
123 bgpSession.getLocalIpv6Unicast() ? "YES" : "NO",
124 bgpSession.getLocalIpv6Multicast() ? "YES" : "NO");
Kunihiro Ishiguro923d9d82014-12-21 15:47:28 +0900125 if (bgpSession.getLocalAs4OctetCapability() || bgpSession.getRemoteAs4OctetCapability()) {
126 print(FORMAT_NEIGHBOR_LINE6,
127 bgpSession.getLocalAs4OctetCapability() ? "Advertised" : "",
128 bgpSession.getRemoteAs4OctetCapability() ? "Received" : "");
129 }
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800130 }
131
132 /**
133 * Produces a JSON array of BGP neighbors.
134 *
135 * @param bgpSessions the BGP sessions with the data
136 * @return JSON array with the neighbors
137 */
138 private JsonNode json(Collection<BgpSession> bgpSessions) {
139 ObjectMapper mapper = new ObjectMapper();
140 ArrayNode result = mapper.createArrayNode();
141
142 for (BgpSession bgpSession : bgpSessions) {
143 result.add(json(mapper, bgpSession));
144 }
145 return result;
146 }
147
148 /**
149 * Produces JSON object for a BGP neighbor.
150 *
151 * @param mapper the JSON object mapper to use
152 * @param bgpSession the BGP session with the data
153 * @return JSON object for the route
154 */
155 private ObjectNode json(ObjectMapper mapper, BgpSession bgpSession) {
156 ObjectNode result = mapper.createObjectNode();
157
158 result.put("remoteAddress", bgpSession.getRemoteAddress().toString());
159 result.put("remoteBgpVersion", bgpSession.getRemoteBgpVersion());
160 result.put("remoteAs", bgpSession.getRemoteAs());
161 result.put("remoteHoldtime", bgpSession.getRemoteHoldtime());
162 result.put("remoteBgpId", bgpSession.getRemoteBgpId().toString());
Pavlin Radoslavov278cdde2014-12-16 14:09:31 -0800163 result.put("remoteIpv4Unicast", bgpSession.getRemoteIpv4Unicast());
164 result.put("remoteIpv4Multicast", bgpSession.getRemoteIpv4Multicast());
165 result.put("remoteIpv6Unicast", bgpSession.getRemoteIpv6Unicast());
166 result.put("remoteIpv6Multicast", bgpSession.getRemoteIpv6Multicast());
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800167 //
168 result.put("localAddress", bgpSession.getLocalAddress().toString());
169 result.put("localBgpVersion", bgpSession.getLocalBgpVersion());
170 result.put("localAs", bgpSession.getLocalAs());
171 result.put("localHoldtime", bgpSession.getLocalHoldtime());
172 result.put("localBgpId", bgpSession.getLocalBgpId().toString());
Pavlin Radoslavov278cdde2014-12-16 14:09:31 -0800173 result.put("localIpv4Unicast", bgpSession.getLocalIpv4Unicast());
174 result.put("localIpv4Multicast", bgpSession.getLocalIpv4Multicast());
175 result.put("localIpv6Unicast", bgpSession.getLocalIpv6Unicast());
176 result.put("localIpv6Multicast", bgpSession.getLocalIpv6Multicast());
Pavlin Radoslavov0c84da82014-11-07 17:53:34 -0800177
178 return result;
179 }
180}