blob: 3c9f5aaba51a816061e9cbab777974b72f321586 [file] [log] [blame]
Shubham Sharma47f2caf2020-02-18 12:13:40 +00001/*
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
developere400c582020-03-24 19:42:08 +010017package org.opencord.igmpproxy.impl;
Shubham Sharma47f2caf2020-02-18 12:13:40 +000018
19import static org.junit.Assert.assertEquals;
20import static org.onlab.junit.TestTools.assertAfter;
21
22import java.util.List;
23
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000024import com.google.common.collect.Maps;
Shubham Sharma47f2caf2020-02-18 12:13:40 +000025import org.junit.After;
26import org.junit.Before;
27import org.junit.Test;
28import org.onlab.junit.TestUtils;
29import org.onlab.packet.Ethernet;
Sonal Kasliwalf11c0672020-03-18 11:11:50 +000030import org.onlab.packet.Ip4Address;
Esin Karaman586f1d62020-06-04 10:15:34 +000031import org.onlab.packet.VlanId;
Shubham Sharma47f2caf2020-02-18 12:13:40 +000032import org.onosproject.core.CoreServiceAdapter;
33import org.onosproject.net.flow.FlowRuleServiceAdapter;
34import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter;
35
Ilayda Ozdemir0872abd2020-06-03 20:20:20 +030036import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
37import org.onosproject.store.service.TestStorageService;
38import org.opencord.igmpproxy.IgmpStatisticType;
developere400c582020-03-24 19:42:08 +010039import org.opencord.igmpproxy.IgmpStatisticsEvent;
40
Shubham Sharma47f2caf2020-02-18 12:13:40 +000041import com.google.common.collect.Lists;
developere400c582020-03-24 19:42:08 +010042import org.opencord.igmpproxy.IgmpStatisticsEventListener;
Shubham Sharma47f2caf2020-02-18 12:13:40 +000043
44/**
45 * Set of tests of the ONOS application component for IGMP Statistics.
46 */
47public 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 Ozdemir4c5947c2020-05-05 13:14:32 +000061 igmpManager.igmpLeadershipService = new TestIgmpLeaderShipService();
Shubham Sharma47f2caf2020-02-18 12:13:40 +000062 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 Ozdemir4c5947c2020-05-05 13:14:32 +000070 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 Sharma47f2caf2020-02-18 12:13:40 +000075 igmpStatisticsManager = new IgmpStatisticsManager();
76 igmpStatisticsManager.cfgService = new MockCfgService();
77 igmpStatisticsManager.addListener(mockListener);
78 TestUtils.setField(igmpStatisticsManager, "eventDispatcher", new TestEventDispatcher());
Ilayda Ozdemir0872abd2020-06-03 20:20:20 +030079 igmpStatisticsManager.storageService = new TestStorageService();
80 igmpStatisticsManager.leadershipManager = new TestIgmpLeaderShipService();
81 igmpStatisticsManager.clusterCommunicationService = new ClusterCommunicationServiceAdapter();
Shubham Sharma47f2caf2020-02-18 12:13:40 +000082 igmpStatisticsManager.activate(new MockComponentContext());
83 igmpManager.igmpStatisticsManager = this.igmpStatisticsManager;
84 // By default - we send query messages
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000085 StateMachineManager.sendQuery = true;
Shubham Sharma47f2caf2020-02-18 12:13:40 +000086 }
87
88 // Tear Down the IGMP application.
89 @After
90 public void tearDown() {
91 igmpStatisticsManager.removeListener(mockListener);
92 igmpStatisticsManager.deactivate();
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000093 igmpManager.stateMachineService.clearAllMaps();
Shubham Sharma47f2caf2020-02-18 12:13:40 +000094 }
95
96 //Test Igmp Statistics.
97 @Test
98 public void testIgmpStatistics() throws InterruptedException {
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +000099 StateMachineManager.sendQuery = false;
Shubham Sharma47f2caf2020-02-18 12:13:40 +0000100 igmpManager.networkConfig = new TestNetworkConfigRegistry(false);
101 igmpManager.activate();
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000102
Shubham Sharma47f2caf2020-02-18 12:13:40 +0000103 //IGMPv3 Join
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000104 flagForPacket = false;
Shubham Sharma47f2caf2020-02-18 12:13:40 +0000105 Ethernet igmpv3MembershipReportPkt = IgmpSender.getInstance().buildIgmpV3Join(GROUP_IP, SOURCE_IP_OF_A);
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000106 sendPacket(igmpv3MembershipReportPkt);
Shubham Sharma47f2caf2020-02-18 12:13:40 +0000107 synchronized (savedPackets) {
108 savedPackets.wait(WAIT_TIMEOUT);
109 }
110 //Leave
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000111 flagForPacket = false;
Shubham Sharma47f2caf2020-02-18 12:13:40 +0000112 Ethernet igmpv3LeavePkt = IgmpSender.getInstance().buildIgmpV3Leave(GROUP_IP, SOURCE_IP_OF_A);
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000113 sendPacket(igmpv3LeavePkt);
Shubham Sharma47f2caf2020-02-18 12:13:40 +0000114 synchronized (savedPackets) {
115 savedPackets.wait(WAIT_TIMEOUT);
116 }
117
118 assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () ->
Ilayda Ozdemir0872abd2020-06-03 20:20:20 +0300119 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 Sharma47f2caf2020-02-18 12:13:40 +0000138
139 }
140
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000141 //Test packet with Unknown Multicast IpAddress
142 @Test
143 public void testIgmpUnknownMulticastIpAddress() throws InterruptedException {
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +0000144 StateMachineManager.sendQuery = false;
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000145
146 igmpManager.networkConfig = new TestNetworkConfigRegistry(false);
147 igmpManager.activate();
148
149 Ethernet firstPacket =
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +0000150 IgmpSender.getInstance().buildIgmpV3Join(UNKNOWN_GRP_IP, SOURCE_IP_OF_A);
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000151 // Sending first packet
152 sendPacket(firstPacket);
153 assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () ->
Ilayda Ozdemir0872abd2020-06-03 20:20:20 +0300154 assertEquals((long) 1, igmpStatisticsManager
155 .getStat(IgmpStatisticType.FAIL_JOIN_REQ_UNKNOWN_MULTICAST_IP_COUNTER).longValue()));
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000156 }
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 Karaman586f1d62020-06-04 10:15:34 +0000166 Ethernet igmpv3MembershipQueryPkt = IgmpSender.getInstance().
167 buildIgmpV3Query(GROUP_IP, SOURCE_IP_OF_A, VlanId.MAX_VLAN);
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000168 sendPacket(igmpv3MembershipQueryPkt);
169
170 //IGMPV3 General Membership Query packet
171 Ethernet igmpv3MembershipQueryPkt1 =
Esin Karaman586f1d62020-06-04 10:15:34 +0000172 IgmpSender.getInstance().buildIgmpV3Query(Ip4Address.valueOf(0), SOURCE_IP_OF_A, VlanId.MAX_VLAN);
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000173 sendPacket(igmpv3MembershipQueryPkt1);
174 assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () ->
Ilayda Ozdemir0872abd2020-06-03 20:20:20 +0300175 assertEquals(igmpStatisticsManager
176 .getStat(IgmpStatisticType.IGMP_GRP_AND_SRC_SPESIFIC_MEMBERSHIP_QUERY).longValue(), 1));
177 assertEquals(igmpStatisticsManager
178 .getStat(IgmpStatisticType.IGMP_GENERAL_MEMBERSHIP_QUERY).longValue(), 1);
179 assertEquals(igmpStatisticsManager.getStat(IgmpStatisticType.CURRENT_GRP_NUMBER_COUNTER).longValue(), 1);
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000180 }
181
Shubham Sharma47f2caf2020-02-18 12:13:40 +0000182 //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 Ozdemir4c5947c2020-05-05 13:14:32 +0000188 assertEquals(mockListener.events.size(), 1));
Shubham Sharma47f2caf2020-02-18 12:13:40 +0000189
190 assertAfter(waitEventGeneration / 2, waitEventGeneration, () ->
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +0000191 assertEquals(mockListener.events.size(), 2));
Shubham Sharma47f2caf2020-02-18 12:13:40 +0000192
193 for (IgmpStatisticsEvent event : mockListener.events) {
194 assertEquals(event.type(), IgmpStatisticsEvent.Type.STATS_UPDATE);
195 }
196 }
197
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000198 //Test packet with Unknown Wrong Membership mode
199 @Test
200 public void testWrongIgmpPacket() throws InterruptedException {
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +0000201 StateMachineManager.sendQuery = false;
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000202
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 Ozdemir0872abd2020-06-03 20:20:20 +0300210 assertEquals((long) 1, igmpStatisticsManager
211 .getStat(IgmpStatisticType.REPORTS_RX_WITH_WRONG_MODE_COUNTER).longValue()));
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000212 }
213
214 //Test packet with Unknown IGMP type.
215 @Test
216 public void testUnknownIgmpPacket() throws InterruptedException {
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +0000217 StateMachineManager.sendQuery = false;
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000218
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 Ozdemir0872abd2020-06-03 20:20:20 +0300226 assertEquals((long) 1, igmpStatisticsManager
227 .getStat(IgmpStatisticType.UNKNOWN_IGMP_TYPE_PACKETS_RX_COUNTER).longValue()));
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000228 }
229
230 //Test packet with Insufficient Permission.
231 @Test
232 public void testSufficientPermission() throws InterruptedException {
Ilayda Ozdemir4c5947c2020-05-05 13:14:32 +0000233 StateMachineManager.sendQuery = false;
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000234
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 Ozdemir0872abd2020-06-03 20:20:20 +0300243 assertEquals((long) 1, igmpStatisticsManager
244 .getStat(IgmpStatisticType.FAIL_JOIN_REQ_INSUFF_PERMISSION_ACCESS_COUNTER).longValue()));
Sonal Kasliwalf11c0672020-03-18 11:11:50 +0000245 }
246
Shubham Sharma47f2caf2020-02-18 12:13:40 +0000247 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}