Merge "Remove nagasena dependency declaration"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / AddressNormalizationUtil.java
index b61e2b6cb69b1db333c58030f03323bcdf99853d..15681a1a6f9dc7bfdf3c3e5dd8551ebe8d9d5532 100644 (file)
@@ -5,13 +5,12 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.impl.util;
 
 import java.net.InetAddress;
 import java.net.UnknownHostException;
-import java.util.Objects;
-import javax.annotation.Nullable;
+import java.util.Locale;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.IpConversionUtil;
 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
@@ -25,18 +24,22 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DottedQuad;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.opendaylight.ipv6.arbitrary.bitmask.fields.rev160224.Ipv6ArbitraryMask;
+import org.opendaylight.yangtools.yang.common.Uint32;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * Utility class used for converting OpenFlow port numbers, Ipv4 and Ipv6 addresses to normalized format.
  */
-public class AddressNormalizationUtil {
+public final class AddressNormalizationUtil {
     private static final Logger LOG = LoggerFactory.getLogger(AddressNormalizationUtil.class);
 
     private static final String NO_ETH_MASK = "ff:ff:ff:ff:ff:ff";
     private static final String PREFIX_SEPARATOR = "/";
 
+    private AddressNormalizationUtil() {
+    }
+
     /**
      * Extract port number from URI and convert it to OpenFlow specific textual representation.
      *
@@ -46,12 +49,14 @@ public class AddressNormalizationUtil {
      */
     @Nullable
     public static Uri normalizeProtocolAgnosticPort(@Nullable final Uri port, final short protocolVersion) {
-        if (Objects.isNull(port)) {
+        if (port == null) {
             return null;
         }
 
-        return OpenflowPortsUtil.getProtocolAgnosticPortUri(protocolVersion, InventoryDataServiceUtil
-                .portNumberfromNodeConnectorId(OpenflowVersion.get(protocolVersion), port.getValue()));
+        Uint32 portValue = InventoryDataServiceUtil
+                .portNumberfromNodeConnectorId(OpenflowVersion.get(protocolVersion), port.getValue());
+
+        return portValue == null ? null : OpenflowPortsUtil.getProtocolAgnosticPortUri(protocolVersion, portValue);
     }
 
     /**
@@ -62,12 +67,13 @@ public class AddressNormalizationUtil {
      */
     @Nullable
     public static Ipv6Prefix normalizeIpv6Prefix(@Nullable final Ipv6Prefix ipv6Prefix) {
-        if (Objects.isNull(ipv6Prefix)) {
+        if (ipv6Prefix == null) {
             return null;
         }
 
         final byte[] address = IetfInetUtil.INSTANCE.ipv6AddressBytes(IpConversionUtil.extractIpv6Address(ipv6Prefix));
-        final byte[] mask = IpConversionUtil.convertIpv6PrefixToByteArray(IpConversionUtil.extractIpv6Prefix(ipv6Prefix));
+        final byte[] mask =
+                IpConversionUtil.convertIpv6PrefixToByteArray(IpConversionUtil.extractIpv6Prefix(ipv6Prefix));
         return normalizeIpv6Address(address, mask);
     }
 
@@ -79,8 +85,9 @@ public class AddressNormalizationUtil {
      * @return normalized Ipv6 prefix
      */
     @Nullable
-    public static Ipv6Prefix normalizeIpv6Arbitrary(@Nullable final Ipv6Address ipv6Address, @Nullable final Ipv6ArbitraryMask ipv4Mask) {
-        if (Objects.isNull(ipv6Address)) {
+    public static Ipv6Prefix normalizeIpv6Arbitrary(@Nullable final Ipv6Address ipv6Address,
+                                                    @Nullable final Ipv6ArbitraryMask ipv4Mask) {
+        if (ipv6Address == null) {
             return null;
         }
 
@@ -98,9 +105,7 @@ public class AddressNormalizationUtil {
     @Nullable
     public static Ipv6Address normalizeIpv6AddressWithoutMask(@Nullable final Ipv6Address ipv6Address) {
         final Ipv6Prefix ipv6Prefix = normalizeIpv6Arbitrary(ipv6Address, null);
-        return Objects.nonNull(ipv6Prefix)
-                ? new Ipv6Address(ipv6Prefix.getValue().split(PREFIX_SEPARATOR)[0])
-                : null;
+        return ipv6Prefix == null ? null : new Ipv6Address(ipv6Prefix.getValue().split(PREFIX_SEPARATOR)[0]);
     }
 
     /**
@@ -111,12 +116,13 @@ public class AddressNormalizationUtil {
      */
     @Nullable
     public static Ipv4Prefix normalizeIpv4Prefix(@Nullable final Ipv4Prefix ipv4Prefix) {
-        if (Objects.isNull(ipv4Prefix)) {
+        if (ipv4Prefix == null) {
             return null;
         }
 
         final byte[] address = IetfInetUtil.INSTANCE.ipv4AddressBytes(IpConversionUtil.extractIpv4Address(ipv4Prefix));
-        final byte[] mask = IpConversionUtil.convertArbitraryMaskToByteArray(IpConversionUtil.extractIpv4AddressMask(ipv4Prefix));
+        final byte[] mask =
+                IpConversionUtil.convertArbitraryMaskToByteArray(IpConversionUtil.extractIpv4AddressMask(ipv4Prefix));
         return normalizeIpv4Address(address, mask);
     }
 
@@ -128,8 +134,9 @@ public class AddressNormalizationUtil {
      * @return normalized Ipv4 prefix
      */
     @Nullable
-    public static Ipv4Prefix normalizeIpv4Arbitrary(@Nullable final Ipv4Address ipv4Address, @Nullable final DottedQuad ipv4Mask) {
-        if (Objects.isNull(ipv4Address)) {
+    public static Ipv4Prefix normalizeIpv4Arbitrary(@Nullable final Ipv4Address ipv4Address,
+                                                    @Nullable final DottedQuad ipv4Mask) {
+        if (ipv4Address == null) {
             return null;
         }
 
@@ -149,7 +156,7 @@ public class AddressNormalizationUtil {
     public static Ipv4Prefix normalizeIpv4Address(@Nullable final byte[] address, @Nullable final byte[] mask) {
         final String addressPrefix = normalizeInetAddressWithMask(normalizeIpAddress(address, mask), mask);
 
-        if (Objects.isNull(addressPrefix)) {
+        if (addressPrefix == null) {
             return null;
         }
 
@@ -168,7 +175,7 @@ public class AddressNormalizationUtil {
     public static Ipv6Prefix normalizeIpv6Address(@Nullable final byte[] address, @Nullable final byte[] mask) {
         final String addressPrefix = normalizeInetAddressWithMask(normalizeIpAddress(address, mask), mask);
 
-        if (Objects.isNull(addressPrefix)) {
+        if (addressPrefix == null) {
             return null;
         }
 
@@ -184,16 +191,14 @@ public class AddressNormalizationUtil {
      */
     @Nullable
     public static InetAddress normalizeIpAddress(@Nullable final byte[] address, @Nullable final byte[] mask) {
-        if (Objects.isNull(address)) {
+        if (address == null) {
             return null;
         }
 
         final byte[] result = new byte[address.length];
 
         for (int i = 0; i < address.length; i++) {
-            result[i] = Objects.nonNull(mask) ?
-                    (byte) (address[i] & mask[i]) :
-                    address[i];
+            result[i] = mask != null ? (byte) (address[i] & mask[i]) : address[i];
         }
 
         try {
@@ -205,41 +210,36 @@ public class AddressNormalizationUtil {
     }
 
     /**
-     * Convert arbitrary mask to prefix mask and append it to textual representation of Inet address
+     * Convert arbitrary mask to prefix mask and append it to textual representation of Inet address.
      *
      * @param address the address
      * @param mask    the mask
      * @return the string
      */
     @Nullable
-    public static String normalizeInetAddressWithMask(@Nullable final InetAddress address, @Nullable final byte[] mask) {
-        if (Objects.isNull(address)) {
+    public static String normalizeInetAddressWithMask(@Nullable final InetAddress address,
+                                                      @Nullable final byte[] mask) {
+        if (address == null) {
             return null;
         }
 
-        return address.getHostAddress() +
-                (Objects.nonNull(mask)
-                        ? PREFIX_SEPARATOR + String.valueOf(IpConversionUtil.countBits(mask))
-                        : "");
+        return address.getHostAddress()
+                + (mask == null ? "" : PREFIX_SEPARATOR + String.valueOf(IpConversionUtil.countBits(mask)));
     }
 
     /**
-     * Convert MAC address to it's lower case format
+     * Convert MAC address to it's lower case format.
      *
      * @param macAddress the MAC address
      * @return normalized MAC address
      */
     @Nullable
     public static MacAddress normalizeMacAddress(@Nullable final MacAddress macAddress) {
-        if (Objects.isNull(macAddress)) {
-            return null;
-        }
-
-        return new MacAddress(macAddress.getValue().toLowerCase());
+        return macAddress == null ? null : new MacAddress(macAddress.getValue().toLowerCase(Locale.ROOT));
     }
 
     /**
-     * Convert MAC address mask to it's lower case format and if it is full F mask, return null
+     * Convert MAC address mask to it's lower case format and if it is full F mask, return null.
      *
      * @param macAddress the MAC address
      * @return normalized MAC address
@@ -248,7 +248,7 @@ public class AddressNormalizationUtil {
     public static MacAddress normalizeMacAddressMask(@Nullable final MacAddress macAddress) {
         final MacAddress normalizedMacAddress = normalizeMacAddress(macAddress);
 
-        if (Objects.isNull(normalizedMacAddress)) {
+        if (normalizedMacAddress == null) {
             return null;
         }
 
@@ -258,5 +258,4 @@ public class AddressNormalizationUtil {
 
         return normalizedMacAddress;
     }
-
 }