blob: c6bf5fa83bac440c6eb9c173f5d3d7abca1986f2 [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;
23import org.onosproject.lisp.msg.protocols.DefaultLispLocator.DefaultLocatorBuilder;
24import org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.DefaultNotifyBuilder;
25import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder;
26import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.DefaultReplyBuilder;
27import org.onosproject.lisp.msg.protocols.LispLocator;
28import org.onosproject.lisp.msg.protocols.LispLocator.LocatorBuilder;
29import org.onosproject.lisp.msg.protocols.LispMapNotify;
30import org.onosproject.lisp.msg.protocols.LispMapNotify.NotifyBuilder;
31import org.onosproject.lisp.msg.protocols.LispMapRecord;
32import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder;
33import org.onosproject.lisp.msg.protocols.LispMapReply;
34import org.onosproject.lisp.msg.protocols.LispMapReply.ReplyBuilder;
35import org.onosproject.lisp.msg.protocols.LispMapReplyAction;
36import org.onosproject.lisp.msg.types.LispIpv4Address;
37import org.onosproject.mapping.MappingEntry;
38import org.onosproject.mapping.MappingKey;
39import org.onosproject.mapping.MappingTreatment;
40import org.onosproject.mapping.MappingValue;
41import org.onosproject.mapping.actions.MappingAction;
42import org.onosproject.mapping.addresses.IPMappingAddress;
43import org.onosproject.net.DeviceId;
44
45import java.util.List;
46
47import static org.hamcrest.MatcherAssert.assertThat;
48import static org.hamcrest.Matchers.is;
49
50/**
51 * Mapping entry builder unit test.
52 */
53public class MappingEntryBuilderTest {
54
55 private static final String IP_RECORD_ADDRESS = "192.168.1.1";
56 private static final String IP_LOCATOR_ADDRESS = "10.1.1.1";
57 private static final int IP_RECORD_MASK_LENGTH = 24;
58 private static final int IP_LOCATOR_MASK_LENGTH = 32;
59
60 private static final String AUTH_KEY = "onos";
61
62 private static final byte UNIQUE_VALUE = (byte) 0x01;
63 private static final DeviceId DEVICE_ID = DeviceId.deviceId("lisp:10.1.1.2");
64
65 private LispMapReply mapReply;
66 private LispMapNotify mapNotify;
67
68 @Before
69 public void setUp() {
70 ReplyBuilder replyBuilder = new DefaultReplyBuilder();
71
72 List<LispMapRecord> records = ImmutableList.of(getMapRecord());
73
74 mapReply = replyBuilder
75 .withIsEtr(true)
76 .withIsProbe(false)
77 .withIsSecurity(true)
78 .withNonce(1L)
79 .withMapRecords(records)
80 .build();
81
82 NotifyBuilder notifyBuilder = new DefaultNotifyBuilder();
83
84 mapNotify = notifyBuilder
85 .withKeyId((short) 1)
86 .withAuthKey(AUTH_KEY)
87 .withNonce(1L)
88 .withMapRecords(records)
89 .build();
90 }
91
92 @Test
93 public void testMapReplyConversion() {
94 List<LispMapRecord> replyRecords = mapReply.getMapRecords();
95
96 assertThat(replyRecords.size(), is(1));
97
98 testMapRecorConversion(replyRecords.get(0));
99 }
100
101 @Test
102 public void testMapNotifyConversion() {
103 List<LispMapRecord> notifyRecords = mapNotify.getMapRecords();
104
105 assertThat(notifyRecords.size(), is(1));
106
107 testMapRecorConversion(notifyRecords.get(0));
108 }
109
110 private void testMapRecorConversion(LispMapRecord record) {
111 MappingEntry mappingEntry =
112 new MappingEntryBuilder(DEVICE_ID, record).build();
113 MappingKey key = mappingEntry.key();
114 MappingValue value = mappingEntry.value();
115
116 IPMappingAddress recordAddress = (IPMappingAddress) key.address();
117
118 assertThat(recordAddress.ip(), is(IpPrefix.valueOf(IP_RECORD_ADDRESS + "/" +
119 IP_RECORD_MASK_LENGTH)));
120
121 assertThat(value.action().type(), is(MappingAction.Type.NATIVE_FORWARD));
122
123 assertThat(value.treatments().size(), is(1));
124
125 MappingTreatment treatment = value.treatments().get(0);
126 IPMappingAddress locatorAddress = (IPMappingAddress) treatment.address();
127
128 assertThat(locatorAddress.ip(), is(IpPrefix.valueOf(IP_LOCATOR_ADDRESS + "/" +
129 IP_LOCATOR_MASK_LENGTH)));
130 }
131
132 private LispMapRecord getMapRecord() {
133 MapRecordBuilder recordBuilder = new DefaultMapRecordBuilder();
134
135 LispIpv4Address recordAddress =
136 new LispIpv4Address(IpAddress.valueOf(IP_RECORD_ADDRESS));
137
138 LocatorBuilder locatorBuilder = new DefaultLocatorBuilder();
139
140 LispIpv4Address locatorAddress =
141 new LispIpv4Address(IpAddress.valueOf(IP_LOCATOR_ADDRESS));
142
143 LispLocator locator1 = locatorBuilder
144 .withPriority(UNIQUE_VALUE)
145 .withWeight(UNIQUE_VALUE)
146 .withMulticastPriority(UNIQUE_VALUE)
147 .withMulticastWeight(UNIQUE_VALUE)
148 .withLocalLocator(true)
149 .withRlocProbed(false)
150 .withRouted(true)
151 .withLocatorAfi(locatorAddress)
152 .build();
153
154 return recordBuilder
155 .withRecordTtl(100)
156 .withIsAuthoritative(true)
157 .withMapVersionNumber((short) 1)
158 .withMaskLength((byte) IP_RECORD_MASK_LENGTH)
159 .withAction(LispMapReplyAction.NativelyForward)
160 .withEidPrefixAfi(recordAddress)
161 .withLocators(ImmutableList.of(locator1))
162 .build();
163 }
164}