Show better results for network and port list command
Change-Id: I66638110fc6e66fcab85f761bd92cb24b81be94c
diff --git a/src/main/java/org/opencord/cordvtn/api/net/VtnNetwork.java b/src/main/java/org/opencord/cordvtn/api/net/VtnNetwork.java
index 84cac7b..c4ae68e 100644
--- a/src/main/java/org/opencord/cordvtn/api/net/VtnNetwork.java
+++ b/src/main/java/org/opencord/cordvtn/api/net/VtnNetwork.java
@@ -25,6 +25,7 @@
import org.openstack4j.model.network.Network;
import org.openstack4j.model.network.Subnet;
+import java.util.Comparator;
import java.util.Objects;
import java.util.Set;
@@ -60,6 +61,9 @@
this.serviceIp = serviceIp;
}
+ public static final Comparator<VtnNetwork> VTN_NETWORK_COMPARATOR =
+ (net1, net2) -> net1.serviceIp().compareTo(net2.serviceIp());
+
/**
* Returns the network ID.
*
diff --git a/src/main/java/org/opencord/cordvtn/api/net/VtnPort.java b/src/main/java/org/opencord/cordvtn/api/net/VtnPort.java
index 3ba4192..1f07218 100644
--- a/src/main/java/org/opencord/cordvtn/api/net/VtnPort.java
+++ b/src/main/java/org/opencord/cordvtn/api/net/VtnPort.java
@@ -58,7 +58,7 @@
}
public static final Comparator<VtnPort> VTN_PORT_COMPARATOR =
- (port1, port2) -> port1.netId().id().compareTo(port2.netId().id());
+ (port1, port2) -> port1.ip().compareTo(port2.ip());
/**
* Returns the network ID of this port.
diff --git a/src/main/java/org/opencord/cordvtn/cli/CordVtnNetworkListCommand.java b/src/main/java/org/opencord/cordvtn/cli/CordVtnNetworkListCommand.java
index a97a7ae..fac50f8 100644
--- a/src/main/java/org/opencord/cordvtn/cli/CordVtnNetworkListCommand.java
+++ b/src/main/java/org/opencord/cordvtn/cli/CordVtnNetworkListCommand.java
@@ -18,12 +18,14 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.google.common.collect.Lists;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.opencord.cordvtn.api.core.CordVtnService;
import org.opencord.cordvtn.api.net.VtnNetwork;
-import java.util.Set;
+import java.util.Collections;
+import java.util.List;
import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
@@ -39,7 +41,8 @@
@Override
protected void execute() {
CordVtnService service = AbstractShellCommand.get(CordVtnService.class);
- Set<VtnNetwork> networks = service.vtnNetworks();
+ List<VtnNetwork> networks = Lists.newArrayList(service.vtnNetworks());
+ Collections.sort(networks, VtnNetwork.VTN_NETWORK_COMPARATOR);
if (outputJson()) {
try {
@@ -59,7 +62,7 @@
}
}
- private JsonNode json(Set<VtnNetwork> networks) {
+ private JsonNode json(List<VtnNetwork> networks) {
ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode();
for (VtnNetwork net: networks) {
ArrayNode providers = mapper().createArrayNode();
diff --git a/src/main/java/org/opencord/cordvtn/cli/CordVtnPortListCommand.java b/src/main/java/org/opencord/cordvtn/cli/CordVtnPortListCommand.java
index 361b220..32eaa22 100644
--- a/src/main/java/org/opencord/cordvtn/cli/CordVtnPortListCommand.java
+++ b/src/main/java/org/opencord/cordvtn/cli/CordVtnPortListCommand.java
@@ -24,8 +24,6 @@
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.opencord.cordvtn.api.core.CordVtnService;
-import org.opencord.cordvtn.api.net.NetworkId;
-import org.opencord.cordvtn.api.net.VtnNetwork;
import org.opencord.cordvtn.api.net.VtnPort;
import java.util.Collections;
@@ -41,7 +39,7 @@
description = "Lists all VTN ports")
public class CordVtnPortListCommand extends AbstractShellCommand {
- private static final String FORMAT = "%-40s%-20s%-18s%-6s%s";
+ private static final String FORMAT = "%-40s%-20s%-18s%-8s%s";
@Argument(name = "networkId", description = "Network ID")
private String networkId = null;
@@ -50,19 +48,10 @@
protected void execute() {
CordVtnService service = AbstractShellCommand.get(CordVtnService.class);
- List<VtnPort> ports;
- if (Strings.isNullOrEmpty(networkId)) {
- ports = Lists.newArrayList(service.vtnPorts());
- } else {
- VtnNetwork vtnNet = service.vtnNetwork(NetworkId.of(networkId));
- if (vtnNet == null) {
- print("Network %s does not exists", networkId);
- return;
- }
- ports = service.vtnPorts().stream()
- .filter(p -> p.netId().equals(NetworkId.of(networkId)))
- .collect(Collectors.toList());
- Collections.sort(ports, VtnPort.VTN_PORT_COMPARATOR);
+ List<VtnPort> ports = Lists.newArrayList(service.vtnPorts());
+ Collections.sort(ports, VtnPort.VTN_PORT_COMPARATOR);
+ if (!Strings.isNullOrEmpty(networkId)) {
+ ports.removeIf(port -> !port.netId().id().equals(networkId));
}
if (outputJson()) {
@@ -72,7 +61,7 @@
print("Failed to list networks in JSON format");
}
} else {
- print(FORMAT, "ID", "MAC", "IP", "VLAN", "Additional IPs");
+ print(FORMAT, "ID", "MAC", "IP", "VLAN", "WAN IPs");
for (VtnPort port: ports) {
List<String> floatingIps = port.addressPairs().stream()
.map(ip -> ip.ip().toString())