blob: 17ee9d15c67de2454e90bd68212056e11f026d4e [file] [log] [blame]
Ray Milkey7ec0d1b2015-11-13 08:51:35 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Prince Pereirae7798032016-07-08 16:31:58 +053055 TestExecutorService errorMsgExecutorService;
Ray Milkey7ec0d1b2015-11-13 08:51:35 -080056 /**
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);
107
108 packetListener = new TestPacketListener();
109 controller.addPacketListener(100, packetListener);
110
Jian Li28247b52016-01-07 17:24:15 -0800111 statsExecutorService = new TestExecutorService();
Prince Pereirae7798032016-07-08 16:31:58 +0530112 errorMsgExecutorService = new TestExecutorService();
Jian Li28247b52016-01-07 17:24:15 -0800113
114 controller.executorMsgs = statsExecutorService;
Prince Pereirae7798032016-07-08 16:31:58 +0530115 controller.executorErrorMsgs = errorMsgExecutorService;
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800116 }
117
118 /**
119 * Tests a port status operation.
120 */
121 @Test
122 public void testPortStatus() {
123 OFMessage portStatusPacket = new MockOfPortStatus();
124 controller.processPacket(dpid1, portStatusPacket);
125 assertThat(switchListener.portChangedDpids().size(), is(1));
126 assertThat(switchListener.portChangedDpids().containsKey(dpid1),
127 is(true));
128 assertThat(switchListener.portChangedDpids().get(dpid1),
129 equalTo(portStatusPacket));
130 }
131
132 /**
133 * Tests a features reply operation.
134 */
135 @Test
136 public void testFeaturesReply() {
137 OFMessage ofFeaturesReplyPacket = new MockOfFeaturesReply();
138 controller.processPacket(dpid1, ofFeaturesReplyPacket);
139 assertThat(switchListener.changedDpids(), hasSize(1));
140 assertThat(switchListener.changedDpids().get(0),
141 equalTo(dpid1));
142 }
143
144 /**
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800145 * Tests an error operation.
146 */
147 @Test
148 public void testError() {
149 agent.addConnectedSwitch(dpid1, switch1);
150 OfMessageAdapter errorPacket = new OfMessageAdapter(OFType.ERROR);
151 controller.processPacket(dpid1, errorPacket);
Prince Pereirae7798032016-07-08 16:31:58 +0530152 assertThat(errorMsgExecutorService.submittedMessages(), hasSize(1));
153 assertThat(errorMsgExecutorService.submittedMessages().get(0), is(errorPacket));
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800154 }
Ray Milkey7ec0d1b2015-11-13 08:51:35 -0800155}