Service Handler optimizations and technical debt
[transportpce.git] / servicehandler / src / main / java / org / opendaylight / transportpce / servicehandler / validation / checks / ServicehandlerTxRxCheck.java
index 519f85cfb761678f2049edd8a63a060ce9f76b6c..fbca473aff2a63f1cac8913bd778bbb4b7d0cb92 100644 (file)
@@ -25,6 +25,7 @@ public final class ServicehandlerTxRxCheck {
     // This is class is public so that these messages can be accessed from Junit (avoid duplications).
     public static final class LogMessages {
 
+        private static final String SERVICE = "Service ";
         public static final String TXDIR_NOT_SET;
         public static final String TXDIR_PORT_NOT_SET;
         public static final String TXDIR_LGX_NOT_SET;
@@ -43,19 +44,19 @@ public final class ServicehandlerTxRxCheck {
         }
 
         public static String endpointTypeNotSet(ServiceEndpointType endpointType) {
-            return "Service " + endpointType + " is not set";
+            return SERVICE + endpointType + " is not set";
         }
 
         public static String rateNotSet(ServiceEndpointType endpointType) {
-            return "Service " + endpointType + " rate is not set";
+            return SERVICE + endpointType + " rate is not set";
         }
 
         public static String formatNotSet(ServiceEndpointType endpointType) {
-            return "Service " + endpointType + " format is not set";
+            return SERVICE + endpointType + " format is not set";
         }
 
         public static String clliNotSet(ServiceEndpointType endpointType) {
-            return "Service " + endpointType + " clli is not set";
+            return SERVICE + endpointType + " clli is not set";
         }
 
         private LogMessages() {
@@ -71,7 +72,7 @@ public final class ServicehandlerTxRxCheck {
      * @return true if String ok false if not
      */
     public static boolean checkString(String value) {
-        return ((value != null) && (value.compareTo("") != 0));
+        return (value != null && !value.isEmpty());
     }
 
     /**
@@ -81,21 +82,24 @@ public final class ServicehandlerTxRxCheck {
      *            port info
      * @return true if String ok false if not
      */
+    @SuppressWarnings("java:S1067")
+    //sonar issue Reduce the number of conditional operators (4) used in the expression (maximum allowed 3)
+    //won't be fixed because of functional checks needed
     public static boolean checkPort(Port port) {
-        boolean result = false;
-        if (port != null) {
-            String portDeviceName = port.getPortDeviceName();
-            String portType = port.getPortType();
-            String portName = port.getPortName();
-            String portRack = port.getPortRack();
-            String portShelf = port.getPortShelf();
-
-            if (checkString(portDeviceName) && checkString(portType) && checkString(portName) && checkString(portRack)
-                    && checkString(portShelf)) {
-                result = true;
-            }
-        }
-        return result;
+        if (port == null) {
+            return false;
+        }
+        String portDeviceName = port.getPortDeviceName();
+        String portType = port.getPortType();
+        String portName = port.getPortName();
+        String portRack = port.getPortRack();
+        String portShelf = port.getPortShelf();
+
+        return checkString(portDeviceName)
+                && checkString(portType)
+                && checkString(portName)
+                && checkString(portRack)
+                && checkString(portShelf);
     }
 
     /**
@@ -106,18 +110,17 @@ public final class ServicehandlerTxRxCheck {
      * @return true if String ok false if not
      */
     public static boolean checkLgx(Lgx lgx) {
-        boolean result = false;
-        if (lgx != null) {
-            String lgxDeviceName = lgx.getLgxDeviceName();
-            String lgxPortName = lgx.getLgxPortName();
-            String lgxPortRack = lgx.getLgxPortRack();
-            String lgxPortShelf = lgx.getLgxPortShelf();
-            if (checkString(lgxDeviceName) && checkString(lgxPortName) && checkString(lgxPortRack)
-                    && checkString(lgxPortShelf)) {
-                result = true;
-            }
-        }
-        return result;
+        if (lgx == null) {
+            return false;
+        }
+        String lgxDeviceName = lgx.getLgxDeviceName();
+        String lgxPortName = lgx.getLgxPortName();
+        String lgxPortRack = lgx.getLgxPortRack();
+        String lgxPortShelf = lgx.getLgxPortShelf();
+        return checkString(lgxDeviceName)
+                && checkString(lgxPortName)
+                && checkString(lgxPortRack)
+                && checkString(lgxPortShelf);
     }
 
     /**
@@ -164,18 +167,14 @@ public final class ServicehandlerTxRxCheck {
             return new ComplianceCheckResult(false, LogMessages.endpointTypeNotSet(endpointType));
         }
 
-//TODO check if an expected bug was justifying this NPE handling
-//        try {
+        if (serviceEnd.getServiceRate() == null) {
+            String message = "Something wrong when accessing Service " + endpointType + " rate, format or clli";
+            return new ComplianceCheckResult(false, message);
+        }
         Long serviceRate = serviceEnd.getServiceRate().toJava();
         ServiceFormat serviceformat = serviceEnd.getServiceFormat();
         String clli = serviceEnd.getClli();
-//        } catch (NullPointerException e) {
-//            String message = "Something wrong when accessing Service " + endpointType + " rate, format or clli";
-//            LOG.error("Service TxRx info check: {}",message, e);
-//            return new ComplianceCheckResult(false, message);
-//        }
-
-        if ((serviceRate == null) || (serviceRate <= 0)) {
+        if (serviceRate <= 0) {
             return new ComplianceCheckResult(false, LogMessages.rateNotSet(endpointType));
         }
         if (serviceformat == null) {