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; |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 22 | import org.onosproject.openflow.MockOfPortStatus; |
Jian Li | 152b885 | 2015-12-07 14:47:25 -0800 | [diff] [blame] | 23 | import org.onosproject.openflow.OfMessageAdapter; |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 24 | import org.onosproject.openflow.OpenFlowSwitchListenerAdapter; |
| 25 | import org.onosproject.openflow.OpenflowSwitchDriverAdapter; |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 26 | import org.onosproject.openflow.controller.Dpid; |
| 27 | import org.onosproject.openflow.controller.OpenFlowPacketContext; |
| 28 | import org.onosproject.openflow.controller.OpenFlowSwitch; |
| 29 | import org.onosproject.openflow.controller.PacketListener; |
| 30 | import org.projectfloodlight.openflow.protocol.OFMessage; |
| 31 | import org.projectfloodlight.openflow.protocol.OFType; |
| 32 | |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 33 | import java.net.URI; |
| 34 | import java.net.URISyntaxException; |
| 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
| 37 | |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 38 | import static junit.framework.TestCase.fail; |
| 39 | import static org.hamcrest.MatcherAssert.assertThat; |
Jian Li | 2266bff | 2016-04-21 11:01:25 -0700 | [diff] [blame^] | 40 | import static org.hamcrest.Matchers.equalTo; |
| 41 | import static org.hamcrest.Matchers.hasSize; |
| 42 | import static org.hamcrest.Matchers.is; |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Tests for packet processing in the open flow controller impl class. |
| 46 | */ |
| 47 | public class OpenFlowControllerImplPacketsTest { |
| 48 | OpenFlowControllerImpl controller; |
| 49 | OpenFlowControllerImpl.OpenFlowSwitchAgent agent; |
| 50 | Dpid dpid1; |
| 51 | OpenFlowSwitch switch1; |
| 52 | OpenFlowSwitchListenerAdapter switchListener; |
| 53 | TestPacketListener packetListener; |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 54 | TestExecutorService statsExecutorService; |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Mock packet listener that accumulates packets. |
| 58 | */ |
| 59 | class TestPacketListener implements PacketListener { |
| 60 | List<OpenFlowPacketContext> contexts = new ArrayList<>(); |
| 61 | |
| 62 | @Override |
| 63 | public void handlePacket(OpenFlowPacketContext pktCtx) { |
| 64 | contexts.add(pktCtx); |
| 65 | } |
| 66 | |
| 67 | List<OpenFlowPacketContext> contexts() { |
| 68 | return contexts; |
| 69 | } |
| 70 | } |
| 71 | |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 72 | /** |
| 73 | * Mock executor service that tracks submits. |
| 74 | */ |
| 75 | static class TestExecutorService extends ExecutorServiceAdapter { |
| 76 | private List<OFMessage> submittedMessages = new ArrayList<>(); |
| 77 | |
| 78 | List<OFMessage> submittedMessages() { |
| 79 | return submittedMessages; |
| 80 | } |
| 81 | |
| 82 | @Override |
Andrea Campanella | dda9356 | 2016-03-02 11:08:12 -0800 | [diff] [blame] | 83 | public void execute(Runnable task) { |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 84 | OpenFlowControllerImpl.OFMessageHandler handler = |
| 85 | (OpenFlowControllerImpl.OFMessageHandler) task; |
| 86 | submittedMessages.add(handler.msg); |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Sets up switches to use as data, mocks and launches a controller instance. |
| 92 | */ |
| 93 | @Before |
| 94 | public void setUp() { |
| 95 | try { |
| 96 | switch1 = new OpenflowSwitchDriverAdapter(); |
| 97 | dpid1 = Dpid.dpid(new URI("of:0000000000000111")); |
| 98 | } catch (URISyntaxException ex) { |
| 99 | // Does not happen |
| 100 | fail(); |
| 101 | } |
| 102 | |
| 103 | controller = new OpenFlowControllerImpl(); |
| 104 | agent = controller.agent; |
| 105 | switchListener = new OpenFlowSwitchListenerAdapter(); |
| 106 | controller.addListener(switchListener); |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 107 | controller.monitorAllEvents(true); |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 108 | |
| 109 | packetListener = new TestPacketListener(); |
| 110 | controller.addPacketListener(100, packetListener); |
| 111 | |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 112 | statsExecutorService = new TestExecutorService(); |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 113 | |
| 114 | controller.executorMsgs = statsExecutorService; |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Tests a port status operation. |
| 119 | */ |
| 120 | @Test |
| 121 | public void testPortStatus() { |
| 122 | OFMessage portStatusPacket = new MockOfPortStatus(); |
| 123 | controller.processPacket(dpid1, portStatusPacket); |
| 124 | assertThat(switchListener.portChangedDpids().size(), is(1)); |
| 125 | assertThat(switchListener.portChangedDpids().containsKey(dpid1), |
| 126 | is(true)); |
| 127 | assertThat(switchListener.portChangedDpids().get(dpid1), |
| 128 | equalTo(portStatusPacket)); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Tests a features reply operation. |
| 133 | */ |
| 134 | @Test |
| 135 | public void testFeaturesReply() { |
| 136 | OFMessage ofFeaturesReplyPacket = new MockOfFeaturesReply(); |
| 137 | controller.processPacket(dpid1, ofFeaturesReplyPacket); |
| 138 | assertThat(switchListener.changedDpids(), hasSize(1)); |
| 139 | assertThat(switchListener.changedDpids().get(0), |
| 140 | equalTo(dpid1)); |
| 141 | } |
| 142 | |
| 143 | /** |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 144 | * Tests an error operation. |
| 145 | */ |
| 146 | @Test |
| 147 | public void testError() { |
| 148 | agent.addConnectedSwitch(dpid1, switch1); |
| 149 | OfMessageAdapter errorPacket = new OfMessageAdapter(OFType.ERROR); |
| 150 | controller.processPacket(dpid1, errorPacket); |
Jian Li | 28247b5 | 2016-01-07 17:24:15 -0800 | [diff] [blame] | 151 | assertThat(statsExecutorService.submittedMessages(), hasSize(1)); |
| 152 | assertThat(statsExecutorService.submittedMessages().get(0), is(errorPacket)); |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 153 | } |
Ray Milkey | 7ec0d1b | 2015-11-13 08:51:35 -0800 | [diff] [blame] | 154 | } |