IllegalArgument Exception changed to Preconditions checks 70/23770/8
authorIveta Halanova <iveta.halanova@pantheon.sk>
Mon, 6 Jul 2015 07:31:10 +0000 (09:31 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 6 Jul 2015 18:16:31 +0000 (18:16 +0000)
Change-Id: I30f607cbc6cbe1e6abd20b0bb375a4073e28cd17
Signed-off-by: Iveta Halanova <iveta.halanova@pantheon.sk>
28 files changed:
bgp/concepts/src/main/java/org/opendaylight/bgp/concepts/NextHopUtil.java
bgp/parser-api/src/main/java/org/opendaylight/protocol/bgp/parser/BGPError.java
bgp/parser-impl/src/main/java/org/opendaylight/protocol/bgp/parser/impl/message/open/CapabilityParameterParser.java
bgp/parser-mock/src/main/java/org/opendaylight/protocol/bgp/parser/mock/BGPMessageParserMock.java
bgp/rib-impl/src/main/java/org/opendaylight/controller/config/yang/bgp/rib/impl/BGPPeerAcceptorModule.java
bgp/rib-impl/src/main/java/org/opendaylight/controller/config/yang/bgp/rib/impl/BGPPeerModule.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/StrictBGPPeerRegistry.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/crabbe/initiated00/PcinitiateMessageParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02PCReplyMessageParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02PCReportMessageParser.java
pcep/ietf-stateful02/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful02/Stateful02PCUpdateRequestMessageParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/initiated00/CInitiated00PCInitiateMessageParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07ErrorMessageParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LSPIdentifierIpv4TlvParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07LSPIdentifierIpv6TlvParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07PCReportMessageParser.java
pcep/ietf-stateful07/src/main/java/org/opendaylight/protocol/pcep/ietf/stateful07/Stateful07PCUpdateRequestMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPCloseMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPErrorMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPMonitoringReplyMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPMonitoringRequestMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPNotificationMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPOpenMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPReplyMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/message/PCEPRequestMessageParser.java
pcep/impl/src/main/java/org/opendaylight/protocol/pcep/impl/tls/SslKeyStore.java
pcep/topology-provider/src/main/java/org/opendaylight/controller/config/yang/pcep/topology/provider/PCEPTopologyProviderModule.java
util/src/main/java/org/opendaylight/protocol/util/ByteArray.java

index 78b3da4f8bb5b6a9cc9e3366eb825c8414c392f8..04e56ec2e7e1d8c1eb736e9bd2dd1e44091e90da 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.bgp.concepts;
 
+import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import org.opendaylight.protocol.util.Ipv4Util;
 import org.opendaylight.protocol.util.Ipv6Util;
@@ -39,9 +40,7 @@ public final class NextHopUtil {
             byteAggregator.writeBytes(Ipv4Util.bytesForAddress(((Ipv4NextHopCase) cnextHop).getIpv4NextHop().getGlobal()));
         } else if (cnextHop instanceof Ipv6NextHopCase) {
             final Ipv6NextHop nextHop = ((Ipv6NextHopCase) cnextHop).getIpv6NextHop();
-            if (nextHop.getGlobal() == null) {
-                throw new IllegalArgumentException("Ipv6 Next Hop is missing Global address.");
-            }
+            Preconditions.checkArgument(nextHop.getGlobal() != null, "Ipv6 Next Hop is missing Global address.");
             byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHop.getGlobal()));
             if (nextHop.getLinkLocal() != null) {
                 byteAggregator.writeBytes(Ipv6Util.bytesForAddress(nextHop.getLinkLocal()));
index f3e98dd80a7febe8613cea688a321cf221683db2..4bed06f59b6cc7a6e887242e26dae5a7c939a900 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.protocol.bgp.parser;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Maps;
 import java.util.Map;
 
@@ -172,9 +173,7 @@ public enum BGPError {
 
     public static BGPError forValue(final int code, final int subcode) {
         final BGPError e = VALUE_MAP.get(new BGPErrorIdentifier((short) code, (short) subcode));
-        if (e != null) {
-            return e;
-        }
-        throw new IllegalArgumentException("BGP Error code " + code + " and subcode " + subcode + " not recognized.");
+        Preconditions.checkArgument(e != null, "BGP Error code %s and subcode %s not recognized.", code, subcode);
+        return e;
     }
 }
index 04c7cf82feb6f7d6a89c821f0d96d83bdff082fc..cb35a54c0f138764fa775fa89f78de107bb8b33b 100644 (file)
@@ -89,9 +89,7 @@ public final class CapabilityParameterParser implements ParameterParser, Paramet
             final CParameters cap = optionalCapa.getCParameters();
             final ByteBuf bytes = Unpooled.buffer();
             this.reg.serializeCapability(cap, bytes);
-            if (bytes == null) {
-                throw new IllegalArgumentException("Unhandled capability class" + cap.getImplementedInterface());
-            }
+            Preconditions.checkArgument(bytes != null, "Unhandled capability class %s", cap.getImplementedInterface());
 
             if (LOG.isTraceEnabled()) {
                 LOG.trace("BGP capability serialized to: {}", ByteBufUtil.hexDump(bytes));
index 99a70e1a58dc30a6cffa53db71e708f5b7b0f923..43aa10638a28bba8241d6702a77fff7e320e222a 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.protocol.bgp.parser.mock;
 
+import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import java.util.Map;
 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
@@ -34,9 +35,7 @@ public class BGPMessageParserMock implements MessageRegistry {
     @Override
     public Notification parseMessage(final ByteBuf buffer) throws BGPParsingException, BGPDocumentedException {
         final Notification ret = this.messages.get(buffer);
-        if (ret == null) {
-            throw new IllegalArgumentException("Undefined message encountered");
-        }
+        Preconditions.checkArgument(ret != null, "Undefined message encountered");
         return ret;
     }
 
index 60fb42cd1d1903023c668bf8c3adba4800d25326..c223f7a588db2ef0e25f2f6131f584e2218909ea 100644 (file)
@@ -1,5 +1,6 @@
 package org.opendaylight.controller.config.yang.bgp.rib.impl;
 
+import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import io.netty.channel.ChannelFuture;
 import io.netty.util.concurrent.Future;
@@ -19,11 +20,11 @@ public class BGPPeerAcceptorModule extends org.opendaylight.controller.config.ya
 
     private static final int PRIVILEGED_PORTS = 1024;
 
-    public BGPPeerAcceptorModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
+    public BGPPeerAcceptorModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
         super(identifier, dependencyResolver);
     }
 
-    public BGPPeerAcceptorModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerAcceptorModule oldModule, java.lang.AutoCloseable oldInstance) {
+    public BGPPeerAcceptorModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, final org.opendaylight.controller.config.yang.bgp.rib.impl.BGPPeerAcceptorModule oldModule, final java.lang.AutoCloseable oldInstance) {
         super(identifier, dependencyResolver, oldModule, oldInstance);
     }
 
@@ -48,10 +49,8 @@ public class BGPPeerAcceptorModule extends org.opendaylight.controller.config.ya
         // Validate future success
         future.addListener(new GenericFutureListener<Future<Void>>() {
             @Override
-            public void operationComplete(Future<Void> future) {
-                if(!future.isSuccess()) {
-                    throw new IllegalStateException(String.format("Unable to start bgp server on %s", getAddress()), future.cause());
-                }
+            public void operationComplete(final Future<Void> future) {
+                Preconditions.checkArgument(future.isSuccess(), "Unable to start bgp server on %s", getAddress(), future.cause());
             }
         });
 
index 867579146648f99696290071258ea624500f964b..c5153be899e532cbff953fb4223774901db16590 100644 (file)
@@ -17,6 +17,7 @@
 package org.opendaylight.controller.config.yang.bgp.rib.impl;
 
 import com.google.common.base.Charsets;
+import com.google.common.base.Preconditions;
 import com.google.common.net.InetAddresses;
 import io.netty.util.concurrent.Future;
 import java.net.InetSocketAddress;
@@ -92,13 +93,11 @@ public final class BGPPeerModule extends org.opendaylight.controller.config.yang
 
     private InetSocketAddress createAddress() {
         final IpAddress ip = getHost();
+        Preconditions.checkArgument(ip.getIpv4Address() != null || ip.getIpv6Address() != null, "Failed to handle host %s", ip);
         if (ip.getIpv4Address() != null) {
             return new InetSocketAddress(InetAddresses.forString(ip.getIpv4Address().getValue()), getPort().getValue());
-        } else if (ip.getIpv6Address() != null) {
-            return new InetSocketAddress(InetAddresses.forString(ip.getIpv6Address().getValue()), getPort().getValue());
-        } else {
-            throw new IllegalStateException("Failed to handle host " + getHost());
         }
+        return new InetSocketAddress(InetAddresses.forString(ip.getIpv6Address().getValue()), getPort().getValue());
     }
 
     @Override
@@ -193,13 +192,11 @@ public final class BGPPeerModule extends org.opendaylight.controller.config.yang
         // FIXME we need to remove field "value" from IpAddress since equals does not work as expected when value being present
         // Remove after this bug is fixed https://bugs.opendaylight.org/show_bug.cgi?id=1276
         final IpAddress host = super.getHost();
+        Preconditions.checkArgument(host.getIpv4Address() != null || host.getIpv6Address() != null, "Unexpected host %s", host);
         if(host.getIpv4Address() != null) {
             return new IpAddress(host.getIpv4Address());
-        } else if(host.getIpv6Address() != null){
-            return new IpAddress(host.getIpv6Address());
         }
-
-        throw new IllegalArgumentException("Unexpected host " + host);
+        return new IpAddress(host.getIpv6Address());
     }
 
     private io.netty.util.concurrent.Future<Void> initiateConnection(final InetSocketAddress address, final String password, final AsNumber remoteAs, final BGPPeerRegistry registry) {
index 57afa27e02ada4643bffa69a0e8299caf8cb485a..a3960a606e3a7cbb0c4fe7ec09f868a62657d50c 100644 (file)
@@ -177,13 +177,11 @@ public final class StrictBGPPeerRegistry implements BGPPeerRegistry {
         Preconditions.checkArgument(socketAddress instanceof InetSocketAddress, "Expecting InetSocketAddress but was %s", socketAddress.getClass());
         final InetAddress inetAddress = ((InetSocketAddress) socketAddress).getAddress();
 
+        Preconditions.checkArgument(inetAddress instanceof Inet4Address || inetAddress instanceof Inet6Address, "Expecting %s or %s but was %s", Inet4Address.class, Inet6Address.class, inetAddress.getClass());
         if(inetAddress instanceof Inet4Address) {
             return new IpAddress(new Ipv4Address(inetAddress.getHostAddress()));
-        } else if(inetAddress instanceof Inet6Address) {
-            return new IpAddress(new Ipv6Address(inetAddress.getHostAddress()));
         }
-
-        throw new IllegalArgumentException("Expecting " + Inet4Address.class + " or " + Inet6Address.class + " but was " + inetAddress.getClass());
+        return new IpAddress(new Ipv6Address(inetAddress.getHostAddress()));
     }
 
     @Override
index 429a3d2909e8764dacef62e3db73e5564a5969da..2a9c28d65f17afbd5acc2aa09b541b8fa15fc61e 100644 (file)
@@ -63,9 +63,7 @@ public class PcinitiateMessageParser extends AbstractMessageParser {
 
     @Override
     protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
         final PcinitiateMessageBuilder builder = new PcinitiateMessageBuilder();
         final List<Requests> reqs = Lists.newArrayList();
         while (!objects.isEmpty()) {
index 25800dc2236a07f09bdf7b0124e9561fa1b88bec..cfdf213d435ad31f34abc8678bcc3c21b8798dd9 100644 (file)
@@ -53,14 +53,10 @@ public final class Stateful02PCReplyMessageParser extends PCEPReplyMessageParser
     public void serializeMessage(final Message message, final ByteBuf out) {
         Preconditions.checkArgument(message instanceof Pcrep, "Wrong instance of Message. Passed instance of %s. Need Pcrep.", message.getClass());
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.PcrepMessage repMsg = ((Pcrep) message).getPcrepMessage();
-        if (repMsg.getReplies() == null || repMsg.getReplies().isEmpty()) {
-            throw new IllegalArgumentException("Replies cannot be null or empty.");
-        }
+        Preconditions.checkArgument(repMsg.getReplies() != null && !repMsg.getReplies().isEmpty(), "Replies cannot be null or empty.");
         final ByteBuf buffer = Unpooled.buffer();
         for (final Replies reply : repMsg.getReplies()) {
-            if (reply.getRp() == null) {
-                throw new IllegalArgumentException("Reply must contain RP object.");
-            }
+            Preconditions.checkArgument(reply.getRp() != null, "Reply must contain RP object.");
             serializeObject(reply.getRp(), buffer);
             if (reply.getAugmentation(Replies1.class) != null && reply.getAugmentation(Replies1.class).getLsp() != null) {
                 serializeObject(reply.getAugmentation(Replies1.class).getLsp(), buffer);
index 3c0dbe27775e8bc6d27b00e7fbe5487e6522b2ca..09fac37914168559a52e479c557e5d8ee2eaf981 100644 (file)
@@ -75,9 +75,7 @@ public final class Stateful02PCReportMessageParser extends AbstractMessageParser
 
     @Override
     public Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
         if (objects.isEmpty()) {
             throw new PCEPDeserializerException("Pcrpt message cannot be empty.");
         }
index e0f7b78c16ac17d504dd625d776becbdc6a21542..e7929497ab6df7c337d779673baab799908a5c6d 100644 (file)
@@ -75,9 +75,7 @@ public final class Stateful02PCUpdateRequestMessageParser extends AbstractMessag
 
     @Override
     protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
         if (objects.isEmpty()) {
             throw new PCEPDeserializerException("Pcup message cannot be empty.");
         }
index 74e644a934af731bca8d00686e51c95a302ec861..aecdf533ea1fcde7809385e8862035063b6698b6 100644 (file)
@@ -73,9 +73,7 @@ public class CInitiated00PCInitiateMessageParser extends AbstractMessageParser {
 
     @Override
     protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
         final PcinitiateMessageBuilder builder = new PcinitiateMessageBuilder();
         final List<Requests> reqs = Lists.newArrayList();
         while (!objects.isEmpty()) {
index ec9e16e77b164942291422280856bb339fb3040b..006b4b15d08b8bf48e7dff6361ad74e5f121954e 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.protocol.pcep.ietf.stateful07;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import java.util.ArrayList;
 import java.util.List;
@@ -67,9 +68,7 @@ public final class Stateful07ErrorMessageParser extends PCEPErrorMessageParser {
 
     @Override
     protected PcerrMessage validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
         if (objects.isEmpty()) {
             throw new PCEPDeserializerException("Error message is empty.");
         }
index 579888e39f3db2852952c0ec983051157192d490..9a946d7ee27fb3d77ef7fd2c72f4dac3bfd070b0 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.protocol.pcep.ietf.stateful07;
 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv4Address;
 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeShort;
 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
-
 import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
@@ -46,9 +45,7 @@ public final class Stateful07LSPIdentifierIpv4TlvParser implements TlvParser, Tl
         if (buffer == null) {
             return null;
         }
-        if (buffer.readableBytes() != V4_LENGTH) {
-            throw new IllegalArgumentException("Length " + buffer.readableBytes() + " does not match LSP Identifiers Ipv4 tlv length.");
-        }
+        Preconditions.checkArgument(buffer.readableBytes() == V4_LENGTH, "Length %s does not match LSP Identifiers Ipv4 tlv length.", buffer.readableBytes());
         final Ipv4Builder builder = new Ipv4Builder();
         builder.setIpv4TunnelSenderAddress(Ipv4Util.addressForByteBuf(buffer));
         final LspId lspId = new LspId((long) buffer.readUnsignedShort());
index d625d2fab9dd4105db0ff1132de9fb54a64fe10f..cb82536ef1851c133f7bf07e3e7c96e1a7c64102 100644 (file)
@@ -10,7 +10,6 @@ package org.opendaylight.protocol.pcep.ietf.stateful07;
 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeIpv6Address;
 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeShort;
 import static org.opendaylight.protocol.util.ByteBufWriteUtil.writeUnsignedShort;
-
 import com.google.common.base.Preconditions;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
@@ -45,9 +44,7 @@ public final class Stateful07LSPIdentifierIpv6TlvParser implements TlvParser, Tl
         if (buffer == null) {
             return null;
         }
-        if (buffer.readableBytes() != V6_LENGTH) {
-            throw new IllegalArgumentException("Length " + buffer.readableBytes() + " does not match LSP Identifiers Ipv6 tlv length.");
-        }
+        Preconditions.checkArgument(buffer.readableBytes() == V6_LENGTH, "Length %s does not match LSP Identifiers Ipv6 tlv length.", buffer.readableBytes());
         final Ipv6Builder builder = new Ipv6Builder();
         builder.setIpv6TunnelSenderAddress(Ipv6Util.addressForByteBuf(buffer));
         final LspId lspId = new LspId((long) buffer.readUnsignedShort());
index e7d5df30e887599635b8417501b2622f3157a0ae..ddeb7902deefdc22df9e736b5661c74ab19db136 100644 (file)
@@ -84,9 +84,7 @@ public class Stateful07PCReportMessageParser extends AbstractMessageParser {
 
     @Override
     public Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
         if (objects.isEmpty()) {
             throw new PCEPDeserializerException("Pcrpt message cannot be empty.");
         }
index d6faefc1cf6b184f9d1a7f5641ec13a9856f18e1..57cc3e02d049326a247bf3eecb2e355606f69126 100644 (file)
@@ -80,9 +80,7 @@ public class Stateful07PCUpdateRequestMessageParser extends AbstractMessageParse
 
     @Override
     protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
         if (objects.isEmpty()) {
             throw new PCEPDeserializerException("Pcup message cannot be empty.");
         }
index ab630ad4dc4e79d26fe3f0cf58929bfafdd38fc2..ee72b870641e1cf65a4171f2e24d45fbec3ab558 100644 (file)
@@ -8,12 +8,9 @@
 package org.opendaylight.protocol.pcep.impl.message;
 
 import com.google.common.base.Preconditions;
-
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
-
 import java.util.List;
-
 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
 import org.opendaylight.protocol.pcep.spi.MessageUtil;
 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
@@ -42,20 +39,15 @@ public class PCEPCloseMessageParser extends AbstractMessageParser {
     public void serializeMessage(final Message message, final ByteBuf out) {
         Preconditions.checkArgument(message instanceof CloseMessage, "Wrong instance of Message. Passed instance of %s. Need CloseMessage.", message.getClass());
         final CCloseMessage close = ((CloseMessage) message).getCCloseMessage();
-
-        if (close.getCClose() == null) {
-            throw new IllegalArgumentException("Close Object must be present in Close Message.");
-        }
-        ByteBuf buffer = Unpooled.buffer();
+        Preconditions.checkArgument(close.getCClose() != null, "Close Object must be present in Close Message.");
+        final ByteBuf buffer = Unpooled.buffer();
         serializeObject(close.getCClose(), buffer);
         MessageUtil.formatMessage(TYPE, buffer, out);
     }
 
     @Override
     protected Close validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
         if (objects.isEmpty() || !(objects.get(0) instanceof CClose)) {
             throw new PCEPDeserializerException("Close message doesn't contain CLOSE object.");
         }
index 18380d697296dcce4a0bf43f75131a090aeb6e70..2f3ee849c634b8cdeae065d5b6045507434b8146 100644 (file)
@@ -53,10 +53,7 @@ public class PCEPErrorMessageParser extends AbstractMessageParser {
     public void serializeMessage(final Message message, final ByteBuf out) {
         Preconditions.checkArgument(message instanceof PcerrMessage, "Wrong instance of Message. Passed instance of %s. Need PcerrMessage.", message.getClass());
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcerr.message.PcerrMessage err = ((PcerrMessage) message).getPcerrMessage();
-
-        if (err.getErrors() == null || err.getErrors().isEmpty()) {
-            throw new IllegalArgumentException("Errors should not be empty.");
-        }
+        Preconditions.checkArgument(err.getErrors() != null && !err.getErrors().isEmpty(), "Errors should not be empty.");
         final ByteBuf buffer = Unpooled.buffer();
         serializeCases(err, buffer);
         for (final Errors e : err.getErrors()) {
@@ -83,9 +80,7 @@ public class PCEPErrorMessageParser extends AbstractMessageParser {
 
     @Override
     protected PcerrMessage validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
         if (objects.isEmpty()) {
             throw new PCEPDeserializerException("Error message is empty.");
         }
index e9221dc2ecad5750f87fb3d81d029b3c501fceb4..4d7bffce0054517e9518166cf953998d050d5eed 100644 (file)
@@ -91,9 +91,7 @@ public class PCEPMonitoringReplyMessageParser extends AbstractMessageParser {
 
     @Override
     protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
         if (objects.isEmpty()) {
             throw new PCEPDeserializerException("Pcmonrep message cannot be empty.");
         }
index 5c19957581011a4b8b83349fffc65361d78120d2..efabe43e741dfa4b807d496f36cee9b5326a29fb 100644 (file)
@@ -45,9 +45,7 @@ public class PCEPMonitoringRequestMessageParser extends PCEPRequestMessageParser
     public void serializeMessage(final Message message, final ByteBuf out) {
         Preconditions.checkArgument(message instanceof Pcmonreq, "Wrong instance of Message. Passed instance of %s. Need Pcmonreq.", message.getClass());
         final PcreqMessage msg = ((Pcmonreq) message).getPcreqMessage();
-        if (msg.getMonitoringRequest() == null) {
-            throw new IllegalArgumentException("MONITORING object MUST be present.");
-        }
+        Preconditions.checkArgument(msg.getMonitoringRequest() != null, "MONITORING object MUST be present.");
         final ByteBuf buffer = Unpooled.buffer();
         serializeMonitoringRequest(msg.getMonitoringRequest(), buffer);
         if (msg.getSvec() != null) {
@@ -61,9 +59,7 @@ public class PCEPMonitoringRequestMessageParser extends PCEPRequestMessageParser
 
     @Override
     protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
         if (objects.isEmpty()) {
             throw new PCEPDeserializerException("Pcmonreq message cannot be empty.");
         }
index 626e172e2aab104d4b395a5ea285c043e155fc4b..307a62c7c0ffd2aa08a805941252c7ae2d8e0869 100644 (file)
@@ -51,9 +51,7 @@ public class PCEPNotificationMessageParser extends AbstractMessageParser {
                     serializeObject(rps.getRp(), buffer);
                 }
             }
-            if (n.getNotifications() == null || n.getNotifications().isEmpty()) {
-                throw new IllegalArgumentException("Message must contain at least one notification object");
-            }
+            Preconditions.checkArgument(n.getNotifications() != null && !n.getNotifications().isEmpty(), "Message must contain at least one notification object");
             for (final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcntf.message.pcntf.message.notifications.Notifications not : n.getNotifications()) {
                 serializeObject(not.getCNotification(), buffer);
             }
@@ -63,9 +61,7 @@ public class PCEPNotificationMessageParser extends AbstractMessageParser {
 
     @Override
     protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
         if (objects.isEmpty()) {
             throw new PCEPDeserializerException("Notification message cannot be empty.");
         }
index 348eecc9edbc796d77998ea579d8e04f6c531be1..73c70aa958373006035410b2d737958c6bc84b9b 100644 (file)
@@ -8,12 +8,9 @@
 package org.opendaylight.protocol.pcep.impl.message;
 
 import com.google.common.base.Preconditions;
-
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
-
 import java.util.List;
-
 import org.opendaylight.protocol.pcep.spi.AbstractMessageParser;
 import org.opendaylight.protocol.pcep.spi.MessageUtil;
 import org.opendaylight.protocol.pcep.spi.ObjectRegistry;
@@ -40,11 +37,8 @@ public class PCEPOpenMessageParser extends AbstractMessageParser {
     public void serializeMessage(final Message message, final ByteBuf out) {
         Preconditions.checkArgument(message instanceof OpenMessage, "Wrong instance of Message. Passed instance of %s. Need OpenMessage.", message.getClass());
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.message.OpenMessage open = ((OpenMessage) message).getOpenMessage();
-
-        if (open.getOpen() == null) {
-            throw new IllegalArgumentException("Open Object must be present in Open Message.");
-        }
-        ByteBuf buffer = Unpooled.buffer();
+        Preconditions.checkArgument(open.getOpen() != null, "Open Object must be present in Open Message.");
+        final ByteBuf buffer = Unpooled.buffer();
         serializeObject(open.getOpen(), buffer);
         MessageUtil.formatMessage(TYPE, buffer, out);
     }
@@ -52,9 +46,7 @@ public class PCEPOpenMessageParser extends AbstractMessageParser {
     @Override
     protected org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.message.rev131007.Open validate(
             final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
 
         if (objects.isEmpty() || !(objects.get(0) instanceof Open)) {
             throw new PCEPDeserializerException("Open message doesn't contain OPEN object.");
index d948fcf0eefdf35bb68b8b6ce5c35355d72b0bd0..baec04d8e54b6a571c31475892b2fc1bd6b02525 100644 (file)
@@ -66,14 +66,10 @@ public class PCEPReplyMessageParser extends AbstractMessageParser {
     public void serializeMessage(final Message message, final ByteBuf out) {
         Preconditions.checkArgument(message instanceof Pcrep, "Wrong instance of Message. Passed instance of %s. Need Pcrep.", message.getClass());
         final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.PcrepMessage repMsg = ((Pcrep) message).getPcrepMessage();
-        if (repMsg.getReplies() == null || repMsg.getReplies().isEmpty()) {
-            throw new IllegalArgumentException("Replies cannot be null or empty.");
-        }
+        Preconditions.checkArgument(repMsg.getReplies() != null && !repMsg.getReplies().isEmpty(), "Replies cannot be null or empty.");
         final ByteBuf buffer = Unpooled.buffer();
         for (final Replies reply : repMsg.getReplies()) {
-            if (reply.getRp() == null) {
-                throw new IllegalArgumentException("Reply must contain RP object.");
-            }
+            Preconditions.checkArgument(reply.getRp() != null, "Reply must contain RP object.");
             serializeReply(reply, buffer);
         }
         MessageUtil.formatMessage(TYPE, buffer, out);
@@ -152,9 +148,7 @@ public class PCEPReplyMessageParser extends AbstractMessageParser {
 
     @Override
     protected Pcrep validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
         if (objects.isEmpty()) {
             throw new PCEPDeserializerException("Pcrep message cannot be empty.");
         }
index cd24333ce985b41147c0efbef0dc87cba231583c..ddb69b6ea34739fb8f207af8f8277ef37edca031 100644 (file)
@@ -75,9 +75,7 @@ public class PCEPRequestMessageParser extends AbstractMessageParser {
     public void serializeMessage(final Message message, final ByteBuf out) {
         Preconditions.checkArgument(message instanceof Pcreq, "Wrong instance of Message. Passed instance of %s. Need Pcreq.", message.getClass());
         final PcreqMessage msg = ((Pcreq) message).getPcreqMessage();
-        if (msg.getRequests() == null || msg.getRequests().isEmpty()) {
-            throw new IllegalArgumentException("Requests cannot be null or empty.");
-        }
+        Preconditions.checkArgument(msg.getRequests() != null && !msg.getRequests().isEmpty(), "Requests cannot be null or empty.");
         final ByteBuf buffer = Unpooled.buffer();
         if (msg.getMonitoringRequest() != null) {
             serializeMonitoringRequest(msg.getMonitoringRequest(), buffer);
@@ -157,9 +155,7 @@ public class PCEPRequestMessageParser extends AbstractMessageParser {
 
     @Override
     protected Message validate(final List<Object> objects, final List<Message> errors) throws PCEPDeserializerException {
-        if (objects == null) {
-            throw new IllegalArgumentException("Passed list can't be null.");
-        }
+        Preconditions.checkArgument(objects != null, "Passed list can't be null.");
         if (objects.isEmpty()) {
             throw new PCEPDeserializerException("Pcrep message cannot be empty.");
         }
@@ -358,9 +354,7 @@ public class PCEPRequestMessageParser extends AbstractMessageParser {
     }
 
     private Svec getValidSvec(final SvecBuilder builder, final List<Object> objects) {
-        if (objects == null || objects.isEmpty()) {
-            throw new IllegalArgumentException("List cannot be null or empty.");
-        }
+        Preconditions.checkArgument(objects != null && !objects.isEmpty(), "Passed list can't be null or empty.");
 
         if (objects.get(0) instanceof org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.svec.object.Svec) {
             builder.setSvec((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.svec.object.Svec) objects.get(0));
index 85c6b5a3f2fa3864c9261a3ae84eb7fb9f71db64..fb0bfdae6298f3f4d33107b9a42d249a398b907b 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.protocol.pcep.impl.tls;
 
+import com.google.common.base.Preconditions;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -38,19 +39,15 @@ public final class SslKeyStore {
         switch (pathType) {
         case CLASSPATH:
             in = SslKeyStore.class.getResourceAsStream(filename);
-            if (in == null) {
-                throw new IllegalStateException("KeyStore file not found: "
-                        + filename);
-            }
+            Preconditions.checkArgument(in != null, "KeyStore file not found: %s", filename);
             break;
         case PATH:
             LOG.debug("Current dir using System: {}", System.getProperty("user.dir"));
             final File keystorefile = new File(filename);
             try {
                 in = new FileInputStream(keystorefile);
-            } catch (FileNotFoundException e) {
-                throw new IllegalStateException("KeyStore file not found: "
-                        + filename,e);
+            } catch (final FileNotFoundException e) {
+                throw new IllegalStateException("KeyStore file not found: " + filename,e);
             }
             break;
         default:
index 542f96e556c63dd4c5e18c3c1421dc12f98e690f..e0a8dd09a7227a524702fd6cd65ddbaf02fca5bf 100644 (file)
@@ -18,6 +18,7 @@ package org.opendaylight.controller.config.yang.pcep.topology.provider;
 
 import com.google.common.base.Charsets;
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 import com.google.common.net.InetAddresses;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
@@ -59,7 +60,7 @@ public final class PCEPTopologyProviderModule extends
     private KeyMapping contructKeys() {
         final KeyMapping ret = new KeyMapping();
         if (getClient() != null) {
-            for (Client c : getClient()) {
+            for (final Client c : getClient()) {
                 if (c.getAddress() == null) {
                     LOG.warn("Client {} does not have an address skipping it", c);
                     continue;
@@ -75,14 +76,11 @@ public final class PCEPTopologyProviderModule extends
 
 
     private String getAddressString(final IpAddress address) {
+        Preconditions.checkArgument(address.getIpv4Address() != null || address.getIpv6Address() != null, "Address %s is invalid", address);
         if (address.getIpv4Address() != null) {
             return address.getIpv4Address().getValue();
         }
-        if (address.getIpv6Address() != null) {
-            return address.getIpv6Address().getValue();
-        }
-
-        throw new IllegalArgumentException(String.format("Address %s is invalid", address));
+        return address.getIpv6Address().getValue();
     }
 
 
@@ -104,9 +102,9 @@ public final class PCEPTopologyProviderModule extends
              *         should something like isMd5ServerSupported()
              */
 
-            PCEPDispatcherImplModuleMXBean dispatcherProxy = dependencyResolver.newMXBeanProxy(getDispatcher(),
+            final PCEPDispatcherImplModuleMXBean dispatcherProxy = this.dependencyResolver.newMXBeanProxy(getDispatcher(),
                     PCEPDispatcherImplModuleMXBean.class);
-            boolean md5ServerSupported = dispatcherProxy.getMd5ServerChannelFactory() != null;
+            final boolean md5ServerSupported = dispatcherProxy.getMd5ServerChannelFactory() != null;
             JmxAttributeValidationException.checkCondition(md5ServerSupported,
                     "password is not compatible with selected dispatcher", clientJmxAttribute);
 
@@ -115,13 +113,11 @@ public final class PCEPTopologyProviderModule extends
 
     private InetAddress listenAddress() {
         final IpAddress a = getListenAddress();
+        Preconditions.checkArgument(a.getIpv4Address() != null || a.getIpv6Address() != null, "Address %s not supported", a);
         if (a.getIpv4Address() != null) {
             return InetAddresses.forString(a.getIpv4Address().getValue());
-        } else if (a.getIpv6Address() != null) {
-            return InetAddresses.forString(a.getIpv6Address().getValue());
-        } else {
-            throw new IllegalArgumentException("Address " + a + " not supported");
         }
+        return InetAddresses.forString(a.getIpv6Address().getValue());
     }
 
     @Override
index 2cb15c2c22cf1ca46b0b258fc7fb86273c116a18..b09856e3f45a130085e224159261ee16b1d1b9ad 100644 (file)
@@ -95,9 +95,7 @@ public final class ByteArray {
      * @return a new byte array that is a sub-array of the original
      */
     public static byte[] subByte(final byte[] bytes, final int startIndex, final int length) {
-        if (!checkLength(bytes, length) || !checkStartIndex(bytes, startIndex, length)) {
-            throw new IllegalArgumentException("Cannot create subByte, invalid arguments: Length: " + length + " startIndex: " + startIndex);
-        }
+        Preconditions.checkArgument(checkLength(bytes, length) && checkStartIndex(bytes, startIndex, length), "Cannot create subByte, invalid arguments: Length: %s startIndex: %s", length, startIndex);
         final byte[] res = new byte[length];
         System.arraycopy(bytes, startIndex, res, 0, length);
         return res;
@@ -119,9 +117,7 @@ public final class ByteArray {
      * @return int
      */
     public static int bytesToInt(final byte[] bytes) {
-        if (bytes.length > Integer.SIZE / Byte.SIZE) {
-            throw new IllegalArgumentException("Cannot convert bytes to integer. Byte array too big.");
-        }
+        Preconditions.checkArgument(bytes.length <= Integer.SIZE / Byte.SIZE, "Cannot convert bytes to integer. Byte array too big.");
         byte[] res = new byte[Integer.SIZE / Byte.SIZE];
         if (bytes.length != Integer.SIZE / Byte.SIZE) {
             System.arraycopy(bytes, 0, res, Integer.SIZE / Byte.SIZE - bytes.length, bytes.length);
@@ -140,9 +136,7 @@ public final class ByteArray {
      * @return long
      */
     public static long bytesToLong(final byte[] bytes) {
-        if (bytes.length > Long.SIZE / Byte.SIZE) {
-            throw new IllegalArgumentException("Cannot convert bytes to long.Byte array too big.");
-        }
+        Preconditions.checkArgument(bytes.length <= Long.SIZE / Byte.SIZE, "Cannot convert bytes to long.Byte array too big.");
         byte[] res = new byte[Long.SIZE / Byte.SIZE];
         if (bytes.length != Long.SIZE / Byte.SIZE) {
             System.arraycopy(bytes, 0, res, Long.SIZE / Byte.SIZE - bytes.length, bytes.length);
@@ -161,9 +155,7 @@ public final class ByteArray {
      * @return bytes array without first 'count' bytes
      */
     public static byte[] cutBytes(final byte[] bytes, final int count) {
-        if (bytes.length == 0 || count > bytes.length || count <= 0) {
-            throw new IllegalArgumentException("Cannot cut bytes, invalid arguments: Count: " + count + " bytes.length: " + bytes.length);
-        }
+        Preconditions.checkArgument(bytes.length != 0 && count <= bytes.length && count > 0, "Cannot cut bytes, invalid arguments: Count: %s bytes.length: %s", count, bytes.length);
         return Arrays.copyOfRange(bytes, count, bytes.length);
     }
 
@@ -259,12 +251,8 @@ public final class ByteArray {
      * @return copied value aligned to right
      */
     public static byte copyBitsRange(final byte src, final int fromBit, final int length) {
-        if (fromBit < 0 || fromBit > Byte.SIZE - 1 || length < 1 || length > Byte.SIZE) {
-            throw new IllegalArgumentException("fromBit or toBit is out of range.");
-        }
-        if (fromBit + length > Byte.SIZE) {
-            throw new IllegalArgumentException("Out of range.");
-        }
+        Preconditions.checkArgument(fromBit >= 0 && fromBit <= Byte.SIZE - 1 && length >= 1 && length <= Byte.SIZE, "fromBit or toBit is out of range.");
+        Preconditions.checkArgument(fromBit + length <= Byte.SIZE, "Out of range.");
 
         byte retByte = 0;
         int retI = 0;