Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.openflow.controller.impl; |
| 17 | |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
| 20 | import org.onosproject.openflow.ExecutorServiceAdapter; |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 21 | import org.onosproject.openflow.MockOfFeaturesReply; |
| 22 | import org.onosproject.openflow.MockOfPacketIn; |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 23 | import org.onosproject.openflow.MockOfPortStatus; |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 24 | import org.onosproject.openflow.OfMessageAdapter; |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 25 | import org.onosproject.openflow.OpenFlowSwitchListenerAdapter; |
| 26 | import org.onosproject.openflow.OpenflowSwitchDriverAdapter; |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 27 | import org.onosproject.openflow.controller.Dpid; |
| 28 | import org.onosproject.openflow.controller.OpenFlowPacketContext; |
| 29 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 30 | import org.onosproject.openflow.controller.PacketListener; |
| 31 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 32 | import org.projectfloodlight.openflow.protocol.OFType; |
| 33 | |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 34 | import java.net.URI; |
| 35 | import java.net.URISyntaxException; |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.List; |
| 38 | |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 39 | import static junit.framework.TestCase.fail; |
| 40 | import static org.hamcrest.MatcherAssert.assertThat; |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 41 | import static org.hamcrest.Matchers.*; |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Tests for packet processing in the open flow controller impl class. |
| 45 | */ |
| 46 | public class OpenFlowControllerImplPacketsTest { |
| 47 | OpenFlowControllerImpl controller; |
| 48 | OpenFlowControllerImpl.OpenFlowSwitchAgent agent; |
| 49 | Dpid dpid1; |
| 50 | OpenFlowSwitch switch1; |
| 51 | OpenFlowSwitchListenerAdapter switchListener; |
| 52 | TestPacketListener packetListener; |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 53 | TestExecutorService statsExecutorService; |
| 54 | TestExecutorService pktInExecutorService; |
| 55 | TestExecutorService flowRmvExecutorService; |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Mock packet listener that accumulates packets. |
| 59 | */ |
| 60 | class TestPacketListener implements PacketListener { |
| 61 | List<OpenFlowPacketContext> contexts = new ArrayList<>(); |
| 62 | |
| 63 | @Override |
| 64 | public void handlePacket(OpenFlowPacketContext pktCtx) { |
| 65 | contexts.add(pktCtx); |
| 66 | } |
| 67 | |
| 68 | List<OpenFlowPacketContext> contexts() { |
| 69 | return contexts; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * Mock executor service that tracks submits. |
| 76 | */ |
| 77 | static class TestExecutorService extends ExecutorServiceAdapter { |
| 78 | private List<OFMessage> submittedMessages = new ArrayList<>(); |
| 79 | |
| 80 | List<OFMessage> submittedMessages() { |
| 81 | return submittedMessages; |
| 82 | } |
| 83 | |
| 84 | @Override |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 85 | public void execute(Runnable task) { |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 86 | OpenFlowControllerImpl.OFMessageHandler handler = |
| 87 | (OpenFlowControllerImpl.OFMessageHandler) task; |
| 88 | submittedMessages.add(handler.msg); |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Sets up switches to use as data, mocks and launches a controller instance. |
| 94 | */ |
| 95 | @Before |
| 96 | public void setUp() { |
| 97 | try { |
| 98 | switch1 = new OpenflowSwitchDriverAdapter(); |
| 99 | dpid1 = Dpid.dpid(new URI("of:0000000000000111")); |
| 100 | } catch (URISyntaxException ex) { |
| 101 | // Does not happen |
| 102 | fail(); |
| 103 | } |
| 104 | |
| 105 | controller = new OpenFlowControllerImpl(); |
| 106 | agent = controller.agent; |
| 107 | switchListener = new OpenFlowSwitchListenerAdapter(); |
| 108 | controller.addListener(switchListener); |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 109 | controller.monitorAllEvents(true); |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 110 | |
| 111 | packetListener = new TestPacketListener(); |
| 112 | controller.addPacketListener(100, packetListener); |
| 113 | |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 114 | statsExecutorService = new TestExecutorService(); |
| 115 | pktInExecutorService = new TestExecutorService(); |
| 116 | flowRmvExecutorService = new TestExecutorService(); |
| 117 | |
| 118 | controller.executorMsgs = statsExecutorService; |
| 119 | controller.executorPacketIn = pktInExecutorService; |
| 120 | controller.executorFlowRemoved = flowRmvExecutorService; |
| 121 | |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Tests a port status operation. |
| 126 | */ |
| 127 | @Test |
| 128 | public void testPortStatus() { |
| 129 | OFMessage portStatusPacket = new MockOfPortStatus(); |
| 130 | controller.processPacket(dpid1, portStatusPacket); |
| 131 | assertThat(switchListener.portChangedDpids().size(), is(1)); |
| 132 | assertThat(switchListener.portChangedDpids().containsKey(dpid1), |
| 133 | is(true)); |
| 134 | assertThat(switchListener.portChangedDpids().get(dpid1), |
| 135 | equalTo(portStatusPacket)); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Tests a features reply operation. |
| 140 | */ |
| 141 | @Test |
| 142 | public void testFeaturesReply() { |
| 143 | OFMessage ofFeaturesReplyPacket = new MockOfFeaturesReply(); |
| 144 | controller.processPacket(dpid1, ofFeaturesReplyPacket); |
| 145 | assertThat(switchListener.changedDpids(), hasSize(1)); |
| 146 | assertThat(switchListener.changedDpids().get(0), |
| 147 | equalTo(dpid1)); |
| 148 | } |
| 149 | |
| 150 | /** |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 151 | * Tests a packet in listen operation. |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 152 | */ |
| 153 | @Test |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 154 | public void testPacketInListen() { |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 155 | agent.addConnectedSwitch(dpid1, switch1); |
| 156 | OFMessage packetInPacket = new MockOfPacketIn(); |
| 157 | controller.processPacket(dpid1, packetInPacket); |
| 158 | assertThat(packetListener.contexts(), hasSize(1)); |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 159 | assertThat(pktInExecutorService.submittedMessages(), hasSize(1)); |
| 160 | assertThat(pktInExecutorService.submittedMessages().get(0), is(packetInPacket)); |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Tests an error operation. |
| 165 | */ |
| 166 | @Test |
| 167 | public void testError() { |
| 168 | agent.addConnectedSwitch(dpid1, switch1); |
| 169 | OfMessageAdapter errorPacket = new OfMessageAdapter(OFType.ERROR); |
| 170 | controller.processPacket(dpid1, errorPacket); |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 171 | assertThat(statsExecutorService.submittedMessages(), hasSize(1)); |
| 172 | assertThat(statsExecutorService.submittedMessages().get(0), is(errorPacket)); |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 173 | } |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 174 | |
| 175 | /** |
| 176 | * Tests a packet in operation. |
| 177 | */ |
| 178 | @Test |
| 179 | public void testFlowRemoved() { |
| 180 | agent.addConnectedSwitch(dpid1, switch1); |
| 181 | OFMessage flowRemovedPacket = new MockOfFlowRemoved(); |
| 182 | controller.processPacket(dpid1, flowRemovedPacket); |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 183 | assertThat(flowRmvExecutorService.submittedMessages(), hasSize(1)); |
| 184 | assertThat(flowRmvExecutorService.submittedMessages().get(0), is(flowRemovedPacket)); |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 185 | } |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 186 | } |