From e63291850fd0795c5700e25e67e5dee89ba54c5f Mon Sep 17 00:00:00 2001 From: Ashlee Young Date: Tue, 1 Dec 2015 05:49:27 -0800 Subject: onos commit hash c2999f30c69e50df905a9d175ef80b3f23a98514 Change-Id: I2bb8562c4942b6d6a6d60b663db2e17540477b81 Signed-off-by: Ashlee Young --- .../impl/OpenFlowControllerImplPacketsTest.java | 167 +++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 framework/src/onos/protocols/openflow/ctl/src/test/java/org/onosproject/openflow/controller/impl/OpenFlowControllerImplPacketsTest.java (limited to 'framework/src/onos/protocols/openflow/ctl/src/test/java/org/onosproject/openflow/controller/impl/OpenFlowControllerImplPacketsTest.java') diff --git a/framework/src/onos/protocols/openflow/ctl/src/test/java/org/onosproject/openflow/controller/impl/OpenFlowControllerImplPacketsTest.java b/framework/src/onos/protocols/openflow/ctl/src/test/java/org/onosproject/openflow/controller/impl/OpenFlowControllerImplPacketsTest.java new file mode 100644 index 00000000..13086ca7 --- /dev/null +++ b/framework/src/onos/protocols/openflow/ctl/src/test/java/org/onosproject/openflow/controller/impl/OpenFlowControllerImplPacketsTest.java @@ -0,0 +1,167 @@ +/* + * Copyright 2015 Open Networking Laboratory + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onosproject.openflow.controller.impl; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Future; + +import org.junit.Before; +import org.junit.Test; +import org.onosproject.openflow.ExecutorServiceAdapter; +import org.onosproject.openflow.MockOfFeaturesReply; +import org.onosproject.openflow.MockOfPacketIn; +import org.onosproject.openflow.MockOfPortStatus; +import org.onosproject.openflow.OfMessageAdapter; +import org.onosproject.openflow.OpenFlowSwitchListenerAdapter; +import org.onosproject.openflow.OpenflowSwitchDriverAdapter; +import org.onosproject.openflow.controller.Dpid; +import org.onosproject.openflow.controller.OpenFlowPacketContext; +import org.onosproject.openflow.controller.OpenFlowSwitch; +import org.onosproject.openflow.controller.PacketListener; +import org.projectfloodlight.openflow.protocol.OFMessage; +import org.projectfloodlight.openflow.protocol.OFType; + +import static junit.framework.TestCase.fail; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; + +/** + * Tests for packet processing in the open flow controller impl class. + */ +public class OpenFlowControllerImplPacketsTest { + OpenFlowControllerImpl controller; + OpenFlowControllerImpl.OpenFlowSwitchAgent agent; + Dpid dpid1; + OpenFlowSwitch switch1; + OpenFlowSwitchListenerAdapter switchListener; + TestPacketListener packetListener; + TestExecutorService executorService; + + /** + * Mock packet listener that accumulates packets. + */ + class TestPacketListener implements PacketListener { + List contexts = new ArrayList<>(); + + @Override + public void handlePacket(OpenFlowPacketContext pktCtx) { + contexts.add(pktCtx); + } + + List contexts() { + return contexts; + } + } + + + /** + * Mock executor service that tracks submits. + */ + static class TestExecutorService extends ExecutorServiceAdapter { + private List submittedMessages = new ArrayList<>(); + + List submittedMessages() { + return submittedMessages; + } + + @Override + public Future submit(Runnable task) { + OpenFlowControllerImpl.OFMessageHandler handler = + (OpenFlowControllerImpl.OFMessageHandler) task; + submittedMessages.add(handler.msg); + return null; + } + } + + /** + * Sets up switches to use as data, mocks and launches a controller instance. + */ + @Before + public void setUp() { + try { + switch1 = new OpenflowSwitchDriverAdapter(); + dpid1 = Dpid.dpid(new URI("of:0000000000000111")); + } catch (URISyntaxException ex) { + // Does not happen + fail(); + } + + controller = new OpenFlowControllerImpl(); + agent = controller.agent; + switchListener = new OpenFlowSwitchListenerAdapter(); + controller.addListener(switchListener); + + packetListener = new TestPacketListener(); + controller.addPacketListener(100, packetListener); + + executorService = new TestExecutorService(); + controller.executorMsgs = executorService; + } + + /** + * Tests a port status operation. + */ + @Test + public void testPortStatus() { + OFMessage portStatusPacket = new MockOfPortStatus(); + controller.processPacket(dpid1, portStatusPacket); + assertThat(switchListener.portChangedDpids().size(), is(1)); + assertThat(switchListener.portChangedDpids().containsKey(dpid1), + is(true)); + assertThat(switchListener.portChangedDpids().get(dpid1), + equalTo(portStatusPacket)); + } + + /** + * Tests a features reply operation. + */ + @Test + public void testFeaturesReply() { + OFMessage ofFeaturesReplyPacket = new MockOfFeaturesReply(); + controller.processPacket(dpid1, ofFeaturesReplyPacket); + assertThat(switchListener.changedDpids(), hasSize(1)); + assertThat(switchListener.changedDpids().get(0), + equalTo(dpid1)); + } + + /** + * Tests a packet in operation. + */ + @Test + public void testPacketIn() { + agent.addConnectedSwitch(dpid1, switch1); + OFMessage packetInPacket = new MockOfPacketIn(); + controller.processPacket(dpid1, packetInPacket); + assertThat(packetListener.contexts(), hasSize(1)); + } + + /** + * Tests an error operation. + */ + @Test + public void testError() { + agent.addConnectedSwitch(dpid1, switch1); + OfMessageAdapter errorPacket = new OfMessageAdapter(OFType.ERROR); + controller.processPacket(dpid1, errorPacket); + assertThat(executorService.submittedMessages(), hasSize(1)); + assertThat(executorService.submittedMessages().get(0), is(errorPacket)); + } +} -- cgit 1.2.3-korg