Jian Li | b8436bb | 2017-02-23 03:39:08 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.provider.lisp.mapping.util; |
| 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.onlab.packet.IpPrefix; |
| 23 | import org.onosproject.lisp.msg.protocols.DefaultLispLocator.DefaultLocatorBuilder; |
| 24 | import org.onosproject.lisp.msg.protocols.DefaultLispMapNotify.DefaultNotifyBuilder; |
| 25 | import org.onosproject.lisp.msg.protocols.DefaultLispMapRecord.DefaultMapRecordBuilder; |
| 26 | import org.onosproject.lisp.msg.protocols.DefaultLispMapReply.DefaultReplyBuilder; |
| 27 | import org.onosproject.lisp.msg.protocols.LispLocator; |
| 28 | import org.onosproject.lisp.msg.protocols.LispLocator.LocatorBuilder; |
| 29 | import org.onosproject.lisp.msg.protocols.LispMapNotify; |
| 30 | import org.onosproject.lisp.msg.protocols.LispMapNotify.NotifyBuilder; |
| 31 | import org.onosproject.lisp.msg.protocols.LispMapRecord; |
| 32 | import org.onosproject.lisp.msg.protocols.LispMapRecord.MapRecordBuilder; |
| 33 | import org.onosproject.lisp.msg.protocols.LispMapReply; |
| 34 | import org.onosproject.lisp.msg.protocols.LispMapReply.ReplyBuilder; |
| 35 | import org.onosproject.lisp.msg.protocols.LispMapReplyAction; |
| 36 | import org.onosproject.lisp.msg.types.LispIpv4Address; |
| 37 | import org.onosproject.mapping.MappingEntry; |
| 38 | import org.onosproject.mapping.MappingKey; |
| 39 | import org.onosproject.mapping.MappingTreatment; |
| 40 | import org.onosproject.mapping.MappingValue; |
| 41 | import org.onosproject.mapping.actions.MappingAction; |
| 42 | import org.onosproject.mapping.addresses.IPMappingAddress; |
| 43 | import org.onosproject.net.DeviceId; |
| 44 | |
| 45 | import java.util.List; |
| 46 | |
| 47 | import static org.hamcrest.MatcherAssert.assertThat; |
| 48 | import static org.hamcrest.Matchers.is; |
| 49 | |
| 50 | /** |
| 51 | * Mapping entry builder unit test. |
| 52 | */ |
| 53 | public 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 | } |