Jonathan Hart

Added CLI command to view address bindings

Change-Id: I1f1ac8ec7766691fb7dadee33a6f75cc560b0da8
...@@ -15,16 +15,18 @@ ...@@ -15,16 +15,18 @@
15 */ 15 */
16 package org.onlab.onos.cli; 16 package org.onlab.onos.cli;
17 17
18 -import org.onlab.onos.core.ApplicationId; 18 +import java.util.Comparator;
19 +
19 import org.onlab.onos.cluster.ControllerNode; 20 import org.onlab.onos.cluster.ControllerNode;
21 +import org.onlab.onos.core.ApplicationId;
22 +import org.onlab.onos.net.ConnectPoint;
20 import org.onlab.onos.net.Element; 23 import org.onlab.onos.net.Element;
21 import org.onlab.onos.net.ElementId; 24 import org.onlab.onos.net.ElementId;
22 import org.onlab.onos.net.Port; 25 import org.onlab.onos.net.Port;
23 import org.onlab.onos.net.flow.FlowRule; 26 import org.onlab.onos.net.flow.FlowRule;
27 +import org.onlab.onos.net.host.PortAddresses;
24 import org.onlab.onos.net.topology.TopologyCluster; 28 import org.onlab.onos.net.topology.TopologyCluster;
25 29
26 -import java.util.Comparator;
27 -
28 /** 30 /**
29 * Various comparators. 31 * Various comparators.
30 */ 32 */
...@@ -84,4 +86,21 @@ public final class Comparators { ...@@ -84,4 +86,21 @@ public final class Comparators {
84 } 86 }
85 }; 87 };
86 88
89 + public static final Comparator<ConnectPoint> CONNECT_POINT_COMPARATOR = new Comparator<ConnectPoint>() {
90 + @Override
91 + public int compare(ConnectPoint o1, ConnectPoint o2) {
92 + int compareId = ELEMENT_ID_COMPARATOR.compare(o1.elementId(), o2.elementId());
93 + return (compareId != 0) ?
94 + compareId :
95 + Long.signum(o1.port().toLong() - o2.port().toLong());
96 + }
97 + };
98 +
99 + public static final Comparator<PortAddresses> ADDRESSES_COMPARATOR = new Comparator<PortAddresses>() {
100 + @Override
101 + public int compare(PortAddresses arg0, PortAddresses arg1) {
102 + return CONNECT_POINT_COMPARATOR.compare(arg0.connectPoint(), arg1.connectPoint());
103 + }
104 + };
105 +
87 } 106 }
......
1 +package org.onlab.onos.cli.net;
2 +
3 +import java.util.Collections;
4 +import java.util.List;
5 +import java.util.Set;
6 +
7 +import org.apache.karaf.shell.commands.Command;
8 +import org.onlab.onos.cli.AbstractShellCommand;
9 +import org.onlab.onos.cli.Comparators;
10 +import org.onlab.onos.net.host.HostService;
11 +import org.onlab.onos.net.host.InterfaceIpAddress;
12 +import org.onlab.onos.net.host.PortAddresses;
13 +
14 +import com.google.common.collect.Lists;
15 +
16 +/**
17 + * Lists all configured address port bindings.
18 + */
19 +@Command(scope = "onos", name = "address-bindings",
20 + description = "Lists all configured address port bindings.")
21 +public class AddressBindingsListCommand extends AbstractShellCommand {
22 +
23 + private static final String FORMAT =
24 + "port=%s/%s, ip(s)=%s, mac=%s";
25 +
26 + @Override
27 + protected void execute() {
28 + HostService hostService = get(HostService.class);
29 +
30 + List<PortAddresses> addresses =
31 + Lists.newArrayList(hostService.getAddressBindings());
32 +
33 + Collections.sort(addresses, Comparators.ADDRESSES_COMPARATOR);
34 +
35 + for (PortAddresses pa : addresses) {
36 + print(FORMAT, pa.connectPoint().deviceId(), pa.connectPoint().port(),
37 + printIpAddresses(pa.ipAddresses()), pa.mac());
38 + }
39 + }
40 +
41 + private String printIpAddresses(Set<InterfaceIpAddress> addresses) {
42 + StringBuilder output = new StringBuilder("[");
43 + for (InterfaceIpAddress address : addresses) {
44 + output.append(address.ipAddress().toString());
45 + output.append("/");
46 + output.append(address.subnetAddress().prefixLength());
47 + output.append(", ");
48 + }
49 + // Remove the last comma
50 + output.delete(output.length() - 2 , output.length());
51 + output.append("]");
52 + return output.toString();
53 + }
54 +
55 +}
...@@ -169,6 +169,9 @@ ...@@ -169,6 +169,9 @@
169 <command> 169 <command>
170 <action class="org.onlab.onos.cli.net.HostsListCommand"/> 170 <action class="org.onlab.onos.cli.net.HostsListCommand"/>
171 </command> 171 </command>
172 + <command>
173 + <action class="org.onlab.onos.cli.net.AddressBindingsListCommand"/>
174 + </command>
172 175
173 <command> 176 <command>
174 <action class="org.onlab.onos.cli.net.FlowsListCommand"/> 177 <action class="org.onlab.onos.cli.net.FlowsListCommand"/>
......