blob: 7bfaab36f29914c9873ba05da4608f18fa8cbe50 [file] [log] [blame]
Jian Licae2cf02017-04-03 23:22:57 +09001/*
2 * Copyright 2017-present Open Networking Laboratory
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.onosproject.drivers.lisp.extensions;
17
18import com.google.common.collect.ImmutableList;
19import com.google.common.testing.EqualsTester;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.IpPrefix;
24import org.onosproject.lisp.msg.types.LispAfiAddress;
25import org.onosproject.lisp.msg.types.LispIpv4Address;
26import org.onosproject.lisp.msg.types.LispIpv6Address;
27import org.onosproject.lisp.msg.types.lcaf.LispAppDataLcafAddress;
28import org.onosproject.lisp.msg.types.lcaf.LispAsLcafAddress;
29import org.onosproject.lisp.msg.types.lcaf.LispGeoCoordinateLcafAddress;
30import org.onosproject.lisp.msg.types.lcaf.LispLcafAddress;
31import org.onosproject.lisp.msg.types.lcaf.LispListLcafAddress;
32import org.onosproject.lisp.msg.types.lcaf.LispMulticastLcafAddress;
33import org.onosproject.lisp.msg.types.lcaf.LispNatLcafAddress;
34import org.onosproject.lisp.msg.types.lcaf.LispNonceLcafAddress;
35import org.onosproject.lisp.msg.types.lcaf.LispSegmentLcafAddress;
36import org.onosproject.lisp.msg.types.lcaf.LispSourceDestLcafAddress;
37import org.onosproject.lisp.msg.types.lcaf.LispTeLcafAddress;
38import org.onosproject.lisp.msg.types.lcaf.LispTeRecord;
39import org.onosproject.mapping.addresses.ExtensionMappingAddress;
40import org.onosproject.mapping.addresses.ExtensionMappingAddressType;
41import org.onosproject.mapping.addresses.MappingAddress;
42import org.onosproject.mapping.addresses.MappingAddresses;
43
44import static org.junit.Assert.assertTrue;
45import static org.onosproject.mapping.addresses.ExtensionMappingAddressType.ExtensionMappingAddressTypes.*;
46/**
47 * Unit tests for LispExtensionMappingAddressInterpreter.
48 */
49public class LispExtensionMappingAddressInterpreterTest {
50
51 private static final String IPV4_STRING = "1.2.3.4";
52 private static final String IPV6_STRING = "1111:2222:3333:4444:5555:6666:7777:8886";
53 private static final IpPrefix IPV4_PREFIX = IpPrefix.valueOf(IPV4_STRING + "/32");
54 private static final IpPrefix IPV6_PREFIX = IpPrefix.valueOf(IPV6_STRING + "/128");
55 private static final IpAddress IPV4_ADDRESS = IpAddress.valueOf(IPV4_STRING);
56 private static final IpAddress IPV6_ADDRESS = IpAddress.valueOf(IPV6_STRING);
57
58 private static final int UNIQUE_INT = 1;
59 private static final short UNIQUE_SHORT = (short) 1;
60 private static final byte UNIQUE_BYTE = (byte) 1;
61 private static final boolean UNIQUE_BOOLEAN = true;
62
63 private LispExtensionMappingAddressInterpreter interpreter;
64 private ExtensionMappingAddress listExtAddress;
65 private ExtensionMappingAddress segmentExtAddress;
66 private ExtensionMappingAddress asExtAddress;
67 private ExtensionMappingAddress appDataExtAddress;
68 private ExtensionMappingAddress gcExtAddress;
69 private ExtensionMappingAddress natExtAddress;
70 private ExtensionMappingAddress nonceExtAddress;
71 private ExtensionMappingAddress multicastExtAddress;
72 private ExtensionMappingAddress teExtAddress;
73 private ExtensionMappingAddress srcDstExtAddress;
74
75 private LispLcafAddress listLcafAddress;
76 private LispLcafAddress segmentLcafAddress;
77 private LispLcafAddress asLcafAddress;
78 private LispLcafAddress appDataLcafAddress;
79 private LispLcafAddress gcLcafAddress;
80 private LispLcafAddress natLcafAddress;
81 private LispLcafAddress nonceLcafAddress;
82 private LispLcafAddress multicastLcafAddress;
83 private LispLcafAddress teLcafAddress;
84 private LispLcafAddress srcDstLcafAddress;
85
86 @Before
87 public void setUp() {
88 interpreter = new LispExtensionMappingAddressInterpreter();
89 initExtAddresses();
90 initLcafAddresses();
91 }
92
93 private void initExtAddresses() {
94 listExtAddress = getExtMappingAddress(LIST_ADDRESS.type());
95 segmentExtAddress = getExtMappingAddress(SEGMENT_ADDRESS.type());
96 asExtAddress = getExtMappingAddress(AS_ADDRESS.type());
97 appDataExtAddress = getExtMappingAddress(APPLICATION_DATA_ADDRESS.type());
98 gcExtAddress = getExtMappingAddress(GEO_COORDINATE_ADDRESS.type());
99 natExtAddress = getExtMappingAddress(NAT_ADDRESS.type());
100 nonceExtAddress = getExtMappingAddress(NONCE_ADDRESS.type());
101 multicastExtAddress = getExtMappingAddress(MULTICAST_ADDRESS.type());
102 teExtAddress = getExtMappingAddress(TRAFFIC_ENGINEERING_ADDRESS.type());
103 srcDstExtAddress = getExtMappingAddress(SOURCE_DEST_ADDRESS.type());
104 }
105
106 private void initLcafAddresses() {
107 listLcafAddress = getLcafMappingAddress(LIST_ADDRESS.type());
108 segmentLcafAddress = getLcafMappingAddress(SEGMENT_ADDRESS.type());
109 asLcafAddress = getLcafMappingAddress(AS_ADDRESS.type());
110 appDataLcafAddress = getLcafMappingAddress(APPLICATION_DATA_ADDRESS.type());
111 gcLcafAddress = getLcafMappingAddress(GEO_COORDINATE_ADDRESS.type());
112 natLcafAddress = getLcafMappingAddress(NAT_ADDRESS.type());
113 nonceLcafAddress = getLcafMappingAddress(NONCE_ADDRESS.type());
114 multicastLcafAddress = getLcafMappingAddress(MULTICAST_ADDRESS.type());
115 teLcafAddress = getLcafMappingAddress(TRAFFIC_ENGINEERING_ADDRESS.type());
116 srcDstLcafAddress = getLcafMappingAddress(SOURCE_DEST_ADDRESS.type());
117 }
118
119 @Test
120 public void testSupported() {
121 assertTrue("List extension address should be supported",
122 interpreter.supported(listExtAddress.type()));
123 assertTrue("Segment extension address should be supported",
124 interpreter.supported(segmentExtAddress.type()));
125 assertTrue("AS extension address should be supported",
126 interpreter.supported(asExtAddress.type()));
127 assertTrue("Application data extension address should be supported",
128 interpreter.supported(appDataExtAddress.type()));
129 assertTrue("Geo Coordinate extension address should be supported",
130 interpreter.supported(gcExtAddress.type()));
131 assertTrue("NAT extension address should be supported ",
132 interpreter.supported(natExtAddress.type()));
133 assertTrue("Nonce extension address should be supported",
134 interpreter.supported(nonceExtAddress.type()));
135 assertTrue("Multicast extension address should be supported",
136 interpreter.supported(multicastExtAddress.type()));
137 assertTrue("Traffic engineering extension address should be supported",
138 interpreter.supported(teExtAddress.type()));
139 assertTrue("Source Destination extension address should be supported",
140 interpreter.supported(srcDstExtAddress.type()));
141 }
142
143 @Test
144 public void testMapMappingAddress() {
145
146 new EqualsTester()
147 .addEqualityGroup(listLcafAddress, interpreter.mapMappingAddress(listExtAddress))
148 .addEqualityGroup(segmentLcafAddress, interpreter.mapMappingAddress(segmentExtAddress))
149 .addEqualityGroup(asLcafAddress, interpreter.mapMappingAddress(asExtAddress))
150 .addEqualityGroup(appDataLcafAddress, interpreter.mapMappingAddress(appDataExtAddress))
151 .addEqualityGroup(gcLcafAddress, interpreter.mapMappingAddress(gcExtAddress))
152 .addEqualityGroup(natLcafAddress, interpreter.mapMappingAddress(natExtAddress))
153 .addEqualityGroup(nonceLcafAddress, interpreter.mapMappingAddress(nonceExtAddress))
154 .addEqualityGroup(multicastLcafAddress, interpreter.mapMappingAddress(multicastExtAddress))
155 .addEqualityGroup(teLcafAddress, interpreter.mapMappingAddress(teExtAddress))
156 .addEqualityGroup(srcDstLcafAddress, interpreter.mapMappingAddress(srcDstExtAddress))
157 .testEquals();
158 }
159
160 @Test
161 public void testMapLcafAddress() {
162
163 new EqualsTester()
164 .addEqualityGroup(listExtAddress, interpreter.mapLcafAddress(listLcafAddress))
165 .addEqualityGroup(segmentExtAddress, interpreter.mapLcafAddress(segmentLcafAddress))
166 .addEqualityGroup(asExtAddress, interpreter.mapLcafAddress(asLcafAddress))
167 .addEqualityGroup(appDataExtAddress, interpreter.mapLcafAddress(appDataLcafAddress))
168 .addEqualityGroup(gcExtAddress, interpreter.mapLcafAddress(gcLcafAddress))
169 .addEqualityGroup(natExtAddress, interpreter.mapLcafAddress(natLcafAddress))
170 .addEqualityGroup(nonceExtAddress, interpreter.mapLcafAddress(nonceLcafAddress))
171 .addEqualityGroup(multicastExtAddress, interpreter.mapLcafAddress(multicastLcafAddress))
172 .addEqualityGroup(teExtAddress, interpreter.mapLcafAddress(teLcafAddress))
173 .addEqualityGroup(srcDstExtAddress, interpreter.mapLcafAddress(srcDstLcafAddress))
174 .testEquals();
175 }
176
177 private LispLcafAddress getLcafMappingAddress(ExtensionMappingAddressType type) {
178 LispLcafAddress address = null;
179
180 LispAfiAddress ipv4Addr = new LispIpv4Address(IPV4_ADDRESS);
181 LispAfiAddress ipv6Addr = new LispIpv6Address(IPV6_ADDRESS);
182
183 if (type.equals(LIST_ADDRESS.type())) {
184 address = new LispListLcafAddress(ImmutableList.of(ipv4Addr, ipv6Addr));
185 }
186
187 if (type.equals(SEGMENT_ADDRESS.type())) {
188 address = new LispSegmentLcafAddress.SegmentAddressBuilder()
189 .withInstanceId(UNIQUE_INT)
190 .withAddress(ipv4Addr)
191 .build();
192 }
193
194 if (type.equals(AS_ADDRESS.type())) {
195 address = new LispAsLcafAddress.AsAddressBuilder()
196 .withAsNumber(UNIQUE_INT)
197 .withAddress(ipv4Addr)
198 .build();
199 }
200
201 if (type.equals(APPLICATION_DATA_ADDRESS.type())) {
202 address = new LispAppDataLcafAddress.AppDataAddressBuilder()
203 .withProtocol(UNIQUE_BYTE)
204 .withIpTos(UNIQUE_INT)
205 .withLocalPortLow(UNIQUE_SHORT)
206 .withLocalPortHigh(UNIQUE_SHORT)
207 .withRemotePortLow(UNIQUE_SHORT)
208 .withRemotePortHigh(UNIQUE_SHORT)
209 .withAddress(ipv4Addr)
210 .build();
211 }
212
213 if (type.equals(GEO_COORDINATE_ADDRESS.type())) {
214 address = new LispGeoCoordinateLcafAddress.GeoCoordinateAddressBuilder()
215 .withIsNorth(UNIQUE_BOOLEAN)
216 .withLatitudeDegree(UNIQUE_SHORT)
217 .withLatitudeMinute(UNIQUE_BYTE)
218 .withLatitudeSecond(UNIQUE_BYTE)
219 .withIsEast(UNIQUE_BOOLEAN)
220 .withLongitudeDegree(UNIQUE_SHORT)
221 .withLongitudeMinute(UNIQUE_BYTE)
222 .withLongitudeSecond(UNIQUE_BYTE)
223 .withAltitude(UNIQUE_INT)
224 .withAddress(ipv4Addr)
225 .build();
226 }
227
228 if (type.equals(NAT_ADDRESS.type())) {
229 address = new LispNatLcafAddress.NatAddressBuilder()
230 .withMsUdpPortNumber(UNIQUE_SHORT)
231 .withEtrUdpPortNumber(UNIQUE_SHORT)
232 .withGlobalEtrRlocAddress(ipv4Addr)
233 .withMsRlocAddress(ipv4Addr)
234 .withPrivateEtrRlocAddress(ipv4Addr)
235 .withRtrRlocAddresses(ImmutableList.of(ipv4Addr, ipv6Addr))
236 .build();
237 }
238
239 if (type.equals(NONCE_ADDRESS.type())) {
240 address = new LispNonceLcafAddress.NonceAddressBuilder()
241 .withNonce(UNIQUE_INT)
242 .withAddress(ipv4Addr)
243 .build();
244 }
245
246 if (type.equals(MULTICAST_ADDRESS.type())) {
247 address = new LispMulticastLcafAddress.MulticastAddressBuilder()
248 .withInstanceId(UNIQUE_INT)
249 .withSrcMaskLength(UNIQUE_BYTE)
250 .withSrcAddress(ipv4Addr)
251 .withGrpMaskLength(UNIQUE_BYTE)
252 .withGrpAddress(ipv4Addr)
253 .build();
254 }
255
256 if (type.equals(TRAFFIC_ENGINEERING_ADDRESS.type())) {
257 LispTeRecord tr = new LispTeRecord.TeRecordBuilder()
258 .withIsLookup(UNIQUE_BOOLEAN)
259 .withIsRlocProbe(UNIQUE_BOOLEAN)
260 .withIsStrict(UNIQUE_BOOLEAN)
261 .withRtrRlocAddress(ipv4Addr)
262 .build();
263
264 address = new LispTeLcafAddress.TeAddressBuilder()
265 .withTeRecords(ImmutableList.of(tr))
266 .build();
267 }
268
269 if (type.equals(SOURCE_DEST_ADDRESS.type())) {
270 address = new LispSourceDestLcafAddress.SourceDestAddressBuilder()
271 .withSrcMaskLength(UNIQUE_BYTE)
272 .withSrcPrefix(ipv4Addr)
273 .withDstMaskLength(UNIQUE_BYTE)
274 .withDstPrefix(ipv4Addr)
275 .build();
276 }
277
278 return address;
279 }
280
281 private ExtensionMappingAddress getExtMappingAddress(ExtensionMappingAddressType type) {
282
283 ExtensionMappingAddress address = null;
284
285 MappingAddress ipv4Addr = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX);
286 MappingAddress ipv6Addr = MappingAddresses.ipv6MappingAddress(IPV6_PREFIX);
287
288 if (type.equals(LIST_ADDRESS.type())) {
289 address = new LispListAddress.Builder()
290 .withIpv4(ipv4Addr)
291 .withIpv6(ipv6Addr)
292 .build();
293 }
294
295 if (type.equals(SEGMENT_ADDRESS.type())) {
296 address = new LispSegmentAddress.Builder()
297 .withInstanceId(UNIQUE_INT)
298 .withAddress(ipv4Addr)
299 .build();
300 }
301
302 if (type.equals(AS_ADDRESS.type())) {
303 address = new LispAsAddress.Builder()
304 .withAsNumber(UNIQUE_INT)
305 .withAddress(ipv4Addr)
306 .build();
307 }
308
309 if (type.equals(APPLICATION_DATA_ADDRESS.type())) {
310 address = new LispAppDataAddress.Builder()
311 .withProtocol(UNIQUE_BYTE)
312 .withIpTos(UNIQUE_INT)
313 .withLocalPortLow(UNIQUE_SHORT)
314 .withLocalPortHigh(UNIQUE_SHORT)
315 .withRemotePortLow(UNIQUE_SHORT)
316 .withRemotePortHigh(UNIQUE_SHORT)
317 .withAddress(ipv4Addr)
318 .build();
319 }
320
321 if (type.equals(GEO_COORDINATE_ADDRESS.type())) {
322 address = new LispGcAddress.Builder()
323 .withIsNorth(UNIQUE_BOOLEAN)
324 .withLatitudeDegree(UNIQUE_SHORT)
325 .withLatitudeMinute(UNIQUE_BYTE)
326 .withLatitudeSecond(UNIQUE_BYTE)
327 .withIsEast(UNIQUE_BOOLEAN)
328 .withLongitudeDegree(UNIQUE_SHORT)
329 .withLongitudeMinute(UNIQUE_BYTE)
330 .withLongitudeSecond(UNIQUE_BYTE)
331 .withAltitude(UNIQUE_INT)
332 .withAddress(ipv4Addr)
333 .build();
334 }
335
336 if (type.equals(NAT_ADDRESS.type())) {
337 address = new LispNatAddress.Builder()
338 .withMsUdpPortNumber(UNIQUE_SHORT)
339 .withEtrUdpPortNumber(UNIQUE_SHORT)
340 .withGlobalEtrRlocAddress(ipv4Addr)
341 .withMsRlocAddress(ipv4Addr)
342 .withPrivateEtrRlocAddress(ipv4Addr)
343 .withRtrRlocAddresses(ImmutableList.of(ipv4Addr, ipv6Addr))
344 .build();
345 }
346
347 if (type.equals(NONCE_ADDRESS.type())) {
348 address = new LispNonceAddress.Builder()
349 .withNonce(UNIQUE_INT)
350 .withAddress(ipv4Addr)
351 .build();
352 }
353
354 if (type.equals(MULTICAST_ADDRESS.type())) {
355 address = new LispMulticastAddress.Builder()
356 .withInstanceId(UNIQUE_INT)
357 .withSrcMaskLength(UNIQUE_BYTE)
358 .withSrcAddress(ipv4Addr)
359 .withGrpMaskLength(UNIQUE_BYTE)
360 .withGrpAddress(ipv4Addr)
361 .build();
362 }
363
364 if (type.equals(TRAFFIC_ENGINEERING_ADDRESS.type())) {
365
366 LispTeAddress.TeRecord tr = new LispTeAddress.TeRecord.Builder()
367 .withIsLookup(UNIQUE_BOOLEAN)
368 .withIsRlocProbe(UNIQUE_BOOLEAN)
369 .withIsStrict(UNIQUE_BOOLEAN)
370 .withRtrRlocAddress(ipv4Addr)
371 .build();
372
373 address = new LispTeAddress.Builder()
374 .withTeRecords(ImmutableList.of(tr))
375 .build();
376 }
377
378 if (type.equals(SOURCE_DEST_ADDRESS.type())) {
379 address = new LispSrcDstAddress.Builder()
380 .withSrcMaskLength(UNIQUE_BYTE)
381 .withSrcPrefix(ipv4Addr)
382 .withDstMaskLength(UNIQUE_BYTE)
383 .withDstPrefix(ipv4Addr)
384 .build();
385 }
386 return address;
387 }
388}