tom

Added output of appId to the flow list command. Fixed defect in CoreManager - we…

… forgot to put registered ids in our map.
package org.onlab.onos.cli.net;
import static com.google.common.collect.Lists.newArrayList;
import static org.onlab.onos.cli.net.DevicesListCommand.getSortedDevices;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.google.common.collect.Maps;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onlab.onos.CoreService;
import org.onlab.onos.cli.AbstractShellCommand;
import org.onlab.onos.cli.Comparators;
import org.onlab.onos.net.Device;
......@@ -18,37 +13,43 @@ import org.onlab.onos.net.flow.FlowEntry;
import org.onlab.onos.net.flow.FlowEntry.FlowEntryState;
import org.onlab.onos.net.flow.FlowRuleService;
import com.google.common.collect.Maps;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static com.google.common.collect.Lists.newArrayList;
import static org.onlab.onos.cli.net.DevicesListCommand.getSortedDevices;
/**
* Lists all currently-known hosts.
*/
@Command(scope = "onos", name = "flows",
description = "Lists all currently-known flows.")
description = "Lists all currently-known flows.")
public class FlowsListCommand extends AbstractShellCommand {
public static final String ANY = "any";
private static final String FMT =
" id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s";
" id=%s, state=%s, bytes=%s, packets=%s, duration=%s, priority=%s, appId=%s";
private static final String TFMT = " treatment=%s";
private static final String SFMT = " selector=%s";
@Argument(index = 1, name = "uri", description = "Device ID",
required = false, multiValued = false)
required = false, multiValued = false)
String uri = null;
@Argument(index = 0, name = "state", description = "Flow Rule state",
required = false, multiValued = false)
required = false, multiValued = false)
String state = null;
@Override
protected void execute() {
CoreService coreService = get(CoreService.class);
DeviceService deviceService = get(DeviceService.class);
FlowRuleService service = get(FlowRuleService.class);
Map<Device, List<FlowEntry>> flows = getSortedFlows(deviceService, service);
for (Device d : getSortedDevices(deviceService)) {
printFlows(d, flows.get(d));
printFlows(d, flows.get(d), coreService);
}
}
......@@ -67,7 +68,7 @@ public class FlowsListCommand extends AbstractShellCommand {
s = FlowEntryState.valueOf(state.toUpperCase());
}
Iterable<Device> devices = uri == null ? deviceService.getDevices() :
Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
Collections.singletonList(deviceService.getDevice(DeviceId.deviceId(uri)));
for (Device d : devices) {
if (s == null) {
rules = newArrayList(service.getFlowEntries(d.id()));
......@@ -87,16 +88,19 @@ public class FlowsListCommand extends AbstractShellCommand {
/**
* Prints flows.
* @param d the device
*
* @param d the device
* @param flows the set of flows for that device.
*/
protected void printFlows(Device d, List<FlowEntry> flows) {
protected void printFlows(Device d, List<FlowEntry> flows,
CoreService coreService) {
boolean empty = flows == null || flows.isEmpty();
print("deviceId=%s, flowRuleCount=%d", d.id(), empty ? 0 : flows.size());
if (!empty) {
for (FlowEntry f : flows) {
print(FMT, Long.toHexString(f.id().value()), f.state(), f.bytes(),
f.packets(), f.life(), f.priority());
print(FMT, Long.toHexString(f.id().value()), f.state(),
f.bytes(), f.packets(), f.life(), f.priority(),
coreService.getAppId(f.appId()).name());
print(SFMT, f.selector().criteria());
print(TFMT, f.treatment().instructions());
}
......
package org.onlab.onos.impl;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
......@@ -14,6 +8,11 @@ import org.onlab.onos.CoreService;
import org.onlab.onos.Version;
import org.onlab.util.Tools;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Core service implementation.
......@@ -23,10 +22,11 @@ import org.onlab.util.Tools;
public class CoreManager implements CoreService {
private static final AtomicInteger ID_DISPENSER = new AtomicInteger(1);
private static final File VERSION_FILE = new File("../VERSION");
private static Version version = Version.version("1.0.0-SNAPSHOT");
private final Map<Short, DefaultApplicationId> ids = new ConcurrentHashMap<>();
private final Map<Short, DefaultApplicationId> appIds = new ConcurrentHashMap<>();
// TODO: work in progress
......@@ -45,12 +45,15 @@ public class CoreManager implements CoreService {
@Override
public ApplicationId getAppId(Short id) {
return ids.get(id);
return appIds.get(id);
}
@Override
public ApplicationId registerApplication(String name) {
return new DefaultApplicationId((short) ID_DISPENSER.getAndIncrement(), name);
short id = (short) ID_DISPENSER.getAndIncrement();
DefaultApplicationId appId = new DefaultApplicationId(id, name);
appIds.put(id, appId);
return appId;
}
}
......
package org.onlab.onos.impl;
import org.onlab.onos.ApplicationId;
import java.util.Objects;
import org.onlab.onos.ApplicationId;
import static com.google.common.base.MoreObjects.toStringHelper;
/**
* Application id generator class.
*/
public class DefaultApplicationId implements ApplicationId {
private final short id;
private final String name;
// Ban public construction
protected DefaultApplicationId(Short id, String identifier) {
this.id = id;
......@@ -40,13 +40,16 @@ public class DefaultApplicationId implements ApplicationId {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
if (obj instanceof DefaultApplicationId) {
DefaultApplicationId other = (DefaultApplicationId) obj;
return Objects.equals(this.id, other.id);
}
if (!(obj instanceof DefaultApplicationId)) {
return false;
}
DefaultApplicationId other = (DefaultApplicationId) obj;
return Objects.equals(this.id, other.id);
return false;
}
@Override
public String toString() {
return toStringHelper(this).add("id", id).add("name", name).toString();
}
}
......
......@@ -33,6 +33,7 @@ alias obs='onos-build-selective'
alias op='onos-package'
alias ot='onos-test'
alias ol='onos-log'
alias ow='onos-watch'
alias go='ob && ot && onos -w'
alias pub='onos-push-update-bundle'
......
......@@ -11,7 +11,7 @@ node=${1:-$OCI}
commands="${2:-summary,intents,flows,hosts}"
aux=/tmp/onos-watch.$$
trap "rm -f $aux'" EXIT
trap "rm -f $aux" EXIT
echo "$commands" | tr ',' '\n' > $aux
watch $3 "onos $node -b <$aux 2>/dev/null"
......