2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.openflow.md.core;
11 import java.math.BigInteger;
12 import java.util.Stack;
13 import java.util.concurrent.ScheduledThreadPoolExecutor;
14 import java.util.concurrent.TimeUnit;
16 import org.junit.After;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.openflowplugin.openflow.md.core.plan.ConnectionAdapterStackImpl;
21 import org.opendaylight.openflowplugin.openflow.md.core.plan.EventFactory;
22 import org.opendaylight.openflowplugin.openflow.md.core.plan.SwitchTestEvent;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ErrorType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessageBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessageBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
36 public class ConnectionConductorImplTest {
38 private static final Logger LOG = LoggerFactory
39 .getLogger(ConnectionConductorImplTest.class);
41 protected ConnectionAdapterStackImpl adapter;
42 private ConnectionConductorImpl connectionConductor;
43 private Stack<SwitchTestEvent> eventPlan;
45 private Thread libSimulation;
46 private ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(
50 * @throws java.lang.Exception
53 public void setUp() throws Exception {
54 adapter = new ConnectionAdapterStackImpl();
55 connectionConductor = new ConnectionConductorImpl(adapter);
56 connectionConductor.init();
57 eventPlan = new Stack<>();
58 adapter.setEventPlan(eventPlan);
59 adapter.setProceedTimeout(5000L);
60 adapter.checkListeners();
64 * @throws java.lang.Exception
67 public void tearDown() throws Exception {
68 if (libSimulation != null) {
71 for (Exception problem : adapter.getOccuredExceptions()) {
72 LOG.error("during simulation on adapter side: "
73 + problem.getMessage());
75 Assert.assertEquals(0, adapter.getOccuredExceptions().size());
77 if (LOG.isDebugEnabled()) {
78 if (eventPlan.size() > 0) {
79 LOG.debug("eventPlan size: " + eventPlan.size());
80 for (SwitchTestEvent event : eventPlan) {
81 LOG.debug(" # EVENT:: " + event.toString());
85 Assert.assertTrue("plan is not finished", eventPlan.isEmpty());
91 * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#onEchoRequestMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage)}
96 public void testOnEchoRequestMessage() throws Exception {
97 eventPlan.add(0, EventFactory.createDefaultNotificationEvent(42L,
98 EventFactory.DEFAULT_VERSION, new EchoRequestMessageBuilder()));
100 EventFactory.createDefaultWaitForRpcEvent(42, "echoReply"));
105 * Test of handshake, covering version negotiation and features .
109 public void testHandshake1() throws Exception {
110 eventPlan.add(0, EventFactory.createDefaultNotificationEvent(42L,
111 EventFactory.DEFAULT_VERSION, new HelloMessageBuilder()));
113 EventFactory.createDefaultWaitForRpcEvent(42, "helloReply"));
115 EventFactory.createDefaultWaitForRpcEvent(42, "getFeatures"));
116 GetFeaturesOutputBuilder getFeaturesOutputBuilder = new GetFeaturesOutputBuilder();
117 getFeaturesOutputBuilder.setDatapathId(new BigInteger("102030405060"));
118 getFeaturesOutputBuilder.setAuxiliaryId((short) 0);
119 getFeaturesOutputBuilder.setBuffers(4L);
120 getFeaturesOutputBuilder.setReserved(0L);
121 getFeaturesOutputBuilder.setTables((short) 2);
122 getFeaturesOutputBuilder.setCapabilities(84L);
124 eventPlan.add(0, EventFactory.createDefaultRpcResponseEvent(42,
125 EventFactory.DEFAULT_VERSION, getFeaturesOutputBuilder));
128 Assert.assertEquals(ConnectionConductor.CONDUCTOR_STATE.WORKING,
129 connectionConductor.getConductorState());
130 Assert.assertEquals((short) 0x04, connectionConductor.getVersion()
135 * Test of handshake, covering version negotiation and features .
139 public void testHandshake2() throws Exception {
140 eventPlan.add(0, EventFactory.createDefaultNotificationEvent(42L,
141 (short) 0x05, new HelloMessageBuilder()));
143 EventFactory.createDefaultWaitForRpcEvent(42, "helloReply"));
144 eventPlan.add(0, EventFactory.createDefaultNotificationEvent(42L,
145 (short) 0x03, new HelloMessageBuilder()));
147 EventFactory.createDefaultWaitForRpcEvent(42, "helloReply"));
148 eventPlan.add(0, EventFactory.createDefaultNotificationEvent(42L,
149 (short) 0x01, new HelloMessageBuilder()));
151 EventFactory.createDefaultWaitForRpcEvent(42, "helloReply"));
153 EventFactory.createDefaultWaitForRpcEvent(42, "getFeatures"));
154 GetFeaturesOutputBuilder getFeaturesOutputBuilder = new GetFeaturesOutputBuilder();
155 getFeaturesOutputBuilder.setDatapathId(new BigInteger("102030405060"));
156 getFeaturesOutputBuilder.setAuxiliaryId((short) 0);
157 getFeaturesOutputBuilder.setBuffers(4L);
158 getFeaturesOutputBuilder.setReserved(0L);
159 getFeaturesOutputBuilder.setTables((short) 2);
160 getFeaturesOutputBuilder.setCapabilities(84L);
162 eventPlan.add(0, EventFactory.createDefaultRpcResponseEvent(42,
163 EventFactory.DEFAULT_VERSION, getFeaturesOutputBuilder));
166 Assert.assertEquals(ConnectionConductor.CONDUCTOR_STATE.WORKING,
167 connectionConductor.getConductorState());
168 Assert.assertEquals((short) 0x01, connectionConductor.getVersion()
173 * Test of handshake, covering version negotiation and features .
177 public void testHandshake3() throws Exception {
178 eventPlan.add(0, EventFactory.createDefaultNotificationEvent(42L,
179 (short) 0x00, new HelloMessageBuilder()));
182 Assert.assertEquals(ConnectionConductor.CONDUCTOR_STATE.HANDSHAKING,
183 connectionConductor.getConductorState());
184 Assert.assertNull(connectionConductor.getVersion());
189 * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#onExperimenterMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage)}
191 * @throws InterruptedException
194 public void testOnExperimenterMessage1() throws InterruptedException {
196 EventFactory.createDefaultWaitForRpcEvent(42, "experimenter"));
197 ExperimenterMessageBuilder builder1 = new ExperimenterMessageBuilder();
198 builder1.setExperimenter(84L).setExpType(4L);
199 eventPlan.add(0, EventFactory.createDefaultNotificationEvent(42L,
200 EventFactory.DEFAULT_VERSION, builder1));
203 Runnable sendExperimenterCmd = new Runnable() {
207 ExperimenterInputBuilder builder2 = new ExperimenterInputBuilder();
208 builder2.setExperimenter(84L).setExpType(4L);
209 EventFactory.setupHeader(42L, builder2);
210 adapter.experimenter(builder2.build());
213 pool.schedule(sendExperimenterCmd,
214 ConnectionAdapterStackImpl.JOB_DELAY, TimeUnit.MILLISECONDS);
219 * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#onExperimenterMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage)}
221 * @throws InterruptedException
224 public void testOnExperimenterMessage2() throws InterruptedException {
226 EventFactory.createDefaultWaitForRpcEvent(42, "experimenter"));
227 ErrorMessageBuilder builder1 = new ErrorMessageBuilder();
228 builder1.setType(ErrorType.BADREQUEST).setCode(3)
229 .setData(new byte[] { 1, 2, 3 });
231 eventPlan.add(0, EventFactory.createDefaultNotificationEvent(42L,
232 EventFactory.DEFAULT_VERSION, builder1));
236 Runnable sendExperimenterCmd = new Runnable() {
240 ExperimenterInputBuilder builder2 = new ExperimenterInputBuilder();
241 builder2.setExperimenter(84L).setExpType(4L);
242 EventFactory.setupHeader(42L, builder2);
243 adapter.experimenter(builder2.build());
246 pool.schedule(sendExperimenterCmd,
247 ConnectionAdapterStackImpl.JOB_DELAY, TimeUnit.MILLISECONDS);
252 * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#onFlowRemovedMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage)}
256 public void testOnFlowRemovedMessage() {
257 // fail("Not yet implemented");
263 * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#onMultipartReplyMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage)}
267 public void testOnMultipartReplyMessage() {
268 // fail("Not yet implemented");
274 * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#onMultipartRequestMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage)}
278 public void testOnMultipartRequestMessage() {
279 // fail("Not yet implemented");
285 * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#onPacketInMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage)}
289 public void testOnPacketInMessage() {
290 // fail("Not yet implemented");
296 * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#onPortStatusMessage(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage)}
300 public void testOnPortStatusMessage() {
301 // fail("Not yet implemented");
307 * {@link org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl#proposeVersion(short)}
311 public void testProposeVersion() {
312 short[] remoteVer = new short[] { 0x05, 0x04, 0x03, 0x02, 0x01, 0x8f,
314 short[] expectedProposal = new short[] { 0x04, 0x04, 0x01, 0x01, 0x01,
317 for (int i = 0; i < remoteVer.length; i++) {
318 short actualProposal = connectionConductor
319 .proposeVersion(remoteVer[i]);
321 String.format("proposing for version: %04x", remoteVer[i]),
322 expectedProposal[i], actualProposal);
326 connectionConductor.proposeVersion((short) 0);
327 Assert.fail("there should be no proposition for this version");
328 } catch (Exception e) {
334 * @throws InterruptedException
336 private void executeLater() throws InterruptedException {
341 * @throws InterruptedException
343 private void executeNow() throws InterruptedException {
348 * @throws InterruptedException
350 private void execute(boolean join) throws InterruptedException {
351 libSimulation = new Thread(adapter, "junit-adapter");
352 libSimulation.start();
354 libSimulation.join();