blob: b61ec6a23b32eab1552391828760d2521ebd0252 [file] [log] [blame]
Pier Luigibdcd9672017-10-13 13:54:48 +02001/*
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
17package org.onosproject.driver.trafficcontrol;
18
19import com.google.common.collect.ImmutableList;
20import org.junit.Test;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.behaviour.trafficcontrol.PolicerConfigurable;
23import org.onosproject.net.behaviour.trafficcontrol.PolicerId;
24import org.onosproject.net.driver.Behaviour;
25import org.onosproject.net.driver.DefaultDriverData;
26import org.onosproject.net.driver.Driver;
27import org.onosproject.net.driver.DriverData;
28import org.onosproject.net.driver.DriverHandler;
29import org.onosproject.net.driver.DriverService;
30import org.onosproject.net.driver.DriverServiceAdapter;
31import org.onosproject.net.meter.MeterId;
32import org.onosproject.net.meter.MeterServiceAdapter;
33
34import java.util.Comparator;
35import java.util.Set;
36import java.util.TreeSet;
37
38import static org.hamcrest.Matchers.is;
39import static org.junit.Assert.assertThat;
40import static org.onosproject.net.NetTestTools.did;
41
42/**
43 * OpenFlow Policer config test.
44 */
45public class OpenFlowPolicerConfigurableTest {
46
47 // Device id used during the tests
48 private DeviceId ofDid = did("1");
49 private DeviceId fooDid = DeviceId.deviceId(FOO_SCHEME + ":1");
50
51 // Schemes used during the tests
52 private static final String OF_SCHEME = "of";
53 private static final String FOO_SCHEME = "foo";
54
55 // Policer id used during the tests
56 private PolicerId fooPid = PolicerId.policerId(FOO_SCHEME + ":1");
57
58 // Meter ids used during the tests
59 private MeterId mid1 = MeterId.meterId(1);
60 private MeterId mid10 = MeterId.meterId(10);
61 private MeterId mid100 = MeterId.meterId(100);
62
63 // Test Driver service used during the tests
64 private DriverService driverService = new TestDriverService();
65
66 // Test Meter service used during the tests
67 private TestMeterService meterService = new TestMeterService();
68
69 /**
70 * Test allocate policer id.
71 */
72 @Test
73 public void testAllocateId() {
74 // Get device handler
75 DriverHandler driverHandler = driverService.createHandler(ofDid);
76 // Get policer config behavior
77 PolicerConfigurable policerConfigurable = driverHandler.behaviour(PolicerConfigurable.class);
78 // Get policer id
79 PolicerId policerId = policerConfigurable.allocatePolicerId();
80 // Assert that scheme is equal to OF
81 assertThat(policerId.uri().getScheme(), is(OF_SCHEME));
82 // Convert in hex the id
83 String hexId = Long.toHexString((mid1.id()));
84 // Assert that specific part contains hex of meter id
85 assertThat(policerId.uri().getSchemeSpecificPart(), is(hexId));
86 }
87
88 /**
89 * Test no corresponding device.
90 */
91 @Test
92 public void testWrongDevice() {
93 // Get device handler
94 DriverHandler driverHandler = driverService.createHandler(fooDid);
95 // Get policer config behavior
96 PolicerConfigurable policerConfigurable = driverHandler.behaviour(PolicerConfigurable.class);
97 // Get policer id
98 PolicerId policerId = policerConfigurable.allocatePolicerId();
99 // Assert that is none
100 assertThat(policerId, is(PolicerId.NONE));
101 }
102
103 /**
104 * Test meter problems.
105 */
106 @Test
107 public void testMeterNull() {
108 // Get device handler
109 DriverHandler driverHandler = driverService.createHandler(ofDid);
110 // Get policer config behavior
111 PolicerConfigurable policerConfigurable = driverHandler.behaviour(PolicerConfigurable.class);
112 // Get policer id
113 PolicerId policerId = policerConfigurable.allocatePolicerId();
114 // this works
115 assertThat(policerId.uri().getScheme(), is(OF_SCHEME));
116 String hexId = Long.toHexString((mid1.id()));
117 assertThat(policerId.uri().getSchemeSpecificPart(), is(hexId));
118 // Get another policer id
119 policerId = policerConfigurable.allocatePolicerId();
120 assertThat(policerId.uri().getScheme(), is(OF_SCHEME));
121 hexId = Long.toHexString((mid10.id()));
122 assertThat(policerId.uri().getSchemeSpecificPart(), is(hexId));
123 // Get the last policer id
124 policerId = policerConfigurable.allocatePolicerId();
125 assertThat(policerId.uri().getScheme(), is(OF_SCHEME));
126 hexId = Long.toHexString((mid100.id()));
127 assertThat(policerId.uri().getSchemeSpecificPart(), is(hexId));
128 // this does not work
129 policerId = policerConfigurable.allocatePolicerId();
130 // Assert that is none
131 assertThat(policerId, is(PolicerId.NONE));
132 }
133
134 /**
135 * Test free policer id.
136 */
137 @Test
138 public void testFreeId() {
139 // Get device handler
140 DriverHandler driverHandler = driverService.createHandler(ofDid);
141 // Get policer config behavior
142 PolicerConfigurable policerConfigurable = driverHandler.behaviour(PolicerConfigurable.class);
143 // Get policer id
144 PolicerId policerId = policerConfigurable.allocatePolicerId();
145 // this works
146 assertThat(policerId.uri().getScheme(), is(OF_SCHEME));
147 String hexId = Long.toHexString((mid1.id()));
148 assertThat(policerId.uri().getSchemeSpecificPart(), is(hexId));
149 // Verify the allocation before free
150 assertThat(meterService.availableIds.size(), is(2));
151 // Let's free the policer id
152 policerConfigurable.freePolicerId(policerId);
153 // Verify the availability after free
154 assertThat(meterService.availableIds.size(), is(3));
155 }
156
157 /**
158 * Test wrong policer id.
159 */
160 @Test
161 public void testWrongId() {
162 // Get device handler
163 DriverHandler driverHandler = driverService.createHandler(ofDid);
164 // Get policer config behavior
165 PolicerConfigurable policerConfigurable = driverHandler.behaviour(PolicerConfigurable.class);
166 // Get policer id
167 PolicerId policerId = policerConfigurable.allocatePolicerId();
168 // this works
169 assertThat(policerId.uri().getScheme(), is(OF_SCHEME));
170 String hexId = Long.toHexString((mid1.id()));
171 assertThat(policerId.uri().getSchemeSpecificPart(), is(hexId));
172 // Verify the allocation before free
173 assertThat(meterService.availableIds.size(), is(2));
174 // Update the pid with a wrong id (same id but wrong schema)
175 policerId = fooPid;
176 // Let's free the policer id
177 policerConfigurable.freePolicerId(policerId);
178 // Free does not end correctly
179 assertThat(meterService.availableIds.size(), is(2));
180 }
181
182 // Test class for driver handler
183 private class TestDriverHandler implements DriverHandler {
184
185 private final DeviceId deviceId;
186
187 TestDriverHandler(DeviceId deviceId) {
188 this.deviceId = deviceId;
189 }
190
191 @Override
192 public Driver driver() {
193 return null;
194 }
195
196 @Override
197 public DriverData data() {
198 // Just create a fake driver data
199 return new DefaultDriverData(null, deviceId);
200 }
201
202 @Override
203 @SuppressWarnings("unchecked")
204 public <T extends Behaviour> T behaviour(Class<T> behaviourClass) {
205 // Let's create the behavior
206 PolicerConfigurable policerConfigurable = new OpenFlowPolicerConfigurable();
207 // Set the handler
208 policerConfigurable.setHandler(this);
209 // Done, return the behavior
210 return (T) policerConfigurable;
211 }
212
213 @Override
214 @SuppressWarnings("unchecked")
215 public <T> T get(Class<T> serviceClass) {
216 return (T) meterService;
217 }
218
219 @Override
220 public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) {
221 return true;
222 }
223 }
224
225 // Test class for driver service
226 private class TestDriverService extends DriverServiceAdapter {
227
228 @Override
229 public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
230 return new TestDriverHandler(deviceId);
231 }
232
233 }
234
235 // Test class for meter service
236 private class TestMeterService extends MeterServiceAdapter {
237
238 // Let's simulate a store
239 Set<MeterId> availableIds = new TreeSet<>(
240 (Comparator<MeterId>) (id1, id2) -> id1.id().compareTo(id2.id())
241 );
242
243 TestMeterService() {
244 availableIds.addAll(ImmutableList.of(mid1, mid10, mid100));
245 }
246
247 @Override
248 public MeterId allocateMeterId(DeviceId deviceId) {
249 // If there are no more ids, return null
250 if (availableIds.isEmpty()) {
251 return null;
252 }
253 // Get the next id
254 MeterId meterId = availableIds.iterator().next();
255 // Make it unavailable
256 availableIds.remove(meterId);
257 // Done, return it
258 return meterId;
259 }
260
261 @Override
262 public void freeMeterId(DeviceId deviceId, MeterId meterId) {
263 // Make the id available
264 availableIds.add(meterId);
265 }
266
267 }
268
269}