Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
developer | e400c58 | 2020-03-24 19:42:08 +0100 | [diff] [blame] | 17 | package org.opencord.igmpproxy.impl; |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.onlab.junit.TestTools.assertAfter; |
| 21 | |
| 22 | import java.util.List; |
| 23 | |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 24 | import com.google.common.collect.Maps; |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.onlab.junit.TestUtils; |
| 29 | import org.onlab.packet.Ethernet; |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 30 | import org.onlab.packet.Ip4Address; |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 31 | import org.onosproject.core.CoreServiceAdapter; |
| 32 | import org.onosproject.net.flow.FlowRuleServiceAdapter; |
| 33 | import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter; |
| 34 | |
developer | e400c58 | 2020-03-24 19:42:08 +0100 | [diff] [blame] | 35 | import org.opencord.igmpproxy.IgmpStatisticsEvent; |
| 36 | |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 37 | import com.google.common.collect.Lists; |
developer | e400c58 | 2020-03-24 19:42:08 +0100 | [diff] [blame] | 38 | import org.opencord.igmpproxy.IgmpStatisticsEventListener; |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Set of tests of the ONOS application component for IGMP Statistics. |
| 42 | */ |
| 43 | public class IgmpStatisticsTest extends IgmpManagerBase { |
| 44 | |
| 45 | private static final int WAIT_TIMEOUT = 500; |
| 46 | |
| 47 | private IgmpManager igmpManager; |
| 48 | |
| 49 | private IgmpStatisticsManager igmpStatisticsManager; |
| 50 | |
| 51 | private MockIgmpStatisticsEventListener mockListener = new MockIgmpStatisticsEventListener(); |
| 52 | |
| 53 | // Set up the IGMP application. |
| 54 | @Before |
| 55 | public void setUp() { |
| 56 | igmpManager = new IgmpManager(); |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 57 | igmpManager.igmpLeadershipService = new TestIgmpLeaderShipService(); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 58 | igmpManager.coreService = new CoreServiceAdapter(); |
| 59 | igmpManager.mastershipService = new MockMastershipService(); |
| 60 | igmpManager.flowObjectiveService = new FlowObjectiveServiceAdapter(); |
| 61 | igmpManager.deviceService = new MockDeviceService(); |
| 62 | igmpManager.packetService = new MockPacketService(); |
| 63 | igmpManager.flowRuleService = new FlowRuleServiceAdapter(); |
| 64 | igmpManager.multicastService = new TestMulticastRouteService(); |
| 65 | igmpManager.sadisService = new MockSadisService(); |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 66 | igmpManager.groupMemberStore = new TestGroupMemberStoreService(); |
| 67 | StateMachineManager stateMachineService = new StateMachineManager(); |
| 68 | stateMachineService.stateMachineStore = new TestStateMachineStoreService(Maps.newConcurrentMap()); |
| 69 | stateMachineService.activate(new MockComponentContext()); |
| 70 | igmpManager.stateMachineService = stateMachineService; |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 71 | igmpStatisticsManager = new IgmpStatisticsManager(); |
| 72 | igmpStatisticsManager.cfgService = new MockCfgService(); |
| 73 | igmpStatisticsManager.addListener(mockListener); |
| 74 | TestUtils.setField(igmpStatisticsManager, "eventDispatcher", new TestEventDispatcher()); |
| 75 | igmpStatisticsManager.activate(new MockComponentContext()); |
| 76 | igmpManager.igmpStatisticsManager = this.igmpStatisticsManager; |
| 77 | // By default - we send query messages |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 78 | StateMachineManager.sendQuery = true; |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // Tear Down the IGMP application. |
| 82 | @After |
| 83 | public void tearDown() { |
| 84 | igmpStatisticsManager.removeListener(mockListener); |
| 85 | igmpStatisticsManager.deactivate(); |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 86 | igmpManager.stateMachineService.clearAllMaps(); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | //Test Igmp Statistics. |
| 90 | @Test |
| 91 | public void testIgmpStatistics() throws InterruptedException { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 92 | StateMachineManager.sendQuery = false; |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 93 | igmpManager.networkConfig = new TestNetworkConfigRegistry(false); |
| 94 | igmpManager.activate(); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 95 | |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 96 | //IGMPv3 Join |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 97 | flagForPacket = false; |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 98 | Ethernet igmpv3MembershipReportPkt = IgmpSender.getInstance().buildIgmpV3Join(GROUP_IP, SOURCE_IP_OF_A); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 99 | sendPacket(igmpv3MembershipReportPkt); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 100 | synchronized (savedPackets) { |
| 101 | savedPackets.wait(WAIT_TIMEOUT); |
| 102 | } |
| 103 | //Leave |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 104 | flagForPacket = false; |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 105 | Ethernet igmpv3LeavePkt = IgmpSender.getInstance().buildIgmpV3Leave(GROUP_IP, SOURCE_IP_OF_A); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 106 | sendPacket(igmpv3LeavePkt); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 107 | synchronized (savedPackets) { |
| 108 | savedPackets.wait(WAIT_TIMEOUT); |
| 109 | } |
| 110 | |
| 111 | assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () -> |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 112 | assertEquals((long) 2, igmpStatisticsManager.getIgmpStats().getTotalMsgReceived().longValue())); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 113 | assertEquals((long) 1, igmpStatisticsManager.getIgmpStats().getIgmpJoinReq().longValue()); |
| 114 | assertEquals((long) 2, igmpStatisticsManager.getIgmpStats().getIgmpv3MembershipReport().longValue()); |
| 115 | assertEquals((long) 1, igmpStatisticsManager.getIgmpStats().getIgmpSuccessJoinRejoinReq().longValue()); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 116 | assertEquals((long) 1, igmpStatisticsManager.getIgmpStats().getUnconfiguredGroupCounter().longValue()); |
| 117 | assertEquals((long) 2, igmpStatisticsManager.getIgmpStats().getValidIgmpPacketCounter().longValue()); |
| 118 | assertEquals((long) 1, igmpStatisticsManager.getIgmpStats().getIgmpChannelJoinCounter().longValue()); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 119 | assertEquals((long) 1, igmpStatisticsManager.getIgmpStats().getIgmpLeaveReq().longValue()); |
| 120 | assertEquals((long) 2, igmpStatisticsManager.getIgmpStats().getIgmpMsgReceived().longValue()); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 121 | assertEquals((long) 2, igmpStatisticsManager.getIgmpStats().getIgmpValidChecksumCounter().longValue()); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 122 | |
| 123 | } |
| 124 | |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 125 | //Test packet with Unknown Multicast IpAddress |
| 126 | @Test |
| 127 | public void testIgmpUnknownMulticastIpAddress() throws InterruptedException { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 128 | StateMachineManager.sendQuery = false; |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 129 | |
| 130 | igmpManager.networkConfig = new TestNetworkConfigRegistry(false); |
| 131 | igmpManager.activate(); |
| 132 | |
| 133 | Ethernet firstPacket = |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 134 | IgmpSender.getInstance().buildIgmpV3Join(UNKNOWN_GRP_IP, SOURCE_IP_OF_A); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 135 | // Sending first packet |
| 136 | sendPacket(firstPacket); |
| 137 | assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () -> |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 138 | assertEquals((long) 1, |
| 139 | igmpStatisticsManager.getIgmpStats().getFailJoinReqUnknownMulticastIpCounter().longValue())); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | //Test Igmp Query Statistics. |
| 143 | @Test |
| 144 | public void testIgmpQueryStatistics() throws InterruptedException { |
| 145 | igmpManager.networkConfig = new TestNetworkConfigRegistry(false); |
| 146 | igmpManager.activate(); |
| 147 | |
| 148 | flagForQueryPacket = true; |
| 149 | //IGMPV3 Group Specific Membership Query packet |
| 150 | Ethernet igmpv3MembershipQueryPkt = IgmpSender.getInstance().buildIgmpV3Query(GROUP_IP, SOURCE_IP_OF_A); |
| 151 | sendPacket(igmpv3MembershipQueryPkt); |
| 152 | |
| 153 | //IGMPV3 General Membership Query packet |
| 154 | Ethernet igmpv3MembershipQueryPkt1 = |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 155 | IgmpSender.getInstance().buildIgmpV3Query(Ip4Address.valueOf(0), SOURCE_IP_OF_A); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 156 | sendPacket(igmpv3MembershipQueryPkt1); |
| 157 | assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () -> |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 158 | assertEquals(igmpStatisticsManager.getIgmpStats() |
| 159 | .getIgmpGrpAndSrcSpecificMembershipQuery().longValue(), 1)); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 160 | assertEquals(igmpStatisticsManager.getIgmpStats() |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 161 | .getIgmpGeneralMembershipQuery().longValue(), 1); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 162 | assertEquals(igmpStatisticsManager.getIgmpStats() |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 163 | .getCurrentGrpNumCounter().longValue(), 1); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 166 | //Test Events |
| 167 | @Test |
| 168 | public void testIgmpStatisticsEvent() { |
| 169 | final int waitEventGeneration = igmpStatisticsManager.statisticsGenerationPeriodInSeconds * 1000; |
| 170 | //assert that event listened as the app activates |
| 171 | assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () -> |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 172 | assertEquals(mockListener.events.size(), 1)); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 173 | |
| 174 | assertAfter(waitEventGeneration / 2, waitEventGeneration, () -> |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 175 | assertEquals(mockListener.events.size(), 2)); |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 176 | |
| 177 | for (IgmpStatisticsEvent event : mockListener.events) { |
| 178 | assertEquals(event.type(), IgmpStatisticsEvent.Type.STATS_UPDATE); |
| 179 | } |
| 180 | } |
| 181 | |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 182 | //Test packet with Unknown Wrong Membership mode |
| 183 | @Test |
| 184 | public void testWrongIgmpPacket() throws InterruptedException { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 185 | StateMachineManager.sendQuery = false; |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 186 | |
| 187 | igmpManager.networkConfig = new TestNetworkConfigRegistry(false); |
| 188 | igmpManager.activate(); |
| 189 | |
| 190 | Ethernet firstPacket = buildWrongIgmpPacket(GROUP_IP, SOURCE_IP_OF_A); |
| 191 | // Sending first packet |
| 192 | sendPacket(firstPacket); |
| 193 | assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () -> |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 194 | assertEquals((long) 1, |
| 195 | igmpStatisticsManager.getIgmpStats().getReportsRxWithWrongModeCounter().longValue())); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | //Test packet with Unknown IGMP type. |
| 199 | @Test |
| 200 | public void testUnknownIgmpPacket() throws InterruptedException { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 201 | StateMachineManager.sendQuery = false; |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 202 | |
| 203 | igmpManager.networkConfig = new TestNetworkConfigRegistry(false); |
| 204 | igmpManager.activate(); |
| 205 | |
| 206 | Ethernet firstPacket = buildUnknownIgmpPacket(GROUP_IP, SOURCE_IP_OF_A); |
| 207 | // Sending first packet |
| 208 | sendPacket(firstPacket); |
| 209 | assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () -> |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 210 | assertEquals((long) 1, |
| 211 | igmpStatisticsManager.getIgmpStats().getUnknownIgmpTypePacketsRxCounter().longValue())); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | //Test packet with Insufficient Permission. |
| 215 | @Test |
| 216 | public void testSufficientPermission() throws InterruptedException { |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 217 | StateMachineManager.sendQuery = false; |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 218 | |
| 219 | flagForPermission = true; |
| 220 | igmpManager.networkConfig = new TestNetworkConfigRegistry(false); |
| 221 | igmpManager.activate(); |
| 222 | |
| 223 | Ethernet firstPacket = IgmpSender.getInstance().buildIgmpV3Join(GROUP_IP, SOURCE_IP_OF_A); |
| 224 | // Sending first packet |
| 225 | sendPacket(firstPacket); |
| 226 | assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () -> |
Ilayda Ozdemir | 4c5947c | 2020-05-05 13:14:32 +0000 | [diff] [blame^] | 227 | assertEquals((long) 1, |
| 228 | igmpStatisticsManager.getIgmpStats() |
| 229 | .getFailJoinReqInsuffPermissionAccessCounter().longValue())); |
Sonal Kasliwal | f11c067 | 2020-03-18 11:11:50 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Shubham Sharma | 47f2caf | 2020-02-18 12:13:40 +0000 | [diff] [blame] | 232 | public class MockIgmpStatisticsEventListener implements IgmpStatisticsEventListener { |
| 233 | protected List<IgmpStatisticsEvent> events = Lists.newArrayList(); |
| 234 | |
| 235 | @Override |
| 236 | public void event(IgmpStatisticsEvent event) { |
| 237 | events.add(event); |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | } |