Unblock the Aluminium Jenkins Verified Build Pass
[netvirt.git] / aclservice / impl / src / main / java / org / opendaylight / netvirt / aclservice / stats / AclLiveStatisticsHelper.java
index c85deec88c7a9ddc9885b123b57abf08b578b1e3..cb1b425c049e9e3acd67a08a737f49bb420a8f54 100644 (file)
@@ -13,9 +13,11 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.genius.mdsalutil.MetaDataUtil;
 import org.opendaylight.genius.mdsalutil.NwConstants;
+import org.opendaylight.mdsal.binding.api.DataBroker;
 import org.opendaylight.netvirt.aclservice.utils.AclConstants;
 import org.opendaylight.netvirt.aclservice.utils.AclServiceUtils;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
@@ -42,7 +44,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.acl.port.stats.acl.drop.stats.BytesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.acl.live.statistics.rev161129.acl.stats.output.acl.port.stats.acl.drop.stats.PacketsBuilder;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -70,12 +71,11 @@ public final class AclLiveStatisticsHelper {
      * @param dataBroker the data broker
      * @return the acl port stats
      */
-    public static List<AclPortStats> getAclPortStats(Direction direction, List<String> interfaceNames,
+    public static List<AclPortStats> getAclPortStats(Direction direction, @NonNull List<String> interfaceNames,
             OpendaylightDirectStatisticsService odlDirectStatsService, DataBroker dataBroker) {
         LOG.trace("Get ACL port stats for direction {} and interfaces {}", direction, interfaceNames);
         List<AclPortStats> lstAclPortStats = new ArrayList<>();
 
-        Short tableId = getTableId(direction);
         FlowCookie aclDropFlowCookieMask = new FlowCookie(COOKIE_ACL_DROP_FLOW_MASK);
 
         for (String interfaceName : interfaceNames) {
@@ -101,9 +101,6 @@ public final class AclLiveStatisticsHelper {
             GetFlowStatisticsInputBuilder input =
                     new GetFlowStatisticsInputBuilder().setNode(nodeRef).setCookie(aclDropFlowCookie)
                             .setCookieMask(aclDropFlowCookieMask).setStoreStats(false);
-            if (direction != Direction.Both) {
-                input.setTableId(tableId);
-            }
 
             Future<RpcResult<GetFlowStatisticsOutput>> rpcResultFuture =
                     odlDirectStatsService.getFlowStatistics(input.build());
@@ -135,14 +132,11 @@ public final class AclLiveStatisticsHelper {
      * @param rpcResult the rpc result
      */
     private static void handleRpcErrors(List<AclPortStats> lstAclPortStats, AclPortStatsBuilder aclStatsBuilder,
-            RpcResult<GetFlowStatisticsOutput> rpcResult) {
+            @Nullable RpcResult<GetFlowStatisticsOutput> rpcResult) {
         LOG.error("Unable to retrieve drop counts due to error: {}", rpcResult);
         String errMsg = "Unable to retrieve drop counts due to error: ";
-        if (rpcResult != null && rpcResult.getErrors() != null) {
-            for (RpcError error : rpcResult.getErrors()) {
-                errMsg += error.getMessage();
-                break;
-            }
+        if (rpcResult != null && rpcResult.getErrors() != null && !rpcResult.getErrors().isEmpty()) {
+            errMsg += rpcResult.getErrors().iterator().next().getMessage();
         } else {
             errMsg += "Internal RPC call failed.";
         }
@@ -172,86 +166,81 @@ public final class AclLiveStatisticsHelper {
         PacketsBuilder portIngressPacketsBuilder = new PacketsBuilder();
 
         for (FlowAndStatisticsMapList flowStats : flowAndStatisticsMapList) {
-            BigInteger portEgressBytesBuilderDropCount = BigInteger.valueOf(0);
-            BigInteger portEgressPacketsBuilderDropCount = BigInteger.valueOf(0);
-            BigInteger portIngressBytesBuilderDropCount = BigInteger.valueOf(0);
-            BigInteger portIngressPacketsBuilderDropCount = BigInteger.valueOf(0);
-
-            switch (flowStats.getTableId()) {
+            switch (flowStats.getTableId().toJava()) {
                 case NwConstants.INGRESS_ACL_FILTER_CUM_DISPATCHER_TABLE:
-                    if (flowStats.getPriority().equals(AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY)) {
+                    if (AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY.equals(flowStats.getPriority().toJava())) {
                         portEgressBytesBuilder.setInvalidDropCount(flowStats.getByteCount().getValue());
                         portEgressPacketsBuilder.setInvalidDropCount(flowStats.getPacketCount().getValue());
-                    } else if (flowStats.getPriority().equals(AclConstants.ACL_PORT_SPECIFIC_DROP_PRIORITY)) {
-                        if (portEgressBytesBuilder.getDropCount() != null) {
-                            portEgressBytesBuilderDropCount = portEgressBytesBuilder.getDropCount()
-                                    .add(flowStats.getByteCount().getValue());
-                            portEgressPacketsBuilderDropCount = portEgressPacketsBuilder.getDropCount()
-                                    .add(flowStats.getPacketCount().getValue());
-                        } else {
-                            portEgressBytesBuilderDropCount = flowStats.getByteCount().getValue();
-                            portEgressPacketsBuilderDropCount = flowStats.getPacketCount().getValue();
-                        }
-                    } else if (flowStats.getPriority().equals(AclConstants.ACE_LAST_REMOTE_ACL_PRIORITY)) {
+                    } else if (AclConstants.ACL_PORT_SPECIFIC_DROP_PRIORITY.equals(flowStats.getPriority().toJava())
+                            || AclConstants.ACE_LAST_REMOTE_ACL_PRIORITY.equals(flowStats.getPriority().toJava())) {
+                        BigInteger portEgressBytesBuilderDropCount = BigInteger.valueOf(0);
+                        BigInteger portEgressPacketsBuilderDropCount = BigInteger.valueOf(0);
                         if (portEgressBytesBuilder.getDropCount() != null) {
-                            portEgressBytesBuilderDropCount = portEgressBytesBuilder.getDropCount()
-                                    .add(flowStats.getByteCount().getValue());
-                            portEgressPacketsBuilderDropCount = portEgressPacketsBuilder.getDropCount()
-                                    .add(flowStats.getPacketCount().getValue());
+                            portEgressBytesBuilderDropCount = portEgressBytesBuilder.getDropCount().toJava()
+                                    .add(flowStats.getByteCount().getValue().toJava());
+                            portEgressPacketsBuilderDropCount = portEgressPacketsBuilder.getDropCount().toJava()
+                                    .add(flowStats.getPacketCount().getValue().toJava());
                         } else {
-                            portEgressBytesBuilderDropCount = flowStats.getByteCount().getValue();
-                            portEgressPacketsBuilderDropCount = flowStats.getPacketCount().getValue();
+                            portEgressBytesBuilderDropCount = flowStats.getByteCount().getValue().toJava();
+                            portEgressPacketsBuilderDropCount = flowStats.getPacketCount().getValue().toJava();
                         }
+                        portEgressBytesBuilder.setDropCount(portEgressBytesBuilderDropCount);
+                        portEgressPacketsBuilder.setDropCount(portEgressPacketsBuilderDropCount);
                     }
                     // TODO: Update stats for other drops
                     break;
 
                 case NwConstants.EGRESS_ACL_FILTER_CUM_DISPATCHER_TABLE:
-                    if (flowStats.getPriority().equals(AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY)) {
+                    if (AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY.equals(flowStats.getPriority().toJava())) {
                         portIngressBytesBuilder.setInvalidDropCount(flowStats.getByteCount().getValue());
                         portIngressPacketsBuilder.setInvalidDropCount(flowStats.getPacketCount().getValue());
-                    } else if (flowStats.getPriority().equals(AclConstants.ACL_PORT_SPECIFIC_DROP_PRIORITY)) {
-                        if (portIngressBytesBuilder.getDropCount() != null) {
-                            portIngressBytesBuilderDropCount = portIngressBytesBuilder.getDropCount()
-                                    .add(flowStats.getByteCount().getValue());
-                            portIngressPacketsBuilderDropCount = portIngressPacketsBuilder.getDropCount()
-                                    .add(flowStats.getPacketCount().getValue());
-                        } else {
-                            portIngressBytesBuilderDropCount = flowStats.getByteCount().getValue();
-                            portIngressPacketsBuilderDropCount = flowStats.getPacketCount().getValue();
-                        }
-                    } else if (flowStats.getPriority().equals(AclConstants.ACE_LAST_REMOTE_ACL_PRIORITY)) {
+                    } else if (AclConstants.ACL_PORT_SPECIFIC_DROP_PRIORITY.equals(flowStats.getPriority().toJava())
+                            || AclConstants.ACE_LAST_REMOTE_ACL_PRIORITY.equals(flowStats.getPriority().toJava())) {
+                        BigInteger portIngressBytesBuilderDropCount = BigInteger.valueOf(0);
+                        BigInteger portIngressPacketsBuilderDropCount = BigInteger.valueOf(0);
                         if (portIngressBytesBuilder.getDropCount() != null) {
-                            portIngressBytesBuilderDropCount = portIngressBytesBuilder.getDropCount()
-                                    .add(flowStats.getByteCount().getValue());
-                            portIngressPacketsBuilderDropCount = portIngressPacketsBuilder.getDropCount()
-                                    .add(flowStats.getPacketCount().getValue());
+                            portIngressBytesBuilderDropCount = portIngressBytesBuilder.getDropCount().toJava()
+                                    .add(flowStats.getByteCount().getValue().toJava());
+                            portIngressPacketsBuilderDropCount = portIngressPacketsBuilder.getDropCount().toJava()
+                                    .add(flowStats.getPacketCount().getValue().toJava());
                         } else {
-                            portIngressBytesBuilderDropCount = flowStats.getByteCount().getValue();
-                            portIngressPacketsBuilderDropCount = flowStats.getPacketCount().getValue();
+                            portIngressBytesBuilderDropCount = flowStats.getByteCount().getValue().toJava();
+                            portIngressPacketsBuilderDropCount = flowStats.getPacketCount().getValue().toJava();
                         }
+                        portIngressBytesBuilder.setDropCount(portIngressBytesBuilderDropCount);
+                        portIngressPacketsBuilder.setDropCount(portIngressPacketsBuilderDropCount);
                     }
                     // TODO: Update stats for other drops
                     break;
+                case NwConstants.INGRESS_ACL_COMMITTER_TABLE:
+                    if (AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY.equals(flowStats.getPriority().toJava())) {
+                        portEgressBytesBuilder.setAntiSpoofDropCount(flowStats.getByteCount().getValue());
+                        portEgressPacketsBuilder.setAntiSpoofDropCount(flowStats.getPacketCount().getValue());
+                    }
+                    break;
+                case NwConstants.EGRESS_ACL_COMMITTER_TABLE:
+                    if (AclConstants.CT_STATE_TRACKED_INVALID_PRIORITY.equals(flowStats.getPriority().toJava())) {
+                        portIngressBytesBuilder.setAntiSpoofDropCount(flowStats.getByteCount().getValue());
+                        portIngressPacketsBuilder.setAntiSpoofDropCount(flowStats.getPacketCount().getValue());
+                    }
+                    break;
 
                 default:
                     LOG.warn("Invalid table ID filtered for Acl flow stats: {}", flowStats);
                     break;
             }
-            portEgressBytesBuilder.setDropCount(portEgressBytesBuilderDropCount);
-            portEgressPacketsBuilder.setDropCount(portEgressPacketsBuilderDropCount);
-            portIngressBytesBuilder.setDropCount(portIngressBytesBuilderDropCount);
-            portIngressPacketsBuilder.setDropCount(portIngressPacketsBuilderDropCount);
         }
 
         List<AclDropStats> lstAclDropStats = new ArrayList<>();
         if (direction == Direction.Egress || direction == Direction.Both) {
+            updateTotalDropCount(portEgressBytesBuilder,portEgressPacketsBuilder);
             AclDropStats aclEgressDropStats = new AclDropStatsBuilder().setDirection(Direction.Egress)
                     .setBytes(portEgressBytesBuilder.build()).setPackets(portEgressPacketsBuilder.build()).build();
             lstAclDropStats.add(aclEgressDropStats);
         }
 
         if (direction == Direction.Ingress || direction == Direction.Both) {
+            updateTotalDropCount(portIngressBytesBuilder,portIngressPacketsBuilder);
             AclDropStats aclIngressDropStats = new AclDropStatsBuilder().setDirection(Direction.Ingress)
                     .setBytes(portIngressBytesBuilder.build()).setPackets(portIngressPacketsBuilder.build()).build();
             lstAclDropStats.add(aclIngressDropStats);
@@ -259,6 +248,37 @@ public final class AclLiveStatisticsHelper {
         aclStatsBuilder.setAclDropStats(lstAclDropStats);
     }
 
+    private static void updateTotalDropCount(BytesBuilder portBytesBuilder, PacketsBuilder portPacketsBuilder) {
+        BigInteger dropCountByt = BigInteger.ZERO;
+        BigInteger invalidDropCountByt = BigInteger.ZERO;
+        BigInteger antispoofDropCountByt = BigInteger.ZERO;
+        BigInteger dropCountPkt = BigInteger.ZERO;
+        BigInteger invalidDropCountPkt = BigInteger.ZERO;
+        BigInteger antispoofDropCountPkt = BigInteger.ZERO;
+
+        if (portBytesBuilder.getDropCount() != null) {
+            dropCountByt = portBytesBuilder.getDropCount().toJava();
+        }
+        if (portPacketsBuilder.getDropCount() != null) {
+            dropCountPkt = portPacketsBuilder.getDropCount().toJava();
+        }
+        if (portBytesBuilder.getDropCount() != null) {
+            invalidDropCountByt = portBytesBuilder.getInvalidDropCount().toJava();
+        }
+        if (portPacketsBuilder.getDropCount() != null) {
+            invalidDropCountPkt = portPacketsBuilder.getInvalidDropCount().toJava();
+        }
+        if (portBytesBuilder.getDropCount() != null) {
+            antispoofDropCountByt = portBytesBuilder.getAntiSpoofDropCount().toJava();
+        }
+        if (portPacketsBuilder.getDropCount() != null) {
+            antispoofDropCountPkt = portPacketsBuilder.getAntiSpoofDropCount().toJava();
+        }
+        portBytesBuilder.setTotalDropCount(antispoofDropCountByt.add(dropCountByt.add(invalidDropCountByt)));
+        portPacketsBuilder.setTotalDropCount(antispoofDropCountPkt.add(dropCountPkt.add(invalidDropCountPkt)));
+
+    }
+
     /**
      * Adds the error.
      *
@@ -284,23 +304,6 @@ public final class AclLiveStatisticsHelper {
         return new MatchBuilder().setMetadata(metadata).build();
     }
 
-    /**
-     * Gets the table id.
-     *
-     * @param direction the direction
-     * @return the table id
-     */
-    private static Short getTableId(Direction direction) {
-        Short tableId;
-        if (direction == Direction.Egress) {
-            tableId = NwConstants.INGRESS_ACL_FILTER_CUM_DISPATCHER_TABLE;
-        } else {
-            // in case of ingress
-            tableId = NwConstants.EGRESS_ACL_FILTER_CUM_DISPATCHER_TABLE;
-        }
-        return tableId;
-    }
-
     /**
      * Builds the node ref.
      *