Jian Li | f9ec083 | 2017-04-09 20:49:48 +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.drivers.lisp.extensions.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import org.hamcrest.Description; |
| 22 | import org.hamcrest.TypeSafeDiagnosingMatcher; |
| 23 | import org.junit.After; |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.packet.IpPrefix; |
| 27 | import org.onosproject.codec.CodecContext; |
| 28 | import org.onosproject.codec.JsonCodec; |
| 29 | import org.onosproject.codec.impl.CodecManager; |
| 30 | import org.onosproject.drivers.lisp.extensions.LispMappingExtensionCodecRegistrator; |
| 31 | import org.onosproject.drivers.lisp.extensions.LispNatAddress; |
| 32 | import org.onosproject.mapping.addresses.MappingAddress; |
| 33 | import org.onosproject.mapping.addresses.MappingAddresses; |
| 34 | import org.onosproject.mapping.web.codec.MappingAddressJsonMatcher; |
| 35 | |
| 36 | import java.io.IOException; |
| 37 | import java.io.InputStream; |
| 38 | import java.util.List; |
| 39 | |
| 40 | import static org.hamcrest.MatcherAssert.assertThat; |
| 41 | import static org.hamcrest.Matchers.is; |
| 42 | import static org.hamcrest.Matchers.notNullValue; |
| 43 | |
| 44 | /** |
| 45 | * Unit tests for LispNatAddressCodec. |
| 46 | */ |
| 47 | public class LispNatAddressCodecTest { |
| 48 | |
| 49 | private static final short MS_UDP_PORT_NUMBER = (short) 1; |
| 50 | private static final short ETR_UDP_PORT_NUMBER = (short) 2; |
| 51 | private static final IpPrefix GLOBAL_ETR_RLOC_ADDRESS = IpPrefix.valueOf("10.1.1.1/24"); |
| 52 | private static final IpPrefix MS_RLOC_ADDRESS = IpPrefix.valueOf("10.1.1.2/24"); |
| 53 | private static final IpPrefix PRIVATE_ETR_RLOC_ADDRESS = IpPrefix.valueOf("10.1.1.3/24"); |
| 54 | |
| 55 | private CodecContext context; |
| 56 | private JsonCodec<LispNatAddress> natAddressCodec; |
| 57 | private LispMappingExtensionCodecRegistrator registrator; |
| 58 | |
| 59 | /** |
| 60 | * Sets up for each test. |
| 61 | * Creates a context and fetches the LispNatAddress codec. |
| 62 | */ |
| 63 | @Before |
| 64 | public void setUp() { |
| 65 | CodecManager manager = new CodecManager(); |
| 66 | registrator = new LispMappingExtensionCodecRegistrator(); |
| 67 | registrator.codecService = manager; |
| 68 | registrator.activate(); |
| 69 | |
| 70 | context = new LispMappingExtensionCodecContextAdapter(registrator.codecService); |
| 71 | natAddressCodec = context.codec(LispNatAddress.class); |
| 72 | assertThat("NAT address codec should not be null", |
| 73 | natAddressCodec, notNullValue()); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Deactivates the codec registrator. |
| 78 | */ |
| 79 | @After |
| 80 | public void tearDown() { |
| 81 | registrator.deactivate(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Tests encoding of a LispNatAddress object. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testLispNatAddressEncode() { |
| 89 | |
| 90 | List<MappingAddress> rtrRlocs = |
| 91 | ImmutableList.of(MappingAddresses.ipv4MappingAddress(GLOBAL_ETR_RLOC_ADDRESS), |
| 92 | MappingAddresses.ipv4MappingAddress(MS_RLOC_ADDRESS), |
| 93 | MappingAddresses.ipv4MappingAddress(PRIVATE_ETR_RLOC_ADDRESS)); |
| 94 | |
| 95 | |
| 96 | LispNatAddress address = new LispNatAddress.Builder() |
| 97 | .withMsUdpPortNumber(MS_UDP_PORT_NUMBER) |
| 98 | .withEtrUdpPortNumber(ETR_UDP_PORT_NUMBER) |
| 99 | .withGlobalEtrRlocAddress(MappingAddresses.ipv4MappingAddress(GLOBAL_ETR_RLOC_ADDRESS)) |
| 100 | .withMsRlocAddress(MappingAddresses.ipv4MappingAddress(MS_RLOC_ADDRESS)) |
| 101 | .withPrivateEtrRlocAddress(MappingAddresses.ipv4MappingAddress(PRIVATE_ETR_RLOC_ADDRESS)) |
| 102 | .withRtrRlocAddresses(rtrRlocs) |
| 103 | .build(); |
| 104 | |
| 105 | ObjectNode addressJson = natAddressCodec.encode(address, context); |
| 106 | assertThat("errors in encoding NAT address JSON", |
| 107 | addressJson, LispNatAddressJsonMatcher.matchesNatAddress(address)); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Tests decoding of a LispNatAddress JSON object. |
| 112 | */ |
| 113 | @Test |
| 114 | public void testLispNatAddressDecode() throws IOException { |
| 115 | LispNatAddress address = getLispNatAddress("LispNatAddress.json"); |
| 116 | |
| 117 | assertThat("incorrect MS UDP port number", |
| 118 | address.getMsUdpPortNumber(), is(MS_UDP_PORT_NUMBER)); |
| 119 | assertThat("incorrect ETR UDP port number", |
| 120 | address.getEtrUdpPortNumber(), is(ETR_UDP_PORT_NUMBER)); |
| 121 | assertThat("incorrect global ETR RLOC address", address.getGlobalEtrRlocAddress(), |
| 122 | is(MappingAddresses.ipv4MappingAddress(GLOBAL_ETR_RLOC_ADDRESS))); |
| 123 | assertThat("incorrect MS RLOC address", address.getMsRlocAddress(), |
| 124 | is(MappingAddresses.ipv4MappingAddress(MS_RLOC_ADDRESS))); |
| 125 | assertThat("incorrect private ETR RLOC address", address.getPrivateEtrRlocAddress(), |
| 126 | is(MappingAddresses.ipv4MappingAddress(PRIVATE_ETR_RLOC_ADDRESS))); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Hamcrest matcher for LispNatAddress. |
| 131 | */ |
| 132 | public static final class LispNatAddressJsonMatcher |
| 133 | extends TypeSafeDiagnosingMatcher<JsonNode> { |
| 134 | |
| 135 | private final LispNatAddress address; |
| 136 | |
| 137 | /** |
| 138 | * Default constructor. |
| 139 | * |
| 140 | * @param address LispNatAddress object |
| 141 | */ |
| 142 | private LispNatAddressJsonMatcher(LispNatAddress address) { |
| 143 | this.address = address; |
| 144 | } |
| 145 | |
| 146 | private int filteredSize(JsonNode node) { |
| 147 | return node.size(); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | protected boolean matchesSafely(JsonNode jsonNode, Description description) { |
| 152 | |
| 153 | // check MS UDP port number |
| 154 | short jsonMsUdpPortNumber = (short) |
| 155 | jsonNode.get(LispNatAddressCodec.MS_UDP_PORT_NUMBER).asInt(); |
| 156 | short msUdpPortNumber = address.getMsUdpPortNumber(); |
| 157 | if (jsonMsUdpPortNumber != msUdpPortNumber) { |
| 158 | description.appendText("MS UDP port number was " + jsonMsUdpPortNumber); |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | // check ETR UDP port number |
| 163 | short jsonEtrUdpPortNumber = (short) |
| 164 | jsonNode.get(LispNatAddressCodec.ETR_UDP_PORT_NUMBER).asInt(); |
| 165 | short etrUdpPortNumber = address.getEtrUdpPortNumber(); |
| 166 | if (jsonEtrUdpPortNumber != etrUdpPortNumber) { |
| 167 | description.appendText("ETR UDP port number was " + jsonEtrUdpPortNumber); |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | // check RTR RLOC addresses |
| 172 | final JsonNode jsonRtrRlocs = jsonNode.get(LispNatAddressCodec.RTR_RLOC_ADDRESSES); |
| 173 | |
| 174 | if (address.getRtrRlocAddresses().size() != filteredSize(jsonRtrRlocs)) { |
| 175 | description.appendText("addresses array size of " + |
| 176 | Integer.toString(address.getRtrRlocAddresses().size())); |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | for (final MappingAddress address : address.getRtrRlocAddresses()) { |
| 181 | boolean addressFound = false; |
| 182 | for (int addressIndex = 0; addressIndex < jsonRtrRlocs.size(); addressIndex++) { |
| 183 | final String jsonType = |
| 184 | jsonRtrRlocs.get(addressIndex).get("type").asText(); |
| 185 | final String addressType = address.type().name(); |
| 186 | if (jsonType.equals(addressType)) { |
| 187 | addressFound = true; |
| 188 | } |
| 189 | } |
| 190 | if (!addressFound) { |
| 191 | description.appendText("address " + address.toString()); |
| 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // check global ETR RLOC address |
| 197 | MappingAddressJsonMatcher globalEtrRlocMatcher = |
| 198 | MappingAddressJsonMatcher.matchesMappingAddress( |
| 199 | address.getGlobalEtrRlocAddress()); |
| 200 | |
| 201 | // check MS RLOC address |
| 202 | MappingAddressJsonMatcher msRlocMatcher = |
| 203 | MappingAddressJsonMatcher.matchesMappingAddress( |
| 204 | address.getMsRlocAddress()); |
| 205 | |
| 206 | // check private ETR RLOC address |
| 207 | MappingAddressJsonMatcher privateEtrRlocMatcher = |
| 208 | MappingAddressJsonMatcher.matchesMappingAddress( |
| 209 | address.getPrivateEtrRlocAddress()); |
| 210 | |
| 211 | return globalEtrRlocMatcher.matches(jsonNode.get(LispNatAddressCodec.GLOBAL_ETR_RLOC_ADDRESS)) || |
| 212 | msRlocMatcher.matches(jsonNode.get(LispNatAddressCodec.MS_RLOC_ADDRESS)) || |
| 213 | privateEtrRlocMatcher.matches(jsonNode.get(LispNatAddressCodec.PRIVATE_ETR_RLOC_ADDRESS)); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public void describeTo(Description description) { |
| 218 | description.appendText(address.toString()); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Factory to allocate a LispNatAddress matcher. |
| 223 | * |
| 224 | * @param address LispNatAddress object we are looking for |
| 225 | * @return matcher |
| 226 | */ |
| 227 | public static LispNatAddressJsonMatcher matchesNatAddress(LispNatAddress address) { |
| 228 | return new LispNatAddressJsonMatcher(address); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Reads in a LispNatAddress from the given resource and decodes it. |
| 234 | * |
| 235 | * @param resourceName resource to use to read the JSON for the rule |
| 236 | * @return decoded LispGcAddress |
| 237 | * @throws IOException if processing the resource fails |
| 238 | */ |
| 239 | private LispNatAddress getLispNatAddress(String resourceName) throws IOException { |
| 240 | InputStream jsonStream = LispNatAddressCodecTest.class.getResourceAsStream(resourceName); |
| 241 | JsonNode json = context.mapper().readTree(jsonStream); |
| 242 | assertThat("JSON string should not be null", json, notNullValue()); |
| 243 | LispNatAddress natAddress = natAddressCodec.decode((ObjectNode) json, context); |
| 244 | assertThat("decoded address should not be null", natAddress, notNullValue()); |
| 245 | return natAddress; |
| 246 | } |
| 247 | } |