blob: e1e4f35d16da17bf1a9bbe35e3e0c39414d0e00c [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
17package org.opencord.igmpproxy;
18
19import static org.junit.Assert.assertEquals;
20import static org.onlab.junit.TestTools.assertAfter;
21
22import java.util.List;
23
24import org.junit.After;
25import org.junit.Before;
26import org.junit.Test;
27import org.onlab.junit.TestUtils;
28import org.onlab.packet.Ethernet;
29import org.onosproject.core.CoreServiceAdapter;
30import org.onosproject.net.flow.FlowRuleServiceAdapter;
31import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter;
32
33import com.google.common.collect.Lists;
34
35/**
36 * Set of tests of the ONOS application component for IGMP Statistics.
37 */
38public class IgmpStatisticsTest extends IgmpManagerBase {
39
40 private static final int WAIT_TIMEOUT = 500;
41
42 private IgmpManager igmpManager;
43
44 private IgmpStatisticsManager igmpStatisticsManager;
45
46 private MockIgmpStatisticsEventListener mockListener = new MockIgmpStatisticsEventListener();
47
48 // Set up the IGMP application.
49 @Before
50 public void setUp() {
51 igmpManager = new IgmpManager();
52 igmpManager.coreService = new CoreServiceAdapter();
53 igmpManager.mastershipService = new MockMastershipService();
54 igmpManager.flowObjectiveService = new FlowObjectiveServiceAdapter();
55 igmpManager.deviceService = new MockDeviceService();
56 igmpManager.packetService = new MockPacketService();
57 igmpManager.flowRuleService = new FlowRuleServiceAdapter();
58 igmpManager.multicastService = new TestMulticastRouteService();
59 igmpManager.sadisService = new MockSadisService();
60 igmpStatisticsManager = new IgmpStatisticsManager();
61 igmpStatisticsManager.cfgService = new MockCfgService();
62 igmpStatisticsManager.addListener(mockListener);
63 TestUtils.setField(igmpStatisticsManager, "eventDispatcher", new TestEventDispatcher());
64 igmpStatisticsManager.activate(new MockComponentContext());
65 igmpManager.igmpStatisticsManager = this.igmpStatisticsManager;
66 // By default - we send query messages
67 SingleStateMachine.sendQuery = true;
68 }
69
70 // Tear Down the IGMP application.
71 @After
72 public void tearDown() {
73 igmpStatisticsManager.removeListener(mockListener);
74 igmpStatisticsManager.deactivate();
75 IgmpManager.groupMemberMap.clear();
76 StateMachine.clearMap();
77 }
78
79 //Test Igmp Statistics.
80 @Test
81 public void testIgmpStatistics() throws InterruptedException {
82 igmpManager.networkConfig = new TestNetworkConfigRegistry(false);
83 igmpManager.activate();
84 //IGMPv3 Join
85 Ethernet igmpv3MembershipReportPkt = IgmpSender.getInstance().buildIgmpV3Join(GROUP_IP, SOURCE_IP_OF_A);
86 sendPacket(igmpv3MembershipReportPkt, true);
87 synchronized (savedPackets) {
88 savedPackets.wait(WAIT_TIMEOUT);
89 }
90 //Leave
91 Ethernet igmpv3LeavePkt = IgmpSender.getInstance().buildIgmpV3Leave(GROUP_IP, SOURCE_IP_OF_A);
92 sendPacket(igmpv3LeavePkt, true);
93 synchronized (savedPackets) {
94 savedPackets.wait(WAIT_TIMEOUT);
95 }
96
97 assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () ->
98 assertEquals((long) 2, igmpStatisticsManager.getIgmpStats().getTotalMsgReceived().longValue()));
99 assertEquals((long) 1, igmpStatisticsManager.getIgmpStats().getIgmpJoinReq().longValue());
100 assertEquals((long) 2, igmpStatisticsManager.getIgmpStats().getIgmpv3MembershipReport().longValue());
101 assertEquals((long) 1, igmpStatisticsManager.getIgmpStats().getIgmpSuccessJoinRejoinReq().longValue());
102
103 assertEquals((long) 1, igmpStatisticsManager.getIgmpStats().getIgmpLeaveReq().longValue());
104 assertEquals((long) 2, igmpStatisticsManager.getIgmpStats().getIgmpMsgReceived().longValue());
105
106 }
107
108 //Test Events
109 @Test
110 public void testIgmpStatisticsEvent() {
111 final int waitEventGeneration = igmpStatisticsManager.statisticsGenerationPeriodInSeconds * 1000;
112 //assert that event listened as the app activates
113 assertAfter(WAIT_TIMEOUT, WAIT_TIMEOUT * 2, () ->
114 assertEquals(mockListener.events.size(), 1));
115
116 assertAfter(waitEventGeneration / 2, waitEventGeneration, () ->
117 assertEquals(mockListener.events.size(), 2));
118
119 for (IgmpStatisticsEvent event : mockListener.events) {
120 assertEquals(event.type(), IgmpStatisticsEvent.Type.STATS_UPDATE);
121 }
122 }
123
124 public class MockIgmpStatisticsEventListener implements IgmpStatisticsEventListener {
125 protected List<IgmpStatisticsEvent> events = Lists.newArrayList();
126
127 @Override
128 public void event(IgmpStatisticsEvent event) {
129 events.add(event);
130 }
131
132 }
133}