blob: 1c4a3d1f81d47cf40daca2b3d149b22fd9e121ce [file] [log] [blame]
pier7f292e72019-07-16 15:52:50 +02001/*
2 * Copyright 2019-present Open Networking Foundation
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
17package org.onosproject.openflow.controller.impl;
18
19import com.google.common.collect.ImmutableSet;
20import io.netty.channel.ChannelHandlerContext;
21import org.junit.Before;
22import org.junit.Test;
23import org.onosproject.openflow.ChannelHandlerContextAdapter;
24import org.onosproject.openflow.MockOfPortStatus;
25import org.onosproject.openflow.OFDescStatsReplyAdapter;
26import org.onosproject.openflow.OpenflowSwitchDriverAdapter;
27import org.onosproject.openflow.controller.Dpid;
28import org.projectfloodlight.openflow.protocol.OFDescStatsReply;
29
30import static org.easymock.EasyMock.*;
31import static org.hamcrest.CoreMatchers.is;
32import static org.junit.Assert.assertNotNull;
33import static org.junit.Assert.assertThat;
34import static org.onosproject.openflow.controller.impl.OFChannelHandler.ChannelState.ACTIVE;
35import static org.onosproject.openflow.controller.impl.OFChannelHandler.ChannelState.WAIT_DESCRIPTION_STAT_REPLY;
36import static org.onosproject.openflow.controller.impl.OFChannelHandler.ChannelState.WAIT_SWITCH_DRIVER_SUB_HANDSHAKE;
37import static org.projectfloodlight.openflow.protocol.OFVersion.OF_13;
38
39/**
40 * Unit tests for the OpenFlow channel handler.
41 */
42public class OFChannelHandlerTest {
43
44 private Controller controller;
45 private OFChannelHandler channelHandler;
46 private ChannelHandlerContext channelHandlerContext;
47
48 @Before
49 public void setUp() {
50 controller = createMock(Controller.class);
51 channelHandler = new OFChannelHandler(controller);
52 channelHandler.ofVersion = OF_13;
53 channelHandlerContext = new ChannelHandlerContextAdapter();
54 }
55
56 // Normal workflow - connect
57 @Test
58 public void testActiveDpid() {
59 // Expected behavior
60 OFDescStatsReply reply = new OFDescStatsReplyAdapter();
61 expect(controller.getOFSwitchInstance(0, reply, OF_13)).andReturn(
62 new OpenflowSwitchDriverAdapter(ImmutableSet.of(), Dpid.dpid(Dpid.uri(0)), true));
63 replay(controller);
64
65 try {
66 channelHandler.channelActive(channelHandlerContext);
67 channelHandler.setState(WAIT_DESCRIPTION_STAT_REPLY);
68 channelHandler.channelRead(channelHandlerContext, reply);
69 } catch (Exception e) {
70 channelHandler = null;
71 }
72 // exception should not be fired
73 assertNotNull(channelHandler);
74 assertThat(channelHandler.getStateForTesting(), is(ACTIVE));
75
76 // Finally verify
77 verify(controller);
78 }
79
80 // Normal workflow - duplicate Dpid
81 @Test
82 public void testDuplicateDpid() {
83 // Expected behavior
84 OFDescStatsReply reply = new OFDescStatsReplyAdapter();
85 expect(controller.getOFSwitchInstance(0, reply, OF_13)).andReturn(new OpenflowSwitchDriverAdapter(
86 ImmutableSet.of(Dpid.dpid(Dpid.uri(0))), Dpid.dpid(Dpid.uri(0)), true));
87 replay(controller);
88
89 try {
90 channelHandler.channelActive(channelHandlerContext);
91 channelHandler.setState(WAIT_DESCRIPTION_STAT_REPLY);
92 channelHandler.channelRead(channelHandlerContext, reply);
93 } catch (Exception e) {
94 channelHandler = null;
95 }
96 // exception should not be fired
97 assertNotNull(channelHandler);
98 assertThat(channelHandler.getStateForTesting(), is(WAIT_DESCRIPTION_STAT_REPLY));
99
100 // Finally verify
101 verify(controller);
102 }
103
104 // Through subhandshake
105 @Test
106 public void testActiveDpidSub() {
107 // Expected behavior
108 OFDescStatsReply reply = new OFDescStatsReplyAdapter();
109 expect(controller.getOFSwitchInstance(0, reply, OF_13)).andReturn(new OpenflowSwitchDriverAdapter(
110 ImmutableSet.of(), Dpid.dpid(Dpid.uri(0)), false));
111 replay(controller);
112
113 try {
114 channelHandler.channelActive(channelHandlerContext);
115 channelHandler.setState(WAIT_DESCRIPTION_STAT_REPLY);
116 channelHandler.channelRead(channelHandlerContext, reply);
117 channelHandler.channelRead(channelHandlerContext, new MockOfPortStatus());
118 } catch (Exception e) {
119 channelHandler = null;
120 }
121 // exception should not be fired
122 assertNotNull(channelHandler);
123 assertThat(channelHandler.getStateForTesting(), is(ACTIVE));
124
125 // Finally verify
126 verify(controller);
127 }
128
129 // Through subhandshake - duplicate dpid
130 @Test
131 public void testDuplicateDpidSub() {
132 // Expected behavior
133 OFDescStatsReply reply = new OFDescStatsReplyAdapter();
134 expect(controller.getOFSwitchInstance(0, reply, OF_13)).andReturn(new OpenflowSwitchDriverAdapter(
135 ImmutableSet.of(Dpid.dpid(Dpid.uri(0))), Dpid.dpid(Dpid.uri(0)), false));
136 replay(controller);
137
138 try {
139 channelHandler.channelActive(channelHandlerContext);
140 channelHandler.setState(WAIT_DESCRIPTION_STAT_REPLY);
141 channelHandler.channelRead(channelHandlerContext, reply);
142 channelHandler.channelRead(channelHandlerContext, new MockOfPortStatus());
143 } catch (Exception e) {
144 channelHandler = null;
145 }
146 // exception should not be fired
147 assertNotNull(channelHandler);
148 assertThat(channelHandler.getStateForTesting(), is(WAIT_SWITCH_DRIVER_SUB_HANDSHAKE));
149
150 // Finally verify
151 verify(controller);
152 }
153
154}