blob: 8b60e2e06bf1705a41a7fc0b3806bba453b98350 [file] [log] [blame]
Jian Licdd276b2016-11-15 15:53:21 +09001/*
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 */
16package org.onosproject.lisp.ctl;
17
18import com.google.common.collect.ImmutableList;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.packet.IpAddress;
22import org.onosproject.lisp.msg.protocols.DefaultLispLocatorRecord.DefaultLocatorRecordBuilder;
23import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder;
24import org.onosproject.lisp.msg.protocols.LispEidRecord;
25import org.onosproject.lisp.msg.protocols.LispLocatorRecord;
26import org.onosproject.lisp.msg.protocols.LispMapRecord;
27import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder;
28import org.onosproject.lisp.msg.types.LispIpv4Address;
29
30import java.util.List;
31
32import static org.hamcrest.MatcherAssert.assertThat;
33import static org.hamcrest.Matchers.is;
34
35/**
36 * Tests for LISP EID RLOC Map class.
37 */
Jian Li29986d82016-12-01 03:25:12 +090038public class LispMappingDatabaseTest {
Jian Licdd276b2016-11-15 15:53:21 +090039
Jian Li29986d82016-12-01 03:25:12 +090040 final LispMappingDatabase mapDb = LispMappingDatabase.getInstance();
Jian Licdd276b2016-11-15 15:53:21 +090041
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 Li29986d82016-12-01 03:25:12 +090092 builder1.withRecordTtl(60);
Jian Licdd276b2016-11-15 15:53:21 +090093 LispMapRecord mapRecord1 = builder1.build();
94
95 MapRecordBuilder builder2 = new DefaultMapRecordBuilder();
96 builder2.withMaskLength(cidr2);
97 builder2.withEidPrefixAfi(eid2);
98 builder2.withLocators(locatorRecords2);
Jian Li29986d82016-12-01 03:25:12 +090099 builder2.withRecordTtl(60);
Jian Licdd276b2016-11-15 15:53:21 +0900100 LispMapRecord mapRecord2 = builder2.build();
101
102 MapRecordBuilder builder3 = new DefaultMapRecordBuilder();
103 builder3.withMaskLength(cidr3);
104 builder3.withEidPrefixAfi(eid3);
105 builder3.withLocators(locatorRecords3);
Jian Li29986d82016-12-01 03:25:12 +0900106 builder3.withRecordTtl(60);
Jian Licdd276b2016-11-15 15:53:21 +0900107 LispMapRecord mapRecord3 = builder3.build();
108
Jian Li29986d82016-12-01 03:25:12 +0900109 mapDb.putMapRecord(eidRecord1, mapRecord1);
110 mapDb.putMapRecord(eidRecord2, mapRecord2);
111 mapDb.putMapRecord(eidRecord3, mapRecord3);
Jian Licdd276b2016-11-15 15:53:21 +0900112 }
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 Li29986d82016-12-01 03:25:12 +0900119 LispMapRecord mapRecord = mapDb.getMapRecordByEidRecord(record);
Jian Licdd276b2016-11-15 15:53:21 +0900120
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 Li29986d82016-12-01 03:25:12 +0900130 LispMapRecord mapRecord32 = mapDb.getMapRecordByEidRecord(record32);
Jian Licdd276b2016-11-15 15:53:21 +0900131
132 byte cidr24 = (byte) 24;
133 LispIpv4Address eid24 = new LispIpv4Address(IpAddress.valueOf("10.1.2.0"));
134 LispEidRecord record24 = new LispEidRecord(cidr24, eid24);
Jian Li29986d82016-12-01 03:25:12 +0900135 LispMapRecord mapRecord24 = mapDb.getMapRecordByEidRecord(record24);
Jian Licdd276b2016-11-15 15:53:21 +0900136
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 Li29986d82016-12-01 03:25:12 +0900148 LispMapRecord mapRecord32 = mapDb.getMapRecordByEidRecord(record32);
Jian Licdd276b2016-11-15 15:53:21 +0900149
150 byte cidr24 = (byte) 24;
151 LispIpv4Address eid24 = new LispIpv4Address(IpAddress.valueOf("10.2.1.0"));
152 LispEidRecord record24 = new LispEidRecord(cidr24, eid24);
Jian Li29986d82016-12-01 03:25:12 +0900153 LispMapRecord mapRecord24 = mapDb.getMapRecordByEidRecord(record24);
Jian Licdd276b2016-11-15 15:53:21 +0900154
155 byte cidr16 = (byte) 16;
156 LispIpv4Address eid16 = new LispIpv4Address(IpAddress.valueOf("10.2.0.0"));
157 LispEidRecord record16 = new LispEidRecord(cidr16, eid16);
Jian Li29986d82016-12-01 03:25:12 +0900158 LispMapRecord mapRecord16 = mapDb.getMapRecordByEidRecord(record16);
Jian Licdd276b2016-11-15 15:53:21 +0900159
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}