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