blob: 30f651f2c532d3a058932c4fdeab126520a4922c [file] [log] [blame]
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -03001/*
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 */
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 Hart617bc3e2020-02-14 10:42:23 -080025import org.onosproject.cluster.LeadershipServiceAdapter;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030026import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter;
Jonathan Hart617bc3e2020-02-14 10:42:23 -080027import org.onosproject.store.service.TestStorageService;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030028import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
29
30import java.util.Iterator;
31import java.util.Map;
32import java.util.concurrent.atomic.AtomicLong;
33
34import static org.junit.Assert.assertEquals;
35
36
37public class DhcpL2RelayCountersStoreTest extends DhcpL2RelayTestBase {
38
39 private DhcpL2Relay dhcpL2Relay;
40 private SimpleDhcpL2RelayCountersStore store;
41
42 ComponentConfigService mockConfigService =
43 EasyMock.createMock(ComponentConfigService.class);
44
45 /**
46 * Sets up the services required by the dhcpl2relay app.
47 */
48 @Before
49 public void setUp() {
50 dhcpL2Relay = new DhcpL2Relay();
51 dhcpL2Relay.cfgService = new DhcpL2RelayConfigTest.TestNetworkConfigRegistry();
52 dhcpL2Relay.coreService = new MockCoreServiceAdapter();
53 dhcpL2Relay.flowObjectiveService = new FlowObjectiveServiceAdapter();
54 dhcpL2Relay.packetService = new MockPacketService();
55 dhcpL2Relay.componentConfigService = mockConfigService;
56 dhcpL2Relay.deviceService = new MockDeviceService();
57 dhcpL2Relay.sadisService = new MockSadisService();
58 dhcpL2Relay.mastershipService = new MockMastershipService();
Jonathan Hart617bc3e2020-02-14 10:42:23 -080059 dhcpL2Relay.storageService = new TestStorageService();
60 dhcpL2Relay.leadershipService = new LeadershipServiceAdapter();
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030061 TestUtils.setField(dhcpL2Relay, "eventDispatcher", new TestEventDispatcher());
62 dhcpL2Relay.activate(new ComponentContextAdapter());
63 store = new SimpleDhcpL2RelayCountersStore();
64 TestUtils.setField(store, "eventDispatcher", new TestEventDispatcher());
65 store.activate();
66 dhcpL2Relay.dhcpL2RelayCounters = this.store;
67 }
68
69 /**
70 * Tears down the dhcpL2Relay application.
71 */
72 @After
73 public void tearDown() {
74 dhcpL2Relay.deactivate();
75 }
76
77 /**
78 * Tests the initialization of the counter.
79 */
80 @Test
81 public void testInitCounter() {
82 // Init the supported global counter
83 dhcpL2Relay.dhcpL2RelayCounters.initCounters(DhcpL2RelayEvent.GLOBAL_COUNTER);
84 // Init the supported counter for a specific subscriber
85 dhcpL2Relay.dhcpL2RelayCounters.initCounters(CLIENT_ID_1);
86
87 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
88 for (Iterator<DhcpL2RelayCounters> it = DhcpL2RelayCounters.SUPPORTED_COUNTERS.iterator();
89 it.hasNext();) {
90 DhcpL2RelayCounters counterType = it.next();
91 long globalCounterValue = countersMap.get(new DhcpL2RelayCountersIdentifier(
92 DhcpL2RelayEvent.GLOBAL_COUNTER, counterType)).longValue();
93 long perSubscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
94 counterType)).longValue();
95 assertEquals((long) 0, globalCounterValue);
96 assertEquals((long) 0, perSubscriberValue);
97 }
98 }
99
100 /**
101 * Tests the insertion of the counter entry if it is not already in the set
102 * otherwise increment the existing counter entry.
103 */
104 @Test
105 public void testIncrementCounter() {
106 // Init the supported global counter
107 dhcpL2Relay.dhcpL2RelayCounters.initCounters(DhcpL2RelayEvent.GLOBAL_COUNTER);
108
109 for (Iterator<DhcpL2RelayCounters> it = DhcpL2RelayCounters.SUPPORTED_COUNTERS.iterator();
110 it.hasNext();) {
111 DhcpL2RelayCounters counterType = it.next();
112 // Increment of existing supported global counter
113 dhcpL2Relay.dhcpL2RelayCounters.incrementCounter(DhcpL2RelayEvent.GLOBAL_COUNTER, counterType);
114 // Add of a Subscriber entry that is not already in the set
115 dhcpL2Relay.dhcpL2RelayCounters.incrementCounter(CLIENT_ID_1, counterType);
116 }
117
118 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
119 for (Iterator<DhcpL2RelayCounters> it = DhcpL2RelayCounters.SUPPORTED_COUNTERS.iterator();
120 it.hasNext();) {
121 DhcpL2RelayCounters counterType = it.next();
122 long globalCounterValue = countersMap.get(new DhcpL2RelayCountersIdentifier(
123 DhcpL2RelayEvent.GLOBAL_COUNTER, counterType)).longValue();
124 long perSubscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
125 counterType)).longValue();
126 assertEquals((long) 1, globalCounterValue);
127 assertEquals((long) 1, perSubscriberValue);
128 }
129 }
130
131 /**
132 * Tests the increment and reset functions of the counters map for the given counter class.
133 */
134 @Test
135 public void testIncrementAndResetCounter() {
136 DhcpL2RelayCounters counterType;
137 long subscriberValue;
138 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap;
139
140 // First start incrementing the counter of a specific subscriber
141 for (Iterator<DhcpL2RelayCounters> it = DhcpL2RelayCounters.SUPPORTED_COUNTERS.iterator();
142 it.hasNext();) {
143 counterType = it.next();
144 // Insert of a Subscriber entry that is not already in the set
145 dhcpL2Relay.dhcpL2RelayCounters.incrementCounter(CLIENT_ID_1, counterType);
146 }
147
148 // Make sure that the counter is incremented
149 countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
150 for (Iterator<DhcpL2RelayCounters> it = DhcpL2RelayCounters.SUPPORTED_COUNTERS.iterator();
151 it.hasNext();) {
152 counterType = it.next();
153 subscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
154 counterType)).longValue();
155 assertEquals((long) 1, subscriberValue);
156 }
157
158 // Reset the counter
159 dhcpL2Relay.dhcpL2RelayCounters.resetCounters(CLIENT_ID_1);
160 countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
161 for (Iterator<DhcpL2RelayCounters> it = DhcpL2RelayCounters.SUPPORTED_COUNTERS.iterator();
162 it.hasNext();) {
163 counterType = it.next();
164 subscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier(CLIENT_ID_1,
165 counterType)).longValue();
166 assertEquals((long) 0, subscriberValue);
167 }
168 }
169
170 /**
171 * Tests the insert of the counter value for a subscriber entry if it is not already in the set
172 * otherwise update the existing counter entry.
173 */
174 @Test
175 public void testInsertOrUpdateCounter() {
176 dhcpL2Relay.dhcpL2RelayCounters.setCounter(CLIENT_ID_1, DhcpL2RelayCounters.valueOf("DHCPDISCOVER"), (long) 50);
177
178 Map<DhcpL2RelayCountersIdentifier, AtomicLong> countersMap = dhcpL2Relay.dhcpL2RelayCounters.getCountersMap();
179 long subscriberValue = countersMap.get(new DhcpL2RelayCountersIdentifier(
180 CLIENT_ID_1, DhcpL2RelayCounters.valueOf("DHCPDISCOVER"))).longValue();
181
182 assertEquals((long) 50, subscriberValue);
183 }
184
185}