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 | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 22 | import org.onosproject.lisp.msg.protocols.DefaultLispLocator.DefaultLocatorBuilder; |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 23 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder; |
| 24 | import org.onosproject.lisp.msg.protocols.LispEidRecord; |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 25 | import org.onosproject.lisp.msg.protocols.LispLocator; |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 26 | import org.onosproject.lisp.msg.protocols.LispMapRecord; |
| 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 | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 40 | private static final String LOCATOR_IP_1_1 = "123.1.1.1"; |
| 41 | private static final String LOCATOR_IP_1_2 = "123.1.1.2"; |
| 42 | private static final String LOCATOR_IP_1_3 = "123.1.1.3"; |
| 43 | |
| 44 | private static final String LOCATOR_IP_2_1 = "123.2.1.1"; |
| 45 | private static final String LOCATOR_IP_2_2 = "123.2.1.2"; |
| 46 | |
| 47 | private static final String LOCATOR_IP_3_1 = "123.3.1.1"; |
| 48 | |
| 49 | private static final String EID_IP_1 = "10.1.1.1"; |
| 50 | private static final String EID_IP_2 = "10.1.2.0"; |
| 51 | private static final String EID_IP_3 = "10.2.0.0"; |
| 52 | |
| 53 | private static final String EID_IP_PREFIX_1_32 = "10.2.1.1"; |
| 54 | private static final String EID_IP_PREFIX_1_24 = "10.2.1.0"; |
| 55 | private static final String EID_IP_PREFIX_1_16 = "10.2.0.0"; |
| 56 | |
| 57 | private static final String EID_IP_PREFIX_2_32 = "10.1.2.1"; |
| 58 | private static final String EID_IP_PREFIX_2_24 = "10.1.2.0"; |
| 59 | |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 60 | final LispMappingDatabase mapDb = LispMappingDatabase.getInstance(); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 61 | |
| 62 | @Before |
| 63 | public void setup() { |
| 64 | |
| 65 | byte cidr1 = (byte) 32; |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 66 | LispIpv4Address eid1 = new LispIpv4Address(IpAddress.valueOf(EID_IP_1)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 67 | LispEidRecord eidRecord1 = new LispEidRecord(cidr1, eid1); |
| 68 | |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 69 | LispIpv4Address locator11 = new LispIpv4Address(IpAddress.valueOf(LOCATOR_IP_1_1)); |
| 70 | LispIpv4Address locator12 = new LispIpv4Address(IpAddress.valueOf(LOCATOR_IP_1_2)); |
| 71 | LispIpv4Address locator13 = new LispIpv4Address(IpAddress.valueOf(LOCATOR_IP_1_3)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 72 | |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 73 | LispLocator locatorRecord11 = new DefaultLocatorBuilder() |
| 74 | .withLocatorAfi(locator11).build(); |
| 75 | LispLocator locatorRecord12 = new DefaultLocatorBuilder() |
| 76 | .withLocatorAfi(locator12).build(); |
| 77 | LispLocator locatorRecord13 = new DefaultLocatorBuilder() |
| 78 | .withLocatorAfi(locator13).build(); |
| 79 | List<LispLocator> locatorRecords1 = |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 80 | ImmutableList.of(locatorRecord11, locatorRecord12, locatorRecord13); |
| 81 | |
| 82 | byte cidr2 = (byte) 24; |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 83 | LispIpv4Address eid2 = new LispIpv4Address(IpAddress.valueOf(EID_IP_2)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 84 | LispEidRecord eidRecord2 = new LispEidRecord(cidr2, eid2); |
| 85 | |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 86 | LispIpv4Address locator21 = new LispIpv4Address(IpAddress.valueOf(LOCATOR_IP_2_1)); |
| 87 | LispIpv4Address locator22 = new LispIpv4Address(IpAddress.valueOf(LOCATOR_IP_2_2)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 88 | |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 89 | LispLocator locatorRecord21 = new DefaultLocatorBuilder() |
| 90 | .withLocatorAfi(locator21).build(); |
| 91 | LispLocator locatorRecord22 = new DefaultLocatorBuilder() |
| 92 | .withLocatorAfi(locator22).build(); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 93 | |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 94 | List<LispLocator> locatorRecords2 = |
| 95 | ImmutableList.of(locatorRecord21, locatorRecord22); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 96 | |
| 97 | byte cidr3 = (byte) 16; |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 98 | LispIpv4Address eid3 = new LispIpv4Address(IpAddress.valueOf(EID_IP_3)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 99 | LispEidRecord eidRecord3 = new LispEidRecord(cidr3, eid3); |
| 100 | |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 101 | LispIpv4Address locator31 = new LispIpv4Address(IpAddress.valueOf(LOCATOR_IP_3_1)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 102 | |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 103 | LispLocator locatorRecord31 = new DefaultLocatorBuilder() |
| 104 | .withLocatorAfi(locator31).build(); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 105 | |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 106 | List<LispLocator> locatorRecords3 = ImmutableList.of(locatorRecord31); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 107 | |
| 108 | MapRecordBuilder builder1 = new DefaultMapRecordBuilder(); |
| 109 | builder1.withMaskLength(cidr1); |
| 110 | builder1.withEidPrefixAfi(eid1); |
| 111 | builder1.withLocators(locatorRecords1); |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 112 | builder1.withRecordTtl(60); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 113 | LispMapRecord mapRecord1 = builder1.build(); |
| 114 | |
| 115 | MapRecordBuilder builder2 = new DefaultMapRecordBuilder(); |
| 116 | builder2.withMaskLength(cidr2); |
| 117 | builder2.withEidPrefixAfi(eid2); |
| 118 | builder2.withLocators(locatorRecords2); |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 119 | builder2.withRecordTtl(60); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 120 | LispMapRecord mapRecord2 = builder2.build(); |
| 121 | |
| 122 | MapRecordBuilder builder3 = new DefaultMapRecordBuilder(); |
| 123 | builder3.withMaskLength(cidr3); |
| 124 | builder3.withEidPrefixAfi(eid3); |
| 125 | builder3.withLocators(locatorRecords3); |
Jian Li | 29986d8 | 2016-12-01 03:25:12 +0900 | [diff] [blame] | 126 | builder3.withRecordTtl(60); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 127 | LispMapRecord mapRecord3 = builder3.build(); |
| 128 | |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 129 | mapDb.putMapRecord(eidRecord1, mapRecord1, true); |
| 130 | mapDb.putMapRecord(eidRecord2, mapRecord2, true); |
| 131 | mapDb.putMapRecord(eidRecord3, mapRecord3, true); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void test32MaskRange() { |
| 136 | byte cidr32 = (byte) 32; |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 137 | LispIpv4Address eid = new LispIpv4Address(IpAddress.valueOf(EID_IP_1)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 138 | LispEidRecord record = new LispEidRecord(cidr32, eid); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 139 | LispMapRecord mapRecord = mapDb.getMapRecordByEidRecord(record, true); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 140 | |
| 141 | assertThat("Failed to fetch the RLOCs with /32 EID record", |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 142 | mapRecord.getLocatorCount(), is(3)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void test24MaskRange() { |
| 147 | byte cidr32 = (byte) 32; |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 148 | LispIpv4Address eid = new LispIpv4Address(IpAddress.valueOf(EID_IP_PREFIX_2_32)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 149 | LispEidRecord record32 = new LispEidRecord(cidr32, eid); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 150 | LispMapRecord mapRecord32 = mapDb.getMapRecordByEidRecord(record32, true); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 151 | |
| 152 | byte cidr24 = (byte) 24; |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 153 | LispIpv4Address eid24 = new LispIpv4Address(IpAddress.valueOf(EID_IP_PREFIX_2_24)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 154 | LispEidRecord record24 = new LispEidRecord(cidr24, eid24); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 155 | LispMapRecord mapRecord24 = mapDb.getMapRecordByEidRecord(record24, true); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 156 | |
| 157 | assertThat("Failed to fetch the RLOCs with /32 EID record", |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 158 | mapRecord32.getLocatorCount(), is(2)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 159 | assertThat("Failed to fetch the RLOCs with /24 EID record", |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 160 | mapRecord24.getLocatorCount(), is(2)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void test16MaskRange() { |
| 165 | byte cidr32 = (byte) 32; |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 166 | LispIpv4Address eid = new LispIpv4Address(IpAddress.valueOf(EID_IP_PREFIX_1_32)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 167 | LispEidRecord record32 = new LispEidRecord(cidr32, eid); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 168 | LispMapRecord mapRecord32 = mapDb.getMapRecordByEidRecord(record32, true); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 169 | |
| 170 | byte cidr24 = (byte) 24; |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 171 | LispIpv4Address eid24 = new LispIpv4Address(IpAddress.valueOf(EID_IP_PREFIX_1_24)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 172 | LispEidRecord record24 = new LispEidRecord(cidr24, eid24); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 173 | LispMapRecord mapRecord24 = mapDb.getMapRecordByEidRecord(record24, true); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 174 | |
| 175 | byte cidr16 = (byte) 16; |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 176 | LispIpv4Address eid16 = new LispIpv4Address(IpAddress.valueOf(EID_IP_PREFIX_1_16)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 177 | LispEidRecord record16 = new LispEidRecord(cidr16, eid16); |
Jian Li | cdbc087 | 2016-12-05 17:23:53 +0900 | [diff] [blame] | 178 | LispMapRecord mapRecord16 = mapDb.getMapRecordByEidRecord(record16, true); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 179 | |
| 180 | assertThat("Failed to fetch the RLOCs with /32 EID record", |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 181 | mapRecord32.getLocatorCount(), is(1)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 182 | assertThat("Failed to fetch the RLOCs with /24 EID record", |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 183 | mapRecord24.getLocatorCount(), is(1)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 184 | assertThat("Failed to fetch the RLOCs with /16 EID record", |
Jian Li | 672ebda | 2017-02-06 20:21:04 +0900 | [diff] [blame^] | 185 | mapRecord16.getLocatorCount(), is(1)); |
Jian Li | cdd276b | 2016-11-15 15:53:21 +0900 | [diff] [blame] | 186 | } |
| 187 | } |