blob: 2a7c941c6c109d69653780d5f4ca5765ee9ae65a [file] [log] [blame]
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +09003 *
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.ofagent.impl;
17
18import com.google.common.collect.Lists;
19import com.google.common.collect.Sets;
Hyunsun Moon0bfc04a2017-05-08 11:02:14 +090020import com.google.common.util.concurrent.MoreExecutors;
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090021import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +090024import org.onlab.junit.TestUtils;
25import org.onlab.packet.IpAddress;
26import org.onlab.packet.TpPort;
27import org.onosproject.cluster.ClusterService;
28import org.onosproject.cluster.ControllerNode;
29import org.onosproject.cluster.DefaultControllerNode;
30import org.onosproject.cluster.LeadershipService;
31import org.onosproject.cluster.NodeId;
32import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
34import org.onosproject.core.DefaultApplicationId;
35import org.onosproject.event.Event;
36import org.onosproject.incubator.net.virtual.NetworkId;
37import org.onosproject.incubator.net.virtual.VirtualNetworkService;
38import org.onosproject.ofagent.api.OFAgent;
39import org.onosproject.ofagent.api.OFAgentEvent;
40import org.onosproject.ofagent.api.OFAgentListener;
41import org.onosproject.ofagent.api.OFController;
42import org.onosproject.store.service.TestStorageService;
43
44import java.util.List;
45import java.util.Set;
46
47import static org.easymock.EasyMock.*;
48import static org.junit.Assert.assertEquals;
49import static org.junit.Assert.assertTrue;
50import static org.onosproject.ofagent.api.OFAgent.State.STARTED;
51import static org.onosproject.ofagent.api.OFAgent.State.STOPPED;
52import static org.onosproject.ofagent.api.OFAgentEvent.Type.*;
53
54/**
55 * Junit tests for OFAgent target.
56 */
57public class OFAgentManagerTest {
58
59 private static final ApplicationId APP_ID = new DefaultApplicationId(1, "test");
60 private static final ControllerNode LOCAL_NODE =
61 new DefaultControllerNode(new NodeId("local"), IpAddress.valueOf("127.0.0.1"));
62
63 private static final Set<OFController> CONTROLLER_1 = Sets.newHashSet(
64 DefaultOFController.of(
65 IpAddress.valueOf("192.168.0.3"),
66 TpPort.tpPort(6653)));
67
68 private static final Set<OFController> CONTROLLER_2 = Sets.newHashSet(
69 DefaultOFController.of(
70 IpAddress.valueOf("192.168.0.3"),
71 TpPort.tpPort(6653)),
72 DefaultOFController.of(
73 IpAddress.valueOf("192.168.0.4"),
74 TpPort.tpPort(6653)));
75
76 private static final NetworkId NETWORK_1 = NetworkId.networkId(1);
77 private static final NetworkId NETWORK_2 = NetworkId.networkId(2);
78
79 private static final OFAgent OFAGENT_1 = DefaultOFAgent.builder()
80 .networkId(NETWORK_1)
81 .state(STOPPED)
82 .build();
83
84 private static final OFAgent OFAGENT_1_CTRL_1 = DefaultOFAgent.builder()
85 .networkId(NETWORK_1)
86 .controllers(CONTROLLER_1)
87 .state(STOPPED)
88 .build();
89
90 private static final OFAgent OFAGENT_1_CTRL_2 = DefaultOFAgent.builder()
91 .networkId(NETWORK_1)
92 .controllers(CONTROLLER_2)
93 .state(STOPPED)
94 .build();
95
96 private static final OFAgent OFAGENT_2 = DefaultOFAgent.builder()
97 .networkId(NETWORK_2)
98 .state(STOPPED)
99 .build();
100
101 private final TestOFAgentListener testListener = new TestOFAgentListener();
102 private final CoreService mockCoreService = createMock(CoreService.class);
103 private final LeadershipService mockLeadershipService = createMock(LeadershipService.class);
104 private final VirtualNetworkService mockVirtualNetService = createMock(VirtualNetworkService.class);
105 private final ClusterService mockClusterService = createMock(ClusterService.class);
106
107 private OFAgentManager target;
108 private DistributedOFAgentStore ofAgentStore;
109
110 @Before
111 public void setUp() throws Exception {
112 ofAgentStore = new DistributedOFAgentStore();
113 TestUtils.setField(ofAgentStore, "coreService", createMock(CoreService.class));
114 TestUtils.setField(ofAgentStore, "storageService", new TestStorageService());
Hyunsun Moon0bfc04a2017-05-08 11:02:14 +0900115 TestUtils.setField(ofAgentStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900116 ofAgentStore.activate();
117
118 expect(mockCoreService.registerApplication(anyObject()))
119 .andReturn(APP_ID)
120 .anyTimes();
121 replay(mockCoreService);
122
123 expect(mockClusterService.getLocalNode())
124 .andReturn(LOCAL_NODE)
125 .anyTimes();
126 replay(mockClusterService);
127
128 expect(mockLeadershipService.runForLeadership(anyObject()))
129 .andReturn(null)
130 .anyTimes();
131 mockLeadershipService.addListener(anyObject());
132 mockLeadershipService.removeListener(anyObject());
133 mockLeadershipService.withdraw(anyObject());
134 replay(mockLeadershipService);
135
136 target = new OFAgentManager();
137 target.coreService = mockCoreService;
138 target.leadershipService = mockLeadershipService;
139 target.virtualNetService = mockVirtualNetService;
140 target.clusterService = mockClusterService;
141 target.ofAgentStore = ofAgentStore;
142 target.addListener(testListener);
143 target.activate();
144 }
145
146 @After
147 public void tearDown() {
148 target.removeListener(testListener);
149 ofAgentStore.deactivate();
150 target.deactivate();
151 ofAgentStore = null;
152 target = null;
153 }
154
155 @Test
156 public void testCreateAndRemoveAgent() {
157 target.createAgent(OFAGENT_1);
158 Set<OFAgent> agents = target.agents();
159 assertEquals("OFAgent set size did not match", 1, agents.size());
160
161 target.createAgent(OFAGENT_2);
162 agents = target.agents();
163 assertEquals("OFAgent set size did not match", 2, agents.size());
164
165 target.removeAgent(NETWORK_1);
166 agents = target.agents();
167 assertEquals("OFAgent set size did not match", 1, agents.size());
168
169 target.removeAgent(NETWORK_2);
170 agents = target.agents();
171 assertEquals("OFAgent set size did not match", 0, agents.size());
172
173 validateEvents(OFAGENT_CREATED, OFAGENT_CREATED, OFAGENT_REMOVED, OFAGENT_REMOVED);
174 }
175
176 @Test(expected = NullPointerException.class)
177 public void testCreateNullAgent() {
178 target.createAgent(null);
179 }
180
181 @Test(expected = IllegalArgumentException.class)
182 public void testCreateDuplicateAgent() {
183 target.createAgent(OFAGENT_1);
184 target.createAgent(OFAGENT_1);
185 }
186
187 @Test(expected = NullPointerException.class)
188 public void testRemoveNullAgent() {
189 target.removeAgent(null);
190 }
191
192 @Test(expected = IllegalStateException.class)
193 public void testRemoveNotFoundAgent() {
194 target.removeAgent(NETWORK_1);
195 }
196
197 @Test(expected = IllegalStateException.class)
198 public void testRemoveStartedAgent() {
199 target.createAgent(OFAGENT_1);
200 target.startAgent(NETWORK_1);
201 target.removeAgent(NETWORK_1);
202 }
203
204 @Test
205 public void testStartAndStopAgent() {
206 target.createAgent(OFAGENT_1);
207 target.startAgent(NETWORK_1);
208 OFAgent ofAgent = target.agent(NETWORK_1);
209 assertEquals("OFAgent state did not match", STARTED, ofAgent.state());
210
211 target.stopAgent(NETWORK_1);
212 ofAgent = target.agent(NETWORK_1);
213 assertEquals("OFAgent state did not match", STOPPED, ofAgent.state());
214
215 validateEvents(OFAGENT_CREATED, OFAGENT_STARTED, OFAGENT_STOPPED);
216 }
217
218 @Test
219 public void testAddController() {
220 target.createAgent(OFAGENT_1);
221 target.updateAgent(OFAGENT_1_CTRL_1);
222 OFAgent ofAgent = target.agent(NETWORK_1);
223 assertEquals("OFAgent controller did not match", CONTROLLER_1, ofAgent.controllers());
224
225 target.updateAgent(OFAGENT_1_CTRL_2);
226 ofAgent = target.agent(NETWORK_1);
227 assertEquals("OFAgent controller did not match", CONTROLLER_2, ofAgent.controllers());
228
229 validateEvents(OFAGENT_CREATED, OFAGENT_CONTROLLER_ADDED, OFAGENT_CONTROLLER_ADDED);
230 }
231
232 @Test
233 public void testRemoveController() {
234 target.createAgent(OFAGENT_1_CTRL_2);
235 target.updateAgent(OFAGENT_1_CTRL_1);
236 OFAgent ofAgent = target.agent(NETWORK_1);
237 assertEquals("OFAgent controller did not match", CONTROLLER_1, ofAgent.controllers());
238
239 target.updateAgent(OFAGENT_1);
240 ofAgent = target.agent(NETWORK_1);
241 assertTrue("OFAgent controller did not match", ofAgent.controllers().isEmpty());
242
243 validateEvents(OFAGENT_CREATED, OFAGENT_CONTROLLER_REMOVED, OFAGENT_CONTROLLER_REMOVED);
244 }
245
246 private void validateEvents(Enum... types) {
Hyunsun Moon0bfc04a2017-05-08 11:02:14 +0900247 int i = 0;
248 assertEquals("Number of events does not match", types.length, testListener.events.size());
249 for (Event event : testListener.events) {
250 assertEquals("Incorrect event received", types[i], event.type());
251 i++;
252 }
253 testListener.events.clear();
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900254 }
255
256 private static class TestOFAgentListener implements OFAgentListener {
257
258 private List<OFAgentEvent> events = Lists.newArrayList();
259
260 @Override
261 public void event(OFAgentEvent event) {
262 events.add(event);
263 }
264 }
265}