tom

Merge remote-tracking branch 'origin/master'

...@@ -24,6 +24,11 @@ ...@@ -24,6 +24,11 @@
24 </dependency> 24 </dependency>
25 <dependency> 25 <dependency>
26 <groupId>org.onlab.onos</groupId> 26 <groupId>org.onlab.onos</groupId>
27 + <artifactId>onlab-osgi</artifactId>
28 + <version>${project.version}</version>
29 + </dependency>
30 + <dependency>
31 + <groupId>org.onlab.onos</groupId>
27 <artifactId>onlab-nio</artifactId> 32 <artifactId>onlab-nio</artifactId>
28 <version>${project.version}</version> 33 <version>${project.version}</version>
29 </dependency> 34 </dependency>
......
1 +package org.onlab.onos.foo;
2 +
3 +import java.io.IOException;
4 +
5 +import org.onlab.netty.Message;
6 +import org.onlab.netty.MessageHandler;
7 +import org.slf4j.Logger;
8 +import org.slf4j.LoggerFactory;
9 +
10 +
11 +/**
12 + * Message handler that echos the message back to the sender.
13 + */
14 +public class NettyEchoHandler implements MessageHandler {
15 +
16 + private final Logger log = LoggerFactory.getLogger(getClass());
17 +
18 + @Override
19 + public void handle(Message message) throws IOException {
20 + //log.info("Received message. Echoing it back to the sender.");
21 + message.respond(message.payload());
22 + }
23 +}
1 +package org.onlab.onos.foo;
2 +
3 +import org.onlab.netty.Message;
4 +import org.onlab.netty.MessageHandler;
5 +import org.slf4j.Logger;
6 +import org.slf4j.LoggerFactory;
7 +
8 +/**
9 + * A MessageHandler that simply logs the information.
10 + */
11 +public class NettyLoggingHandler implements MessageHandler {
12 +
13 + private final Logger log = LoggerFactory.getLogger(getClass());
14 +
15 + @Override
16 + public void handle(Message message) {
17 + log.info("Received message. Payload has {} bytes", message.payload().length);
18 + }
19 +}
...@@ -2,7 +2,6 @@ package org.onlab.onos.foo; ...@@ -2,7 +2,6 @@ package org.onlab.onos.foo;
2 2
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.util.concurrent.ExecutionException; 4 import java.util.concurrent.ExecutionException;
5 -import java.util.concurrent.TimeUnit;
6 import java.util.concurrent.TimeoutException; 5 import java.util.concurrent.TimeoutException;
7 6
8 import org.onlab.metrics.MetricsComponent; 7 import org.onlab.metrics.MetricsComponent;
...@@ -35,28 +34,35 @@ public final class SimpleNettyClient { ...@@ -35,28 +34,35 @@ public final class SimpleNettyClient {
35 MetricsManager metrics = new MetricsManager(); 34 MetricsManager metrics = new MetricsManager();
36 messaging.activate(); 35 messaging.activate();
37 metrics.activate(); 36 metrics.activate();
38 - MetricsFeature feature = new MetricsFeature("timers"); 37 + MetricsFeature feature = new MetricsFeature("latency");
39 MetricsComponent component = metrics.registerComponent("NettyMessaging"); 38 MetricsComponent component = metrics.registerComponent("NettyMessaging");
40 - Timer sendAsyncTimer = metrics.createTimer(component, feature, "AsyncSender"); 39 +
41 - final int warmup = 100; 40 + final int warmup = 10000;
42 for (int i = 0; i < warmup; i++) { 41 for (int i = 0; i < warmup; i++) {
42 + messaging.sendAsync(new Endpoint("localhost", 8081), "simple", "Hello World".getBytes());
43 + Response response = messaging
44 + .sendAndReceive(new Endpoint("localhost", 8081), "echo",
45 + "Hello World".getBytes());
46 + }
47 +
48 + Timer sendAsyncTimer = metrics.createTimer(component, feature, "AsyncSender");
49 + Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive");
50 +
51 + final int iterations = 10000000;
52 + for (int i = 0; i < iterations; i++) {
43 Timer.Context context = sendAsyncTimer.time(); 53 Timer.Context context = sendAsyncTimer.time();
44 - messaging.sendAsync(new Endpoint("localhost", 8080), "simple", "Hello World".getBytes()); 54 + messaging.sendAsync(new Endpoint("localhost", 8081), "simple", "Hello World".getBytes());
45 context.stop(); 55 context.stop();
46 } 56 }
47 - metrics.registerMetric(component, feature, "AsyncTimer", sendAsyncTimer);
48 57
49 - Timer sendAndReceiveTimer = metrics.createTimer(component, feature, "SendAndReceive");
50 - final int iterations = 1000000;
51 for (int i = 0; i < iterations; i++) { 58 for (int i = 0; i < iterations; i++) {
52 Timer.Context context = sendAndReceiveTimer.time(); 59 Timer.Context context = sendAndReceiveTimer.time();
53 Response response = messaging 60 Response response = messaging
54 - .sendAndReceive(new Endpoint("localhost", 8080), "echo", 61 + .sendAndReceive(new Endpoint("localhost", 8081), "echo",
55 "Hello World".getBytes()); 62 "Hello World".getBytes());
56 - System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS))); 63 + // System.out.println("Got back:" + new String(response.get(2, TimeUnit.SECONDS)));
57 context.stop(); 64 context.stop();
58 } 65 }
59 - metrics.registerMetric(component, feature, "AsyncTimer", sendAndReceiveTimer);
60 } 66 }
61 67
62 public static class TestNettyMessagingService extends NettyMessagingService { 68 public static class TestNettyMessagingService extends NettyMessagingService {
......
...@@ -13,6 +13,7 @@ import org.onlab.onos.cli.AbstractShellCommand; ...@@ -13,6 +13,7 @@ import org.onlab.onos.cli.AbstractShellCommand;
13 description = "Starts the simple Netty client") 13 description = "Starts the simple Netty client")
14 public class SimpleNettyClientCommand extends AbstractShellCommand { 14 public class SimpleNettyClientCommand extends AbstractShellCommand {
15 15
16 + //FIXME: replace these arguments with proper ones needed for the test.
16 @Argument(index = 0, name = "serverIp", description = "Server IP address", 17 @Argument(index = 0, name = "serverIp", description = "Server IP address",
17 required = false, multiValued = false) 18 required = false, multiValued = false)
18 String serverIp = "127.0.0.1"; 19 String serverIp = "127.0.0.1";
......
1 package org.onlab.onos.foo; 1 package org.onlab.onos.foo;
2 2
3 -import org.onlab.netty.EchoHandler;
4 import org.onlab.netty.NettyMessagingService; 3 import org.onlab.netty.NettyMessagingService;
5 import org.slf4j.Logger; 4 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory; 5 import org.slf4j.LoggerFactory;
...@@ -19,10 +18,10 @@ import org.slf4j.LoggerFactory; ...@@ -19,10 +18,10 @@ import org.slf4j.LoggerFactory;
19 } 18 }
20 19
21 public static void startStandalone(String[] args) throws Exception { 20 public static void startStandalone(String[] args) throws Exception {
22 - NettyMessagingService server = new NettyMessagingService(8080); 21 + NettyMessagingService server = new NettyMessagingService(8081);
23 server.activate(); 22 server.activate();
24 - server.registerHandler("simple", new org.onlab.netty.LoggingHandler()); 23 + server.registerHandler("simple", new NettyLoggingHandler());
25 - server.registerHandler("echo", new EchoHandler()); 24 + server.registerHandler("echo", new NettyEchoHandler());
26 } 25 }
27 } 26 }
28 27
......
...@@ -9,10 +9,11 @@ import org.onlab.onos.cli.AbstractShellCommand; ...@@ -9,10 +9,11 @@ import org.onlab.onos.cli.AbstractShellCommand;
9 /** 9 /**
10 * Starts the Simple Netty server. 10 * Starts the Simple Netty server.
11 */ 11 */
12 -@Command(scope = "onos", name = "test-netty-server", 12 +@Command(scope = "onos", name = "simple-netty-server",
13 description = "Starts the simple netty server") 13 description = "Starts the simple netty server")
14 public class SimpleNettyServerCommand extends AbstractShellCommand { 14 public class SimpleNettyServerCommand extends AbstractShellCommand {
15 15
16 + //FIXME: Replace these with parameters for
16 @Argument(index = 0, name = "serverIp", description = "Server IP address", 17 @Argument(index = 0, name = "serverIp", description = "Server IP address",
17 required = false, multiValued = false) 18 required = false, multiValued = false)
18 String serverIp = "127.0.0.1"; 19 String serverIp = "127.0.0.1";
......
...@@ -7,6 +7,12 @@ ...@@ -7,6 +7,12 @@
7 <command> 7 <command>
8 <action class="org.onlab.onos.foo.TestIOServerCommand"/> 8 <action class="org.onlab.onos.foo.TestIOServerCommand"/>
9 </command> 9 </command>
10 + <command>
11 + <action class="org.onlab.onos.foo.SimpleNettyServerCommand"/>
12 + </command>
13 + <command>
14 + <action class="org.onlab.onos.foo.SimpleNettyClientCommand"/>
15 + </command>
10 </command-bundle> 16 </command-bundle>
11 17
12 </blueprint> 18 </blueprint>
......
1 package org.onlab.onos.net.device.impl; 1 package org.onlab.onos.net.device.impl;
2 2
3 import com.google.common.collect.Sets; 3 import com.google.common.collect.Sets;
4 +
4 import org.junit.After; 5 import org.junit.After;
5 import org.junit.Before; 6 import org.junit.Before;
6 import org.junit.Ignore; 7 import org.junit.Ignore;
7 import org.junit.Test; 8 import org.junit.Test;
9 +import org.onlab.onos.cluster.ClusterEventListener;
10 +import org.onlab.onos.cluster.ClusterService;
11 +import org.onlab.onos.cluster.ControllerNode;
12 +import org.onlab.onos.cluster.DefaultControllerNode;
8 import org.onlab.onos.cluster.MastershipServiceAdapter; 13 import org.onlab.onos.cluster.MastershipServiceAdapter;
14 +import org.onlab.onos.cluster.MastershipTerm;
15 +import org.onlab.onos.cluster.MastershipTermService;
9 import org.onlab.onos.cluster.NodeId; 16 import org.onlab.onos.cluster.NodeId;
17 +import org.onlab.onos.cluster.ControllerNode.State;
10 import org.onlab.onos.event.Event; 18 import org.onlab.onos.event.Event;
11 import org.onlab.onos.event.impl.TestEventDispatcher; 19 import org.onlab.onos.event.impl.TestEventDispatcher;
12 import org.onlab.onos.net.Device; 20 import org.onlab.onos.net.Device;
...@@ -27,7 +35,9 @@ import org.onlab.onos.net.device.DeviceService; ...@@ -27,7 +35,9 @@ import org.onlab.onos.net.device.DeviceService;
27 import org.onlab.onos.net.device.PortDescription; 35 import org.onlab.onos.net.device.PortDescription;
28 import org.onlab.onos.net.provider.AbstractProvider; 36 import org.onlab.onos.net.provider.AbstractProvider;
29 import org.onlab.onos.net.provider.ProviderId; 37 import org.onlab.onos.net.provider.ProviderId;
38 +import org.onlab.onos.store.ClockProviderService;
30 import org.onlab.onos.store.trivial.impl.SimpleDeviceStore; 39 import org.onlab.onos.store.trivial.impl.SimpleDeviceStore;
40 +import org.onlab.packet.IpPrefix;
31 41
32 import java.util.ArrayList; 42 import java.util.ArrayList;
33 import java.util.Iterator; 43 import java.util.Iterator;
...@@ -56,6 +66,8 @@ public class DeviceManagerTest { ...@@ -56,6 +66,8 @@ public class DeviceManagerTest {
56 private static final PortNumber P1 = PortNumber.portNumber(1); 66 private static final PortNumber P1 = PortNumber.portNumber(1);
57 private static final PortNumber P2 = PortNumber.portNumber(2); 67 private static final PortNumber P2 = PortNumber.portNumber(2);
58 private static final PortNumber P3 = PortNumber.portNumber(3); 68 private static final PortNumber P3 = PortNumber.portNumber(3);
69 + private static final NodeId NID_LOCAL = new NodeId("local");
70 + private static final IpPrefix LOCALHOST = IpPrefix.valueOf("127.0.0.1");
59 71
60 private DeviceManager mgr; 72 private DeviceManager mgr;
61 73
...@@ -75,6 +87,8 @@ public class DeviceManagerTest { ...@@ -75,6 +87,8 @@ public class DeviceManagerTest {
75 mgr.store = new SimpleDeviceStore(); 87 mgr.store = new SimpleDeviceStore();
76 mgr.eventDispatcher = new TestEventDispatcher(); 88 mgr.eventDispatcher = new TestEventDispatcher();
77 mgr.mastershipService = new TestMastershipService(); 89 mgr.mastershipService = new TestMastershipService();
90 + mgr.clusterService = new TestClusterService();
91 + mgr.clockProviderService = new TestClockProviderService();
78 mgr.activate(); 92 mgr.activate();
79 93
80 service.addListener(listener); 94 service.addListener(listener);
...@@ -273,6 +287,59 @@ public class DeviceManagerTest { ...@@ -273,6 +287,59 @@ public class DeviceManagerTest {
273 public MastershipRole requestRoleFor(DeviceId deviceId) { 287 public MastershipRole requestRoleFor(DeviceId deviceId) {
274 return MastershipRole.MASTER; 288 return MastershipRole.MASTER;
275 } 289 }
290 +
291 + @Override
292 + public MastershipTermService requestTermService() {
293 + return new MastershipTermService() {
294 + @Override
295 + public MastershipTerm getMastershipTerm(DeviceId deviceId) {
296 + // FIXME: just returning something not null
297 + return MastershipTerm.of(NID_LOCAL, 1);
298 + }
299 + };
300 + }
301 + }
302 +
303 + // code clone
304 + private final class TestClusterService implements ClusterService {
305 +
306 + ControllerNode local = new DefaultControllerNode(NID_LOCAL, LOCALHOST);
307 +
308 + @Override
309 + public ControllerNode getLocalNode() {
310 + return local;
311 + }
312 +
313 + @Override
314 + public Set<ControllerNode> getNodes() {
315 + return null;
276 } 316 }
277 317
318 + @Override
319 + public ControllerNode getNode(NodeId nodeId) {
320 + return null;
321 + }
322 +
323 + @Override
324 + public State getState(NodeId nodeId) {
325 + return null;
326 + }
327 +
328 + @Override
329 + public void addListener(ClusterEventListener listener) {
330 + }
331 +
332 + @Override
333 + public void removeListener(ClusterEventListener listener) {
334 + }
335 + }
336 +
337 + private final class TestClockProviderService implements
338 + ClockProviderService {
339 +
340 + @Override
341 + public void setMastershipTerm(DeviceId deviceId, MastershipTerm term) {
342 + // TODO Auto-generated method stub
343 + }
344 + }
278 } 345 }
......
1 -package org.onlab.onos.net.device.impl;
2 -
3 -import com.google.common.collect.Iterables;
4 -import com.google.common.collect.Sets;
5 -import com.hazelcast.config.Config;
6 -import com.hazelcast.core.Hazelcast;
7 -
8 -import org.junit.After;
9 -import org.junit.Before;
10 -import org.junit.Test;
11 -import org.onlab.onos.cluster.DefaultControllerNode;
12 -import org.onlab.onos.cluster.MastershipServiceAdapter;
13 -import org.onlab.onos.cluster.NodeId;
14 -import org.onlab.onos.event.Event;
15 -import org.onlab.onos.event.EventDeliveryService;
16 -import org.onlab.onos.event.impl.TestEventDispatcher;
17 -import org.onlab.onos.net.Device;
18 -import org.onlab.onos.net.DeviceId;
19 -import org.onlab.onos.net.MastershipRole;
20 -import org.onlab.onos.net.Port;
21 -import org.onlab.onos.net.PortNumber;
22 -import org.onlab.onos.net.device.DefaultDeviceDescription;
23 -import org.onlab.onos.net.device.DefaultPortDescription;
24 -import org.onlab.onos.net.device.DeviceAdminService;
25 -import org.onlab.onos.net.device.DeviceDescription;
26 -import org.onlab.onos.net.device.DeviceEvent;
27 -import org.onlab.onos.net.device.DeviceListener;
28 -import org.onlab.onos.net.device.DeviceProvider;
29 -import org.onlab.onos.net.device.DeviceProviderRegistry;
30 -import org.onlab.onos.net.device.DeviceProviderService;
31 -import org.onlab.onos.net.device.DeviceService;
32 -import org.onlab.onos.net.device.PortDescription;
33 -import org.onlab.onos.net.provider.AbstractProvider;
34 -import org.onlab.onos.net.provider.ProviderId;
35 -import org.onlab.onos.store.common.StoreManager;
36 -import org.onlab.onos.store.common.StoreService;
37 -import org.onlab.onos.store.common.TestStoreManager;
38 -import org.onlab.onos.store.device.impl.DistributedDeviceStore;
39 -import org.onlab.packet.IpPrefix;
40 -
41 -import java.util.ArrayList;
42 -import java.util.HashSet;
43 -import java.util.Iterator;
44 -import java.util.List;
45 -import java.util.Map.Entry;
46 -import java.util.Set;
47 -import java.util.concurrent.BlockingQueue;
48 -import java.util.concurrent.ConcurrentHashMap;
49 -import java.util.concurrent.ConcurrentMap;
50 -import java.util.concurrent.LinkedBlockingQueue;
51 -import java.util.concurrent.TimeUnit;
52 -
53 -import static org.junit.Assert.*;
54 -import static org.onlab.onos.net.Device.Type.SWITCH;
55 -import static org.onlab.onos.net.DeviceId.deviceId;
56 -import static org.onlab.onos.net.device.DeviceEvent.Type.*;
57 -
58 -// FIXME This test is slow starting up Hazelcast on each test cases.
59 -// FIXME DistributedDeviceStore should have it's own test cases.
60 -
61 -/**
62 - * Test codifying the device service & device provider service contracts.
63 - */
64 -public class DistributedDeviceManagerTest {
65 -
66 - private static final ProviderId PID = new ProviderId("of", "foo");
67 - private static final DeviceId DID1 = deviceId("of:foo");
68 - private static final DeviceId DID2 = deviceId("of:bar");
69 - private static final String MFR = "whitebox";
70 - private static final String HW = "1.1.x";
71 - private static final String SW1 = "3.8.1";
72 - private static final String SW2 = "3.9.5";
73 - private static final String SN = "43311-12345";
74 -
75 - private static final PortNumber P1 = PortNumber.portNumber(1);
76 - private static final PortNumber P2 = PortNumber.portNumber(2);
77 - private static final PortNumber P3 = PortNumber.portNumber(3);
78 -
79 - private static final DefaultControllerNode SELF
80 - = new DefaultControllerNode(new NodeId("foobar"),
81 - IpPrefix.valueOf("127.0.0.1"));
82 -
83 -
84 - private DeviceManager mgr;
85 -
86 - protected StoreManager storeManager;
87 - protected DeviceService service;
88 - protected DeviceAdminService admin;
89 - protected DeviceProviderRegistry registry;
90 - protected DeviceProviderService providerService;
91 - protected TestProvider provider;
92 - protected TestListener listener = new TestListener();
93 - private DistributedDeviceStore dstore;
94 - private TestMastershipManager masterManager;
95 - private EventDeliveryService eventService;
96 -
97 - @Before
98 - public void setUp() {
99 - mgr = new DeviceManager();
100 - service = mgr;
101 - admin = mgr;
102 - registry = mgr;
103 - // TODO should find a way to clean Hazelcast instance without shutdown.
104 - Config config = TestStoreManager.getTestConfig();
105 -
106 - masterManager = new TestMastershipManager();
107 -
108 - storeManager = new TestStoreManager(Hazelcast.newHazelcastInstance(config));
109 - storeManager.activate();
110 -
111 - dstore = new TestDistributedDeviceStore(storeManager);
112 - dstore.activate();
113 -
114 - mgr.store = dstore;
115 - eventService = new TestEventDispatcher();
116 - mgr.eventDispatcher = eventService;
117 - mgr.mastershipService = masterManager;
118 - mgr.activate();
119 -
120 - service.addListener(listener);
121 -
122 - provider = new TestProvider();
123 - providerService = registry.register(provider);
124 - assertTrue("provider should be registered",
125 - registry.getProviders().contains(provider.id()));
126 - }
127 -
128 - @After
129 - public void tearDown() {
130 - registry.unregister(provider);
131 - assertFalse("provider should not be registered",
132 - registry.getProviders().contains(provider.id()));
133 - service.removeListener(listener);
134 - mgr.deactivate();
135 -
136 - dstore.deactivate();
137 - storeManager.deactivate();
138 - }
139 -
140 - private void connectDevice(DeviceId deviceId, String swVersion) {
141 - DeviceDescription description =
142 - new DefaultDeviceDescription(deviceId.uri(), SWITCH, MFR,
143 - HW, swVersion, SN);
144 - providerService.deviceConnected(deviceId, description);
145 - assertNotNull("device should be found", service.getDevice(DID1));
146 - }
147 -
148 - @Test
149 - public void deviceConnected() {
150 - assertNull("device should not be found", service.getDevice(DID1));
151 - connectDevice(DID1, SW1);
152 - validateEvents(DEVICE_ADDED);
153 -
154 - assertEquals("only one device expected", 1, Iterables.size(service.getDevices()));
155 - Iterator<Device> it = service.getDevices().iterator();
156 - assertNotNull("one device expected", it.next());
157 - assertFalse("only one device expected", it.hasNext());
158 -
159 - assertEquals("incorrect device count", 1, service.getDeviceCount());
160 - assertTrue("device should be available", service.isAvailable(DID1));
161 - }
162 -
163 - @Test
164 - public void deviceDisconnected() {
165 - connectDevice(DID1, SW1);
166 - connectDevice(DID2, SW1);
167 - validateEvents(DEVICE_ADDED, DEVICE_ADDED);
168 - assertTrue("device should be available", service.isAvailable(DID1));
169 -
170 - // Disconnect
171 - providerService.deviceDisconnected(DID1);
172 - assertNotNull("device should not be found", service.getDevice(DID1));
173 - assertFalse("device should not be available", service.isAvailable(DID1));
174 - validateEvents(DEVICE_AVAILABILITY_CHANGED);
175 -
176 - // Reconnect
177 - connectDevice(DID1, SW1);
178 - validateEvents(DEVICE_AVAILABILITY_CHANGED);
179 -
180 - assertEquals("incorrect device count", 2, service.getDeviceCount());
181 - }
182 -
183 - @Test
184 - public void deviceUpdated() {
185 - connectDevice(DID1, SW1);
186 - validateEvents(DEVICE_ADDED);
187 -
188 - connectDevice(DID1, SW2);
189 - validateEvents(DEVICE_UPDATED);
190 - }
191 -
192 - @Test
193 - public void getRole() {
194 - connectDevice(DID1, SW1);
195 - assertEquals("incorrect role", MastershipRole.MASTER, service.getRole(DID1));
196 - }
197 -
198 - @Test
199 - public void updatePorts() {
200 - connectDevice(DID1, SW1);
201 - List<PortDescription> pds = new ArrayList<>();
202 - pds.add(new DefaultPortDescription(P1, true));
203 - pds.add(new DefaultPortDescription(P2, true));
204 - pds.add(new DefaultPortDescription(P3, true));
205 - providerService.updatePorts(DID1, pds);
206 - validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED, PORT_ADDED);
207 - pds.clear();
208 -
209 - pds.add(new DefaultPortDescription(P1, false));
210 - pds.add(new DefaultPortDescription(P3, true));
211 - providerService.updatePorts(DID1, pds);
212 - validateEvents(PORT_UPDATED, PORT_REMOVED);
213 - }
214 -
215 - @Test
216 - public void updatePortStatus() {
217 - connectDevice(DID1, SW1);
218 - List<PortDescription> pds = new ArrayList<>();
219 - pds.add(new DefaultPortDescription(P1, true));
220 - pds.add(new DefaultPortDescription(P2, true));
221 - providerService.updatePorts(DID1, pds);
222 - validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED);
223 -
224 - providerService.portStatusChanged(DID1, new DefaultPortDescription(P1, false));
225 - validateEvents(PORT_UPDATED);
226 - providerService.portStatusChanged(DID1, new DefaultPortDescription(P1, false));
227 - assertTrue("no events expected", listener.events.isEmpty());
228 - }
229 -
230 - @Test
231 - public void getPorts() {
232 - connectDevice(DID1, SW1);
233 - List<PortDescription> pds = new ArrayList<>();
234 - pds.add(new DefaultPortDescription(P1, true));
235 - pds.add(new DefaultPortDescription(P2, true));
236 - providerService.updatePorts(DID1, pds);
237 - validateEvents(DEVICE_ADDED, PORT_ADDED, PORT_ADDED);
238 - assertEquals("wrong port count", 2, service.getPorts(DID1).size());
239 -
240 - Port port = service.getPort(DID1, P1);
241 - assertEquals("incorrect port", P1, service.getPort(DID1, P1).number());
242 - assertEquals("incorrect state", true, service.getPort(DID1, P1).isEnabled());
243 - }
244 -
245 - @Test
246 - public void removeDevice() {
247 - connectDevice(DID1, SW1);
248 - connectDevice(DID2, SW2);
249 - assertEquals("incorrect device count", 2, service.getDeviceCount());
250 - admin.removeDevice(DID1);
251 - validateEvents(DEVICE_ADDED, DEVICE_ADDED, DEVICE_REMOVED);
252 - assertNull("device should not be found", service.getDevice(DID1));
253 - assertNotNull("device should be found", service.getDevice(DID2));
254 - assertEquals("incorrect device count", 1, service.getDeviceCount());
255 - }
256 -
257 - protected void validateEvents(Enum... types) {
258 - for (Enum type : types) {
259 - try {
260 - Event event = listener.events.poll(1, TimeUnit.SECONDS);
261 - assertNotNull("Timed out waiting for " + event, event);
262 - assertEquals("incorrect event type", type, event.type());
263 - } catch (InterruptedException e) {
264 - fail("Unexpected interrupt");
265 - }
266 - }
267 - assertTrue("Unexpected events left", listener.events.isEmpty());
268 - listener.events.clear();
269 - }
270 -
271 -
272 - private class TestProvider extends AbstractProvider implements DeviceProvider {
273 - private Device deviceReceived;
274 - private MastershipRole roleReceived;
275 -
276 - public TestProvider() {
277 - super(PID);
278 - }
279 -
280 - @Override
281 - public void triggerProbe(Device device) {
282 - }
283 -
284 - @Override
285 - public void roleChanged(Device device, MastershipRole newRole) {
286 - deviceReceived = device;
287 - roleReceived = newRole;
288 - }
289 - }
290 -
291 - private static class TestListener implements DeviceListener {
292 - final BlockingQueue<DeviceEvent> events = new LinkedBlockingQueue<>();
293 -
294 - @Override
295 - public void event(DeviceEvent event) {
296 - events.add(event);
297 - }
298 - }
299 -
300 - private class TestDistributedDeviceStore extends DistributedDeviceStore {
301 -
302 - public TestDistributedDeviceStore(StoreService storeService) {
303 - this.storeService = storeService;
304 - }
305 - }
306 -
307 - private static class TestMastershipManager extends MastershipServiceAdapter {
308 -
309 - private ConcurrentMap<DeviceId, NodeId> masters = new ConcurrentHashMap<>();
310 -
311 - public TestMastershipManager() {
312 - // SELF master of all initially
313 - masters.put(DID1, SELF.id());
314 - masters.put(DID1, SELF.id());
315 - }
316 - @Override
317 - public MastershipRole getLocalRole(DeviceId deviceId) {
318 - return MastershipRole.MASTER;
319 - }
320 -
321 - @Override
322 - public Set<DeviceId> getDevicesOf(NodeId nodeId) {
323 - HashSet<DeviceId> set = Sets.newHashSet();
324 - for (Entry<DeviceId, NodeId> e : masters.entrySet()) {
325 - if (e.getValue().equals(nodeId)) {
326 - set.add(e.getKey());
327 - }
328 - }
329 - return set;
330 - }
331 -
332 - @Override
333 - public MastershipRole requestRoleFor(DeviceId deviceId) {
334 - if (SELF.id().equals(masters.get(deviceId))) {
335 - return MastershipRole.MASTER;
336 - } else {
337 - return MastershipRole.STANDBY;
338 - }
339 - }
340 -
341 - @Override
342 - public void relinquishMastership(DeviceId deviceId) {
343 - masters.remove(deviceId, SELF.id());
344 - }
345 - }
346 -}
...@@ -29,6 +29,7 @@ import org.onlab.onos.store.cluster.messaging.MessageSubject; ...@@ -29,6 +29,7 @@ import org.onlab.onos.store.cluster.messaging.MessageSubject;
29 import org.onlab.onos.store.serializers.ClusterMessageSerializer; 29 import org.onlab.onos.store.serializers.ClusterMessageSerializer;
30 import org.onlab.onos.store.serializers.KryoPoolUtil; 30 import org.onlab.onos.store.serializers.KryoPoolUtil;
31 import org.onlab.onos.store.serializers.KryoSerializer; 31 import org.onlab.onos.store.serializers.KryoSerializer;
32 +import org.onlab.onos.store.serializers.MessageSubjectSerializer;
32 import org.onlab.util.KryoPool; 33 import org.onlab.util.KryoPool;
33 import org.onlab.netty.Endpoint; 34 import org.onlab.netty.Endpoint;
34 import org.onlab.netty.Message; 35 import org.onlab.netty.Message;
...@@ -66,7 +67,7 @@ public class ClusterCommunicationManager ...@@ -66,7 +67,7 @@ public class ClusterCommunicationManager
66 .register(ClusterMessage.class, new ClusterMessageSerializer()) 67 .register(ClusterMessage.class, new ClusterMessageSerializer())
67 .register(ClusterMembershipEvent.class) 68 .register(ClusterMembershipEvent.class)
68 .register(byte[].class) 69 .register(byte[].class)
69 - .register(MessageSubject.class) 70 + .register(MessageSubject.class, new MessageSubjectSerializer())
70 .build() 71 .build()
71 .populate(1); 72 .populate(1);
72 } 73 }
...@@ -123,7 +124,8 @@ public class ClusterCommunicationManager ...@@ -123,7 +124,8 @@ public class ClusterCommunicationManager
123 Endpoint nodeEp = new Endpoint(node.ip().toString(), node.tcpPort()); 124 Endpoint nodeEp = new Endpoint(node.ip().toString(), node.tcpPort());
124 try { 125 try {
125 log.info("sending..."); 126 log.info("sending...");
126 - Response resp = messagingService.sendAndReceive(nodeEp, message.subject().value(), SERIALIZER.encode(message)); 127 + Response resp = messagingService.sendAndReceive(nodeEp,
128 + message.subject().value(), SERIALIZER.encode(message));
127 resp.get(1, TimeUnit.SECONDS); 129 resp.get(1, TimeUnit.SECONDS);
128 log.info("sent..."); 130 log.info("sent...");
129 return true; 131 return true;
......
...@@ -3,7 +3,6 @@ package org.onlab.onos.store.serializers; ...@@ -3,7 +3,6 @@ package org.onlab.onos.store.serializers;
3 import org.onlab.onos.cluster.NodeId; 3 import org.onlab.onos.cluster.NodeId;
4 import org.onlab.onos.store.cluster.messaging.ClusterMessage; 4 import org.onlab.onos.store.cluster.messaging.ClusterMessage;
5 import org.onlab.onos.store.cluster.messaging.MessageSubject; 5 import org.onlab.onos.store.cluster.messaging.MessageSubject;
6 -
7 import com.esotericsoftware.kryo.Kryo; 6 import com.esotericsoftware.kryo.Kryo;
8 import com.esotericsoftware.kryo.Serializer; 7 import com.esotericsoftware.kryo.Serializer;
9 import com.esotericsoftware.kryo.io.Input; 8 import com.esotericsoftware.kryo.io.Input;
...@@ -11,6 +10,9 @@ import com.esotericsoftware.kryo.io.Output; ...@@ -11,6 +10,9 @@ import com.esotericsoftware.kryo.io.Output;
11 10
12 public final class ClusterMessageSerializer extends Serializer<ClusterMessage> { 11 public final class ClusterMessageSerializer extends Serializer<ClusterMessage> {
13 12
13 + /**
14 + * Creates a serializer for {@link ClusterMessage}.
15 + */
14 public ClusterMessageSerializer() { 16 public ClusterMessageSerializer() {
15 // does not accept null 17 // does not accept null
16 super(false); 18 super(false);
......
...@@ -14,7 +14,7 @@ import com.esotericsoftware.kryo.io.Output; ...@@ -14,7 +14,7 @@ import com.esotericsoftware.kryo.io.Output;
14 public class MastershipBasedTimestampSerializer extends Serializer<MastershipBasedTimestamp> { 14 public class MastershipBasedTimestampSerializer extends Serializer<MastershipBasedTimestamp> {
15 15
16 /** 16 /**
17 - * Default constructor. 17 + * Creates a serializer for {@link MastershipBasedTimestamp}.
18 */ 18 */
19 public MastershipBasedTimestampSerializer() { 19 public MastershipBasedTimestampSerializer() {
20 // non-null, immutable 20 // non-null, immutable
......
1 +package org.onlab.onos.store.serializers;
2 +
3 +import org.onlab.onos.store.cluster.messaging.MessageSubject;
4 +
5 +import com.esotericsoftware.kryo.Kryo;
6 +import com.esotericsoftware.kryo.Serializer;
7 +import com.esotericsoftware.kryo.io.Input;
8 +import com.esotericsoftware.kryo.io.Output;
9 +
10 +public final class MessageSubjectSerializer extends Serializer<MessageSubject> {
11 +
12 + /**
13 + * Creates a serializer for {@link MessageSubject}.
14 + */
15 + public MessageSubjectSerializer() {
16 + // non-null, immutable
17 + super(false, true);
18 + }
19 +
20 +
21 + @Override
22 + public void write(Kryo kryo, Output output, MessageSubject object) {
23 + output.writeString(object.value());
24 + }
25 +
26 + @Override
27 + public MessageSubject read(Kryo kryo, Input input,
28 + Class<MessageSubject> type) {
29 + return new MessageSubject(input.readString());
30 + }
31 +}
...@@ -153,6 +153,7 @@ ...@@ -153,6 +153,7 @@
153 description="ONOS sample playground application"> 153 description="ONOS sample playground application">
154 <feature>onos-api</feature> 154 <feature>onos-api</feature>
155 <bundle>mvn:org.onlab.onos/onos-app-foo/1.0.0-SNAPSHOT</bundle> 155 <bundle>mvn:org.onlab.onos/onos-app-foo/1.0.0-SNAPSHOT</bundle>
156 + <bundle>mvn:org.onlab.onos/onlab-netty/1.0.0-SNAPSHOT</bundle>
156 </feature> 157 </feature>
157 158
158 <feature name="onos-app-config" version="1.0.0" 159 <feature name="onos-app-config" version="1.0.0"
......
1 package org.onlab.metrics; 1 package org.onlab.metrics;
2 2
3 -import java.io.File;
4 -import java.util.Locale;
5 import java.util.Map; 3 import java.util.Map;
6 import java.util.concurrent.ConcurrentHashMap; 4 import java.util.concurrent.ConcurrentHashMap;
7 import java.util.concurrent.ConcurrentMap; 5 import java.util.concurrent.ConcurrentMap;
...@@ -11,8 +9,8 @@ import org.apache.felix.scr.annotations.Activate; ...@@ -11,8 +9,8 @@ import org.apache.felix.scr.annotations.Activate;
11 import org.apache.felix.scr.annotations.Component; 9 import org.apache.felix.scr.annotations.Component;
12 import org.apache.felix.scr.annotations.Deactivate; 10 import org.apache.felix.scr.annotations.Deactivate;
13 11
12 +import com.codahale.metrics.ConsoleReporter;
14 import com.codahale.metrics.Counter; 13 import com.codahale.metrics.Counter;
15 -import com.codahale.metrics.CsvReporter;
16 import com.codahale.metrics.Gauge; 14 import com.codahale.metrics.Gauge;
17 import com.codahale.metrics.Histogram; 15 import com.codahale.metrics.Histogram;
18 import com.codahale.metrics.Meter; 16 import com.codahale.metrics.Meter;
...@@ -69,15 +67,14 @@ public final class MetricsManager implements MetricsService { ...@@ -69,15 +67,14 @@ public final class MetricsManager implements MetricsService {
69 /** 67 /**
70 * Default Reporter for this metrics manager. 68 * Default Reporter for this metrics manager.
71 */ 69 */
72 - private final CsvReporter reporter; 70 + private final ConsoleReporter reporter;
73 71
74 public MetricsManager() { 72 public MetricsManager() {
75 this.metricsRegistry = new MetricRegistry(); 73 this.metricsRegistry = new MetricRegistry();
76 - this.reporter = CsvReporter.forRegistry(metricsRegistry) 74 + this.reporter = ConsoleReporter.forRegistry(metricsRegistry)
77 - .formatFor(Locale.US)
78 .convertRatesTo(TimeUnit.SECONDS) 75 .convertRatesTo(TimeUnit.SECONDS)
79 .convertDurationsTo(TimeUnit.MICROSECONDS) 76 .convertDurationsTo(TimeUnit.MICROSECONDS)
80 - .build(new File("/var/onos/log/metrics/")); 77 + .build();
81 } 78 }
82 79
83 @Activate 80 @Activate
......
...@@ -36,11 +36,6 @@ public class MessageEncoder extends MessageToByteEncoder<InternalMessage> { ...@@ -36,11 +36,6 @@ public class MessageEncoder extends MessageToByteEncoder<InternalMessage> {
36 // write preamble 36 // write preamble
37 out.writeBytes(PREAMBLE); 37 out.writeBytes(PREAMBLE);
38 38
39 - try {
40 - SERIALIZER.encode(message);
41 - } catch (Exception e) {
42 - e.printStackTrace();
43 - }
44 byte[] payload = SERIALIZER.encode(message); 39 byte[] payload = SERIALIZER.encode(message);
45 40
46 // write payload length 41 // write payload length
......