blob: 47d0f93184407cf7581672bc1173934468220e8e [file] [log] [blame]
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -03001/*
Joey Armstrong7e08d2a2022-12-30 12:25:42 -05002 * Copyright 2017-2023 Open Networking Foundation (ONF) and the ONF Contributors
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -03003 *
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 */
16package org.opencord.dhcpl2relay.impl;
17
18import org.easymock.EasyMock;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.junit.TestUtils;
23import org.onlab.osgi.ComponentContextAdapter;
24import org.onosproject.cfg.ComponentConfigService;
Jonathan Hart77ca3152020-02-21 14:31:21 -080025import org.onosproject.cluster.ClusterServiceAdapter;
Jonathan Hart617bc3e2020-02-14 10:42:23 -080026import org.onosproject.cluster.LeadershipServiceAdapter;
Jonathan Hart77ca3152020-02-21 14:31:21 -080027import org.onosproject.cluster.NodeId;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030028import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter;
Jonathan Hart77ca3152020-02-21 14:31:21 -080029import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
30import org.onosproject.store.cluster.messaging.ClusterMessageHandler;
31import org.onosproject.store.cluster.messaging.MessageSubject;
Jonathan Hart617bc3e2020-02-14 10:42:23 -080032import org.onosproject.store.service.TestStorageService;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030033import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
34
Jonathan Hart77ca3152020-02-21 14:31:21 -080035import java.time.Duration;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030036import java.util.Iterator;
37import java.util.Map;
Jonathan Hart77ca3152020-02-21 14:31:21 -080038import java.util.Set;
39import java.util.concurrent.CompletableFuture;
40import java.util.concurrent.Executor;
41import java.util.concurrent.ExecutorService;
42import java.util.function.Consumer;
43import java.util.function.Function;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030044
45import static org.junit.Assert.assertEquals;
46
47
48public class DhcpL2RelayCountersStoreTest extends DhcpL2RelayTestBase {
49
50 private DhcpL2Relay dhcpL2Relay;
51 private SimpleDhcpL2RelayCountersStore store;
52
53 ComponentConfigService mockConfigService =
54 EasyMock.createMock(ComponentConfigService.class);
55
56 /**
57 * Sets up the services required by the dhcpl2relay app.
58 */
59 @Before
60 public void setUp() {
61 dhcpL2Relay = new DhcpL2Relay();
62 dhcpL2Relay.cfgService = new DhcpL2RelayConfigTest.TestNetworkConfigRegistry();
63 dhcpL2Relay.coreService = new MockCoreServiceAdapter();
64 dhcpL2Relay.flowObjectiveService = new FlowObjectiveServiceAdapter();
65 dhcpL2Relay.packetService = new MockPacketService();
66 dhcpL2Relay.componentConfigService = mockConfigService;
67 dhcpL2Relay.deviceService = new MockDeviceService();
68 dhcpL2Relay.sadisService = new MockSadisService();
69 dhcpL2Relay.mastershipService = new MockMastershipService();
Jonathan Hart617bc3e2020-02-14 10:42:23 -080070 dhcpL2Relay.storageService = new TestStorageService();
71 dhcpL2Relay.leadershipService = new LeadershipServiceAdapter();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030072 TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher());
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030073 store = new SimpleDhcpL2RelayCountersStore();
Jonathan Hart77ca3152020-02-21 14:31:21 -080074 store.storageService = new TestStorageService();
75 store.clusterService = new ClusterServiceAdapter();
76 store.leadershipService = new LeadershipServiceAdapter();
77 store.clusterCommunicationService = new TestClusterCommunicationService<>();
78 store.componentConfigService = mockConfigService;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030079 TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher());
Jonathan Hart77ca3152020-02-21 14:31:21 -080080 store.activate(new MockComponentContext());
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030081 dhcpL2Relay.dhcpL2RelayCounters = this.store;
Jonathan Hart77ca3152020-02-21 14:31:21 -080082 dhcpL2Relay.activate(new ComponentContextAdapter());
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030083 }
84
85 /**
86 * Tears down the dhcpL2Relay application.
87 */
88 @After
89 public void tearDown() {
90 dhcpL2Relay.deactivate();
91 }
92
93 /**
94 * Tests the initialization of the counter.
95 */
96 @Test
97 public void testInitCounter() {
98 // Init the supported global counter
Jonathan Hart77ca3152020-02-21 14:31:21 -080099 store.initCounters(DhcpL2RelayEvent.GLOBAL_COUNTER, new DhcpL2RelayStatistics());
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300100 // Init the supported counter for a specific subscriber
Jonathan Hart77ca3152020-02-21 14:31:21 -0800101 store.initCounters(CLIENT_ID_1, new DhcpL2RelayStatistics());
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300102
Jonathan Hart77ca3152020-02-21 14:31:21 -0800103 Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap();
104 for (Iterator<DhcpL2RelayCounterNames> it = DhcpL2RelayCounterNames.SUPPORTED_COUNTERS.iterator();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300105 it.hasNext();) {
Jonathan Hart77ca3152020-02-21 14:31:21 -0800106 DhcpL2RelayCounterNames counterType = it.next();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300107 long globalCounterValue = countersMap.get(new DhcpL2RelayCountersIdentifier(
Jonathan Hart77ca3152020-02-21 14:31:21 -0800108 DhcpL2RelayEvent.GLOBAL_COUNTER, counterType));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300109 long perSubscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800110 counterType));
111 assertEquals(0, globalCounterValue);
112 assertEquals(0, perSubscriberValue);
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300113 }
114 }
115
116 /**
117 * Tests the insertion of the counter entry if it is not already in the set
118 * otherwise increment the existing counter entry.
119 */
120 @Test
121 public void testIncrementCounter() {
122 // Init the supported global counter
Jonathan Hart77ca3152020-02-21 14:31:21 -0800123 store.initCounters(DhcpL2RelayEvent.GLOBAL_COUNTER, new DhcpL2RelayStatistics());
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300124
Jonathan Hart77ca3152020-02-21 14:31:21 -0800125 for (Iterator<DhcpL2RelayCounterNames> it = DhcpL2RelayCounterNames.SUPPORTED_COUNTERS.iterator();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300126 it.hasNext();) {
Jonathan Hart77ca3152020-02-21 14:31:21 -0800127 DhcpL2RelayCounterNames counterType = it.next();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300128 // Increment of existing supported global counter
129 dhcpL2Relay.dhcpL2RelayCounters.incrementCounter(DhcpL2RelayEvent.GLOBAL_COUNTER, counterType);
130 // Add of a Subscriber entry that is not already in the set
131 dhcpL2Relay.dhcpL2RelayCounters.incrementCounter(CLIENT_ID_1, counterType);
132 }
133
Jonathan Hart77ca3152020-02-21 14:31:21 -0800134 Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap();
135 for (Iterator<DhcpL2RelayCounterNames> it = DhcpL2RelayCounterNames.SUPPORTED_COUNTERS.iterator();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300136 it.hasNext();) {
Jonathan Hart77ca3152020-02-21 14:31:21 -0800137 DhcpL2RelayCounterNames counterType = it.next();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300138 long globalCounterValue = countersMap.get(new DhcpL2RelayCountersIdentifier(
Jonathan Hart77ca3152020-02-21 14:31:21 -0800139 DhcpL2RelayEvent.GLOBAL_COUNTER, counterType));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300140 long perSubscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800141 counterType));
142 assertEquals(1, globalCounterValue);
143 assertEquals(1, perSubscriberValue);
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300144 }
145 }
146
147 /**
148 * Tests the increment and reset functions of the counters map for the given counter class.
149 */
150 @Test
151 public void testIncrementAndResetCounter() {
Jonathan Hart77ca3152020-02-21 14:31:21 -0800152 DhcpL2RelayCounterNames counterType;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300153 long subscriberValue;
Jonathan Hart77ca3152020-02-21 14:31:21 -0800154 Map<DhcpL2RelayCountersIdentifier, Long> countersMap;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300155
156 // First start incrementing the counter of a specific subscriber
Jonathan Hart77ca3152020-02-21 14:31:21 -0800157 for (Iterator<DhcpL2RelayCounterNames> it = DhcpL2RelayCounterNames.SUPPORTED_COUNTERS.iterator();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300158 it.hasNext();) {
159 counterType = it.next();
160 // Insert of a Subscriber entry that is not already in the set
161 dhcpL2Relay.dhcpL2RelayCounters.incrementCounter(CLIENT_ID_1, counterType);
162 }
163
164 // Make sure that the counter is incremented
Jonathan Hart77ca3152020-02-21 14:31:21 -0800165 countersMap = store.getCountersMap();
166 for (Iterator<DhcpL2RelayCounterNames> it = DhcpL2RelayCounterNames.SUPPORTED_COUNTERS.iterator();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300167 it.hasNext();) {
168 counterType = it.next();
169 subscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800170 counterType));
171 assertEquals(1, subscriberValue);
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300172 }
173
174 // Reset the counter
175 dhcpL2Relay.dhcpL2RelayCounters.resetCounters(CLIENT_ID_1);
Jonathan Hart77ca3152020-02-21 14:31:21 -0800176 countersMap = store.getCountersMap();
177 for (Iterator<DhcpL2RelayCounterNames> it = DhcpL2RelayCounterNames.SUPPORTED_COUNTERS.iterator();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300178 it.hasNext();) {
179 counterType = it.next();
180 subscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
Jonathan Hart77ca3152020-02-21 14:31:21 -0800181 counterType));
182 assertEquals(0, subscriberValue);
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300183 }
184 }
185
186 /**
187 * Tests the insert of the counter value for a subscriber entry if it is not already in the set
188 * otherwise update the existing counter entry.
189 */
190 @Test
191 public void testInsertOrUpdateCounter() {
Jonathan Hart77ca3152020-02-21 14:31:21 -0800192 dhcpL2Relay.dhcpL2RelayCounters.setCounter(CLIENT_ID_1,
193 DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER"), (long) 50);
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300194
Jonathan Hart77ca3152020-02-21 14:31:21 -0800195 Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300196 long subscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier(
Jonathan Hart77ca3152020-02-21 14:31:21 -0800197 CLIENT_ID_1, DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER")));
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300198
Jonathan Hart77ca3152020-02-21 14:31:21 -0800199 assertEquals(50, subscriberValue);
200 }
201
202 public class TestClusterCommunicationService<M> implements ClusterCommunicationService {
203
204 private Consumer handler;
205
206 @Override
207 public void addSubscriber(MessageSubject subject,
208 ClusterMessageHandler subscriber, ExecutorService executor) {
209
210 }
211
212 @Override
213 public <M> void broadcast(M message, MessageSubject subject, Function<M, byte[]> encoder) {
214
215 }
216
217 @Override
218 public <M> void broadcastIncludeSelf(M message, MessageSubject subject, Function<M, byte[]> encoder) {
219 handler.accept(message);
220 }
221
222 @Override
223 public <M> CompletableFuture<Void> unicast(M message, MessageSubject subject,
224 Function<M, byte[]> encoder, NodeId toNodeId) {
225 return null;
226 }
227
228 @Override
229 public <M> void multicast(M message, MessageSubject subject,
230 Function<M, byte[]> encoder, Set<NodeId> nodeIds) {
231
232 }
233
234 @Override
235 public <M, R> CompletableFuture<R> sendAndReceive(M message, MessageSubject subject,
236 Function<M, byte[]> encoder,
237 Function<byte[], R> decoder, NodeId toNodeId) {
238 return null;
239 }
240
241 @Override
242 public <M, R> CompletableFuture<R> sendAndReceive(M message, MessageSubject subject,
243 Function<M, byte[]> encoder, Function<byte[], R> decoder,
244 NodeId toNodeId, Duration timeout) {
245 return null;
246 }
247
248 @Override
249 public <M, R> void addSubscriber(MessageSubject subject, Function<byte[], M> decoder,
250 Function<M, R> handler, Function<R, byte[]> encoder, Executor executor) {
251
252 }
253
254 @Override
255 public <M, R> void addSubscriber(MessageSubject subject, Function<byte[], M> decoder,
256 Function<M, CompletableFuture<R>> handler, Function<R, byte[]> encoder) {
257
258 }
259
260 @Override
261 public <M> void addSubscriber(MessageSubject subject, Function<byte[], M> decoder,
262 Consumer<M> handler, Executor executor) {
263 this.handler = handler;
264 }
265
266 @Override
267 public void removeSubscriber(MessageSubject subject) {
268
269 }
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300270 }
271
272}