blob: fe45d40914b6cd8718c35965d047e9568b2ad025 [file] [log] [blame]
kishoreefa3db52020-03-23 16:09:26 +05301/*
2 * Copyright 2018-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.kafka.integrations;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.google.common.collect.Maps;
21import org.junit.jupiter.api.AfterEach;
22import org.junit.jupiter.api.BeforeEach;
23import org.junit.jupiter.api.Test;
24import org.onlab.packet.DHCP;
25import org.onosproject.cluster.ClusterServiceAdapter;
26import org.onosproject.net.ConnectPoint;
27import org.opencord.dhcpl2relay.DhcpAllocationInfo;
28import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
29import org.opencord.dhcpl2relay.DhcpL2RelayListener;
30import org.opencord.dhcpl2relay.DhcpL2RelayService;
31import org.opencord.kafka.EventBusService;
32
33import java.util.Map;
34import java.util.concurrent.atomic.AtomicLong;
35
36import static org.junit.Assert.assertEquals;
37import static org.opencord.kafka.integrations.MockDeviceService.DEVICE_ID_1;
38
39/**
40 * set of unit test cases for DhcpL2RelayKafkaIntegration.
41 */
42class 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,
71 LOCAL_IP);
72 DhcpL2RelayEvent event = new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.STATS_UPDATE,
Andrea Campanellabbb36592020-07-01 16:50:48 +020073 allocationInfo, OLT_CONNECT_POINT, entryCounter, null);
kishoreefa3db52020-03-23 16:09:26 +053074 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,
89 LOCAL_IP);
90 DhcpL2RelayEvent event = new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.STATS_UPDATE,
Andrea Campanellabbb36592020-07-01 16:50:48 +020091 allocationInfo, OLT_CONNECT_POINT, entryCounter, ONU_SERIAL);
kishoreefa3db52020-03-23 16:09:26 +053092 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,
107 LOCAL_IP);
108 DhcpL2RelayEvent event = new DhcpL2RelayEvent(DhcpL2RelayEvent.Type.UPDATED,
Andrea Campanellabbb36592020-07-01 16:50:48 +0200109 allocationInfo, cp, entryCounter, null);
kishoreefa3db52020-03-23 16:09:26 +0530110 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 Campanellabbb36592020-07-01 16:50:48 +0200152
153 @Override
154 public Map<String, DhcpAllocationInfo> getAllocationInfo() {
155 return null;
156 }
157
158 @Override
159 public void clearAllocations() {
160
161 }
162
163 @Override
164 public boolean removeAllocationByConnectPoint(ConnectPoint connectPoint) {
165 return false;
166 }
kishoreefa3db52020-03-23 16:09:26 +0530167 }
168}