Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 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 | package org.onosproject.ofagent.impl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import com.google.common.collect.Sets; |
Hyunsun Moon | 0bfc04a | 2017-05-08 11:02:14 +0900 | [diff] [blame] | 20 | import com.google.common.util.concurrent.MoreExecutors; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 24 | import org.onlab.junit.TestUtils; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.TpPort; |
| 27 | import org.onosproject.cluster.ClusterService; |
| 28 | import org.onosproject.cluster.ControllerNode; |
| 29 | import org.onosproject.cluster.DefaultControllerNode; |
| 30 | import org.onosproject.cluster.LeadershipService; |
| 31 | import org.onosproject.cluster.NodeId; |
| 32 | import org.onosproject.core.ApplicationId; |
| 33 | import org.onosproject.core.CoreService; |
| 34 | import org.onosproject.core.DefaultApplicationId; |
| 35 | import org.onosproject.event.Event; |
| 36 | import org.onosproject.incubator.net.virtual.NetworkId; |
Jovana Vuleta | c884b69 | 2017-11-28 16:52:35 +0100 | [diff] [blame] | 37 | import org.onosproject.incubator.net.virtual.TenantId; |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 38 | import org.onosproject.incubator.net.virtual.VirtualNetworkService; |
| 39 | import org.onosproject.ofagent.api.OFAgent; |
| 40 | import org.onosproject.ofagent.api.OFAgentEvent; |
| 41 | import org.onosproject.ofagent.api.OFAgentListener; |
| 42 | import org.onosproject.ofagent.api.OFController; |
| 43 | import org.onosproject.store.service.TestStorageService; |
| 44 | |
| 45 | import java.util.List; |
| 46 | import java.util.Set; |
| 47 | |
| 48 | import static org.easymock.EasyMock.*; |
| 49 | import static org.junit.Assert.assertEquals; |
| 50 | import static org.junit.Assert.assertTrue; |
| 51 | import static org.onosproject.ofagent.api.OFAgent.State.STARTED; |
| 52 | import static org.onosproject.ofagent.api.OFAgent.State.STOPPED; |
| 53 | import static org.onosproject.ofagent.api.OFAgentEvent.Type.*; |
| 54 | |
| 55 | /** |
| 56 | * Junit tests for OFAgent target. |
| 57 | */ |
| 58 | public 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 Vuleta | c884b69 | 2017-11-28 16:52:35 +0100 | [diff] [blame] | 80 | private static final TenantId TENANT_1 = TenantId.tenantId("Tenant_1"); |
| 81 | private static final TenantId TENANT_2 = TenantId.tenantId("Tenant_2"); |
| 82 | |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 83 | private static final OFAgent OFAGENT_1 = DefaultOFAgent.builder() |
| 84 | .networkId(NETWORK_1) |
Jovana Vuleta | c884b69 | 2017-11-28 16:52:35 +0100 | [diff] [blame] | 85 | .tenantId(TENANT_1) |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 86 | .state(STOPPED) |
| 87 | .build(); |
| 88 | |
| 89 | private static final OFAgent OFAGENT_1_CTRL_1 = DefaultOFAgent.builder() |
| 90 | .networkId(NETWORK_1) |
Jovana Vuleta | c884b69 | 2017-11-28 16:52:35 +0100 | [diff] [blame] | 91 | .tenantId(TENANT_1) |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 92 | .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 Vuleta | c884b69 | 2017-11-28 16:52:35 +0100 | [diff] [blame] | 98 | .tenantId(TENANT_1) |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 99 | .controllers(CONTROLLER_2) |
| 100 | .state(STOPPED) |
| 101 | .build(); |
| 102 | |
| 103 | private static final OFAgent OFAGENT_2 = DefaultOFAgent.builder() |
| 104 | .networkId(NETWORK_2) |
Jovana Vuleta | c884b69 | 2017-11-28 16:52:35 +0100 | [diff] [blame] | 105 | .tenantId(TENANT_2) |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 106 | .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 Moon | 0bfc04a | 2017-05-08 11:02:14 +0900 | [diff] [blame] | 123 | TestUtils.setField(ofAgentStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
Hyunsun Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 124 | 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 Moon | 0bfc04a | 2017-05-08 11:02:14 +0900 | [diff] [blame] | 255 | 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 Moon | f4ba44f | 2017-03-14 03:25:52 +0900 | [diff] [blame] | 262 | } |
| 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 | } |