Merge "Add filtering capability to config.ini in order to reference logging bridge...
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / MultipartMessageManager.java
index 2201cb3930e43427cdec6a1921a27f57538fdb04..425a44946e064c97b44d27f42fdcdc8b1e2b9bc9 100644 (file)
@@ -10,10 +10,12 @@ package org.opendaylight.controller.md.statistics.manager;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.TimeUnit;
 
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.MultipartTransactionAware;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionAware;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+
+import com.google.common.base.Preconditions;
 
 /**
  * Main responsibility of the class is to manage multipart response
@@ -22,8 +24,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
  * @author avishnoi@in.ibm.com
  *
  */
-public class MultipartMessageManager {
-
+class MultipartMessageManager {
     /*
      *  Map for tx id and type of request, to keep track of all the request sent
      *  by Statistics Manager. Statistics Manager won't entertain any multipart
@@ -35,33 +36,25 @@ public class MultipartMessageManager {
      * Because flow table statistics multi part response do not contains the table id.
      */
     private final Map<TxIdEntry,Short> txIdTotableIdMap = new ConcurrentHashMap<>();
+    private final long lifetimeNanos;
 
-    private static final int NUMBER_OF_WAIT_CYCLES =2;
+    public MultipartMessageManager(long lifetimeNanos) {
+        this.lifetimeNanos = lifetimeNanos;
+    }
 
     private static final class TxIdEntry {
         private final TransactionId txId;
-        private final NodeId nodeId;
-        private final StatsRequestType requestType;
 
-        public TxIdEntry(NodeId nodeId, TransactionId txId, StatsRequestType requestType){
+        public TxIdEntry(TransactionId txId) {
             this.txId = txId;
-            this.nodeId = nodeId;
-            this.requestType = requestType;
         }
         public TransactionId getTxId() {
             return txId;
         }
-        public NodeId getNodeId() {
-            return nodeId;
-        }
-        public StatsRequestType getRequestType() {
-            return requestType;
-        }
         @Override
         public int hashCode() {
             final int prime = 31;
             int result = 1;
-            result = prime * result + ((nodeId == null) ? 0 : nodeId.hashCode());
             result = prime * result + ((txId == null) ? 0 : txId.hashCode());
             return result;
         }
@@ -78,13 +71,6 @@ public class MultipartMessageManager {
             }
             TxIdEntry other = (TxIdEntry) obj;
 
-            if (nodeId == null) {
-                if (other.nodeId != null) {
-                    return false;
-                }
-            } else if (!nodeId.equals(other.nodeId)) {
-                return false;
-            }
             if (txId == null) {
                 if (other.txId != null) {
                     return false;
@@ -97,54 +83,61 @@ public class MultipartMessageManager {
 
         @Override
         public String toString() {
-            return "TxIdEntry [txId=" + txId + ", nodeId=" + nodeId + ", requestType=" + requestType + "]";
+            return "TxIdEntry [txId=" + txId + ']';
         }
     }
 
-    public Short getTableIdForTxId(NodeId nodeId,TransactionId id){
-        return txIdTotableIdMap.get(new TxIdEntry(nodeId,id,null));
+    public void recordExpectedTableTransaction(TransactionId id, Short tableId) {
+        recordExpectedTransaction(id);
+        txIdTotableIdMap.put(new TxIdEntry(id), Preconditions.checkNotNull(tableId));
+    }
+
+    public Short isExpectedTableTransaction(TransactionAware transaction) {
+        Boolean more = null;
+        if (transaction instanceof MultipartTransactionAware) {
+            more = ((MultipartTransactionAware)transaction).isMoreReplies();
+        }
+
+        if (!isExpectedTransaction(transaction, more)) {
+            return null;
+        }
+
+        final TxIdEntry key = new TxIdEntry(transaction.getTransactionId());
+        if (more != null && more.booleanValue()) {
+            return txIdTotableIdMap.get(key);
+        } else {
+            return txIdTotableIdMap.remove(key);
+        }
     }
 
-    public void setTxIdAndTableIdMapEntry(NodeId nodeId, TransactionId id,Short tableId){
-        if(id == null)
-            return;
-        txIdTotableIdMap.put(new TxIdEntry(nodeId,id,null), tableId);
+    public void recordExpectedTransaction(TransactionId id) {
+        TxIdEntry entry = new TxIdEntry(Preconditions.checkNotNull(id));
+        txIdToRequestTypeMap.put(entry, getExpiryTime());
     }
 
-    public boolean isRequestTxIdExist(NodeId nodeId, TransactionId id, Boolean moreRepliesToFollow){
-        TxIdEntry entry = new TxIdEntry(nodeId,id,null);
-        if(moreRepliesToFollow.booleanValue()){
+    private boolean isExpectedTransaction(TransactionAware transaction, Boolean more) {
+        final TxIdEntry entry = new TxIdEntry(transaction.getTransactionId());
+        if (more != null && more.booleanValue()) {
             return txIdToRequestTypeMap.containsKey(entry);
-        }else{
+        } else {
             return txIdToRequestTypeMap.remove(entry) != null;
         }
     }
 
-    public void addTxIdToRequestTypeEntry (NodeId nodeId, TransactionId id,StatsRequestType type){
-        if(id == null)
-            return;
-        TxIdEntry entry = new TxIdEntry(nodeId,id,type);
-        txIdToRequestTypeMap.put(entry, getExpiryTime());
-    }
+    public boolean isExpectedTransaction(TransactionAware transaction) {
+        Boolean more = null;
+        if (transaction instanceof MultipartTransactionAware) {
+            more = ((MultipartTransactionAware)transaction).isMoreReplies();
+        }
 
-    private static Long getExpiryTime(){
-        return System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(
-                StatisticsProvider.STATS_COLLECTION_MILLIS*NUMBER_OF_WAIT_CYCLES);
+        return isExpectedTransaction(transaction, more);
     }
 
-    public enum StatsRequestType{
-        ALL_FLOW,
-        AGGR_FLOW,
-        ALL_PORT,
-        ALL_FLOW_TABLE,
-        ALL_QUEUE_STATS,
-        ALL_GROUP,
-        ALL_METER,
-        GROUP_DESC,
-        METER_CONFIG
+    private Long getExpiryTime() {
+        return System.nanoTime() + lifetimeNanos;
     }
 
-    public void cleanStaleTransactionIds(){
+    public void cleanStaleTransactionIds() {
         final long now = System.nanoTime();
 
         for (Iterator<TxIdEntry> it = txIdToRequestTypeMap.keySet().iterator();it.hasNext();){