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