tom

Fixed a number of CLI commands.

Refactored the StoreService/Manager stuff for common serializer pool.
Showing 34 changed files with 183 additions and 66 deletions
......@@ -23,6 +23,10 @@
<artifactId>onos-api</artifactId>
</dependency>
<dependency>
<groupId>org.onlab.onos</groupId>
<artifactId>onlab-osgi</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
</dependency>
......
package org.onlab.onos.cli;
import org.apache.karaf.shell.console.OsgiCommandSupport;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.onlab.osgi.DefaultServiceDirectory;
import org.onlab.osgi.ServiceNotFoundException;
/**
* Base abstraction of Karaf shell commands.
......@@ -17,8 +17,7 @@ public abstract class AbstractShellCommand extends OsgiCommandSupport {
* @return service implementation
*/
public static <T> T get(Class<T> serviceClass) {
BundleContext bc = FrameworkUtil.getBundle(AbstractShellCommand.class).getBundleContext();
return bc.getService(bc.getServiceReference(serviceClass));
return DefaultServiceDirectory.getService(serviceClass);
}
/**
......@@ -41,4 +40,19 @@ public abstract class AbstractShellCommand extends OsgiCommandSupport {
System.err.println(String.format(format, args));
}
/**
* Executes this command.
*/
protected abstract void execute();
@Override
protected Object doExecute() throws Exception {
try {
execute();
} catch (ServiceNotFoundException e) {
error(e.getMessage());
}
return null;
}
}
......
......@@ -29,8 +29,8 @@ public class NodesListCommand extends AbstractShellCommand {
};
@Override
protected Object doExecute() throws Exception {
ClusterService service = getService(ClusterService.class);
protected void execute() {
ClusterService service = get(ClusterService.class);
List<ControllerNode> nodes = newArrayList(service.getNodes());
Collections.sort(nodes, ID_COMPARATOR);
ControllerNode self = service.getLocalNode();
......@@ -39,7 +39,6 @@ public class NodesListCommand extends AbstractShellCommand {
service.getState(node.id()),
node.equals(self) ? "*" : "");
}
return null;
}
}
......
......@@ -31,7 +31,7 @@ public class ClusterDevicesCommand extends ClustersListCommand {
};
@Override
protected Object doExecute() throws Exception {
protected void execute() {
int cid = Integer.parseInt(id);
init();
TopologyCluster cluster = service.getCluster(topology, clusterId(cid));
......@@ -44,8 +44,6 @@ public class ClusterDevicesCommand extends ClustersListCommand {
print("%s", deviceId);
}
}
return null;
}
......
......@@ -20,7 +20,7 @@ public class ClusterLinksCommand extends ClustersListCommand {
String id = null;
@Override
protected Object doExecute() throws Exception {
protected void execute() {
int cid = Integer.parseInt(id);
init();
TopologyCluster cluster = service.getCluster(topology, clusterId(cid));
......@@ -31,7 +31,6 @@ public class ClusterLinksCommand extends ClustersListCommand {
print(linkString(link));
}
}
return null;
}
}
......
......@@ -27,7 +27,7 @@ public class ClustersListCommand extends TopologyCommand {
};
@Override
protected Object doExecute() throws Exception {
protected void execute() {
init();
List<TopologyCluster> clusters = Lists.newArrayList(service.getClusters(topology));
Collections.sort(clusters, ID_COMPARATOR);
......@@ -35,7 +35,6 @@ public class ClustersListCommand extends TopologyCommand {
for (TopologyCluster cluster : clusters) {
print(FMT, cluster.id().index(), cluster.deviceCount(), cluster.linkCount());
}
return null;
}
}
......
......@@ -35,7 +35,7 @@ public class DevicePortsListCommand extends DevicesListCommand {
};
@Override
protected Object doExecute() throws Exception {
protected void execute() {
DeviceService service = getService(DeviceService.class);
if (uri == null) {
for (Device device : getSortedDevices(service)) {
......@@ -49,7 +49,6 @@ public class DevicePortsListCommand extends DevicesListCommand {
printDevice(service, device);
}
}
return null;
}
@Override
......
......@@ -18,9 +18,8 @@ public class DeviceRemoveCommand extends AbstractShellCommand {
String uri = null;
@Override
protected Object doExecute() throws Exception {
protected void execute() {
getService(DeviceAdminService.class).removeDevice(DeviceId.deviceId(uri));
return null;
}
}
......
......@@ -23,11 +23,10 @@ public class DeviceRoleCommand extends AbstractShellCommand {
String role = null;
@Override
protected Object doExecute() throws Exception {
protected void execute() {
MastershipRole mastershipRole = MastershipRole.valueOf(role.toUpperCase());
getService(DeviceAdminService.class).setRole(DeviceId.deviceId(uri),
mastershipRole);
return null;
}
}
......
......@@ -29,12 +29,11 @@ public class DevicesListCommand extends AbstractShellCommand {
};
@Override
protected Object doExecute() throws Exception {
protected void execute() {
DeviceService service = getService(DeviceService.class);
for (Device device : getSortedDevices(service)) {
printDevice(service, device);
}
return null;
}
/**
......
......@@ -34,14 +34,13 @@ public class FlowsListCommand extends AbstractShellCommand {
};
@Override
protected Object doExecute() throws Exception {
protected void execute() {
DeviceService deviceService = getService(DeviceService.class);
FlowRuleService service = getService(FlowRuleService.class);
Map<Device, List<FlowRule>> flows = getSortedFlows(deviceService, service);
for (Device d : deviceService.getDevices()) {
printFlows(d, flows.get(d));
}
return null;
}
......
......@@ -29,12 +29,11 @@ public class HostsListCommand extends AbstractShellCommand {
};
@Override
protected Object doExecute() throws Exception {
protected void execute() {
HostService service = getService(HostService.class);
for (Host host : getSortedHosts(service)) {
printHost(host);
}
return null;
}
/**
......
......@@ -23,14 +23,13 @@ public class LinksListCommand extends AbstractShellCommand {
String uri = null;
@Override
protected Object doExecute() throws Exception {
protected void execute() {
LinkService service = getService(LinkService.class);
Iterable<Link> links = uri != null ?
service.getDeviceLinks(deviceId(uri)) : service.getLinks();
for (Link link : links) {
print(linkString(link));
}
return null;
}
/**
......
......@@ -29,13 +29,12 @@ public class PathListCommand extends TopologyCommand {
String dst = null;
@Override
protected Object doExecute() throws Exception {
protected void execute() {
init();
Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst));
for (Path path : paths) {
print(pathString(path));
}
return null;
}
/**
......
......@@ -28,11 +28,10 @@ public class TopologyCommand extends AbstractShellCommand {
}
@Override
protected Object doExecute() throws Exception {
protected void execute() {
init();
print(FMT, topology.time(), topology.deviceCount(), topology.linkCount(),
topology.clusterCount(), topology.pathCount());
return null;
}
}
......
......@@ -16,7 +16,7 @@ import org.onlab.onos.net.host.HostService;
public class WipeOutCommand extends ClustersListCommand {
@Override
protected Object doExecute() throws Exception {
protected void execute() {
DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
DeviceService deviceService = get(DeviceService.class);
for (Device device : deviceService.getDevices()) {
......@@ -28,7 +28,6 @@ public class WipeOutCommand extends ClustersListCommand {
for (Host host : hostService.getHosts()) {
hostAdminService.removeHost(host.id());
}
return null;
}
......
......@@ -37,6 +37,9 @@ public interface MastershipService {
*/
MastershipRole requestRoleFor(DeviceId deviceId);
// TODO: add facet for requesting a different master than the current one;
// abandon mastership (due to loss of connection)
/**
* Adds the specified mastership change listener.
*
......
......@@ -15,7 +15,7 @@ public interface DeviceAdminService {
* @param role requested role
* @deprecated Will be removed in favor of MastershipAdminService.setRole()
*/
@Deprecated
// @Deprecated
void setRole(DeviceId deviceId, MastershipRole role);
/**
......
package org.onlab.onos.net.device.impl;
import com.google.common.collect.Iterables;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.onlab.onos.event.Event;
import org.onlab.onos.event.impl.TestEventDispatcher;
import org.onlab.onos.net.Device;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.MastershipRole;
......@@ -23,13 +27,9 @@ import org.onlab.onos.net.device.DeviceService;
import org.onlab.onos.net.device.PortDescription;
import org.onlab.onos.net.provider.AbstractProvider;
import org.onlab.onos.net.provider.ProviderId;
import org.onlab.onos.event.impl.TestEventDispatcher;
import org.onlab.onos.store.StoreService;
import org.onlab.onos.store.device.impl.DistributedDeviceStore;
import com.google.common.collect.Iterables;
import com.hazelcast.config.Config;
import com.hazelcast.core.Hazelcast;
import org.onlab.onos.store.impl.StoreManager;
import java.util.ArrayList;
import java.util.Iterator;
......@@ -64,6 +64,7 @@ public class DistributedDeviceManagerTest {
private DeviceManager mgr;
protected StoreManager storeManager;
protected DeviceService service;
protected DeviceAdminService admin;
protected DeviceProviderRegistry registry;
......@@ -89,7 +90,11 @@ public class DistributedDeviceManagerTest {
config.getNetworkConfig().getJoin()
.getMulticastConfig()
.setEnabled(false);
dstore = new TestDistributedDeviceStore(Hazelcast.newHazelcastInstance(config));
storeManager = new TestStoreManager(Hazelcast.newHazelcastInstance(config));
storeManager.activate();
dstore = new TestDistributedDeviceStore(storeManager);
dstore.activate();
mgr.store = dstore;
mgr.eventDispatcher = new TestEventDispatcher();
......@@ -112,7 +117,7 @@ public class DistributedDeviceManagerTest {
mgr.deactivate();
dstore.deactivate();
((TestDistributedDeviceStore) dstore).shutdownHz();
storeManager.deactivate();
}
private void connectDevice(DeviceId deviceId, String swVersion) {
......@@ -282,20 +287,19 @@ public class DistributedDeviceManagerTest {
}
private class TestDistributedDeviceStore extends DistributedDeviceStore {
public TestDistributedDeviceStore(final HazelcastInstance hazelcastInstance) {
storeService = new StoreService() {
@Override
public HazelcastInstance getHazelcastInstance() {
return hazelcastInstance;
}
};
public TestDistributedDeviceStore(StoreService storeService) {
this.storeService = storeService;
}
}
/**
* Shutdowns the hazelcast instance.
*/
public void shutdownHz() {
theInstance.shutdown();
private class TestStoreManager extends StoreManager {
TestStoreManager(HazelcastInstance instance) {
this.instance = instance;
}
@Override
public void activate() {
setupKryoPool();
}
}
}
......
......@@ -15,4 +15,22 @@ public interface StoreService {
*/
HazelcastInstance getHazelcastInstance();
/**
* Serializes the specified object into bytes using one of the
* pre-registered serializers.
*
* @param obj object to be serialized
* @return serialized bytes
*/
public byte[] serialize(final Object obj);
/**
* Deserializes the specified bytes into an object using one of the
* pre-registered serializers.
*
* @param bytes bytes to be deserialized
* @return deserialized object
*/
public <T> T deserialize(final byte[] bytes);
}
......
package org.onlab.onos.store.device.impl;
package org.onlab.onos.store.impl;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
......
......@@ -2,14 +2,33 @@ package org.onlab.onos.store.impl;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import de.javakaffee.kryoserializers.URISerializer;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Service;
import org.onlab.onos.net.DefaultDevice;
import org.onlab.onos.net.DefaultPort;
import org.onlab.onos.net.Device;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.Element;
import org.onlab.onos.net.MastershipRole;
import org.onlab.onos.net.Port;
import org.onlab.onos.net.PortNumber;
import org.onlab.onos.net.provider.ProviderId;
import org.onlab.onos.store.StoreService;
import org.onlab.onos.store.serializers.DefaultPortSerializer;
import org.onlab.onos.store.serializers.DeviceIdSerializer;
import org.onlab.onos.store.serializers.PortNumberSerializer;
import org.onlab.onos.store.serializers.ProviderIdSerializer;
import org.onlab.util.KryoPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Auxiliary bootstrap of distributed store.
*/
......@@ -20,15 +39,45 @@ public class StoreManager implements StoreService {
private final Logger log = LoggerFactory.getLogger(getClass());
protected HazelcastInstance instance;
private KryoPool serializerPool;
@Activate
public void activate() {
instance = Hazelcast.newHazelcastInstance();
setupKryoPool();
log.info("Started");
}
/**
* Sets up the common serialzers pool.
*/
protected void setupKryoPool() {
// FIXME Slice out types used in common to separate pool/namespace.
serializerPool = KryoPool.newBuilder()
.register(
ArrayList.class,
HashMap.class,
Device.Type.class,
DefaultDevice.class,
MastershipRole.class,
Port.class,
Element.class
)
.register(URI.class, new URISerializer())
.register(ProviderId.class, new ProviderIdSerializer())
.register(DeviceId.class, new DeviceIdSerializer())
.register(PortNumber.class, new PortNumberSerializer())
.register(DefaultPort.class, new DefaultPortSerializer())
.build()
.populate(10);
}
@Deactivate
public void deactivate() {
instance.shutdown();
log.info("Stopped");
}
......@@ -36,4 +85,19 @@ public class StoreManager implements StoreService {
public HazelcastInstance getHazelcastInstance() {
return instance;
}
@Override
public byte[] serialize(final Object obj) {
return serializerPool.serialize(obj);
}
@Override
public <T> T deserialize(final byte[] bytes) {
if (bytes == null) {
return null;
}
return serializerPool.deserialize(bytes);
}
}
......
package org.onlab.onos.store.device.impl;
package org.onlab.onos.store.serializers;
import java.util.ArrayList;
import java.util.Collection;
......
package org.onlab.onos.store.device.impl;
package org.onlab.onos.store.serializers;
import org.onlab.onos.net.PortNumber;
......
package org.onlab.onos.store.device.impl;
package org.onlab.onos.store.serializers;
import org.onlab.onos.net.provider.ProviderId;
......
/**
* Various Kryo serializers for use in distributed stores.
*/
package org.onlab.onos.store.serializers;
\ No newline at end of file
......@@ -172,6 +172,11 @@
</dependency>
<dependency>
<groupId>org.onlab.onos</groupId>
<artifactId>onlab-osgi</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.onlab.onos</groupId>
<artifactId>onlab-junit</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>test</scope>
......
......@@ -46,7 +46,8 @@ alias gui='open http://localhost:8181/onos/tvue'
# Test related conveniences
# SSH to a specified ONOS instance
alias sshctl=onos-ssh
alias sshctl='onos-ssh'
alias sshnet='onos-ssh $OCN'
# Applies the settings in the specified cell file or lists current cell definition
# if no cell file is given.
......@@ -62,6 +63,7 @@ function cell {
env | egrep "ONOS_CELL"
env | egrep "OCI"
env | egrep "OC[0-9]+" | sort
env | egrep "OCN"
fi
}
......
......@@ -6,6 +6,6 @@
[ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1
. $ONOS_ROOT/tools/build/envDefaults
for node in $(env | sort | egrep "OC[0-9]+" | cut -d= -f2); do
for node in $(env | sort | egrep "OC[0-9N]+" | cut -d= -f2); do
printf "%s: " $node; ssh -n -o PasswordAuthentication=no $ONOS_USER@$node date
done
\ No newline at end of file
......
# Default virtual box ONOS instances 1,2 & 3
# Default virtual box ONOS instances 1,2 & ONOS mininet box
export OC1="192.168.56.101"
export OC2="192.168.56.102"
export OC3="192.168.56.103"
export OCN="192.168.56.103"
......
......@@ -7,8 +7,15 @@ import org.osgi.framework.FrameworkUtil;
* Default implementation of the service directory using OSGi framework utilities.
*/
public class DefaultServiceDirectory implements ServiceDirectory {
@Override
public <T> T get(Class<T> serviceClass) {
/**
* Returns the reference to the implementation of the specified service.
*
* @param serviceClass service class
* @param <T> type of service
* @return service implementation
*/
public static <T> T getService(Class<T> serviceClass) {
BundleContext bc = FrameworkUtil.getBundle(serviceClass).getBundleContext();
T impl = bc.getService(bc.getServiceReference(serviceClass));
if (impl == null) {
......@@ -16,4 +23,10 @@ public class DefaultServiceDirectory implements ServiceDirectory {
}
return impl;
}
@Override
public <T> T get(Class<T> serviceClass) {
return getService(serviceClass);
}
}
......