Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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 | */ |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 16 | package org.onosproject.lisp.ctl.impl; |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.IpAddress; |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 22 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder; |
| 23 | import org.onosproject.lisp.msg.protocols.LispEidRecord; |
| 24 | import org.onosproject.lisp.msg.protocols.LispLocatorRecord; |
| 25 | import org.onosproject.lisp.msg.protocols.LispMapRecord; |
Jian Li | 5e505c6 | 2016-12-05 02:44:24 +0900 | [diff] [blame] | 26 | import org.onosproject.lisp.msg.protocols.DefaultLispLocatorRecord.DefaultLocatorRecordBuilder; |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 27 | import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder; |
| 28 | import org.onosproject.lisp.msg.types.LispIpv4Address; |
| 29 | |
| 30 | import java.util.List; |
| 31 | |
| 32 | import static org.hamcrest.MatcherAssert.assertThat; |
| 33 | import static org.hamcrest.Matchers.is; |
| 34 | |
| 35 | /** |
| 36 | * Tests for LISP EID RLOC Map class. |
| 37 | */ |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 38 | public class LispMappingDatabaseTest { |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 39 | |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 40 | final LispMappingDatabase mapDb = LispMappingDatabase.getInstance(); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 41 | |
| 42 | @Before |
| 43 | public void setup() { |
| 44 | |
| 45 | byte cidr1 = (byte) 32; |
| 46 | LispIpv4Address eid1 = new LispIpv4Address(IpAddress.valueOf("10.1.1.1")); |
| 47 | LispEidRecord eidRecord1 = new LispEidRecord(cidr1, eid1); |
| 48 | |
| 49 | LispIpv4Address locator11 = new LispIpv4Address(IpAddress.valueOf("123.1.1.1")); |
| 50 | LispIpv4Address locator12 = new LispIpv4Address(IpAddress.valueOf("123.1.1.2")); |
| 51 | LispIpv4Address locator13 = new LispIpv4Address(IpAddress.valueOf("123.1.1.3")); |
| 52 | |
| 53 | LispLocatorRecord locatorRecord11 = new DefaultLocatorRecordBuilder() |
| 54 | .withLocatorAfi(locator11).build(); |
| 55 | LispLocatorRecord locatorRecord12 = new DefaultLocatorRecordBuilder() |
| 56 | .withLocatorAfi(locator12).build(); |
| 57 | LispLocatorRecord locatorRecord13 = new DefaultLocatorRecordBuilder() |
| 58 | .withLocatorAfi(locator13).build(); |
| 59 | List<LispLocatorRecord> locatorRecords1 = |
| 60 | ImmutableList.of(locatorRecord11, locatorRecord12, locatorRecord13); |
| 61 | |
| 62 | byte cidr2 = (byte) 24; |
| 63 | LispIpv4Address eid2 = new LispIpv4Address(IpAddress.valueOf("10.1.2.0")); |
| 64 | LispEidRecord eidRecord2 = new LispEidRecord(cidr2, eid2); |
| 65 | |
| 66 | LispIpv4Address locator21 = new LispIpv4Address(IpAddress.valueOf("123.2.1.1")); |
| 67 | LispIpv4Address locator22 = new LispIpv4Address(IpAddress.valueOf("123.2.1.2")); |
| 68 | |
| 69 | LispLocatorRecord locatorRecord21 = new DefaultLocatorRecordBuilder() |
| 70 | .withLocatorAfi(locator21).build(); |
| 71 | LispLocatorRecord locatorRecord22 = new DefaultLocatorRecordBuilder() |
| 72 | .withLocatorAfi(locator22).build(); |
| 73 | |
| 74 | List<LispLocatorRecord> locatorRecords2 = |
| 75 | ImmutableList.of(locatorRecord21, locatorRecord22); |
| 76 | |
| 77 | byte cidr3 = (byte) 16; |
| 78 | LispIpv4Address eid3 = new LispIpv4Address(IpAddress.valueOf("10.2.0.0")); |
| 79 | LispEidRecord eidRecord3 = new LispEidRecord(cidr3, eid3); |
| 80 | |
| 81 | LispIpv4Address locator31 = new LispIpv4Address(IpAddress.valueOf("123.3.1.1")); |
| 82 | |
| 83 | LispLocatorRecord locatorRecord31 = new DefaultLocatorRecordBuilder() |
| 84 | .withLocatorAfi(locator31).build(); |
| 85 | |
| 86 | List<LispLocatorRecord> locatorRecords3 = ImmutableList.of(locatorRecord31); |
| 87 | |
| 88 | MapRecordBuilder builder1 = new DefaultMapRecordBuilder(); |
| 89 | builder1.withMaskLength(cidr1); |
| 90 | builder1.withEidPrefixAfi(eid1); |
| 91 | builder1.withLocators(locatorRecords1); |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 92 | builder1.withRecordTtl(60); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 93 | LispMapRecord mapRecord1 = builder1.build(); |
| 94 | |
| 95 | MapRecordBuilder builder2 = new DefaultMapRecordBuilder(); |
| 96 | builder2.withMaskLength(cidr2); |
| 97 | builder2.withEidPrefixAfi(eid2); |
| 98 | builder2.withLocators(locatorRecords2); |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 99 | builder2.withRecordTtl(60); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 100 | LispMapRecord mapRecord2 = builder2.build(); |
| 101 | |
| 102 | MapRecordBuilder builder3 = new DefaultMapRecordBuilder(); |
| 103 | builder3.withMaskLength(cidr3); |
| 104 | builder3.withEidPrefixAfi(eid3); |
| 105 | builder3.withLocators(locatorRecords3); |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 106 | builder3.withRecordTtl(60); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 107 | LispMapRecord mapRecord3 = builder3.build(); |
| 108 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 109 | mapDb.putMapRecord(eidRecord1, mapRecord1, true); |
| 110 | mapDb.putMapRecord(eidRecord2, mapRecord2, true); |
| 111 | mapDb.putMapRecord(eidRecord3, mapRecord3, true); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void test32MaskRange() { |
| 116 | byte cidr32 = (byte) 32; |
| 117 | LispIpv4Address eid = new LispIpv4Address(IpAddress.valueOf("10.1.1.1")); |
| 118 | LispEidRecord record = new LispEidRecord(cidr32, eid); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 119 | LispMapRecord mapRecord = mapDb.getMapRecordByEidRecord(record, true); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 120 | |
| 121 | assertThat("Failed to fetch the RLOCs with /32 EID record", |
| 122 | mapRecord.getLocatorCount(), is(3)); |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void test24MaskRange() { |
| 127 | byte cidr32 = (byte) 32; |
| 128 | LispIpv4Address eid = new LispIpv4Address(IpAddress.valueOf("10.1.2.1")); |
| 129 | LispEidRecord record32 = new LispEidRecord(cidr32, eid); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 130 | LispMapRecord mapRecord32 = mapDb.getMapRecordByEidRecord(record32, true); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 131 | |
| 132 | byte cidr24 = (byte) 24; |
| 133 | LispIpv4Address eid24 = new LispIpv4Address(IpAddress.valueOf("10.1.2.0")); |
| 134 | LispEidRecord record24 = new LispEidRecord(cidr24, eid24); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 135 | LispMapRecord mapRecord24 = mapDb.getMapRecordByEidRecord(record24, true); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 136 | |
| 137 | assertThat("Failed to fetch the RLOCs with /32 EID record", |
| 138 | mapRecord32.getLocatorCount(), is(2)); |
| 139 | assertThat("Failed to fetch the RLOCs with /24 EID record", |
| 140 | mapRecord24.getLocatorCount(), is(2)); |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void test16MaskRange() { |
| 145 | byte cidr32 = (byte) 32; |
| 146 | LispIpv4Address eid = new LispIpv4Address(IpAddress.valueOf("10.2.1.1")); |
| 147 | LispEidRecord record32 = new LispEidRecord(cidr32, eid); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 148 | LispMapRecord mapRecord32 = mapDb.getMapRecordByEidRecord(record32, true); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 149 | |
| 150 | byte cidr24 = (byte) 24; |
| 151 | LispIpv4Address eid24 = new LispIpv4Address(IpAddress.valueOf("10.2.1.0")); |
| 152 | LispEidRecord record24 = new LispEidRecord(cidr24, eid24); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 153 | LispMapRecord mapRecord24 = mapDb.getMapRecordByEidRecord(record24, true); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 154 | |
| 155 | byte cidr16 = (byte) 16; |
| 156 | LispIpv4Address eid16 = new LispIpv4Address(IpAddress.valueOf("10.2.0.0")); |
| 157 | LispEidRecord record16 = new LispEidRecord(cidr16, eid16); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 158 | LispMapRecord mapRecord16 = mapDb.getMapRecordByEidRecord(record16, true); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 159 | |
| 160 | assertThat("Failed to fetch the RLOCs with /32 EID record", |
| 161 | mapRecord32.getLocatorCount(), is(1)); |
| 162 | assertThat("Failed to fetch the RLOCs with /24 EID record", |
| 163 | mapRecord24.getLocatorCount(), is(1)); |
| 164 | assertThat("Failed to fetch the RLOCs with /16 EID record", |
| 165 | mapRecord16.getLocatorCount(), is(1)); |
| 166 | } |
| 167 | } |