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 | */ |
| 16 | package org.onosproject.lisp.ctl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableList; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.lisp.msg.protocols.DefaultLispLocatorRecord.DefaultLocatorRecordBuilder; |
| 23 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder; |
| 24 | import org.onosproject.lisp.msg.protocols.LispEidRecord; |
| 25 | import org.onosproject.lisp.msg.protocols.LispLocatorRecord; |
| 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 | */ |
| 38 | public class LispEidRlocMapTest { |
| 39 | |
| 40 | final LispEidRlocMap eidRlocMap = LispEidRlocMap.getInstance(); |
| 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); |
| 92 | LispMapRecord mapRecord1 = builder1.build(); |
| 93 | |
| 94 | MapRecordBuilder builder2 = new DefaultMapRecordBuilder(); |
| 95 | builder2.withMaskLength(cidr2); |
| 96 | builder2.withEidPrefixAfi(eid2); |
| 97 | builder2.withLocators(locatorRecords2); |
| 98 | LispMapRecord mapRecord2 = builder2.build(); |
| 99 | |
| 100 | MapRecordBuilder builder3 = new DefaultMapRecordBuilder(); |
| 101 | builder3.withMaskLength(cidr3); |
| 102 | builder3.withEidPrefixAfi(eid3); |
| 103 | builder3.withLocators(locatorRecords3); |
| 104 | LispMapRecord mapRecord3 = builder3.build(); |
| 105 | |
| 106 | eidRlocMap.insertMapRecord(eidRecord1, mapRecord1); |
| 107 | eidRlocMap.insertMapRecord(eidRecord2, mapRecord2); |
| 108 | eidRlocMap.insertMapRecord(eidRecord3, mapRecord3); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void test32MaskRange() { |
| 113 | byte cidr32 = (byte) 32; |
| 114 | LispIpv4Address eid = new LispIpv4Address(IpAddress.valueOf("10.1.1.1")); |
| 115 | LispEidRecord record = new LispEidRecord(cidr32, eid); |
| 116 | LispMapRecord mapRecord = eidRlocMap.getMapRecordByEidRecord(record); |
| 117 | |
| 118 | assertThat("Failed to fetch the RLOCs with /32 EID record", |
| 119 | mapRecord.getLocatorCount(), is(3)); |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void test24MaskRange() { |
| 124 | byte cidr32 = (byte) 32; |
| 125 | LispIpv4Address eid = new LispIpv4Address(IpAddress.valueOf("10.1.2.1")); |
| 126 | LispEidRecord record32 = new LispEidRecord(cidr32, eid); |
| 127 | LispMapRecord mapRecord32 = eidRlocMap.getMapRecordByEidRecord(record32); |
| 128 | |
| 129 | byte cidr24 = (byte) 24; |
| 130 | LispIpv4Address eid24 = new LispIpv4Address(IpAddress.valueOf("10.1.2.0")); |
| 131 | LispEidRecord record24 = new LispEidRecord(cidr24, eid24); |
| 132 | LispMapRecord mapRecord24 = eidRlocMap.getMapRecordByEidRecord(record24); |
| 133 | |
| 134 | assertThat("Failed to fetch the RLOCs with /32 EID record", |
| 135 | mapRecord32.getLocatorCount(), is(2)); |
| 136 | assertThat("Failed to fetch the RLOCs with /24 EID record", |
| 137 | mapRecord24.getLocatorCount(), is(2)); |
| 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void test16MaskRange() { |
| 142 | byte cidr32 = (byte) 32; |
| 143 | LispIpv4Address eid = new LispIpv4Address(IpAddress.valueOf("10.2.1.1")); |
| 144 | LispEidRecord record32 = new LispEidRecord(cidr32, eid); |
| 145 | LispMapRecord mapRecord32 = eidRlocMap.getMapRecordByEidRecord(record32); |
| 146 | |
| 147 | byte cidr24 = (byte) 24; |
| 148 | LispIpv4Address eid24 = new LispIpv4Address(IpAddress.valueOf("10.2.1.0")); |
| 149 | LispEidRecord record24 = new LispEidRecord(cidr24, eid24); |
| 150 | LispMapRecord mapRecord24 = eidRlocMap.getMapRecordByEidRecord(record24); |
| 151 | |
| 152 | byte cidr16 = (byte) 16; |
| 153 | LispIpv4Address eid16 = new LispIpv4Address(IpAddress.valueOf("10.2.0.0")); |
| 154 | LispEidRecord record16 = new LispEidRecord(cidr16, eid16); |
| 155 | LispMapRecord mapRecord16 = eidRlocMap.getMapRecordByEidRecord(record16); |
| 156 | |
| 157 | assertThat("Failed to fetch the RLOCs with /32 EID record", |
| 158 | mapRecord32.getLocatorCount(), is(1)); |
| 159 | assertThat("Failed to fetch the RLOCs with /24 EID record", |
| 160 | mapRecord24.getLocatorCount(), is(1)); |
| 161 | assertThat("Failed to fetch the RLOCs with /16 EID record", |
| 162 | mapRecord16.getLocatorCount(), is(1)); |
| 163 | } |
| 164 | } |