Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 1 | /* |
| 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 | package org.opencord.dhcpl2relay.impl; |
| 17 | |
| 18 | import org.easymock.EasyMock; |
| 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.junit.TestUtils; |
| 23 | import org.onlab.osgi.ComponentContextAdapter; |
| 24 | import org.onosproject.cfg.ComponentConfigService; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 25 | import org.onosproject.cluster.ClusterServiceAdapter; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 26 | import org.onosproject.cluster.LeadershipServiceAdapter; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 27 | import org.onosproject.cluster.NodeId; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 28 | import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 29 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 30 | import org.onosproject.store.cluster.messaging.ClusterMessageHandler; |
| 31 | import org.onosproject.store.cluster.messaging.MessageSubject; |
Jonathan Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 32 | import org.onosproject.store.service.TestStorageService; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 33 | import org.opencord.dhcpl2relay.DhcpL2RelayEvent; |
| 34 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 35 | import java.time.Duration; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 36 | import java.util.Iterator; |
| 37 | import java.util.Map; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 38 | import java.util.Set; |
| 39 | import java.util.concurrent.CompletableFuture; |
| 40 | import java.util.concurrent.Executor; |
| 41 | import java.util.concurrent.ExecutorService; |
| 42 | import java.util.function.Consumer; |
| 43 | import java.util.function.Function; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 44 | |
| 45 | import static org.junit.Assert.assertEquals; |
| 46 | |
| 47 | |
| 48 | public 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 Hart | 617bc3e | 2020-02-14 10:42:23 -0800 | [diff] [blame] | 70 | dhcpL2Relay.storageService = new TestStorageService(); |
| 71 | dhcpL2Relay.leadershipService = new LeadershipServiceAdapter(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 72 | TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher()); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 73 | store = new SimpleDhcpL2RelayCountersStore(); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 74 | 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 Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 79 | TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher()); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 80 | store.activate(new MockComponentContext()); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 81 | dhcpL2Relay.dhcpL2RelayCounters = this.store; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 82 | dhcpL2Relay.activate(new ComponentContextAdapter()); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 83 | } |
| 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 Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 99 | store.initCounters(DhcpL2RelayEvent.GLOBAL_COUNTER, new DhcpL2RelayStatistics()); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 100 | // Init the supported counter for a specific subscriber |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 101 | store.initCounters(CLIENT_ID_1, new DhcpL2RelayStatistics()); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 102 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 103 | Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap(); |
| 104 | for (Iterator<DhcpL2RelayCounterNames> it = DhcpL2RelayCounterNames.SUPPORTED_COUNTERS.iterator(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 105 | it.hasNext();) { |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 106 | DhcpL2RelayCounterNames counterType = it.next(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 107 | long globalCounterValue = countersMap.get(new DhcpL2RelayCountersIdentifier( |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 108 | DhcpL2RelayEvent.GLOBAL_COUNTER, counterType)); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 109 | long perSubscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 110 | counterType)); |
| 111 | assertEquals(0, globalCounterValue); |
| 112 | assertEquals(0, perSubscriberValue); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 113 | } |
| 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 Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 123 | store.initCounters(DhcpL2RelayEvent.GLOBAL_COUNTER, new DhcpL2RelayStatistics()); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 124 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 125 | for (Iterator<DhcpL2RelayCounterNames> it = DhcpL2RelayCounterNames.SUPPORTED_COUNTERS.iterator(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 126 | it.hasNext();) { |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 127 | DhcpL2RelayCounterNames counterType = it.next(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 128 | // 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 Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 134 | Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap(); |
| 135 | for (Iterator<DhcpL2RelayCounterNames> it = DhcpL2RelayCounterNames.SUPPORTED_COUNTERS.iterator(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 136 | it.hasNext();) { |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 137 | DhcpL2RelayCounterNames counterType = it.next(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 138 | long globalCounterValue = countersMap.get(new DhcpL2RelayCountersIdentifier( |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 139 | DhcpL2RelayEvent.GLOBAL_COUNTER, counterType)); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 140 | long perSubscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 141 | counterType)); |
| 142 | assertEquals(1, globalCounterValue); |
| 143 | assertEquals(1, perSubscriberValue); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 144 | } |
| 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 Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 152 | DhcpL2RelayCounterNames counterType; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 153 | long subscriberValue; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 154 | Map<DhcpL2RelayCountersIdentifier, Long> countersMap; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 155 | |
| 156 | // First start incrementing the counter of a specific subscriber |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 157 | for (Iterator<DhcpL2RelayCounterNames> it = DhcpL2RelayCounterNames.SUPPORTED_COUNTERS.iterator(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 158 | 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 Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 165 | countersMap = store.getCountersMap(); |
| 166 | for (Iterator<DhcpL2RelayCounterNames> it = DhcpL2RelayCounterNames.SUPPORTED_COUNTERS.iterator(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 167 | it.hasNext();) { |
| 168 | counterType = it.next(); |
| 169 | subscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 170 | counterType)); |
| 171 | assertEquals(1, subscriberValue); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | // Reset the counter |
| 175 | dhcpL2Relay.dhcpL2RelayCounters.resetCounters(CLIENT_ID_1); |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 176 | countersMap = store.getCountersMap(); |
| 177 | for (Iterator<DhcpL2RelayCounterNames> it = DhcpL2RelayCounterNames.SUPPORTED_COUNTERS.iterator(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 178 | it.hasNext();) { |
| 179 | counterType = it.next(); |
| 180 | subscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1, |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 181 | counterType)); |
| 182 | assertEquals(0, subscriberValue); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 183 | } |
| 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 Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 192 | dhcpL2Relay.dhcpL2RelayCounters.setCounter(CLIENT_ID_1, |
| 193 | DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER"), (long) 50); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 194 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 195 | Map<DhcpL2RelayCountersIdentifier, Long> countersMap = store.getCountersMap(); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 196 | long subscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier( |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 197 | CLIENT_ID_1, DhcpL2RelayCounterNames.valueOf("DHCPDISCOVER"))); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 198 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame] | 199 | 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 Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | } |