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