blob: ad6d36af8fceecea23f4e3cbb54f2e7d8b449715 [file] [log] [blame]
Jian Lib8436bb2017-02-23 03:39:08 +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.provider.lisp.mapping.util;
17
18import com.google.common.collect.ImmutableList;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.packet.IpAddress;
22import org.onlab.packet.IpPrefix;
Jian Li0f0d7482017-03-20 15:16:13 +090023import org.onlab.packet.MacAddress;
Jian Lib8436bb2017-02-23 03:39:08 +090024import org.onosproject.lisp.msg.protocols.DefaultLispLocator.DefaultLocatorBuilder;
25import org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.DefaultNotifyBuilder;
26import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder;
27import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.DefaultReplyBuilder;
28import org.onosproject.lisp.msg.protocols.LispLocator;
29import org.onosproject.lisp.msg.protocols.LispLocator.LocatorBuilder;
30import org.onosproject.lisp.msg.protocols.LispMapNotify;
31import org.onosproject.lisp.msg.protocols.LispMapNotify.NotifyBuilder;
32import org.onosproject.lisp.msg.protocols.LispMapRecord;
33import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder;
34import org.onosproject.lisp.msg.protocols.LispMapReply;
35import org.onosproject.lisp.msg.protocols.LispMapReply.ReplyBuilder;
36import org.onosproject.lisp.msg.protocols.LispMapReplyAction;
Jian Li0f0d7482017-03-20 15:16:13 +090037import org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum;
38import org.onosproject.lisp.msg.types.LispAfiAddress;
39import org.onosproject.lisp.msg.types.LispAsAddress;
40import org.onosproject.lisp.msg.types.LispDistinguishedNameAddress;
Jian Lib8436bb2017-02-23 03:39:08 +090041import org.onosproject.lisp.msg.types.LispIpv4Address;
Jian Li0f0d7482017-03-20 15:16:13 +090042import org.onosproject.lisp.msg.types.LispIpv6Address;
43import org.onosproject.lisp.msg.types.LispMacAddress;
44import org.onosproject.lisp.msg.types.lcaf.LispAppDataLcafAddress;
45import org.onosproject.lisp.msg.types.lcaf.LispAsLcafAddress;
46import org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum;
47import org.onosproject.lisp.msg.types.lcaf.LispGeoCoordinateLcafAddress;
48import org.onosproject.lisp.msg.types.lcaf.LispLcafAddress;
49import org.onosproject.lisp.msg.types.lcaf.LispListLcafAddress;
50import org.onosproject.lisp.msg.types.lcaf.LispMulticastLcafAddress;
51import org.onosproject.lisp.msg.types.lcaf.LispNatLcafAddress;
52import org.onosproject.lisp.msg.types.lcaf.LispNonceLcafAddress;
53import org.onosproject.lisp.msg.types.lcaf.LispSegmentLcafAddress;
54import org.onosproject.lisp.msg.types.lcaf.LispSourceDestLcafAddress;
55import org.onosproject.lisp.msg.types.lcaf.LispTeLcafAddress;
56import org.onosproject.lisp.msg.types.lcaf.LispTeRecord;
Jian Lib8436bb2017-02-23 03:39:08 +090057import org.onosproject.mapping.MappingEntry;
58import org.onosproject.mapping.MappingKey;
59import org.onosproject.mapping.MappingTreatment;
60import org.onosproject.mapping.MappingValue;
61import org.onosproject.mapping.actions.MappingAction;
Jian Li0f0d7482017-03-20 15:16:13 +090062import org.onosproject.mapping.addresses.ASMappingAddress;
63import org.onosproject.mapping.addresses.DNMappingAddress;
64import org.onosproject.mapping.addresses.EthMappingAddress;
Jian Lib8436bb2017-02-23 03:39:08 +090065import org.onosproject.mapping.addresses.IPMappingAddress;
Jian Li0f0d7482017-03-20 15:16:13 +090066import org.onosproject.mapping.addresses.MappingAddress;
Jian Lib8436bb2017-02-23 03:39:08 +090067import org.onosproject.net.DeviceId;
68
69import java.util.List;
70
71import static org.hamcrest.MatcherAssert.assertThat;
72import static org.hamcrest.Matchers.is;
Jian Li0f0d7482017-03-20 15:16:13 +090073import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.AS;
74import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.DISTINGUISHED_NAME;
75import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.IP4;
76import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.IP6;
77import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.LCAF;
78import static org.onosproject.lisp.msg.types.AddressFamilyIdentifierEnum.MAC;
79import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.APPLICATION_DATA;
80import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.GEO_COORDINATE;
81import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.LIST;
82import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.MULTICAST;
83import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.NAT;
84import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.NONCE;
85import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.SEGMENT;
86import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.SOURCE_DEST;
87import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.UNKNOWN;
Jian Lib8436bb2017-02-23 03:39:08 +090088
89/**
90 * Mapping entry builder unit test.
91 */
92public class MappingEntryBuilderTest {
93
94 private static final String IP_RECORD_ADDRESS = "192.168.1.1";
Jian Li0f0d7482017-03-20 15:16:13 +090095 private static final int IP_RECORD_MASK_LENGTH = 32;
Jian Lib8436bb2017-02-23 03:39:08 +090096 private static final int IP_LOCATOR_MASK_LENGTH = 32;
97
Jian Li0f0d7482017-03-20 15:16:13 +090098 private static final LispAfiAddress IPV4_ADDRESS_1 =
99 new LispIpv4Address(IpAddress.valueOf("1.2.3.4"));
100 private static final LispAfiAddress IPV4_ADDRESS_2 =
101 new LispIpv4Address(IpAddress.valueOf("5.6.7.8"));
102 private static final LispAfiAddress IPV6_ADDRESS =
103 new LispIpv6Address(IpAddress.valueOf(
104 "1111:2222:3333:4444:5555:6666:7777:8885"));
105 private static final LispAfiAddress MAC_ADDRESS =
106 new LispMacAddress(MacAddress.valueOf("00:00:00:00:00:01"));
107
108 private static final IpPrefix IPV4_MAPPING_ADDRESS_1 = IpPrefix.valueOf("1.2.3.4/32");
109 private static final IpPrefix IPV4_MAPPING_ADDRESS_2 = IpPrefix.valueOf("5.6.7.8/32");
110
111 private static final IpPrefix IPV6_MAPPING_ADDRESS =
112 IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/128");
113 private static final MacAddress MAC_MAPPING_ADDRESS =
114 MacAddress.valueOf("00:00:00:00:00:01");
115
116 private static final byte UNIQUE_BYTE = (byte) 0x01;
117 private static final int UNIQUE_INT = 1;
118 private static final short UNIQUE_SHORT = 1;
119 private static final boolean UNIQUE_BOOLEAN = true;
120 private static final long UNIQUE_LONG = 1L;
121 private static final String UNIQUE_STRING = "onos";
122
Jian Lib8436bb2017-02-23 03:39:08 +0900123 private static final String AUTH_KEY = "onos";
124
Jian Lib8436bb2017-02-23 03:39:08 +0900125 private static final DeviceId DEVICE_ID = DeviceId.deviceId("lisp:10.1.1.2");
126
Jian Lib8436bb2017-02-23 03:39:08 +0900127 @Before
128 public void setUp() {
Jian Lib8436bb2017-02-23 03:39:08 +0900129
Jian Lib8436bb2017-02-23 03:39:08 +0900130 }
131
132 @Test
133 public void testMapReplyConversion() {
Jian Li0f0d7482017-03-20 15:16:13 +0900134
135 ReplyBuilder replyBuilder = new DefaultReplyBuilder();
136
137 List<LispMapRecord> records = ImmutableList.of(getMapRecord(IP4, UNKNOWN));
138
139 LispMapReply mapReply = replyBuilder
140 .withIsEtr(true)
141 .withIsProbe(false)
142 .withIsSecurity(true)
143 .withNonce(UNIQUE_LONG)
144 .withMapRecords(records)
145 .build();
146
Jian Lib8436bb2017-02-23 03:39:08 +0900147 List<LispMapRecord> replyRecords = mapReply.getMapRecords();
148
149 assertThat(replyRecords.size(), is(1));
150
Jian Li0f0d7482017-03-20 15:16:13 +0900151 testMapRecordConversion(replyRecords.get(0));
Jian Lib8436bb2017-02-23 03:39:08 +0900152 }
153
154 @Test
155 public void testMapNotifyConversion() {
Jian Li0f0d7482017-03-20 15:16:13 +0900156
157 List<LispMapRecord> records = ImmutableList.of(getMapRecord(IP4, UNKNOWN));
158
159 NotifyBuilder notifyBuilder = new DefaultNotifyBuilder();
160
161 LispMapNotify mapNotify = notifyBuilder
162 .withKeyId(UNIQUE_SHORT)
163 .withAuthKey(AUTH_KEY)
164 .withNonce(UNIQUE_LONG)
165 .withMapRecords(records)
166 .build();
167
Jian Lib8436bb2017-02-23 03:39:08 +0900168 List<LispMapRecord> notifyRecords = mapNotify.getMapRecords();
169
170 assertThat(notifyRecords.size(), is(1));
171
Jian Li0f0d7482017-03-20 15:16:13 +0900172 testMapRecordConversion(notifyRecords.get(0));
Jian Lib8436bb2017-02-23 03:39:08 +0900173 }
174
Jian Li0f0d7482017-03-20 15:16:13 +0900175 @Test
176 public void testIpv4AddressConversion() {
177 IPMappingAddress address = (IPMappingAddress) getMappingAddressByAfiType(IP4, UNKNOWN);
178 assertThat(address.ip(), is(IPV4_MAPPING_ADDRESS_1));
179 }
180
181 @Test
182 public void testIpv6AddressConversion() {
183 IPMappingAddress address = (IPMappingAddress) getMappingAddressByAfiType(IP6, UNKNOWN);
184 assertThat(address.ip(), is(IPV6_MAPPING_ADDRESS));
185 }
186
187 @Test
188 public void testMacAddressConversion() {
189 EthMappingAddress address = (EthMappingAddress) getMappingAddressByAfiType(MAC, UNKNOWN);
190 assertThat(address.mac(), is(MAC_MAPPING_ADDRESS));
191 }
192
193 @Test
194 public void testAsAddressConversion() {
195 ASMappingAddress address = (ASMappingAddress) getMappingAddressByAfiType(AS, UNKNOWN);
196 assertThat(address.asNumber(), is(String.valueOf(UNIQUE_INT)));
197 }
198
199 @Test
200 public void testDnAddressConversion() {
201 DNMappingAddress address = (DNMappingAddress)
202 getMappingAddressByAfiType(DISTINGUISHED_NAME, UNKNOWN);
203 assertThat(address.name(), is(UNIQUE_STRING));
204 }
205
206 @Test
Jian Li0f0d7482017-03-20 15:16:13 +0900207 public void testTeLcafAddressConversion() {
208 // TODO: need to compare TeRecord list
209 }
210
211 private MappingAddress getMappingAddressByAfiType(AddressFamilyIdentifierEnum afiType,
212 LispCanonicalAddressFormatEnum lcafType) {
213 LispMapRecord record = getMapRecord(afiType, lcafType);
214 MappingEntry entry = new MappingEntryBuilder(DEVICE_ID, record).build();
215 return entry.value().treatments().get(0).address();
216 }
217
218 private void testMapRecordConversion(LispMapRecord record) {
Jian Lib8436bb2017-02-23 03:39:08 +0900219 MappingEntry mappingEntry =
220 new MappingEntryBuilder(DEVICE_ID, record).build();
221 MappingKey key = mappingEntry.key();
222 MappingValue value = mappingEntry.value();
223
224 IPMappingAddress recordAddress = (IPMappingAddress) key.address();
225
226 assertThat(recordAddress.ip(), is(IpPrefix.valueOf(IP_RECORD_ADDRESS + "/" +
227 IP_RECORD_MASK_LENGTH)));
228
229 assertThat(value.action().type(), is(MappingAction.Type.NATIVE_FORWARD));
230
231 assertThat(value.treatments().size(), is(1));
232
233 MappingTreatment treatment = value.treatments().get(0);
234 IPMappingAddress locatorAddress = (IPMappingAddress) treatment.address();
235
Jian Li0f0d7482017-03-20 15:16:13 +0900236 assertThat(locatorAddress.ip(), is(IpPrefix.valueOf(IPV4_ADDRESS_1 + "/" +
Jian Lib8436bb2017-02-23 03:39:08 +0900237 IP_LOCATOR_MASK_LENGTH)));
238 }
239
Jian Li0f0d7482017-03-20 15:16:13 +0900240 /**
241 * Obtains a MapRecord instance.
242 *
243 * @param afiType AFI address type
244 * @param lcafType LCAF address type
245 * @return a MapRecord instance
246 */
247 private LispMapRecord getMapRecord(AddressFamilyIdentifierEnum afiType,
248 LispCanonicalAddressFormatEnum lcafType) {
Jian Lib8436bb2017-02-23 03:39:08 +0900249 MapRecordBuilder recordBuilder = new DefaultMapRecordBuilder();
250
251 LispIpv4Address recordAddress =
252 new LispIpv4Address(IpAddress.valueOf(IP_RECORD_ADDRESS));
253
254 LocatorBuilder locatorBuilder = new DefaultLocatorBuilder();
255
Jian Li0f0d7482017-03-20 15:16:13 +0900256 LispAfiAddress locatorAddress = getAfiAddress(afiType, lcafType);
Jian Lib8436bb2017-02-23 03:39:08 +0900257
Jian Li0f0d7482017-03-20 15:16:13 +0900258 LispLocator locator = locatorBuilder
259 .withPriority(UNIQUE_BYTE)
260 .withWeight(UNIQUE_BYTE)
261 .withMulticastPriority(UNIQUE_BYTE)
262 .withMulticastWeight(UNIQUE_BYTE)
Jian Lib8436bb2017-02-23 03:39:08 +0900263 .withLocalLocator(true)
264 .withRlocProbed(false)
265 .withRouted(true)
266 .withLocatorAfi(locatorAddress)
267 .build();
268
269 return recordBuilder
Jian Li0f0d7482017-03-20 15:16:13 +0900270 .withRecordTtl(UNIQUE_INT)
Jian Lib8436bb2017-02-23 03:39:08 +0900271 .withIsAuthoritative(true)
Jian Li0f0d7482017-03-20 15:16:13 +0900272 .withMapVersionNumber(UNIQUE_SHORT)
Jian Lib8436bb2017-02-23 03:39:08 +0900273 .withMaskLength((byte) IP_RECORD_MASK_LENGTH)
274 .withAction(LispMapReplyAction.NativelyForward)
275 .withEidPrefixAfi(recordAddress)
Jian Li0f0d7482017-03-20 15:16:13 +0900276 .withLocators(ImmutableList.of(locator))
Jian Lib8436bb2017-02-23 03:39:08 +0900277 .build();
278 }
Jian Li0f0d7482017-03-20 15:16:13 +0900279
280 /**
281 * Obtains the AFI address with respect to the AFI address type.
282 *
283 * @param afiType AFI address type
284 * @param lcafType LCAF address type
285 * @return AFI address instance
286 */
287 private LispAfiAddress getAfiAddress(AddressFamilyIdentifierEnum afiType,
288 LispCanonicalAddressFormatEnum lcafType) {
289 switch (afiType) {
290 case IP4:
291 return IPV4_ADDRESS_1;
292 case IP6:
293 return IPV6_ADDRESS;
294 case AS:
295 return new LispAsAddress(UNIQUE_INT);
296 case DISTINGUISHED_NAME:
297 return new LispDistinguishedNameAddress(UNIQUE_STRING);
298 case MAC:
299 return MAC_ADDRESS;
300 case LCAF:
301 return getLcafAddress(lcafType);
302 default:
303 return null;
304 }
305 }
306
307 /**
308 * Obtains the LCAF address with respect to the LCAF type.
309 *
310 * @param type LCAF type
311 * @return LCAF address instance
312 */
313 private LispLcafAddress getLcafAddress(LispCanonicalAddressFormatEnum type) {
314
315 List<LispAfiAddress> afiAddresses =
316 ImmutableList.of(IPV4_ADDRESS_1, IPV6_ADDRESS);
317
318 switch (type) {
319 case LIST:
320 return new LispListLcafAddress(afiAddresses);
321 case SEGMENT:
322 return new LispSegmentLcafAddress.SegmentAddressBuilder()
323 .withIdMaskLength(UNIQUE_BYTE)
324 .withInstanceId(UNIQUE_INT)
325 .withAddress(IPV4_ADDRESS_1)
326 .build();
327 case AS:
328 return new LispAsLcafAddress.AsAddressBuilder()
329 .withAsNumber(UNIQUE_INT)
330 .withAddress(IPV4_ADDRESS_1)
331 .build();
332 case APPLICATION_DATA:
333 return new LispAppDataLcafAddress.AppDataAddressBuilder()
334 .withProtocol(UNIQUE_BYTE)
335 .withIpTos(UNIQUE_SHORT)
336 .withLocalPortLow(UNIQUE_SHORT)
337 .withLocalPortHigh(UNIQUE_SHORT)
338 .withRemotePortLow(UNIQUE_SHORT)
339 .withRemotePortHigh(UNIQUE_SHORT)
340 .withAddress(IPV4_ADDRESS_1)
341 .build();
342 case GEO_COORDINATE:
343 return new LispGeoCoordinateLcafAddress.GeoCoordinateAddressBuilder()
344 .withIsNorth(UNIQUE_BOOLEAN)
345 .withLatitudeDegree(UNIQUE_SHORT)
346 .withLatitudeMinute(UNIQUE_BYTE)
347 .withLatitudeSecond(UNIQUE_BYTE)
348 .withIsEast(UNIQUE_BOOLEAN)
349 .withLongitudeDegree(UNIQUE_SHORT)
350 .withLongitudeMinute(UNIQUE_BYTE)
351 .withLongitudeSecond(UNIQUE_BYTE)
352 .withAltitude(UNIQUE_INT)
353 .withAddress(IPV4_ADDRESS_1)
354 .build();
355 case NAT:
356 return new LispNatLcafAddress.NatAddressBuilder()
357 .withLength(UNIQUE_SHORT)
358 .withMsUdpPortNumber(UNIQUE_SHORT)
359 .withEtrUdpPortNumber(UNIQUE_SHORT)
360 .withGlobalEtrRlocAddress(IPV4_ADDRESS_1)
361 .withMsRlocAddress(IPV4_ADDRESS_1)
362 .withPrivateEtrRlocAddress(IPV4_ADDRESS_1)
363 .withRtrRlocAddresses(afiAddresses)
364 .build();
365 case NONCE:
366 return new LispNonceLcafAddress.NonceAddressBuilder()
367 .withNonce(UNIQUE_INT)
368 .withAddress(IPV4_ADDRESS_1)
369 .build();
370 case MULTICAST:
371 return new LispMulticastLcafAddress.MulticastAddressBuilder()
372 .withInstanceId(UNIQUE_INT)
373 .withSrcMaskLength(UNIQUE_BYTE)
374 .withGrpMaskLength(UNIQUE_BYTE)
375 .withSrcAddress(IPV4_ADDRESS_1)
376 .withGrpAddress(IPV4_ADDRESS_1)
377 .build();
378 case TRAFFIC_ENGINEERING:
379 LispTeRecord.TeRecordBuilder recordBuilder1 = new LispTeRecord.TeRecordBuilder();
380
381 recordBuilder1.withIsLookup(UNIQUE_BOOLEAN);
382 recordBuilder1.withIsRlocProbe(UNIQUE_BOOLEAN);
383 recordBuilder1.withIsStrict(UNIQUE_BOOLEAN);
384 recordBuilder1.withRtrRlocAddress(IPV4_ADDRESS_1);
385 LispTeRecord record1 = recordBuilder1.build();
386
387 LispTeRecord.TeRecordBuilder recordBuilder2 = new LispTeRecord.TeRecordBuilder();
388
389 recordBuilder2.withIsLookup(UNIQUE_BOOLEAN);
390 recordBuilder2.withIsRlocProbe(UNIQUE_BOOLEAN);
391 recordBuilder2.withIsStrict(UNIQUE_BOOLEAN);
392 recordBuilder2.withRtrRlocAddress(IPV4_ADDRESS_2);
393 LispTeRecord record2 = recordBuilder2.build();
394
395 return new LispTeLcafAddress.TeAddressBuilder()
396 .withTeRecords(ImmutableList.of(record1, record2))
397 .build();
398 case SOURCE_DEST:
399 return new LispSourceDestLcafAddress.SourceDestAddressBuilder()
400 .withReserved(UNIQUE_SHORT)
401 .withSrcMaskLength(UNIQUE_BYTE)
402 .withDstMaskLength(UNIQUE_BYTE)
403 .withSrcPrefix(IPV4_ADDRESS_1)
404 .withDstPrefix(IPV4_ADDRESS_2)
405 .build();
406 case UNKNOWN:
407 case UNSPECIFIED:
408 default:
409 return null;
410 }
411 }
Jian Lib8436bb2017-02-23 03:39:08 +0900412}