blob: d87913a437f3cec74b86ce0316ade48ecddd7193 [file] [log] [blame]
Ray Milkey7ec0d1b2015-11-13 08:51:35 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkey7ec0d1b2015-11-13 08:51:35 -08003 *
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
Ray Milkey7ec0d1b2015-11-13 08:51:35 -080018import org.junit.Before;
19import org.junit.Test;
20import org.onosproject.openflow.ExecutorServiceAdapter;
Jian Li152b8852015-12-07 14:47:25 -080021import org.onosproject.openflow.MockOfFeaturesReply;
Andrea Campanelladda93562016-03-02 11:08:12 -080022import org.onosproject.openflow.MockOfPortStatus;
Jian Li152b8852015-12-07 14:47:25 -080023import org.onosproject.openflow.OfMessageAdapter;
Andrea Campanelladda93562016-03-02 11:08:12 -080024import org.onosproject.openflow.OpenFlowSwitchListenerAdapter;
25import org.onosproject.openflow.OpenflowSwitchDriverAdapter;
Ray Milkey7ec0d1b2015-11-13 08:51:35 -080026import org.onosproject.openflow.controller.Dpid;
27import org.onosproject.openflow.controller.OpenFlowPacketContext;
28import org.onosproject.openflow.controller.OpenFlowSwitch;
29import org.onosproject.openflow.controller.PacketListener;
30import org.projectfloodlight.openflow.protocol.OFMessage;
31import org.projectfloodlight.openflow.protocol.OFType;
32
Andrea Campanelladda93562016-03-02 11:08:12 -080033import java.net.URI;
34import java.net.URISyntaxException;
35import java.util.ArrayList;
36import java.util.List;
37
Ray Milkey7ec0d1b2015-11-13 08:51:35 -080038import static junit.framework.TestCase.fail;
39import static org.hamcrest.MatcherAssert.assertThat;
Jian Li2266bff2016-04-21 11:01:25 -070040import static org.hamcrest.Matchers.equalTo;
41import static org.hamcrest.Matchers.hasSize;
42import static org.hamcrest.Matchers.is;
Ray Milkey7ec0d1b2015-11-13 08:51:35 -080043
44/**
45 * Tests for packet processing in the open flow controller impl class.
46 */
47public class OpenFlowControllerImplPacketsTest {
48 OpenFlowControllerImpl controller;
49 OpenFlowControllerImpl.OpenFlowSwitchAgent agent;
50 Dpid dpid1;
51 OpenFlowSwitch switch1;
52 OpenFlowSwitchListenerAdapter switchListener;
53 TestPacketListener packetListener;
Jian Li28247b52016-01-07 17:24:15 -080054 TestExecutorService statsExecutorService;
Ray Milkey7ec0d1b2015-11-13 08:51:35 -080055
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 Milkey7ec0d1b2015-11-13 08:51:35 -080072 /**
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 Campanelladda93562016-03-02 11:08:12 -080083 public void execute(Runnable task) {
Ray Milkey7ec0d1b2015-11-13 08:51:35 -080084 OpenFlowControllerImpl.OFMessageHandler handler =
85 (OpenFlowControllerImpl.OFMessageHandler) task;
86 submittedMessages.add(handler.msg);
Ray Milkey7ec0d1b2015-11-13 08:51:35 -080087 }
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 Li28247b52016-01-07 17:24:15 -0800107 controller.monitorAllEvents(true);
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800108
109 packetListener = new TestPacketListener();
110 controller.addPacketListener(100, packetListener);
111
Jian Li28247b52016-01-07 17:24:15 -0800112 statsExecutorService = new TestExecutorService();
Jian Li28247b52016-01-07 17:24:15 -0800113
114 controller.executorMsgs = statsExecutorService;
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800115 }
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 Milkey7ec0d1b2015-11-13 08:51:35 -0800144 * 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 Li28247b52016-01-07 17:24:15 -0800151 assertThat(statsExecutorService.submittedMessages(), hasSize(1));
152 assertThat(statsExecutorService.submittedMessages().get(0), is(errorPacket));
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800153 }
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800154}