kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 1 | /* |
Joey Armstrong | 89c55e0 | 2023-01-09 18:09:41 -0500 | [diff] [blame^] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [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 | |
| 17 | package org.opencord.kafka.integrations; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import org.junit.jupiter.api.AfterEach; |
| 22 | import org.junit.jupiter.api.BeforeEach; |
| 23 | import org.junit.jupiter.api.Test; |
| 24 | import org.onlab.packet.DHCP; |
| 25 | import org.onosproject.cluster.ClusterServiceAdapter; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.opencord.dhcpl2relay.DhcpAllocationInfo; |
| 28 | import org.opencord.dhcpl2relay.DhcpL2RelayEvent; |
| 29 | import org.opencord.dhcpl2relay.DhcpL2RelayListener; |
| 30 | import org.opencord.dhcpl2relay.DhcpL2RelayService; |
| 31 | import org.opencord.kafka.EventBusService; |
| 32 | |
| 33 | import java.util.Map; |
| 34 | import java.util.concurrent.atomic.AtomicLong; |
| 35 | |
| 36 | import static org.junit.Assert.assertEquals; |
| 37 | import static org.opencord.kafka.integrations.MockDeviceService.DEVICE_ID_1; |
| 38 | |
| 39 | /** |
| 40 | * set of unit test cases for DhcpL2RelayKafkaIntegration. |
| 41 | */ |
| 42 | class DhcpL2RelayKafkaIntegrationTest extends KafkaIntegrationTestBase { |
| 43 | |
| 44 | private static final String DHCP_COUNTER_TOPIC = "DHCPREQUEST"; |
| 45 | private DhcpL2RelayKafkaIntegration dhcpL2RelayKafkaIntegration; |
| 46 | private DhcpL2RelayListener dhcpL2RelayListener; |
| 47 | |
| 48 | @BeforeEach |
| 49 | void setUp() { |
| 50 | dhcpL2RelayKafkaIntegration = new DhcpL2RelayKafkaIntegration(); |
| 51 | dhcpL2RelayKafkaIntegration.clusterService = new ClusterServiceAdapter(); |
| 52 | dhcpL2RelayKafkaIntegration.deviceService = new MockDeviceService(); |
| 53 | dhcpL2RelayKafkaIntegration.eventBusService = new MockEventBusService(); |
| 54 | dhcpL2RelayKafkaIntegration.ignore = new MockDhcpL2RelayService(); |
| 55 | dhcpL2RelayKafkaIntegration.bindDhcpL2RelayService(dhcpL2RelayKafkaIntegration.ignore); |
| 56 | dhcpL2RelayKafkaIntegration.activate(); |
| 57 | } |
| 58 | |
| 59 | @AfterEach |
| 60 | void tearDown() { |
| 61 | dhcpL2RelayKafkaIntegration.deactivate(); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | void testDhcpL2RelayStatsUpdate() { |
| 66 | Map.Entry<String, AtomicLong> entryCounter = Maps.immutableEntry(DHCP_COUNTER_TOPIC, |
| 67 | new AtomicLong(1)); |
| 68 | DhcpAllocationInfo allocationInfo = new DhcpAllocationInfo( |
| 69 | OLT_CONNECT_POINT, |
| 70 | DHCP.MsgType.DHCPREQUEST, ONU_SERIAL, OLT_MAC, |
Matteo Scandolo | bd1d3f7 | 2020-09-11 13:26:53 -0700 | [diff] [blame] | 71 | LOCAL_IP, C_TAG, SUBSCRIBER_ID); |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 72 | DhcpL2RelayEvent event = new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.STATS_UPDATE, |
Andrea Campanella | bbb3659 | 2020-07-01 16:50:48 +0200 | [diff] [blame] | 73 | allocationInfo, OLT_CONNECT_POINT, entryCounter, null); |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 74 | dhcpL2RelayListener.event(event); |
| 75 | assertEquals(MockEventBusService.dhcpStats, 1); |
| 76 | assertEquals(MockEventBusService.otherCounter, 0); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * test to verify the DhcpL2RelayEvent.STATS_UPDATE event. |
| 81 | */ |
| 82 | @Test |
| 83 | void testDhcpL2RelayStatsSubscriberUpdate() { |
| 84 | Map.Entry<String, AtomicLong> entryCounter = Maps.immutableEntry(DHCP_COUNTER_TOPIC, new AtomicLong(1)); |
| 85 | |
| 86 | DhcpAllocationInfo allocationInfo = new DhcpAllocationInfo( |
| 87 | OLT_CONNECT_POINT, |
| 88 | DHCP.MsgType.DHCPREQUEST, ONU_SERIAL, OLT_MAC, |
Matteo Scandolo | bd1d3f7 | 2020-09-11 13:26:53 -0700 | [diff] [blame] | 89 | LOCAL_IP, C_TAG, SUBSCRIBER_ID); |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 90 | DhcpL2RelayEvent event = new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.STATS_UPDATE, |
Andrea Campanella | bbb3659 | 2020-07-01 16:50:48 +0200 | [diff] [blame] | 91 | allocationInfo, OLT_CONNECT_POINT, entryCounter, ONU_SERIAL); |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 92 | dhcpL2RelayListener.event(event); |
| 93 | assertEquals(MockEventBusService.dhcpStats, 1); |
| 94 | assertEquals(MockEventBusService.otherCounter, 0); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * test to verify the DhcpL2RelayEvent.UPDATE event. |
| 99 | */ |
| 100 | @Test |
| 101 | void testDhcpL2RelayUpdate() { |
| 102 | ConnectPoint cp = new ConnectPoint(DEVICE_ID_1, PORT.number()); |
| 103 | Map.Entry<String, AtomicLong> entryCounter = Maps.immutableEntry(DHCP_COUNTER_TOPIC, new AtomicLong(1)); |
| 104 | DhcpAllocationInfo allocationInfo = new DhcpAllocationInfo( |
| 105 | new ConnectPoint(DEVICE_ID_1, PORT.number()), |
| 106 | DHCP.MsgType.DHCPREQUEST, ONU_SERIAL, OLT_MAC, |
Matteo Scandolo | bd1d3f7 | 2020-09-11 13:26:53 -0700 | [diff] [blame] | 107 | LOCAL_IP, C_TAG, SUBSCRIBER_ID); |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 108 | DhcpL2RelayEvent event = new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED, |
Andrea Campanella | bbb3659 | 2020-07-01 16:50:48 +0200 | [diff] [blame] | 109 | allocationInfo, cp, entryCounter, null); |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 110 | dhcpL2RelayListener.event(event); |
| 111 | assertEquals(MockEventBusService.dhcpEvents, 1); |
| 112 | assertEquals(MockEventBusService.otherCounter, 0); |
| 113 | } |
| 114 | |
| 115 | private static class MockEventBusService implements EventBusService { |
| 116 | static int dhcpStats; |
| 117 | static int dhcpEvents; |
| 118 | static int otherCounter; |
| 119 | |
| 120 | MockEventBusService() { |
| 121 | dhcpStats = 0; |
| 122 | dhcpEvents = 0; |
| 123 | otherCounter = 0; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void send(String topic, JsonNode data) { |
| 128 | switch (topic) { |
| 129 | case DhcpL2RelayKafkaIntegration.DHCP_STATS_TOPIC: |
| 130 | dhcpStats++; |
| 131 | break; |
| 132 | case DhcpL2RelayKafkaIntegration.TOPIC: |
| 133 | dhcpEvents++; |
| 134 | break; |
| 135 | default: |
| 136 | otherCounter++; |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | private class MockDhcpL2RelayService implements DhcpL2RelayService { |
| 143 | @Override |
| 144 | public void addListener(DhcpL2RelayListener listener) { |
| 145 | dhcpL2RelayListener = listener; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public void removeListener(DhcpL2RelayListener listener) { |
| 150 | dhcpL2RelayListener = null; |
| 151 | } |
Andrea Campanella | bbb3659 | 2020-07-01 16:50:48 +0200 | [diff] [blame] | 152 | |
| 153 | @Override |
| 154 | public Map<String, DhcpAllocationInfo> getAllocationInfo() { |
| 155 | return null; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void clearAllocations() { |
| 160 | |
| 161 | } |
| 162 | |
| 163 | @Override |
Matteo Scandolo | 5e406d2 | 2020-08-24 15:17:54 -0700 | [diff] [blame] | 164 | public boolean removeAllocationsByConnectPoint(ConnectPoint connectPoint) { |
Andrea Campanella | bbb3659 | 2020-07-01 16:50:48 +0200 | [diff] [blame] | 165 | return false; |
| 166 | } |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 167 | } |
| 168 | } |