Pavlin Radoslavov

Added SDN-IP CLI command to show the BGP neighbors:

  onos:bgp-neighbors
OR
  onos:bgp-neighbors -n <NeighborID>
  onos:bgp-neighbors --neighbor <NeighborID>

Change-Id: I4e4185d9484384d9e3aa5304c897410b23a24238
...@@ -30,6 +30,7 @@ import org.onlab.onos.core.CoreService; ...@@ -30,6 +30,7 @@ import org.onlab.onos.core.CoreService;
30 import org.onlab.onos.net.host.HostService; 30 import org.onlab.onos.net.host.HostService;
31 import org.onlab.onos.net.intent.IntentService; 31 import org.onlab.onos.net.intent.IntentService;
32 import org.onlab.onos.sdnip.bgp.BgpRouteEntry; 32 import org.onlab.onos.sdnip.bgp.BgpRouteEntry;
33 +import org.onlab.onos.sdnip.bgp.BgpSession;
33 import org.onlab.onos.sdnip.bgp.BgpSessionManager; 34 import org.onlab.onos.sdnip.bgp.BgpSessionManager;
34 import org.onlab.onos.sdnip.config.SdnIpConfigReader; 35 import org.onlab.onos.sdnip.config.SdnIpConfigReader;
35 import org.slf4j.Logger; 36 import org.slf4j.Logger;
...@@ -97,6 +98,11 @@ public class SdnIp implements SdnIpService { ...@@ -97,6 +98,11 @@ public class SdnIp implements SdnIpService {
97 } 98 }
98 99
99 @Override 100 @Override
101 + public Collection<BgpSession> getBgpSessions() {
102 + return bgpSessionManager.getBgpSessions();
103 + }
104 +
105 + @Override
100 public Collection<BgpRouteEntry> getBgpRoutes() { 106 public Collection<BgpRouteEntry> getBgpRoutes() {
101 return bgpSessionManager.getBgpRoutes(); 107 return bgpSessionManager.getBgpRoutes();
102 } 108 }
......
...@@ -18,12 +18,20 @@ package org.onlab.onos.sdnip; ...@@ -18,12 +18,20 @@ package org.onlab.onos.sdnip;
18 import java.util.Collection; 18 import java.util.Collection;
19 19
20 import org.onlab.onos.sdnip.bgp.BgpRouteEntry; 20 import org.onlab.onos.sdnip.bgp.BgpRouteEntry;
21 +import org.onlab.onos.sdnip.bgp.BgpSession;
21 22
22 /** 23 /**
23 * Service interface exported by SDN-IP. 24 * Service interface exported by SDN-IP.
24 */ 25 */
25 public interface SdnIpService { 26 public interface SdnIpService {
26 /** 27 /**
28 + * Gets the BGP sessions.
29 + *
30 + * @return the BGP sessions
31 + */
32 + public Collection<BgpSession> getBgpSessions();
33 +
34 + /**
27 * Gets the BGP routes. 35 * Gets the BGP routes.
28 * 36 *
29 * @return the BGP routes 37 * @return the BGP routes
......
1 +/*
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 + */
16 +package org.onlab.onos.sdnip.cli;
17 +
18 +import java.util.Collection;
19 +
20 +import com.fasterxml.jackson.databind.JsonNode;
21 +import com.fasterxml.jackson.databind.ObjectMapper;
22 +import com.fasterxml.jackson.databind.node.ArrayNode;
23 +import com.fasterxml.jackson.databind.node.ObjectNode;
24 +import org.apache.karaf.shell.commands.Command;
25 +import org.apache.karaf.shell.commands.Option;
26 +import org.onlab.onos.cli.AbstractShellCommand;
27 +import org.onlab.onos.sdnip.SdnIpService;
28 +import org.onlab.onos.sdnip.bgp.BgpSession;
29 +
30 +/**
31 + * Command to show the BGP neighbors.
32 + */
33 +@Command(scope = "onos", name = "bgp-neighbors",
34 + description = "Lists the BGP neighbors")
35 +public 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 =
46 + " Local router ID %s, IP %s, BGP version %d, Hold time %d";
47 +
48 + @Override
49 + protected void execute() {
50 + SdnIpService service = get(SdnIpService.class);
51 + Collection<BgpSession> bgpSessions = service.getBgpSessions();
52 +
53 + if (bgpNeighbor != null) {
54 + // Print a single neighbor (if found)
55 + BgpSession foundBgpSession = null;
56 + for (BgpSession bgpSession : bgpSessions) {
57 + if (bgpSession.getRemoteBgpId().toString().equals(bgpNeighbor)) {
58 + foundBgpSession = bgpSession;
59 + break;
60 + }
61 + }
62 + if (foundBgpSession != null) {
63 + printNeighbor(foundBgpSession);
64 + } else {
65 + print("BGP neighbor %s not found", bgpNeighbor);
66 + }
67 + return;
68 + }
69 +
70 + // Print all neighbors
71 + printNeighbors(bgpSessions);
72 + }
73 +
74 + /**
75 + * Prints all BGP neighbors.
76 + *
77 + * @param bgpSessions the BGP sessions for the neighbors to print
78 + */
79 + private void printNeighbors(Collection<BgpSession> bgpSessions) {
80 + if (outputJson()) {
81 + print("%s", json(bgpSessions));
82 + } else {
83 + for (BgpSession bgpSession : bgpSessions) {
84 + printNeighbor(bgpSession);
85 + }
86 + }
87 + }
88 +
89 + /**
90 + * Prints a BGP neighbor.
91 + *
92 + * @param bgpSession the BGP session for the neighbor to print
93 + */
94 + private void printNeighbor(BgpSession bgpSession) {
95 + print(FORMAT_NEIGHBOR_LINE1,
96 + bgpSession.getRemoteBgpId().toString(),
97 + bgpSession.getRemoteAs(),
98 + bgpSession.getLocalAs());
99 + print(FORMAT_NEIGHBOR_LINE2,
100 + bgpSession.getRemoteBgpId().toString(),
101 + bgpSession.getRemoteAddress().toString(),
102 + bgpSession.getRemoteBgpVersion(),
103 + bgpSession.getRemoteHoldtime());
104 + print(FORMAT_NEIGHBOR_LINE3,
105 + bgpSession.getLocalBgpId().toString(),
106 + bgpSession.getLocalAddress().toString(),
107 + bgpSession.getLocalBgpVersion(),
108 + bgpSession.getLocalHoldtime());
109 + }
110 +
111 + /**
112 + * Produces a JSON array of BGP neighbors.
113 + *
114 + * @param bgpSessions the BGP sessions with the data
115 + * @return JSON array with the neighbors
116 + */
117 + private JsonNode json(Collection<BgpSession> bgpSessions) {
118 + ObjectMapper mapper = new ObjectMapper();
119 + ArrayNode result = mapper.createArrayNode();
120 +
121 + for (BgpSession bgpSession : bgpSessions) {
122 + result.add(json(mapper, bgpSession));
123 + }
124 + return result;
125 + }
126 +
127 + /**
128 + * Produces JSON object for a BGP neighbor.
129 + *
130 + * @param mapper the JSON object mapper to use
131 + * @param bgpSession the BGP session with the data
132 + * @return JSON object for the route
133 + */
134 + private ObjectNode json(ObjectMapper mapper, BgpSession bgpSession) {
135 + ObjectNode result = mapper.createObjectNode();
136 +
137 + result.put("remoteAddress", bgpSession.getRemoteAddress().toString());
138 + result.put("remoteBgpVersion", bgpSession.getRemoteBgpVersion());
139 + result.put("remoteAs", bgpSession.getRemoteAs());
140 + result.put("remoteHoldtime", bgpSession.getRemoteHoldtime());
141 + result.put("remoteBgpId", bgpSession.getRemoteBgpId().toString());
142 + //
143 + result.put("localAddress", bgpSession.getLocalAddress().toString());
144 + result.put("localBgpVersion", bgpSession.getLocalBgpVersion());
145 + result.put("localAs", bgpSession.getLocalAs());
146 + result.put("localHoldtime", bgpSession.getLocalHoldtime());
147 + result.put("localBgpId", bgpSession.getLocalBgpId().toString());
148 +
149 + return result;
150 + }
151 +}
...@@ -17,6 +17,9 @@ ...@@ -17,6 +17,9 @@
17 17
18 <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0"> 18 <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
19 <command> 19 <command>
20 + <action class="org.onlab.onos.sdnip.cli.BgpNeighborsListCommand"/>
21 + </command>
22 + <command>
20 <action class="org.onlab.onos.sdnip.cli.BgpRoutesListCommand"/> 23 <action class="org.onlab.onos.sdnip.cli.BgpRoutesListCommand"/>
21 </command> 24 </command>
22 <command> 25 <command>
......