Technical Debt part 2 54/41754/2
authormiroslav.macko <miroslav.macko@pantheon.tech>
Wed, 13 Jul 2016 09:56:10 +0000 (11:56 +0200)
committermiroslav.macko <miroslav.macko@pantheon.tech>
Mon, 18 Jul 2016 08:40:44 +0000 (10:40 +0200)
- NodeStaticReplyTranslatorUtil
- Cyclomatic Complexity "nodeGroupFeatureTranslator"
- SystemNotificationsListenerImpl
- Cyclomatic Complexity "onSwitchIdleEvent"
- Reduce lambda expression number of lines

Change-Id: Id042ceba0155f6aa33a49fe1950c2d68b50b2853
Signed-off-by: miroslav.macko <miroslav.macko@pantheon.tech>
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/common/NodeStaticReplyTranslatorUtil.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/connection/listener/SystemNotificationsListenerImpl.java

index 727ed61d04aa11df4e222cfe9ce8e1c212b7098a..449c24cfe2deb67e81a7ac3aca0fd2a886376162 100644 (file)
@@ -147,34 +147,13 @@ public class NodeStaticReplyTranslatorUtil {
         Preconditions.checkArgument(reply != null);
         final GroupFeaturesBuilder groupFeature = new GroupFeaturesBuilder();
         groupFeature.setMaxGroups(reply.getMaxGroups());
+
         final List<Class<? extends GroupType>> supportedGroups = new ArrayList<>();
-        if (reply.getTypes().isOFPGTALL()) {
-            supportedGroups.add(GroupAll.class);
-        }
-        if (reply.getTypes().isOFPGTSELECT()) {
-            supportedGroups.add(GroupSelect.class);
-        }
-        if (reply.getTypes().isOFPGTINDIRECT()) {
-            supportedGroups.add(GroupIndirect.class);
-        }
-        if (reply.getTypes().isOFPGTFF()) {
-            supportedGroups.add(GroupFf.class);
-        }
+        addSupportedGroups(reply, supportedGroups);
         groupFeature.setGroupTypesSupported(supportedGroups);
 
         final List<Class<? extends GroupCapability>> gCapability = new ArrayList<>();
-        if (reply.getCapabilities().isOFPGFCCHAINING()) {
-            gCapability.add(Chaining.class);
-        }
-        if (reply.getCapabilities().isOFPGFCCHAININGCHECKS()) {
-            gCapability.add(ChainingChecks.class);
-        }
-        if (reply.getCapabilities().isOFPGFCSELECTLIVENESS()) {
-            gCapability.add(SelectLiveness.class);
-        }
-        if (reply.getCapabilities().isOFPGFCSELECTWEIGHT()) {
-            gCapability.add(SelectWeight.class);
-        }
+        addGroupCapabilities(reply, gCapability);
         groupFeature.setGroupCapabilitiesSupported(gCapability);
 
         final List<Long> supportActionByGroups = new ArrayList<>();
@@ -202,6 +181,36 @@ public class NodeStaticReplyTranslatorUtil {
         return new NodeGroupFeaturesBuilder().setGroupFeatures(groupFeature.build()).build();
     }
 
+    private static void addGroupCapabilities(final MultipartReplyGroupFeatures reply, final List<Class<? extends GroupCapability>> gCapability) {
+        if (reply.getCapabilities().isOFPGFCCHAINING()) {
+            gCapability.add(Chaining.class);
+        }
+        if (reply.getCapabilities().isOFPGFCCHAININGCHECKS()) {
+            gCapability.add(ChainingChecks.class);
+        }
+        if (reply.getCapabilities().isOFPGFCSELECTLIVENESS()) {
+            gCapability.add(SelectLiveness.class);
+        }
+        if (reply.getCapabilities().isOFPGFCSELECTWEIGHT()) {
+            gCapability.add(SelectWeight.class);
+        }
+    }
+
+    private static void addSupportedGroups(final MultipartReplyGroupFeatures reply, final List<Class<? extends GroupType>> supportedGroups) {
+        if (reply.getTypes().isOFPGTALL()) {
+            supportedGroups.add(GroupAll.class);
+        }
+        if (reply.getTypes().isOFPGTSELECT()) {
+            supportedGroups.add(GroupSelect.class);
+        }
+        if (reply.getTypes().isOFPGTINDIRECT()) {
+            supportedGroups.add(GroupIndirect.class);
+        }
+        if (reply.getTypes().isOFPGTFF()) {
+            supportedGroups.add(GroupFf.class);
+        }
+    }
+
     /**
      * Method transform {@link MultipartReplyTableFeatures} to list of {@link TableFeatures}. Every
      * table can have List of TableFeatures so add it directly to
index ae169a021f8e78730c5031d73eb768b4c7b2d826..1bf37f17825be7b3456f2030a921f197ab4f1513 100644 (file)
@@ -57,59 +57,65 @@ public class SystemNotificationsListenerImpl implements SystemNotificationsListe
 
     @Override
     public void onSwitchIdleEvent(final SwitchIdleEvent notification) {
-        threadPool.execute(() -> {
-            boolean shouldBeDisconnected = true;
-
-            final InetSocketAddress remoteAddress = connectionContext.getConnectionAdapter().getRemoteAddress();
-
-            if (ConnectionContext.CONNECTION_STATE.WORKING.equals(connectionContext.getConnectionState())) {
-                FeaturesReply features = connectionContext.getFeatures();
-                LOG.info("Switch Idle state occurred, node={}|auxId={}", remoteAddress, features.getAuxiliaryId());
-                connectionContext.changeStateToTimeouting();
-                EchoInputBuilder builder = new EchoInputBuilder();
-                builder.setVersion(features.getVersion());
-                Xid xid = new Xid(0L);
-                builder.setXid(xid.getValue());
-
-                Future<RpcResult<EchoOutput>> echoReplyFuture = connectionContext.getConnectionAdapter().echo(builder.build());
-
-                try {
-                    RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(echoReplyTimeout, TimeUnit.MILLISECONDS);
-                    if (echoReplyValue.isSuccessful()) {
-                        connectionContext.changeStateToWorking();
-                        shouldBeDisconnected = false;
-                    } else {
-                        for (RpcError replyError : echoReplyValue.getErrors()) {
-                            Throwable cause = replyError.getCause();
-                            if (LOG.isWarnEnabled()) {
-                                LOG.warn("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause.getMessage());
-                            }
-
-                            if (LOG.isTraceEnabled()) {
-                                LOG.trace("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause);
-                            }
-
-                        }
-                    }
-                } catch (Exception e) {
-                    if (LOG.isWarnEnabled()) {
-                        LOG.warn("Exception while  waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e.getMessage());
-                    }
-
-                    if (LOG.isTraceEnabled()) {
-                        LOG.trace("Exception while  waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e);
-                    }
+        threadPool.execute(this::executeOnSwitchIdleEvent);
+    }
+
+    private void executeOnSwitchIdleEvent() {
+        boolean shouldBeDisconnected = true;
+
+        final InetSocketAddress remoteAddress = connectionContext.getConnectionAdapter().getRemoteAddress();
+
+        if (ConnectionContext.CONNECTION_STATE.WORKING.equals(connectionContext.getConnectionState())) {
+            FeaturesReply features = connectionContext.getFeatures();
+            LOG.info("Switch Idle state occurred, node={}|auxId={}", remoteAddress, features.getAuxiliaryId());
+            connectionContext.changeStateToTimeouting();
+            EchoInputBuilder builder = new EchoInputBuilder();
+            builder.setVersion(features.getVersion());
+            Xid xid = new Xid(0L);
+            builder.setXid(xid.getValue());
 
+            Future<RpcResult<EchoOutput>> echoReplyFuture = connectionContext.getConnectionAdapter().echo(builder.build());
+
+            try {
+                RpcResult<EchoOutput> echoReplyValue = echoReplyFuture.get(echoReplyTimeout, TimeUnit.MILLISECONDS);
+                if (echoReplyValue.isSuccessful()) {
+                    connectionContext.changeStateToWorking();
+                    shouldBeDisconnected = false;
+                } else {
+                    logErrors(remoteAddress, echoReplyValue);
                 }
-            }
-            if (shouldBeDisconnected) {
-                if (LOG.isInfoEnabled()) {
-                    LOG.info("ConnectionEvent:Closing connection as device is idle. Echo sent at {}. Device:{}, NodeId:{}",
-                            new Date(System.currentTimeMillis() - echoReplyTimeout), remoteAddress, connectionContext.getNodeId());
+            } catch (Exception e) {
+                if (LOG.isWarnEnabled()) {
+                    LOG.warn("Exception while  waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e.getMessage());
+                }
+
+                if (LOG.isTraceEnabled()) {
+                    LOG.trace("Exception while  waiting for echoReply from [{}] in TIMEOUTING state: {}", remoteAddress, e);
                 }
 
-                connectionContext.closeConnection(true);
             }
-        });
+        }
+        if (shouldBeDisconnected) {
+            if (LOG.isInfoEnabled()) {
+                LOG.info("ConnectionEvent:Closing connection as device is idle. Echo sent at {}. Device:{}, NodeId:{}",
+                        new Date(System.currentTimeMillis() - echoReplyTimeout), remoteAddress, connectionContext.getNodeId());
+            }
+
+            connectionContext.closeConnection(true);
+        }
+    }
+
+    private void logErrors(InetSocketAddress remoteAddress, RpcResult<EchoOutput> echoReplyValue) {
+        for (RpcError replyError : echoReplyValue.getErrors()) {
+            Throwable cause = replyError.getCause();
+            if (LOG.isWarnEnabled()) {
+                LOG.warn("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause.getMessage());
+            }
+
+            if (LOG.isTraceEnabled()) {
+                LOG.trace("Received EchoReply from [{}] in TIMEOUTING state, Error:{}", remoteAddress, cause);
+            }
+
+        }
     }
 }