tom

Fixed a number of CLI commands.

Refactored the StoreService/Manager stuff for common serializer pool.
Showing 34 changed files with 226 additions and 145 deletions
...@@ -23,6 +23,10 @@ ...@@ -23,6 +23,10 @@
23 <artifactId>onos-api</artifactId> 23 <artifactId>onos-api</artifactId>
24 </dependency> 24 </dependency>
25 <dependency> 25 <dependency>
26 + <groupId>org.onlab.onos</groupId>
27 + <artifactId>onlab-osgi</artifactId>
28 + </dependency>
29 + <dependency>
26 <groupId>org.osgi</groupId> 30 <groupId>org.osgi</groupId>
27 <artifactId>org.osgi.core</artifactId> 31 <artifactId>org.osgi.core</artifactId>
28 </dependency> 32 </dependency>
......
1 package org.onlab.onos.cli; 1 package org.onlab.onos.cli;
2 2
3 import org.apache.karaf.shell.console.OsgiCommandSupport; 3 import org.apache.karaf.shell.console.OsgiCommandSupport;
4 -import org.osgi.framework.BundleContext; 4 +import org.onlab.osgi.DefaultServiceDirectory;
5 -import org.osgi.framework.FrameworkUtil; 5 +import org.onlab.osgi.ServiceNotFoundException;
6 6
7 /** 7 /**
8 * Base abstraction of Karaf shell commands. 8 * Base abstraction of Karaf shell commands.
...@@ -17,8 +17,7 @@ public abstract class AbstractShellCommand extends OsgiCommandSupport { ...@@ -17,8 +17,7 @@ public abstract class AbstractShellCommand extends OsgiCommandSupport {
17 * @return service implementation 17 * @return service implementation
18 */ 18 */
19 public static <T> T get(Class<T> serviceClass) { 19 public static <T> T get(Class<T> serviceClass) {
20 - BundleContext bc = FrameworkUtil.getBundle(AbstractShellCommand.class).getBundleContext(); 20 + return DefaultServiceDirectory.getService(serviceClass);
21 - return bc.getService(bc.getServiceReference(serviceClass));
22 } 21 }
23 22
24 /** 23 /**
...@@ -41,4 +40,19 @@ public abstract class AbstractShellCommand extends OsgiCommandSupport { ...@@ -41,4 +40,19 @@ public abstract class AbstractShellCommand extends OsgiCommandSupport {
41 System.err.println(String.format(format, args)); 40 System.err.println(String.format(format, args));
42 } 41 }
43 42
43 + /**
44 + * Executes this command.
45 + */
46 + protected abstract void execute();
47 +
48 + @Override
49 + protected Object doExecute() throws Exception {
50 + try {
51 + execute();
52 + } catch (ServiceNotFoundException e) {
53 + error(e.getMessage());
54 + }
55 + return null;
56 + }
57 +
44 } 58 }
......
...@@ -29,8 +29,8 @@ public class NodesListCommand extends AbstractShellCommand { ...@@ -29,8 +29,8 @@ public class NodesListCommand extends AbstractShellCommand {
29 }; 29 };
30 30
31 @Override 31 @Override
32 - protected Object doExecute() throws Exception { 32 + protected void execute() {
33 - ClusterService service = getService(ClusterService.class); 33 + ClusterService service = get(ClusterService.class);
34 List<ControllerNode> nodes = newArrayList(service.getNodes()); 34 List<ControllerNode> nodes = newArrayList(service.getNodes());
35 Collections.sort(nodes, ID_COMPARATOR); 35 Collections.sort(nodes, ID_COMPARATOR);
36 ControllerNode self = service.getLocalNode(); 36 ControllerNode self = service.getLocalNode();
...@@ -39,7 +39,6 @@ public class NodesListCommand extends AbstractShellCommand { ...@@ -39,7 +39,6 @@ public class NodesListCommand extends AbstractShellCommand {
39 service.getState(node.id()), 39 service.getState(node.id()),
40 node.equals(self) ? "*" : ""); 40 node.equals(self) ? "*" : "");
41 } 41 }
42 - return null;
43 } 42 }
44 43
45 } 44 }
......
...@@ -31,7 +31,7 @@ public class ClusterDevicesCommand extends ClustersListCommand { ...@@ -31,7 +31,7 @@ public class ClusterDevicesCommand extends ClustersListCommand {
31 }; 31 };
32 32
33 @Override 33 @Override
34 - protected Object doExecute() throws Exception { 34 + protected void execute() {
35 int cid = Integer.parseInt(id); 35 int cid = Integer.parseInt(id);
36 init(); 36 init();
37 TopologyCluster cluster = service.getCluster(topology, clusterId(cid)); 37 TopologyCluster cluster = service.getCluster(topology, clusterId(cid));
...@@ -44,8 +44,6 @@ public class ClusterDevicesCommand extends ClustersListCommand { ...@@ -44,8 +44,6 @@ public class ClusterDevicesCommand extends ClustersListCommand {
44 print("%s", deviceId); 44 print("%s", deviceId);
45 } 45 }
46 } 46 }
47 -
48 - return null;
49 } 47 }
50 48
51 49
......
...@@ -20,7 +20,7 @@ public class ClusterLinksCommand extends ClustersListCommand { ...@@ -20,7 +20,7 @@ public class ClusterLinksCommand extends ClustersListCommand {
20 String id = null; 20 String id = null;
21 21
22 @Override 22 @Override
23 - protected Object doExecute() throws Exception { 23 + protected void execute() {
24 int cid = Integer.parseInt(id); 24 int cid = Integer.parseInt(id);
25 init(); 25 init();
26 TopologyCluster cluster = service.getCluster(topology, clusterId(cid)); 26 TopologyCluster cluster = service.getCluster(topology, clusterId(cid));
...@@ -31,7 +31,6 @@ public class ClusterLinksCommand extends ClustersListCommand { ...@@ -31,7 +31,6 @@ public class ClusterLinksCommand extends ClustersListCommand {
31 print(linkString(link)); 31 print(linkString(link));
32 } 32 }
33 } 33 }
34 - return null;
35 } 34 }
36 35
37 } 36 }
......
...@@ -27,7 +27,7 @@ public class ClustersListCommand extends TopologyCommand { ...@@ -27,7 +27,7 @@ public class ClustersListCommand extends TopologyCommand {
27 }; 27 };
28 28
29 @Override 29 @Override
30 - protected Object doExecute() throws Exception { 30 + protected void execute() {
31 init(); 31 init();
32 List<TopologyCluster> clusters = Lists.newArrayList(service.getClusters(topology)); 32 List<TopologyCluster> clusters = Lists.newArrayList(service.getClusters(topology));
33 Collections.sort(clusters, ID_COMPARATOR); 33 Collections.sort(clusters, ID_COMPARATOR);
...@@ -35,7 +35,6 @@ public class ClustersListCommand extends TopologyCommand { ...@@ -35,7 +35,6 @@ public class ClustersListCommand extends TopologyCommand {
35 for (TopologyCluster cluster : clusters) { 35 for (TopologyCluster cluster : clusters) {
36 print(FMT, cluster.id().index(), cluster.deviceCount(), cluster.linkCount()); 36 print(FMT, cluster.id().index(), cluster.deviceCount(), cluster.linkCount());
37 } 37 }
38 - return null;
39 } 38 }
40 39
41 } 40 }
......
...@@ -35,7 +35,7 @@ public class DevicePortsListCommand extends DevicesListCommand { ...@@ -35,7 +35,7 @@ public class DevicePortsListCommand extends DevicesListCommand {
35 }; 35 };
36 36
37 @Override 37 @Override
38 - protected Object doExecute() throws Exception { 38 + protected void execute() {
39 DeviceService service = getService(DeviceService.class); 39 DeviceService service = getService(DeviceService.class);
40 if (uri == null) { 40 if (uri == null) {
41 for (Device device : getSortedDevices(service)) { 41 for (Device device : getSortedDevices(service)) {
...@@ -49,7 +49,6 @@ public class DevicePortsListCommand extends DevicesListCommand { ...@@ -49,7 +49,6 @@ public class DevicePortsListCommand extends DevicesListCommand {
49 printDevice(service, device); 49 printDevice(service, device);
50 } 50 }
51 } 51 }
52 - return null;
53 } 52 }
54 53
55 @Override 54 @Override
......
...@@ -18,9 +18,8 @@ public class DeviceRemoveCommand extends AbstractShellCommand { ...@@ -18,9 +18,8 @@ public class DeviceRemoveCommand extends AbstractShellCommand {
18 String uri = null; 18 String uri = null;
19 19
20 @Override 20 @Override
21 - protected Object doExecute() throws Exception { 21 + protected void execute() {
22 getService(DeviceAdminService.class).removeDevice(DeviceId.deviceId(uri)); 22 getService(DeviceAdminService.class).removeDevice(DeviceId.deviceId(uri));
23 - return null;
24 } 23 }
25 24
26 } 25 }
......
...@@ -23,11 +23,10 @@ public class DeviceRoleCommand extends AbstractShellCommand { ...@@ -23,11 +23,10 @@ public class DeviceRoleCommand extends AbstractShellCommand {
23 String role = null; 23 String role = null;
24 24
25 @Override 25 @Override
26 - protected Object doExecute() throws Exception { 26 + protected void execute() {
27 MastershipRole mastershipRole = MastershipRole.valueOf(role.toUpperCase()); 27 MastershipRole mastershipRole = MastershipRole.valueOf(role.toUpperCase());
28 getService(DeviceAdminService.class).setRole(DeviceId.deviceId(uri), 28 getService(DeviceAdminService.class).setRole(DeviceId.deviceId(uri),
29 mastershipRole); 29 mastershipRole);
30 - return null;
31 } 30 }
32 31
33 } 32 }
......
...@@ -29,12 +29,11 @@ public class DevicesListCommand extends AbstractShellCommand { ...@@ -29,12 +29,11 @@ public class DevicesListCommand extends AbstractShellCommand {
29 }; 29 };
30 30
31 @Override 31 @Override
32 - protected Object doExecute() throws Exception { 32 + protected void execute() {
33 DeviceService service = getService(DeviceService.class); 33 DeviceService service = getService(DeviceService.class);
34 for (Device device : getSortedDevices(service)) { 34 for (Device device : getSortedDevices(service)) {
35 printDevice(service, device); 35 printDevice(service, device);
36 } 36 }
37 - return null;
38 } 37 }
39 38
40 /** 39 /**
......
...@@ -34,14 +34,13 @@ public class FlowsListCommand extends AbstractShellCommand { ...@@ -34,14 +34,13 @@ public class FlowsListCommand extends AbstractShellCommand {
34 }; 34 };
35 35
36 @Override 36 @Override
37 - protected Object doExecute() throws Exception { 37 + protected void execute() {
38 DeviceService deviceService = getService(DeviceService.class); 38 DeviceService deviceService = getService(DeviceService.class);
39 FlowRuleService service = getService(FlowRuleService.class); 39 FlowRuleService service = getService(FlowRuleService.class);
40 Map<Device, List<FlowRule>> flows = getSortedFlows(deviceService, service); 40 Map<Device, List<FlowRule>> flows = getSortedFlows(deviceService, service);
41 for (Device d : deviceService.getDevices()) { 41 for (Device d : deviceService.getDevices()) {
42 printFlows(d, flows.get(d)); 42 printFlows(d, flows.get(d));
43 } 43 }
44 - return null;
45 } 44 }
46 45
47 46
......
...@@ -29,12 +29,11 @@ public class HostsListCommand extends AbstractShellCommand { ...@@ -29,12 +29,11 @@ public class HostsListCommand extends AbstractShellCommand {
29 }; 29 };
30 30
31 @Override 31 @Override
32 - protected Object doExecute() throws Exception { 32 + protected void execute() {
33 HostService service = getService(HostService.class); 33 HostService service = getService(HostService.class);
34 for (Host host : getSortedHosts(service)) { 34 for (Host host : getSortedHosts(service)) {
35 printHost(host); 35 printHost(host);
36 } 36 }
37 - return null;
38 } 37 }
39 38
40 /** 39 /**
......
...@@ -23,14 +23,13 @@ public class LinksListCommand extends AbstractShellCommand { ...@@ -23,14 +23,13 @@ public class LinksListCommand extends AbstractShellCommand {
23 String uri = null; 23 String uri = null;
24 24
25 @Override 25 @Override
26 - protected Object doExecute() throws Exception { 26 + protected void execute() {
27 LinkService service = getService(LinkService.class); 27 LinkService service = getService(LinkService.class);
28 Iterable<Link> links = uri != null ? 28 Iterable<Link> links = uri != null ?
29 service.getDeviceLinks(deviceId(uri)) : service.getLinks(); 29 service.getDeviceLinks(deviceId(uri)) : service.getLinks();
30 for (Link link : links) { 30 for (Link link : links) {
31 print(linkString(link)); 31 print(linkString(link));
32 } 32 }
33 - return null;
34 } 33 }
35 34
36 /** 35 /**
......
...@@ -29,13 +29,12 @@ public class PathListCommand extends TopologyCommand { ...@@ -29,13 +29,12 @@ public class PathListCommand extends TopologyCommand {
29 String dst = null; 29 String dst = null;
30 30
31 @Override 31 @Override
32 - protected Object doExecute() throws Exception { 32 + protected void execute() {
33 init(); 33 init();
34 Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst)); 34 Set<Path> paths = service.getPaths(topology, deviceId(src), deviceId(dst));
35 for (Path path : paths) { 35 for (Path path : paths) {
36 print(pathString(path)); 36 print(pathString(path));
37 } 37 }
38 - return null;
39 } 38 }
40 39
41 /** 40 /**
......
...@@ -28,11 +28,10 @@ public class TopologyCommand extends AbstractShellCommand { ...@@ -28,11 +28,10 @@ public class TopologyCommand extends AbstractShellCommand {
28 } 28 }
29 29
30 @Override 30 @Override
31 - protected Object doExecute() throws Exception { 31 + protected void execute() {
32 init(); 32 init();
33 print(FMT, topology.time(), topology.deviceCount(), topology.linkCount(), 33 print(FMT, topology.time(), topology.deviceCount(), topology.linkCount(),
34 topology.clusterCount(), topology.pathCount()); 34 topology.clusterCount(), topology.pathCount());
35 - return null;
36 } 35 }
37 36
38 } 37 }
......
...@@ -16,7 +16,7 @@ import org.onlab.onos.net.host.HostService; ...@@ -16,7 +16,7 @@ import org.onlab.onos.net.host.HostService;
16 public class WipeOutCommand extends ClustersListCommand { 16 public class WipeOutCommand extends ClustersListCommand {
17 17
18 @Override 18 @Override
19 - protected Object doExecute() throws Exception { 19 + protected void execute() {
20 DeviceAdminService deviceAdminService = get(DeviceAdminService.class); 20 DeviceAdminService deviceAdminService = get(DeviceAdminService.class);
21 DeviceService deviceService = get(DeviceService.class); 21 DeviceService deviceService = get(DeviceService.class);
22 for (Device device : deviceService.getDevices()) { 22 for (Device device : deviceService.getDevices()) {
...@@ -28,7 +28,6 @@ public class WipeOutCommand extends ClustersListCommand { ...@@ -28,7 +28,6 @@ public class WipeOutCommand extends ClustersListCommand {
28 for (Host host : hostService.getHosts()) { 28 for (Host host : hostService.getHosts()) {
29 hostAdminService.removeHost(host.id()); 29 hostAdminService.removeHost(host.id());
30 } 30 }
31 - return null;
32 } 31 }
33 32
34 33
......
...@@ -37,6 +37,9 @@ public interface MastershipService { ...@@ -37,6 +37,9 @@ public interface MastershipService {
37 */ 37 */
38 MastershipRole requestRoleFor(DeviceId deviceId); 38 MastershipRole requestRoleFor(DeviceId deviceId);
39 39
40 + // TODO: add facet for requesting a different master than the current one;
41 + // abandon mastership (due to loss of connection)
42 +
40 /** 43 /**
41 * Adds the specified mastership change listener. 44 * Adds the specified mastership change listener.
42 * 45 *
......
...@@ -15,7 +15,7 @@ public interface DeviceAdminService { ...@@ -15,7 +15,7 @@ public interface DeviceAdminService {
15 * @param role requested role 15 * @param role requested role
16 * @deprecated Will be removed in favor of MastershipAdminService.setRole() 16 * @deprecated Will be removed in favor of MastershipAdminService.setRole()
17 */ 17 */
18 - @Deprecated 18 +// @Deprecated
19 void setRole(DeviceId deviceId, MastershipRole role); 19 void setRole(DeviceId deviceId, MastershipRole role);
20 20
21 /** 21 /**
......
1 package org.onlab.onos.net.device.impl; 1 package org.onlab.onos.net.device.impl;
2 2
3 +import com.google.common.collect.Iterables;
4 +import com.hazelcast.config.Config;
5 +import com.hazelcast.core.Hazelcast;
3 import com.hazelcast.core.HazelcastInstance; 6 import com.hazelcast.core.HazelcastInstance;
4 import org.junit.After; 7 import org.junit.After;
5 import org.junit.Before; 8 import org.junit.Before;
6 import org.junit.Test; 9 import org.junit.Test;
7 import org.onlab.onos.event.Event; 10 import org.onlab.onos.event.Event;
11 +import org.onlab.onos.event.impl.TestEventDispatcher;
8 import org.onlab.onos.net.Device; 12 import org.onlab.onos.net.Device;
9 import org.onlab.onos.net.DeviceId; 13 import org.onlab.onos.net.DeviceId;
10 import org.onlab.onos.net.MastershipRole; 14 import org.onlab.onos.net.MastershipRole;
...@@ -23,13 +27,9 @@ import org.onlab.onos.net.device.DeviceService; ...@@ -23,13 +27,9 @@ import org.onlab.onos.net.device.DeviceService;
23 import org.onlab.onos.net.device.PortDescription; 27 import org.onlab.onos.net.device.PortDescription;
24 import org.onlab.onos.net.provider.AbstractProvider; 28 import org.onlab.onos.net.provider.AbstractProvider;
25 import org.onlab.onos.net.provider.ProviderId; 29 import org.onlab.onos.net.provider.ProviderId;
26 -import org.onlab.onos.event.impl.TestEventDispatcher;
27 import org.onlab.onos.store.StoreService; 30 import org.onlab.onos.store.StoreService;
28 import org.onlab.onos.store.device.impl.DistributedDeviceStore; 31 import org.onlab.onos.store.device.impl.DistributedDeviceStore;
29 - 32 +import org.onlab.onos.store.impl.StoreManager;
30 -import com.google.common.collect.Iterables;
31 -import com.hazelcast.config.Config;
32 -import com.hazelcast.core.Hazelcast;
33 33
34 import java.util.ArrayList; 34 import java.util.ArrayList;
35 import java.util.Iterator; 35 import java.util.Iterator;
...@@ -64,6 +64,7 @@ public class DistributedDeviceManagerTest { ...@@ -64,6 +64,7 @@ public class DistributedDeviceManagerTest {
64 64
65 private DeviceManager mgr; 65 private DeviceManager mgr;
66 66
67 + protected StoreManager storeManager;
67 protected DeviceService service; 68 protected DeviceService service;
68 protected DeviceAdminService admin; 69 protected DeviceAdminService admin;
69 protected DeviceProviderRegistry registry; 70 protected DeviceProviderRegistry registry;
...@@ -89,7 +90,11 @@ public class DistributedDeviceManagerTest { ...@@ -89,7 +90,11 @@ public class DistributedDeviceManagerTest {
89 config.getNetworkConfig().getJoin() 90 config.getNetworkConfig().getJoin()
90 .getMulticastConfig() 91 .getMulticastConfig()
91 .setEnabled(false); 92 .setEnabled(false);
92 - dstore = new TestDistributedDeviceStore(Hazelcast.newHazelcastInstance(config)); 93 +
94 + storeManager = new TestStoreManager(Hazelcast.newHazelcastInstance(config));
95 + storeManager.activate();
96 +
97 + dstore = new TestDistributedDeviceStore(storeManager);
93 dstore.activate(); 98 dstore.activate();
94 mgr.store = dstore; 99 mgr.store = dstore;
95 mgr.eventDispatcher = new TestEventDispatcher(); 100 mgr.eventDispatcher = new TestEventDispatcher();
...@@ -112,7 +117,7 @@ public class DistributedDeviceManagerTest { ...@@ -112,7 +117,7 @@ public class DistributedDeviceManagerTest {
112 mgr.deactivate(); 117 mgr.deactivate();
113 118
114 dstore.deactivate(); 119 dstore.deactivate();
115 - ((TestDistributedDeviceStore) dstore).shutdownHz(); 120 + storeManager.deactivate();
116 } 121 }
117 122
118 private void connectDevice(DeviceId deviceId, String swVersion) { 123 private void connectDevice(DeviceId deviceId, String swVersion) {
...@@ -282,20 +287,19 @@ public class DistributedDeviceManagerTest { ...@@ -282,20 +287,19 @@ public class DistributedDeviceManagerTest {
282 } 287 }
283 288
284 private class TestDistributedDeviceStore extends DistributedDeviceStore { 289 private class TestDistributedDeviceStore extends DistributedDeviceStore {
285 - public TestDistributedDeviceStore(final HazelcastInstance hazelcastInstance) { 290 + public TestDistributedDeviceStore(StoreService storeService) {
286 - storeService = new StoreService() { 291 + this.storeService = storeService;
287 - @Override
288 - public HazelcastInstance getHazelcastInstance() {
289 - return hazelcastInstance;
290 } 292 }
291 - };
292 } 293 }
293 294
294 - /** 295 + private class TestStoreManager extends StoreManager {
295 - * Shutdowns the hazelcast instance. 296 + TestStoreManager(HazelcastInstance instance) {
296 - */ 297 + this.instance = instance;
297 - public void shutdownHz() { 298 + }
298 - theInstance.shutdown(); 299 +
300 + @Override
301 + public void activate() {
302 + setupKryoPool();
299 } 303 }
300 } 304 }
301 } 305 }
......
...@@ -15,4 +15,22 @@ public interface StoreService { ...@@ -15,4 +15,22 @@ public interface StoreService {
15 */ 15 */
16 HazelcastInstance getHazelcastInstance(); 16 HazelcastInstance getHazelcastInstance();
17 17
18 + /**
19 + * Serializes the specified object into bytes using one of the
20 + * pre-registered serializers.
21 + *
22 + * @param obj object to be serialized
23 + * @return serialized bytes
24 + */
25 + public byte[] serialize(final Object obj);
26 +
27 + /**
28 + * Deserializes the specified bytes into an object using one of the
29 + * pre-registered serializers.
30 + *
31 + * @param bytes bytes to be deserialized
32 + * @return deserialized object
33 + */
34 + public <T> T deserialize(final byte[] bytes);
35 +
18 } 36 }
......
1 package org.onlab.onos.store.device.impl; 1 package org.onlab.onos.store.device.impl;
2 2
3 -import static com.google.common.base.Preconditions.checkArgument; 3 +import com.google.common.base.Optional;
4 -import static com.google.common.base.Preconditions.checkNotNull; 4 +import com.google.common.cache.CacheBuilder;
5 -import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED; 5 +import com.google.common.cache.CacheLoader;
6 -import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_MASTERSHIP_CHANGED; 6 +import com.google.common.cache.LoadingCache;
7 -import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_REMOVED; 7 +import com.google.common.collect.ImmutableList;
8 -import static org.onlab.onos.net.device.DeviceEvent.Type.PORT_ADDED; 8 +import com.google.common.collect.ImmutableSet;
9 -import static org.onlab.onos.net.device.DeviceEvent.Type.PORT_REMOVED; 9 +import com.google.common.collect.ImmutableSet.Builder;
10 -import static org.onlab.onos.net.device.DeviceEvent.Type.PORT_UPDATED; 10 +import com.hazelcast.core.EntryAdapter;
11 -import static org.slf4j.LoggerFactory.getLogger; 11 +import com.hazelcast.core.EntryEvent;
12 - 12 +import com.hazelcast.core.HazelcastInstance;
13 -import java.net.URI; 13 +import com.hazelcast.core.IMap;
14 -import java.util.ArrayList; 14 +import com.hazelcast.core.ISet;
15 -import java.util.Collections; 15 +import com.hazelcast.core.MapEvent;
16 -import java.util.HashMap;
17 -import java.util.HashSet;
18 -import java.util.Iterator;
19 -import java.util.List;
20 -import java.util.Map;
21 -import java.util.Objects;
22 -import java.util.Set;
23 -
24 import org.apache.felix.scr.annotations.Activate; 16 import org.apache.felix.scr.annotations.Activate;
25 import org.apache.felix.scr.annotations.Component; 17 import org.apache.felix.scr.annotations.Component;
26 import org.apache.felix.scr.annotations.Deactivate; 18 import org.apache.felix.scr.annotations.Deactivate;
...@@ -31,7 +23,6 @@ import org.onlab.onos.net.DefaultDevice; ...@@ -31,7 +23,6 @@ import org.onlab.onos.net.DefaultDevice;
31 import org.onlab.onos.net.DefaultPort; 23 import org.onlab.onos.net.DefaultPort;
32 import org.onlab.onos.net.Device; 24 import org.onlab.onos.net.Device;
33 import org.onlab.onos.net.DeviceId; 25 import org.onlab.onos.net.DeviceId;
34 -import org.onlab.onos.net.Element;
35 import org.onlab.onos.net.MastershipRole; 26 import org.onlab.onos.net.MastershipRole;
36 import org.onlab.onos.net.Port; 27 import org.onlab.onos.net.Port;
37 import org.onlab.onos.net.PortNumber; 28 import org.onlab.onos.net.PortNumber;
...@@ -41,24 +32,23 @@ import org.onlab.onos.net.device.DeviceStore; ...@@ -41,24 +32,23 @@ import org.onlab.onos.net.device.DeviceStore;
41 import org.onlab.onos.net.device.PortDescription; 32 import org.onlab.onos.net.device.PortDescription;
42 import org.onlab.onos.net.provider.ProviderId; 33 import org.onlab.onos.net.provider.ProviderId;
43 import org.onlab.onos.store.StoreService; 34 import org.onlab.onos.store.StoreService;
44 -import org.onlab.util.KryoPool; 35 +import org.onlab.onos.store.impl.AbsentInvalidatingLoadingCache;
45 import org.slf4j.Logger; 36 import org.slf4j.Logger;
46 37
47 -import com.google.common.base.Optional; 38 +import java.util.ArrayList;
48 -import com.google.common.cache.CacheBuilder; 39 +import java.util.Collections;
49 -import com.google.common.cache.CacheLoader; 40 +import java.util.HashMap;
50 -import com.google.common.cache.LoadingCache; 41 +import java.util.HashSet;
51 -import com.google.common.collect.ImmutableList; 42 +import java.util.Iterator;
52 -import com.google.common.collect.ImmutableSet; 43 +import java.util.List;
53 -import com.google.common.collect.ImmutableSet.Builder; 44 +import java.util.Map;
54 -import com.hazelcast.core.EntryAdapter; 45 +import java.util.Objects;
55 -import com.hazelcast.core.EntryEvent; 46 +import java.util.Set;
56 -import com.hazelcast.core.HazelcastInstance;
57 -import com.hazelcast.core.IMap;
58 -import com.hazelcast.core.ISet;
59 -import com.hazelcast.core.MapEvent;
60 47
61 -import de.javakaffee.kryoserializers.URISerializer; 48 +import static com.google.common.base.Preconditions.checkArgument;
49 +import static com.google.common.base.Preconditions.checkNotNull;
50 +import static org.onlab.onos.net.device.DeviceEvent.Type.*;
51 +import static org.slf4j.LoggerFactory.getLogger;
62 52
63 53
64 /** 54 /**
...@@ -72,27 +62,6 @@ public class DistributedDeviceStore implements DeviceStore { ...@@ -72,27 +62,6 @@ public class DistributedDeviceStore implements DeviceStore {
72 62
73 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found"; 63 public static final String DEVICE_NOT_FOUND = "Device with ID %s not found";
74 64
75 - // FIXME Slice out types used in common to separate pool/namespace.
76 - private static final KryoPool POOL = KryoPool.newBuilder()
77 - .register(
78 - ArrayList.class,
79 - HashMap.class,
80 -
81 - Device.Type.class,
82 -
83 - DefaultDevice.class,
84 - MastershipRole.class,
85 - Port.class,
86 - Element.class
87 - )
88 - .register(URI.class, new URISerializer())
89 - .register(ProviderId.class, new ProviderIdSerializer())
90 - .register(DeviceId.class, new DeviceIdSerializer())
91 - .register(PortNumber.class, new PortNumberSerializer())
92 - .register(DefaultPort.class, new DefaultPortSerializer())
93 - .build()
94 - .populate(10);
95 -
96 // private IMap<DeviceId, DefaultDevice> cache; 65 // private IMap<DeviceId, DefaultDevice> cache;
97 private IMap<byte[], byte[]> rawDevices; 66 private IMap<byte[], byte[]> rawDevices;
98 private LoadingCache<DeviceId, Optional<DefaultDevice>> devices; 67 private LoadingCache<DeviceId, Optional<DefaultDevice>> devices;
...@@ -126,33 +95,33 @@ public class DistributedDeviceStore implements DeviceStore { ...@@ -126,33 +95,33 @@ public class DistributedDeviceStore implements DeviceStore {
126 95
127 // TODO decide on Map name scheme to avoid collision 96 // TODO decide on Map name scheme to avoid collision
128 rawDevices = theInstance.getMap("devices"); 97 rawDevices = theInstance.getMap("devices");
129 - devices = new AbsentInvalidatingLoadingCache<DeviceId, DefaultDevice>( 98 + devices = new AbsentInvalidatingLoadingCache<>(
130 CacheBuilder.newBuilder() 99 CacheBuilder.newBuilder()
131 .build(new OptionalCacheLoader<DeviceId, DefaultDevice>(rawDevices))); 100 .build(new OptionalCacheLoader<DeviceId, DefaultDevice>(rawDevices)));
132 // refresh/populate cache based on notification from other instance 101 // refresh/populate cache based on notification from other instance
133 rawDevices.addEntryListener( 102 rawDevices.addEntryListener(
134 - new RemoteEventHandler<DeviceId, DefaultDevice>(devices), 103 + new RemoteEventHandler<>(devices),
135 includeValue); 104 includeValue);
136 105
137 rawRoles = theInstance.getMap("roles"); 106 rawRoles = theInstance.getMap("roles");
138 - roles = new AbsentInvalidatingLoadingCache<DeviceId, MastershipRole>( 107 + roles = new AbsentInvalidatingLoadingCache<>(
139 CacheBuilder.newBuilder() 108 CacheBuilder.newBuilder()
140 .build(new OptionalCacheLoader<DeviceId, MastershipRole>(rawRoles))); 109 .build(new OptionalCacheLoader<DeviceId, MastershipRole>(rawRoles)));
141 // refresh/populate cache based on notification from other instance 110 // refresh/populate cache based on notification from other instance
142 rawRoles.addEntryListener( 111 rawRoles.addEntryListener(
143 - new RemoteEventHandler<DeviceId, MastershipRole>(roles), 112 + new RemoteEventHandler<>(roles),
144 includeValue); 113 includeValue);
145 114
146 // TODO cache avai 115 // TODO cache avai
147 availableDevices = theInstance.getSet("availableDevices"); 116 availableDevices = theInstance.getSet("availableDevices");
148 117
149 rawDevicePorts = theInstance.getMap("devicePorts"); 118 rawDevicePorts = theInstance.getMap("devicePorts");
150 - devicePorts = new AbsentInvalidatingLoadingCache<DeviceId, Map<PortNumber, Port>>( 119 + devicePorts = new AbsentInvalidatingLoadingCache<>(
151 CacheBuilder.newBuilder() 120 CacheBuilder.newBuilder()
152 .build(new OptionalCacheLoader<DeviceId, Map<PortNumber, Port>>(rawDevicePorts))); 121 .build(new OptionalCacheLoader<DeviceId, Map<PortNumber, Port>>(rawDevicePorts)));
153 // refresh/populate cache based on notification from other instance 122 // refresh/populate cache based on notification from other instance
154 rawDevicePorts.addEntryListener( 123 rawDevicePorts.addEntryListener(
155 - new RemoteEventHandler<DeviceId, Map<PortNumber, Port>>(devicePorts), 124 + new RemoteEventHandler<>(devicePorts),
156 includeValue); 125 includeValue);
157 126
158 } 127 }
...@@ -181,7 +150,7 @@ public class DistributedDeviceStore implements DeviceStore { ...@@ -181,7 +150,7 @@ public class DistributedDeviceStore implements DeviceStore {
181 // } 150 // }
182 151
183 // TODO builder v.s. copyOf. Guava semms to be using copyOf? 152 // TODO builder v.s. copyOf. Guava semms to be using copyOf?
184 - Builder<Device> builder = ImmutableSet.<Device>builder(); 153 + Builder<Device> builder = ImmutableSet.builder();
185 for (Optional<DefaultDevice> e : devices.asMap().values()) { 154 for (Optional<DefaultDevice> e : devices.asMap().values()) {
186 if (e.isPresent()) { 155 if (e.isPresent()) {
187 builder.add(e.get()); 156 builder.add(e.get());
...@@ -428,16 +397,12 @@ public class DistributedDeviceStore implements DeviceStore { ...@@ -428,16 +397,12 @@ public class DistributedDeviceStore implements DeviceStore {
428 } 397 }
429 398
430 // TODO cache serialized DeviceID if we suffer from serialization cost 399 // TODO cache serialized DeviceID if we suffer from serialization cost
431 - 400 + private byte[] serialize(final Object obj) {
432 - private static byte[] serialize(final Object obj) { 401 + return storeService.serialize(obj);
433 - return POOL.serialize(obj);
434 } 402 }
435 403
436 - private static <T> T deserialize(final byte[] bytes) { 404 + private <T> T deserialize(final byte[] bytes) {
437 - if (bytes == null) { 405 + return storeService.deserialize(bytes);
438 - return null;
439 - }
440 - return POOL.deserialize(bytes);
441 } 406 }
442 407
443 /** 408 /**
...@@ -446,7 +411,7 @@ public class DistributedDeviceStore implements DeviceStore { ...@@ -446,7 +411,7 @@ public class DistributedDeviceStore implements DeviceStore {
446 * @param <K> IMap key type after deserialization 411 * @param <K> IMap key type after deserialization
447 * @param <V> IMap value type after deserialization 412 * @param <V> IMap value type after deserialization
448 */ 413 */
449 - public static final class RemoteEventHandler<K, V> extends 414 + public final class RemoteEventHandler<K, V> extends
450 EntryAdapter<byte[], byte[]> { 415 EntryAdapter<byte[], byte[]> {
451 416
452 private LoadingCache<K, Optional<V>> cache; 417 private LoadingCache<K, Optional<V>> cache;
...@@ -468,14 +433,13 @@ public class DistributedDeviceStore implements DeviceStore { ...@@ -468,14 +433,13 @@ public class DistributedDeviceStore implements DeviceStore {
468 433
469 @Override 434 @Override
470 public void entryUpdated(EntryEvent<byte[], byte[]> event) { 435 public void entryUpdated(EntryEvent<byte[], byte[]> event) {
471 - cache.put(POOL.<K>deserialize(event.getKey()), 436 + cache.put(storeService.<K>deserialize(event.getKey()),
472 - Optional.of(POOL.<V>deserialize( 437 + Optional.of(storeService.<V>deserialize(event.getValue())));
473 - event.getValue())));
474 } 438 }
475 439
476 @Override 440 @Override
477 public void entryRemoved(EntryEvent<byte[], byte[]> event) { 441 public void entryRemoved(EntryEvent<byte[], byte[]> event) {
478 - cache.invalidate(POOL.<DeviceId>deserialize(event.getKey())); 442 + cache.invalidate(storeService.<DeviceId>deserialize(event.getKey()));
479 } 443 }
480 444
481 @Override 445 @Override
...@@ -491,7 +455,7 @@ public class DistributedDeviceStore implements DeviceStore { ...@@ -491,7 +455,7 @@ public class DistributedDeviceStore implements DeviceStore {
491 * @param <K> IMap key type after deserialization 455 * @param <K> IMap key type after deserialization
492 * @param <V> IMap value type after deserialization 456 * @param <V> IMap value type after deserialization
493 */ 457 */
494 - public static final class OptionalCacheLoader<K, V> extends 458 + public final class OptionalCacheLoader<K, V> extends
495 CacheLoader<K, Optional<V>> { 459 CacheLoader<K, Optional<V>> {
496 460
497 private IMap<byte[], byte[]> rawMap; 461 private IMap<byte[], byte[]> rawMap;
...@@ -507,7 +471,7 @@ public class DistributedDeviceStore implements DeviceStore { ...@@ -507,7 +471,7 @@ public class DistributedDeviceStore implements DeviceStore {
507 471
508 @Override 472 @Override
509 public Optional<V> load(K key) throws Exception { 473 public Optional<V> load(K key) throws Exception {
510 - byte[] keyBytes = serialize(key); 474 + byte[] keyBytes = storeService.serialize(key);
511 byte[] valBytes = rawMap.get(keyBytes); 475 byte[] valBytes = rawMap.get(keyBytes);
512 if (valBytes == null) { 476 if (valBytes == null) {
513 return Optional.absent(); 477 return Optional.absent();
......
1 -package org.onlab.onos.store.device.impl; 1 +package org.onlab.onos.store.impl;
2 2
3 import java.util.concurrent.Callable; 3 import java.util.concurrent.Callable;
4 import java.util.concurrent.ExecutionException; 4 import java.util.concurrent.ExecutionException;
......
...@@ -2,14 +2,33 @@ package org.onlab.onos.store.impl; ...@@ -2,14 +2,33 @@ package org.onlab.onos.store.impl;
2 2
3 import com.hazelcast.core.Hazelcast; 3 import com.hazelcast.core.Hazelcast;
4 import com.hazelcast.core.HazelcastInstance; 4 import com.hazelcast.core.HazelcastInstance;
5 +import de.javakaffee.kryoserializers.URISerializer;
5 import org.apache.felix.scr.annotations.Activate; 6 import org.apache.felix.scr.annotations.Activate;
6 import org.apache.felix.scr.annotations.Component; 7 import org.apache.felix.scr.annotations.Component;
7 import org.apache.felix.scr.annotations.Deactivate; 8 import org.apache.felix.scr.annotations.Deactivate;
8 import org.apache.felix.scr.annotations.Service; 9 import org.apache.felix.scr.annotations.Service;
10 +import org.onlab.onos.net.DefaultDevice;
11 +import org.onlab.onos.net.DefaultPort;
12 +import org.onlab.onos.net.Device;
13 +import org.onlab.onos.net.DeviceId;
14 +import org.onlab.onos.net.Element;
15 +import org.onlab.onos.net.MastershipRole;
16 +import org.onlab.onos.net.Port;
17 +import org.onlab.onos.net.PortNumber;
18 +import org.onlab.onos.net.provider.ProviderId;
9 import org.onlab.onos.store.StoreService; 19 import org.onlab.onos.store.StoreService;
20 +import org.onlab.onos.store.serializers.DefaultPortSerializer;
21 +import org.onlab.onos.store.serializers.DeviceIdSerializer;
22 +import org.onlab.onos.store.serializers.PortNumberSerializer;
23 +import org.onlab.onos.store.serializers.ProviderIdSerializer;
24 +import org.onlab.util.KryoPool;
10 import org.slf4j.Logger; 25 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory; 26 import org.slf4j.LoggerFactory;
12 27
28 +import java.net.URI;
29 +import java.util.ArrayList;
30 +import java.util.HashMap;
31 +
13 /** 32 /**
14 * Auxiliary bootstrap of distributed store. 33 * Auxiliary bootstrap of distributed store.
15 */ 34 */
...@@ -20,15 +39,45 @@ public class StoreManager implements StoreService { ...@@ -20,15 +39,45 @@ public class StoreManager implements StoreService {
20 private final Logger log = LoggerFactory.getLogger(getClass()); 39 private final Logger log = LoggerFactory.getLogger(getClass());
21 40
22 protected HazelcastInstance instance; 41 protected HazelcastInstance instance;
42 + private KryoPool serializerPool;
43 +
23 44
24 @Activate 45 @Activate
25 public void activate() { 46 public void activate() {
26 instance = Hazelcast.newHazelcastInstance(); 47 instance = Hazelcast.newHazelcastInstance();
48 + setupKryoPool();
27 log.info("Started"); 49 log.info("Started");
28 } 50 }
29 51
52 + /**
53 + * Sets up the common serialzers pool.
54 + */
55 + protected void setupKryoPool() {
56 + // FIXME Slice out types used in common to separate pool/namespace.
57 + serializerPool = KryoPool.newBuilder()
58 + .register(
59 + ArrayList.class,
60 + HashMap.class,
61 +
62 + Device.Type.class,
63 +
64 + DefaultDevice.class,
65 + MastershipRole.class,
66 + Port.class,
67 + Element.class
68 + )
69 + .register(URI.class, new URISerializer())
70 + .register(ProviderId.class, new ProviderIdSerializer())
71 + .register(DeviceId.class, new DeviceIdSerializer())
72 + .register(PortNumber.class, new PortNumberSerializer())
73 + .register(DefaultPort.class, new DefaultPortSerializer())
74 + .build()
75 + .populate(10);
76 + }
77 +
30 @Deactivate 78 @Deactivate
31 public void deactivate() { 79 public void deactivate() {
80 + instance.shutdown();
32 log.info("Stopped"); 81 log.info("Stopped");
33 } 82 }
34 83
...@@ -36,4 +85,19 @@ public class StoreManager implements StoreService { ...@@ -36,4 +85,19 @@ public class StoreManager implements StoreService {
36 public HazelcastInstance getHazelcastInstance() { 85 public HazelcastInstance getHazelcastInstance() {
37 return instance; 86 return instance;
38 } 87 }
88 +
89 +
90 + @Override
91 + public byte[] serialize(final Object obj) {
92 + return serializerPool.serialize(obj);
93 + }
94 +
95 + @Override
96 + public <T> T deserialize(final byte[] bytes) {
97 + if (bytes == null) {
98 + return null;
99 + }
100 + return serializerPool.deserialize(bytes);
101 + }
102 +
39 } 103 }
......
1 -package org.onlab.onos.store.device.impl; 1 +package org.onlab.onos.store.serializers;
2 2
3 import java.util.ArrayList; 3 import java.util.ArrayList;
4 import java.util.Collection; 4 import java.util.Collection;
......
1 -package org.onlab.onos.store.device.impl; 1 +package org.onlab.onos.store.serializers;
2 2
3 import java.net.URI; 3 import java.net.URI;
4 4
......
1 -package org.onlab.onos.store.device.impl; 1 +package org.onlab.onos.store.serializers;
2 2
3 import org.onlab.packet.IpPrefix; 3 import org.onlab.packet.IpPrefix;
4 4
......
1 -package org.onlab.onos.store.device.impl; 1 +package org.onlab.onos.store.serializers;
2 2
3 import org.onlab.onos.net.PortNumber; 3 import org.onlab.onos.net.PortNumber;
4 4
......
1 -package org.onlab.onos.store.device.impl; 1 +package org.onlab.onos.store.serializers;
2 2
3 import org.onlab.onos.net.provider.ProviderId; 3 import org.onlab.onos.net.provider.ProviderId;
4 4
......
1 +/**
2 + * Various Kryo serializers for use in distributed stores.
3 + */
4 +package org.onlab.onos.store.serializers;
...\ No newline at end of file ...\ No newline at end of file
...@@ -172,6 +172,11 @@ ...@@ -172,6 +172,11 @@
172 </dependency> 172 </dependency>
173 <dependency> 173 <dependency>
174 <groupId>org.onlab.onos</groupId> 174 <groupId>org.onlab.onos</groupId>
175 + <artifactId>onlab-osgi</artifactId>
176 + <version>${project.version}</version>
177 + </dependency>
178 + <dependency>
179 + <groupId>org.onlab.onos</groupId>
175 <artifactId>onlab-junit</artifactId> 180 <artifactId>onlab-junit</artifactId>
176 <version>1.0.0-SNAPSHOT</version> 181 <version>1.0.0-SNAPSHOT</version>
177 <scope>test</scope> 182 <scope>test</scope>
......
...@@ -46,7 +46,8 @@ alias gui='open http://localhost:8181/onos/tvue' ...@@ -46,7 +46,8 @@ alias gui='open http://localhost:8181/onos/tvue'
46 # Test related conveniences 46 # Test related conveniences
47 47
48 # SSH to a specified ONOS instance 48 # SSH to a specified ONOS instance
49 -alias sshctl=onos-ssh 49 +alias sshctl='onos-ssh'
50 +alias sshnet='onos-ssh $OCN'
50 51
51 # Applies the settings in the specified cell file or lists current cell definition 52 # Applies the settings in the specified cell file or lists current cell definition
52 # if no cell file is given. 53 # if no cell file is given.
...@@ -62,6 +63,7 @@ function cell { ...@@ -62,6 +63,7 @@ function cell {
62 env | egrep "ONOS_CELL" 63 env | egrep "ONOS_CELL"
63 env | egrep "OCI" 64 env | egrep "OCI"
64 env | egrep "OC[0-9]+" | sort 65 env | egrep "OC[0-9]+" | sort
66 + env | egrep "OCN"
65 fi 67 fi
66 } 68 }
67 69
......
...@@ -6,6 +6,6 @@ ...@@ -6,6 +6,6 @@
6 [ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1 6 [ ! -d "$ONOS_ROOT" ] && echo "ONOS_ROOT is not defined" >&2 && exit 1
7 . $ONOS_ROOT/tools/build/envDefaults 7 . $ONOS_ROOT/tools/build/envDefaults
8 8
9 -for node in $(env | sort | egrep "OC[0-9]+" | cut -d= -f2); do 9 +for node in $(env | sort | egrep "OC[0-9N]+" | cut -d= -f2); do
10 printf "%s: " $node; ssh -n -o PasswordAuthentication=no $ONOS_USER@$node date 10 printf "%s: " $node; ssh -n -o PasswordAuthentication=no $ONOS_USER@$node date
11 done 11 done
...\ No newline at end of file ...\ No newline at end of file
......
1 -# Default virtual box ONOS instances 1,2 & 3 1 +# Default virtual box ONOS instances 1,2 & ONOS mininet box
2 2
3 export OC1="192.168.56.101" 3 export OC1="192.168.56.101"
4 export OC2="192.168.56.102" 4 export OC2="192.168.56.102"
5 -export OC3="192.168.56.103" 5 +
6 +export OCN="192.168.56.103"
6 7
7 8
......
...@@ -7,8 +7,15 @@ import org.osgi.framework.FrameworkUtil; ...@@ -7,8 +7,15 @@ import org.osgi.framework.FrameworkUtil;
7 * Default implementation of the service directory using OSGi framework utilities. 7 * Default implementation of the service directory using OSGi framework utilities.
8 */ 8 */
9 public class DefaultServiceDirectory implements ServiceDirectory { 9 public class DefaultServiceDirectory implements ServiceDirectory {
10 - @Override 10 +
11 - public <T> T get(Class<T> serviceClass) { 11 + /**
12 + * Returns the reference to the implementation of the specified service.
13 + *
14 + * @param serviceClass service class
15 + * @param <T> type of service
16 + * @return service implementation
17 + */
18 + public static <T> T getService(Class<T> serviceClass) {
12 BundleContext bc = FrameworkUtil.getBundle(serviceClass).getBundleContext(); 19 BundleContext bc = FrameworkUtil.getBundle(serviceClass).getBundleContext();
13 T impl = bc.getService(bc.getServiceReference(serviceClass)); 20 T impl = bc.getService(bc.getServiceReference(serviceClass));
14 if (impl == null) { 21 if (impl == null) {
...@@ -16,4 +23,10 @@ public class DefaultServiceDirectory implements ServiceDirectory { ...@@ -16,4 +23,10 @@ public class DefaultServiceDirectory implements ServiceDirectory {
16 } 23 }
17 return impl; 24 return impl;
18 } 25 }
26 +
27 + @Override
28 + public <T> T get(Class<T> serviceClass) {
29 + return getService(serviceClass);
30 + }
31 +
19 } 32 }
......