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