Bug-731 Fixed few minor Sonar warnings 79/9779/2
authorMilos Fabian <milfabia@cisco.com>
Thu, 7 Aug 2014 10:44:27 +0000 (12:44 +0200)
committerMilos Fabian <milfabia@cisco.com>
Fri, 8 Aug 2014 07:55:33 +0000 (09:55 +0200)
Change-Id: Id1ac7061e395f4feca8d29304785d03ef14007ad
Signed-off-by: Milos Fabian <milfabia@cisco.com>
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/GracefulCapabilityHandler.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/protocol/bgp/rib/impl/AdjRIBOutEntry.java
bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/spi/AdjRIBsOut.java
bgp/rib-mock/src/main/java/org/opendaylight/protocol/bgp/rib/mock/EventBusRegistration.java
bgp/topology-provider/src/main/java/org/opendaylight/bgpcep/bgp/topology/provider/LinkstateTopologyBuilder.java
pcep/spi/src/main/java/org/opendaylight/protocol/pcep/spi/AbstractObjectWithTlvsParser.java
util/src/main/java/org/opendaylight/protocol/util/PCEPHexDumpParser.java

index 8e2ed85b70521891a3989e14ce06239e73545755..489ac8b505fb4253745005f63fabc391ca3fd370 100644 (file)
@@ -7,6 +7,10 @@
  */
 package org.opendaylight.protocol.bgp.parser;
 
+import com.google.common.collect.Maps;
+import java.util.Map;
+
+
 /**
  * Possible errors from implemented RFCs and drafts. Each error consists of error code and error subcode (code/subcode
  * in comments).
@@ -103,91 +107,94 @@ public enum BGPError {
      */
     CEASE((short) 6, (short) 0);
 
-    private final short code;
+    private static final Map<BGPErrorIdentifier, BGPError> VALUE_MAP;
+
+    static {
+        VALUE_MAP = Maps.newHashMap();
+        for (final BGPError enumItem : BGPError.values()) {
+            VALUE_MAP.put(enumItem.getErrorIdentifier(), enumItem);
+        }
+    }
 
-    private final short subcode;
+    private final BGPErrorIdentifier errorId;
 
     BGPError(final short code, final short subcode) {
-        this.code = code;
-        this.subcode = subcode;
+        this.errorId = new BGPErrorIdentifier(code, subcode);
     }
 
     public short getCode() {
-        return this.code;
+        return this.errorId.getCode();
     }
 
     public short getSubcode() {
-        return this.subcode;
+        return this.errorId.getSubCode();
     }
 
-    public static BGPError forValue(final int e, final int s) {
-        if (e == 1) {
-            if (s == 1) {
-                return BGPError.CONNECTION_NOT_SYNC;
-            }
-            if (s == 2) {
-                return BGPError.BAD_MSG_LENGTH;
-            }
-            if (s == 3) {
-                return BGPError.BAD_MSG_TYPE;
-            }
-        } else if (e == 2) {
-            if (s == 0) {
-                return BGPError.UNSPECIFIC_OPEN_ERROR;
-            }
-            if (s == 1) {
-                return BGPError.VERSION_NOT_SUPPORTED;
-            }
-            if (s == 2) {
-                return BGPError.BAD_PEER_AS;
-            }
-            if (s == 3) {
-                return BGPError.BAD_BGP_ID;
-            }
-            if (s == 4) {
-                return BGPError.OPT_PARAM_NOT_SUPPORTED;
-            }
-            if (s == 6) {
-                return BGPError.HOLD_TIME_NOT_ACC;
-            }
-        } else if (e == 3) {
-            if (s == 1) {
-                return BGPError.MALFORMED_ATTR_LIST;
-            }
-            if (s == 2) {
-                return BGPError.WELL_KNOWN_ATTR_NOT_RECOGNIZED;
-            }
-            if (s == 3) {
-                return BGPError.WELL_KNOWN_ATTR_MISSING;
-            }
-            if (s == 4) {
-                return BGPError.ATTR_FLAGS_MISSING;
-            }
-            if (s == 5) {
-                return BGPError.ATTR_LENGTH_ERROR;
-            }
-            if (s == 6) {
-                return BGPError.ORIGIN_ATTR_NOT_VALID;
+    private BGPErrorIdentifier getErrorIdentifier() {
+        return this.errorId;
+    }
+
+    /**
+     * Caret for combination of Error-type and Error-value
+     */
+    private static final class BGPErrorIdentifier {
+        private final short code;
+        private final short subcode;
+
+        BGPErrorIdentifier(final short code, final short subcode) {
+            this.code = code;
+            this.subcode = subcode;
+        }
+
+        public short getCode() {
+            return this.code;
+        }
+
+        public short getSubCode() {
+            return this.subcode;
+        }
+
+        @Override
+        public int hashCode() {
+            final int prime = 31;
+            int result = 1;
+            result = prime * result + this.code;
+            result = prime * result + this.subcode;
+            return result;
+        }
+
+        @Override
+        public boolean equals(final java.lang.Object obj) {
+            if (this == obj) {
+                return true;
             }
-            if (s == 8) {
-                return BGPError.NEXT_HOP_NOT_VALID;
+            if (obj == null) {
+                return false;
             }
-            if (s == 9) {
-                return BGPError.OPT_ATTR_ERROR;
+            if (this.getClass() != obj.getClass()) {
+                return false;
             }
-            if (s == 10) {
-                return BGPError.NETWORK_NOT_VALID;
+            final BGPErrorIdentifier other = (BGPErrorIdentifier) obj;
+            if (this.code != other.code) {
+                return false;
             }
-            if (s == 11) {
-                return BGPError.AS_PATH_MALFORMED;
+            if (this.subcode != other.subcode) {
+                return false;
             }
-        } else if (e == 4) {
-            return BGPError.HOLD_TIMER_EXPIRED;
-        } else if (e == 5) {
-            return BGPError.FSM_ERROR;
-        } else if (e == 6) {
-            return BGPError.CEASE;
+            return true;
+        }
+
+        @Override
+        public String toString() {
+            return "type " + this.code + " value " + this.subcode;
+        }
+    }
+
+    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 " + e + " and subcode " + s + " not recognized.");
+        throw new IllegalArgumentException("BGP Error code " + code + " and subcode " + subcode + " not recognized.");
     }
 }
index 8c980c1c035abf52febf5a43ca8de53dc1c71661..cda8c5fac3f780798e76761a4d319d61f3d3948e 100644 (file)
@@ -73,10 +73,8 @@ public final class GracefulCapabilityHandler implements CapabilityParser, Capabi
 
         int flagBits = 0;
         final RestartFlags flags = grace.getRestartFlags();
-        if (flags != null) {
-            if (flags.isRestartState()) {
-                flagBits |= RESTART_FLAG_STATE;
-            }
+        if (flags != null && flags.isRestartState()) {
+            flagBits |= RESTART_FLAG_STATE;
         }
         int timeval = 0;
         final Integer time = grace.getRestartTime();
index 51a9a9c4de4d6994d7e4613b5f3d10b97962ad60..60fb42cd1d1903023c668bf8c3adba4800d25326 100644 (file)
@@ -16,6 +16,9 @@ import org.opendaylight.protocol.bgp.rib.impl.BGPServerSessionValidator;
 * BGP peer acceptor that handles incoming bgp connections.
 */
 public class BGPPeerAcceptorModule extends org.opendaylight.controller.config.yang.bgp.rib.impl.AbstractBGPPeerAcceptorModule {
+
+    private static final int PRIVILEGED_PORTS = 1024;
+
     public BGPPeerAcceptorModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) {
         super(identifier, dependencyResolver);
     }
@@ -27,7 +30,7 @@ public class BGPPeerAcceptorModule extends org.opendaylight.controller.config.ya
     @Override
     public void customValidation() {
         // check if unix root user
-        if (!PlatformDependent.isWindows() && !PlatformDependent.isRoot() && getBindingPort().getValue() < 1024) {
+        if (!PlatformDependent.isWindows() && !PlatformDependent.isRoot() && getBindingPort().getValue() < PRIVILEGED_PORTS) {
             throw new AccessControlException("Unable to bind port " + getBindingPort().getValue() + " while running as non-root user.");
         }
         // Try to parse address
index 018a7da5bb7af1493b0425b838056a65b16aefdc..2a5544d51e0b4cf5af0977a8b0051ad9ad367d18 100644 (file)
@@ -8,9 +8,7 @@
 package org.opendaylight.protocol.bgp.rib.impl;
 
 import com.google.common.base.Preconditions;
-
 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
-
 import org.opendaylight.protocol.bgp.rib.spi.RouteEncoder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.route.Attributes;
@@ -71,7 +69,7 @@ final class AdjRIBOutEntry<K, V extends Route> {
         return ribOut;
     }
 
-    static final boolean isNone(final Object o) {
+    static boolean isNone(final Object o) {
         return o == NLRIENTRY_NONE_VALUE;
     }
 }
\ No newline at end of file
index 4bd794361d79824375f99bd9ef68ab6038d5ddfe..dc4a526a623e4253784b4e31d76499404a9a0aa5 100644 (file)
@@ -11,5 +11,5 @@ import org.opendaylight.protocol.bgp.rib.spi.RouteEncoder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
 
 public interface AdjRIBsOut {
-    public void put(final RouteEncoder ribOut, final Object key, final Route newValue);
+    void put(final RouteEncoder ribOut, final Object key, final Route newValue);
 }
index 746ae03b04c8f9bdeae91a7a0736792d3eaece53..e6ef2091fdec9d55679e8b488ac79948f7378922 100644 (file)
@@ -10,10 +10,8 @@ package org.opendaylight.protocol.bgp.rib.mock;
 import com.google.common.collect.Sets;
 import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.Subscribe;
-
 import java.util.List;
 import java.util.Set;
-
 import org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl;
 import org.opendaylight.protocol.bgp.rib.spi.BGPSession;
 import org.opendaylight.protocol.bgp.rib.spi.BGPSessionListener;
@@ -80,6 +78,8 @@ final class EventBusRegistration extends AbstractListenerRegistration<BGPSession
 
             listener.onSessionUp(new BGPSession() {
 
+                private static final long AS = 30L;
+
                 @Override
                 public void close() {
                     LOG.debug("Session {} closed", this);
@@ -97,7 +97,7 @@ final class EventBusRegistration extends AbstractListenerRegistration<BGPSession
 
                 @Override
                 public AsNumber getAsNumber() {
-                    return new AsNumber(30L);
+                    return new AsNumber(AS);
                 }
             });
         } else if (!(message instanceof Keepalive)) {
index 064e655c1c9526a7206ae8bc668a321d1fd7db57..c0314e76da3c302752c006a147d79cc1ffcc2895 100644 (file)
@@ -607,14 +607,12 @@ public final class LinkstateTopologyBuilder extends AbstractTopologyBuilder<Link
             // TODO: what should we do with in.getOspfRouterId()?
             // final OspfNode in = ((OspfNodeCase) ri).getOspfNode();
 
-            if (na != null) {
+            if (na != null && na.getNodeFlags() != null) {
                 final NodeFlagBits nf = na.getNodeFlags();
-                if (nf != null) {
-                    if (nf.isAbr()) {
-                        ab.setRouterType(new AbrBuilder().setAbr(Boolean.TRUE).build());
-                    } else if (!nf.isExternal()) {
-                        ab.setRouterType(new InternalBuilder().setInternal(Boolean.TRUE).build());
-                    }
+                if (nf.isAbr()) {
+                    ab.setRouterType(new AbrBuilder().setAbr(Boolean.TRUE).build());
+                } else if (!nf.isExternal()) {
+                    ab.setRouterType(new InternalBuilder().setInternal(Boolean.TRUE).build());
                 }
             }
         }
index 69e891aa6ecbdf864886b5966cad03569d11a13f..f63587dbd60269ffe40001b0c6cc026774566ed9 100644 (file)
@@ -10,12 +10,9 @@ package org.opendaylight.protocol.pcep.spi;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
-
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.ByteBufUtil;
-
 import java.util.List;
-
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Tlv;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
@@ -83,7 +80,7 @@ public abstract class AbstractObjectWithTlvsParser<T> implements ObjectParser, O
         // FIXME: No TLVs by default, fallback to augments
     }
 
-    abstract protected void addVendorInformationTlvs(final T builder, final List<VendorInformationTlv> tlvs);
+    protected abstract void addVendorInformationTlvs(final T builder, final List<VendorInformationTlv> tlvs);
 
     protected final void serializeVendorInformationTlvs(final List<VendorInformationTlv> tlvs, final ByteBuf buffer) {
         if (tlvs != null && !tlvs.isEmpty()) {
index 24560af310f95ab3d309027adc101fd20670f4a0..dd20b7ae6ecbac676546983da64a902190253067 100644 (file)
@@ -61,14 +61,16 @@ public final class PCEPHexDumpParser {
             final int lengthIdx = idx + LENGTH.length();
             final int messageIdx = content.indexOf('.', lengthIdx);
             final int length = Integer.parseInt(content.substring(lengthIdx, messageIdx));
-            final int messageEndIdx = messageIdx + (length * 2) + 1;    // dot
+            // dot
+            final int messageEndIdx = messageIdx + (length * 2) + 1;
 
             // Assert that message is longer than minimum 4(header.length == 4)
             // If length in PCEP message would be 0, loop would never end
             Preconditions.checkArgument(length >= MINIMAL_LENGTH, "Invalid message at index " + idx + ", length atribute is lower than "
                 + MINIMAL_LENGTH);
 
-            final String hexMessage = content.substring(messageIdx + 1, messageEndIdx); // dot
+            // dot
+            final String hexMessage = content.substring(messageIdx + 1, messageEndIdx);
             byte[] message = null;
             try {
                 message = Hex.decodeHex(hexMessage.toCharArray());