Sonar bug fix for L4Classifier.java 64/23564/10
authorDileep Ranganathan <dileep.ranganathan@intel.com>
Tue, 30 Jun 2015 00:47:17 +0000 (17:47 -0700)
committerMartin Sunal <msunal@cisco.com>
Wed, 19 Aug 2015 13:14:23 +0000 (13:14 +0000)
Reduce Cyclomatic complexity of method "checkPresenceOfRequiredParams"
Reduce Cyclomatic complexity of method "update"
Removed redundant conditional checks
See - https://goo.gl/jgXZws

Change-Id: Ia69e4d447c361515a89e01e00023fb106aaa9427
Signed-off-by: Dileep Ranganathan <dileep.ranganathan@intel.com>
renderers/ofoverlay/src/main/java/org/opendaylight/groupbasedpolicy/renderer/ofoverlay/sf/L4Classifier.java

index ba5fd647be8fdde33f6881910cdbe43b36a38d9c..4d049634730933e9ef2d8c53a9d80c863632a3ea 100755 (executable)
@@ -8,8 +8,6 @@
 
 package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.sf;
 
-
-
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
@@ -105,34 +103,35 @@ public class L4Classifier extends Classifier {
 
     @Override
     protected void checkPresenceOfRequiredParams(Map<String, ParameterValue> params) {
-        if (params.get(SRC_PORT_PARAM) != null && params.get(SRC_PORT_RANGE_PARAM) != null) {
-            throw new IllegalArgumentException("Source port parameters " + SRC_PORT_PARAM + " and " + SRC_PORT_RANGE_PARAM
-                    + " are mutually exclusive.");
-        }
-        if (params.get(DST_PORT_PARAM) != null && params.get(DST_PORT_RANGE_PARAM) != null) {
-            throw new IllegalArgumentException("Destination port parameters " + DST_PORT_PARAM + " and " + DST_PORT_RANGE_PARAM
-                    + " are mutually exclusive.");
-        }
-        if (params.get(SRC_PORT_PARAM) != null) {
-            if (params.get(SRC_PORT_PARAM).getIntValue() == null) {
-                throw new IllegalArgumentException("Value of " + SRC_PORT_PARAM + " parameter is not specified.");
+        validatePortParam(params, SRC_PORT_PARAM, SRC_PORT_RANGE_PARAM);
+        validatePortParam(params, DST_PORT_PARAM, DST_PORT_RANGE_PARAM);
+        validateRange(params, SRC_PORT_RANGE_PARAM);
+        validateRange(params, DST_PORT_RANGE_PARAM);
+    }
+
+    private void validatePortParam(Map<String, ParameterValue> params, String portParam, String portRangeParam) {
+        if (params.get(portParam) != null) {
+            StringBuilder paramLog = new StringBuilder();
+            if (params.get(portParam).getIntValue() == null) {
+                paramLog.append("Value of ")
+                    .append(portParam)
+                    .append(" parameter is not specified.");
+                throw new IllegalArgumentException(paramLog.toString());
             }
-        }
-        if (params.get(SRC_PORT_RANGE_PARAM) != null) {
-            if (params.get(SRC_PORT_RANGE_PARAM) != null) {
-                validateRangeValue(params.get(SRC_PORT_RANGE_PARAM).getRangeValue());
+            if(params.get(portRangeParam) != null) {
+                paramLog.append("Source port parameters ")
+                    .append(portParam)
+                    .append(" and ")
+                    .append(portRangeParam)
+                    .append(" are mutually exclusive.");
+                throw new IllegalArgumentException(paramLog.toString());
             }
         }
+    }
 
-        if (params.get(DST_PORT_PARAM) != null) {
-            if (params.get(DST_PORT_PARAM).getIntValue() == null) {
-                throw new IllegalArgumentException("Value of " + DST_PORT_PARAM + " parameter is not specified.");
-            }
-        }
-        if (params.get(DST_PORT_RANGE_PARAM) != null) {
-            if (params.get(DST_PORT_RANGE_PARAM) != null) {
-                validateRangeValue(params.get(DST_PORT_RANGE_PARAM).getRangeValue());
-            }
+    private void validateRange(Map<String, ParameterValue> params, String portRangeParam) {
+        if (params.get(portRangeParam) != null) {
+            validateRangeValue(params.get(portRangeParam).getRangeValue());
         }
     }
 
@@ -151,17 +150,8 @@ public class L4Classifier extends Classifier {
     public List<MatchBuilder> update(List<MatchBuilder> matches, Map<String, ParameterValue> params) {
         Set<Long> sPorts = new HashSet<>();
         Set<Long> dPorts = new HashSet<>();
-        if (params.get(SRC_PORT_PARAM) != null) {
-            sPorts.add(params.get(SRC_PORT_PARAM).getIntValue());
-        } else if (params.get(SRC_PORT_RANGE_PARAM) != null) {
-            sPorts.addAll(createSetFromRange(params.get(SRC_PORT_RANGE_PARAM).getRangeValue()));
-        }
-        if (params.get(DST_PORT_PARAM) != null) {
-            dPorts.add(params.get(DST_PORT_PARAM).getIntValue());
-        } else if (params.get(DST_PORT_RANGE_PARAM) != null) {
-            dPorts.addAll(createSetFromRange(params.get(DST_PORT_RANGE_PARAM).getRangeValue()));
-        }
-
+        addToPortSet(params, SRC_PORT_PARAM, SRC_PORT_RANGE_PARAM, sPorts);
+        addToPortSet(params, DST_PORT_PARAM, DST_PORT_RANGE_PARAM, dPorts);
         List<MatchBuilder> newMatches = new ArrayList<>();
         for (MatchBuilder matchBuilder : matches) {
             Layer4Match l4Match = matchBuilder.getLayer4Match();
@@ -169,13 +159,7 @@ public class L4Classifier extends Classifier {
             if (l4Match == null) {
                 l4Match = resolveL4Match(params);
             }
-            if (l4Match instanceof UdpMatch) {
-                l4Matches = createUdpMatches((UdpMatch) l4Match, sPorts, dPorts);
-            } else if (l4Match instanceof TcpMatch) {
-                l4Matches = createTcpMatches((TcpMatch) l4Match, sPorts, dPorts);
-            } else if (l4Match instanceof SctpMatch) {
-                l4Matches = createSctpMatches((SctpMatch) l4Match, sPorts, dPorts);
-            }
+            l4Matches = createL4Matches(l4Match, sPorts, dPorts);
             for (Layer4Match newL4Match : l4Matches) {
                 newMatches.add(new MatchBuilder(matchBuilder.build()).setLayer4Match(newL4Match));
             }
@@ -183,6 +167,14 @@ public class L4Classifier extends Classifier {
         return newMatches;
     }
 
+    private void addToPortSet(Map<String, ParameterValue> params, String portParam, String portRangeParam, Set<Long> portSet) {
+        if (params.get(portParam) != null) {
+            portSet.add(params.get(portParam).getIntValue());
+        } else if (params.get(portRangeParam) != null) {
+            portSet.addAll(createSetFromRange(params.get(portRangeParam).getRangeValue()));
+        }
+    }
+
     private Layer4Match resolveL4Match(Map<String, ParameterValue> params) {
         Long ipProto = IpProtoClassifier.getIpProtoValue(params);
         if (ipProto == null) {
@@ -211,6 +203,18 @@ public class L4Classifier extends Classifier {
         return res;
     }
 
+    private Set<? extends Layer4Match> createL4Matches(Layer4Match l4Match, Set<Long> sPorts, Set<Long> dPorts) {
+        Set<? extends Layer4Match> l4Matches = null;
+        if (l4Match instanceof UdpMatch) {
+            l4Matches = createUdpMatches((UdpMatch) l4Match, sPorts, dPorts);
+        } else if (l4Match instanceof TcpMatch) {
+            l4Matches = createTcpMatches((TcpMatch) l4Match, sPorts, dPorts);
+        } else if (l4Match instanceof SctpMatch) {
+            l4Matches = createSctpMatches((SctpMatch) l4Match, sPorts, dPorts);
+        }
+        return l4Matches;
+    }
+
     private Set<UdpMatch> createUdpMatches(UdpMatch udpMatch, Set<Long> sPorts, Set<Long> dPorts) {
         Set<UdpMatch> udpMatches = new HashSet<>();
         if (!sPorts.isEmpty() && dPorts.isEmpty()) {