From 4dcba2bceea09e6da71d6191df06d7d0201e699b Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Thu, 5 May 2016 15:00:50 +0200 Subject: Fix physical address parsing Change-Id: Ib5fedab56d9bef556fc13f28a266004d2e5d73a6 Signed-off-by: Marek Gradzki --- .../v3po/interfacesstate/InterfaceUtils.java | 24 +++++++++---- .../v3po/interfacesstate/InterfaceUtilsTest.java | 40 ++++++++++++++++++++++ 2 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtilsTest.java (limited to 'v3po/v3po2vpp') diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtils.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtils.java index 979c9d6bd..691b95f98 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtils.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtils.java @@ -19,6 +19,7 @@ package io.fd.honeycomb.v3po.translate.v3po.interfacesstate; import com.google.common.base.Preconditions; import com.google.common.collect.Iterables; import java.math.BigInteger; +import java.util.Objects; import java.util.concurrent.ExecutionException; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -31,7 +32,7 @@ import org.openvpp.jvpp.future.FutureJVpp; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class InterfaceUtils { +public final class InterfaceUtils { private static final Logger LOG = LoggerFactory.getLogger(InterfaceUtils.class); private static final Gauge64 vppLinkSpeed0 = new Gauge64(BigInteger.ZERO); @@ -44,6 +45,12 @@ public class InterfaceUtils { private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray(); + private static final int PHYSICAL_ADDRESS_LENGTH = 6; + + private InterfaceUtils() { + throw new UnsupportedOperationException("This utility class cannot be instantiated"); + } + /** * Convert VPP's link speed bitmask to Yang type. 1 = 10M, 2 = 100M, 4 = 1G, 8 = 10G, 16 = 40G, 32 = 100G * @@ -76,21 +83,24 @@ public class InterfaceUtils { } /** - * Convert VPP's physical address stored byte array format to string as Yang dictates

Replace later with + * Reads first 6 bytes of supplied byte array and converts to string as Yang dictates + *

Replace later with * https://git.opendaylight.org/gerrit/#/c/34869/10/model/ietf/ietf-type- util/src/main/ * java/org/opendaylight/mdsal/model/ietf/util/AbstractIetfYangUtil.java * - * @param vppPhysAddress byte array of bytes constructing the network IF physical address. + * @param vppPhysAddress byte array of bytes in big endian order, constructing the network IF physical address. * @return String like "aa:bb:cc:dd:ee:ff" + * @throws NullPointerException if vppPhysAddress is null + * @throws IllegalArgumentException if vppPhysAddress.length < 6 */ public static String vppPhysAddrToYang(@Nonnull final byte[] vppPhysAddress) { - Preconditions.checkNotNull(vppPhysAddress, "Empty physicall address bytes"); - Preconditions.checkArgument(vppPhysAddress.length == 6, "Invalid physical address size %s, expected 6", - vppPhysAddress.length); + Objects.requireNonNull(vppPhysAddress, "Empty physical address bytes"); + Preconditions.checkArgument(PHYSICAL_ADDRESS_LENGTH <= vppPhysAddress.length, + "Invalid physical address size %s, expected >= 6", vppPhysAddress.length); StringBuilder physAddr = new StringBuilder(); appendHexByte(physAddr, vppPhysAddress[0]); - for (int i = 1; i < vppPhysAddress.length; i++) { + for (int i=1; i < PHYSICAL_ADDRESS_LENGTH; i++) { physAddr.append(":"); appendHexByte(physAddr, vppPhysAddress[i]); } diff --git a/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtilsTest.java b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtilsTest.java new file mode 100644 index 000000000..ff6ed3dde --- /dev/null +++ b/v3po/v3po2vpp/src/test/java/io/fd/honeycomb/v3po/translate/v3po/interfacesstate/InterfaceUtilsTest.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.v3po.translate.v3po.interfacesstate; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class InterfaceUtilsTest { + + @Test + public void testVppPhysAddrToYang() throws Exception { + assertEquals("01:02:03:04:05:06", InterfaceUtils.vppPhysAddrToYang(new byte[]{1, 2, 3, 4, 5, 6})); + assertEquals("0a:0b:0c:0d:0e:0f", InterfaceUtils.vppPhysAddrToYang(new byte[]{0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 0})); + } + + @Test(expected = NullPointerException.class) + public void testVppPhysAddrToYangFailNullArgument() throws Exception { + InterfaceUtils.vppPhysAddrToYang(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testVppPhysAddrToYangInvalidByteArrayLength() throws Exception { + InterfaceUtils.vppPhysAddrToYang(new byte[]{1, 2, 3, 4, 5}); + } +} \ No newline at end of file -- cgit 1.2.3-korg