blob: 6e98181b28e57d249f89f6143b388be238571ab0 [file] [log] [blame]
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +09001/*
2 * Copyright 2017-present Open Networking Laboratory
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 */
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;
24import org.onlab.junit.TestTools;
25import org.onlab.junit.TestUtils;
26import org.onlab.packet.IpAddress;
27import org.onlab.packet.TpPort;
28import org.onosproject.cluster.ClusterService;
29import org.onosproject.cluster.ControllerNode;
30import org.onosproject.cluster.DefaultControllerNode;
31import org.onosproject.cluster.LeadershipService;
32import org.onosproject.cluster.NodeId;
33import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
35import org.onosproject.core.DefaultApplicationId;
36import org.onosproject.event.Event;
37import org.onosproject.incubator.net.virtual.NetworkId;
38import 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
80 private static final OFAgent OFAGENT_1 = DefaultOFAgent.builder()
81 .networkId(NETWORK_1)
82 .state(STOPPED)
83 .build();
84
85 private static final OFAgent OFAGENT_1_CTRL_1 = DefaultOFAgent.builder()
86 .networkId(NETWORK_1)
87 .controllers(CONTROLLER_1)
88 .state(STOPPED)
89 .build();
90
91 private static final OFAgent OFAGENT_1_CTRL_2 = DefaultOFAgent.builder()
92 .networkId(NETWORK_1)
93 .controllers(CONTROLLER_2)
94 .state(STOPPED)
95 .build();
96
97 private static final OFAgent OFAGENT_2 = DefaultOFAgent.builder()
98 .networkId(NETWORK_2)
99 .state(STOPPED)
100 .build();
101
102 private final TestOFAgentListener testListener = new TestOFAgentListener();
103 private final CoreService mockCoreService = createMock(CoreService.class);
104 private final LeadershipService mockLeadershipService = createMock(LeadershipService.class);
105 private final VirtualNetworkService mockVirtualNetService = createMock(VirtualNetworkService.class);
106 private final ClusterService mockClusterService = createMock(ClusterService.class);
107
108 private OFAgentManager target;
109 private DistributedOFAgentStore ofAgentStore;
110
111 @Before
112 public void setUp() throws Exception {
113 ofAgentStore = new DistributedOFAgentStore();
114 TestUtils.setField(ofAgentStore, "coreService", createMock(CoreService.class));
115 TestUtils.setField(ofAgentStore, "storageService", new TestStorageService());
Hyunsun Moon0bfc04a2017-05-08 11:02:14 +0900116 TestUtils.setField(ofAgentStore, "eventExecutor", MoreExecutors.newDirectExecutorService());
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900117 ofAgentStore.activate();
118
119 expect(mockCoreService.registerApplication(anyObject()))
120 .andReturn(APP_ID)
121 .anyTimes();
122 replay(mockCoreService);
123
124 expect(mockClusterService.getLocalNode())
125 .andReturn(LOCAL_NODE)
126 .anyTimes();
127 replay(mockClusterService);
128
129 expect(mockLeadershipService.runForLeadership(anyObject()))
130 .andReturn(null)
131 .anyTimes();
132 mockLeadershipService.addListener(anyObject());
133 mockLeadershipService.removeListener(anyObject());
134 mockLeadershipService.withdraw(anyObject());
135 replay(mockLeadershipService);
136
137 target = new OFAgentManager();
138 target.coreService = mockCoreService;
139 target.leadershipService = mockLeadershipService;
140 target.virtualNetService = mockVirtualNetService;
141 target.clusterService = mockClusterService;
142 target.ofAgentStore = ofAgentStore;
143 target.addListener(testListener);
144 target.activate();
145 }
146
147 @After
148 public void tearDown() {
149 target.removeListener(testListener);
150 ofAgentStore.deactivate();
151 target.deactivate();
152 ofAgentStore = null;
153 target = null;
154 }
155
156 @Test
157 public void testCreateAndRemoveAgent() {
158 target.createAgent(OFAGENT_1);
159 Set<OFAgent> agents = target.agents();
160 assertEquals("OFAgent set size did not match", 1, agents.size());
161
162 target.createAgent(OFAGENT_2);
163 agents = target.agents();
164 assertEquals("OFAgent set size did not match", 2, agents.size());
165
166 target.removeAgent(NETWORK_1);
167 agents = target.agents();
168 assertEquals("OFAgent set size did not match", 1, agents.size());
169
170 target.removeAgent(NETWORK_2);
171 agents = target.agents();
172 assertEquals("OFAgent set size did not match", 0, agents.size());
173
174 validateEvents(OFAGENT_CREATED, OFAGENT_CREATED, OFAGENT_REMOVED, OFAGENT_REMOVED);
175 }
176
177 @Test(expected = NullPointerException.class)
178 public void testCreateNullAgent() {
179 target.createAgent(null);
180 }
181
182 @Test(expected = IllegalArgumentException.class)
183 public void testCreateDuplicateAgent() {
184 target.createAgent(OFAGENT_1);
185 target.createAgent(OFAGENT_1);
186 }
187
188 @Test(expected = NullPointerException.class)
189 public void testRemoveNullAgent() {
190 target.removeAgent(null);
191 }
192
193 @Test(expected = IllegalStateException.class)
194 public void testRemoveNotFoundAgent() {
195 target.removeAgent(NETWORK_1);
196 }
197
198 @Test(expected = IllegalStateException.class)
199 public void testRemoveStartedAgent() {
200 target.createAgent(OFAGENT_1);
201 target.startAgent(NETWORK_1);
202 target.removeAgent(NETWORK_1);
203 }
204
205 @Test
206 public void testStartAndStopAgent() {
207 target.createAgent(OFAGENT_1);
208 target.startAgent(NETWORK_1);
209 OFAgent ofAgent = target.agent(NETWORK_1);
210 assertEquals("OFAgent state did not match", STARTED, ofAgent.state());
211
212 target.stopAgent(NETWORK_1);
213 ofAgent = target.agent(NETWORK_1);
214 assertEquals("OFAgent state did not match", STOPPED, ofAgent.state());
215
216 validateEvents(OFAGENT_CREATED, OFAGENT_STARTED, OFAGENT_STOPPED);
217 }
218
219 @Test
220 public void testAddController() {
221 target.createAgent(OFAGENT_1);
222 target.updateAgent(OFAGENT_1_CTRL_1);
223 OFAgent ofAgent = target.agent(NETWORK_1);
224 assertEquals("OFAgent controller did not match", CONTROLLER_1, ofAgent.controllers());
225
226 target.updateAgent(OFAGENT_1_CTRL_2);
227 ofAgent = target.agent(NETWORK_1);
228 assertEquals("OFAgent controller did not match", CONTROLLER_2, ofAgent.controllers());
229
230 validateEvents(OFAGENT_CREATED, OFAGENT_CONTROLLER_ADDED, OFAGENT_CONTROLLER_ADDED);
231 }
232
233 @Test
234 public void testRemoveController() {
235 target.createAgent(OFAGENT_1_CTRL_2);
236 target.updateAgent(OFAGENT_1_CTRL_1);
237 OFAgent ofAgent = target.agent(NETWORK_1);
238 assertEquals("OFAgent controller did not match", CONTROLLER_1, ofAgent.controllers());
239
240 target.updateAgent(OFAGENT_1);
241 ofAgent = target.agent(NETWORK_1);
242 assertTrue("OFAgent controller did not match", ofAgent.controllers().isEmpty());
243
244 validateEvents(OFAGENT_CREATED, OFAGENT_CONTROLLER_REMOVED, OFAGENT_CONTROLLER_REMOVED);
245 }
246
247 private void validateEvents(Enum... types) {
Hyunsun Moon0bfc04a2017-05-08 11:02:14 +0900248 int i = 0;
249 assertEquals("Number of events does not match", types.length, testListener.events.size());
250 for (Event event : testListener.events) {
251 assertEquals("Incorrect event received", types[i], event.type());
252 i++;
253 }
254 testListener.events.clear();
Hyunsun Moonf4ba44f2017-03-14 03:25:52 +0900255 }
256
257 private static class TestOFAgentListener implements OFAgentListener {
258
259 private List<OFAgentEvent> events = Lists.newArrayList();
260
261 @Override
262 public void event(OFAgentEvent event) {
263 events.add(event);
264 }
265 }
266}