Fix Sonar issue: Code readability 02/56602/4
authormcauffiez <mcauffiez@inocybe.com>
Fri, 5 May 2017 14:43:43 +0000 (10:43 -0400)
committermcauffiez <mcauffiez@inocybe.com>
Wed, 10 May 2017 14:20:40 +0000 (10:20 -0400)
Adress Major sonar issues for NeutronHostconfigVppListener.java file.
This patch improves code readability.

Use final temporary var instead of using the method parameter
vhostMode.
Reduce the lambda content by creating a new method:
processDataTreeModification.
Reduce the cyclomatic complexity of validateVppNode by adding the
boolean function isCapabilitiesPresent.

Change-Id: Ie2b3fa85772707f8c86901415dd1ca8d0a7eef47
Signed-off-by: mcauffiez <mcauffiez@inocybe.com>
neutron-hostconfig/vpp/src/main/java/org/opendaylight/neutron/hostconfig/vpp/NeutronHostconfigVppListener.java

index 5acfdf9023b5c4c368b44a017c0946d82e92e1c4..7400ef44f234ad530a23e41ce9eb810c955798c0 100644 (file)
@@ -67,11 +67,11 @@ public class NeutronHostconfigVppListener implements ClusteredDataTreeChangeList
     public NeutronHostconfigVppListener(final DataBroker dataBroker, String spath, String sname, String vhostMode) {
         LOG.info("Initializing Neutron-Hostconfig-Vpp-Listener");
         this.dataBroker = Preconditions.checkNotNull(dataBroker);
-        vhostMode = Preconditions.checkNotNull(vhostMode).toLowerCase();
-        Preconditions.checkArgument(vhostMode.equals("server") || vhostMode.equals("client"),
+        final String vhostModeChecked = Preconditions.checkNotNull(vhostMode).toLowerCase();
+        Preconditions.checkArgument(vhostModeChecked.equals("server") || vhostModeChecked.equals("client"),
                 "Supported values for vhostuser-mode are client and server.");
         this.socketInfo =
-                new SocketInfo(Preconditions.checkNotNull(spath), Preconditions.checkNotNull(sname), vhostMode);
+                new SocketInfo(Preconditions.checkNotNull(spath), Preconditions.checkNotNull(sname), vhostModeChecked);
         this.neutronHostconfig = new NeutronHostconfigUtils(dataBroker);
         REQUIRED_CAPABILITIES.add(V3PO_1704_CAPABILITY);
         REQUIRED_CAPABILITIES.add(V3PO_1701_CAPABILITY);
@@ -83,32 +83,36 @@ public class NeutronHostconfigVppListener implements ClusteredDataTreeChangeList
         LOG.info("onDataTreeChanged: Received Data Tree Changed ...", changes);
         executorService.submit(() -> {
             for (DataTreeModification<Node> change : Preconditions.checkNotNull(changes, "Changes may not be null!")) {
-                final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
-                final DataObjectModification<Node> mod = change.getRootNode();
-                LOG.info("onDataTreeChanged: Received Data Tree Changed Update of Type={} for Key={}",
-                        mod.getModificationType(), key);
-                switch (mod.getModificationType()) {
-                    case SUBTREE_MODIFIED:
-                        if (validateVppNode(mod.getDataAfter())) {
-                            updateHostConfig(mod.getDataAfter(), NeutronHostconfigUtils.Action.UPDATE);
-                        } else {
-                            updateHostConfig(mod.getDataBefore(), NeutronHostconfigUtils.Action.DELETE);
-                        }
-                        break;
-                    case DELETE:
-                        updateHostConfig(mod.getDataBefore(), NeutronHostconfigUtils.Action.DELETE);
-                        break;
-                    case WRITE:
-                        if (validateVppNode(mod.getDataAfter())) {
-                            updateHostConfig(mod.getDataAfter(), NeutronHostconfigUtils.Action.ADD);
-                        }
-                        break;
-                    default:
-                }
+                processDataTreeModification(change);
             }
         });
     }
 
+    private void processDataTreeModification(DataTreeModification<Node> change) {
+        final InstanceIdentifier<Node> key = change.getRootPath().getRootIdentifier();
+        final DataObjectModification<Node> mod = change.getRootNode();
+        LOG.info("onDataTreeChanged: Received Data Tree Changed Update of Type={} for Key={}",
+                mod.getModificationType(), key);
+        switch (mod.getModificationType()) {
+            case SUBTREE_MODIFIED:
+                if (validateVppNode(mod.getDataAfter())) {
+                    updateHostConfig(mod.getDataAfter(), NeutronHostconfigUtils.Action.UPDATE);
+                } else {
+                    updateHostConfig(mod.getDataBefore(), NeutronHostconfigUtils.Action.DELETE);
+                }
+                break;
+            case DELETE:
+                updateHostConfig(mod.getDataBefore(), NeutronHostconfigUtils.Action.DELETE);
+                break;
+            case WRITE:
+                if (validateVppNode(mod.getDataAfter())) {
+                    updateHostConfig(mod.getDataAfter(), NeutronHostconfigUtils.Action.ADD);
+                }
+                break;
+            default:
+        }
+    }
+
     public void init() {
         LOG.info("Initializing {}", getClass().getSimpleName());
         DataTreeIdentifier<Node> dataTreeIdentifier = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL,
@@ -143,9 +147,7 @@ public class NeutronHostconfigVppListener implements ClusteredDataTreeChangeList
                 LOG.info("Connecting device {} ...", node.getNodeId().getValue());
                 break;
             case Connected:
-                if (netconfNode.getAvailableCapabilities() == null
-                        || netconfNode.getAvailableCapabilities().getAvailableCapability() == null
-                        || netconfNode.getAvailableCapabilities().getAvailableCapability().isEmpty()) {
+                if (isCapabilitiesPresent(netconfNode)) {
                     LOG.warn("Node {} does not contain any capabilities", node.getNodeId().getValue());
                     break;
                 }
@@ -164,6 +166,12 @@ public class NeutronHostconfigVppListener implements ClusteredDataTreeChangeList
         return false;
     }
 
+    private boolean isCapabilitiesPresent(final NetconfNode netconfNode) {
+        return netconfNode.getAvailableCapabilities() == null
+                || netconfNode.getAvailableCapabilities().getAvailableCapability() == null
+                || netconfNode.getAvailableCapabilities().getAvailableCapability().isEmpty();
+    }
+
     private boolean capabilityCheck(final List<AvailableCapability> capabilities) {
         final List<String> availableCapabilities =
                 capabilities.stream().map(AvailableCapability::getCapability).collect(Collectors.toList());