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 0ce551a17c5f4f0ab94577f863abc4f09d2339ac..425a44946e064c97b44d27f42fdcdc8b1e2b9bc9 100644 (file)
@@ -7,11 +7,11 @@
  */
 package org.opendaylight.controller.md.statistics.manager;
 
-import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.ConcurrentHashMap;
 
+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;
 
@@ -24,35 +24,33 @@ import com.google.common.base.Preconditions;
  * @author avishnoi@in.ibm.com
  *
  */
-public class MultipartMessageManager {
-    private static final int NUMBER_OF_WAIT_CYCLES = 2;
-
+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
      *  response for which it didn't send the request.
      */
-    private final Map<TxIdEntry,Long> txIdToRequestTypeMap = new HashMap<>();
+    private final Map<TxIdEntry,Long> txIdToRequestTypeMap = new ConcurrentHashMap<>();
     /*
      * Map to keep track of the request tx id for flow table statistics request.
      * Because flow table statistics multi part response do not contains the table id.
      */
-    private final Map<TxIdEntry,Short> txIdTotableIdMap = new HashMap<>();
+    private final Map<TxIdEntry,Short> txIdTotableIdMap = new ConcurrentHashMap<>();
+    private final long lifetimeNanos;
+
+    public MultipartMessageManager(long lifetimeNanos) {
+        this.lifetimeNanos = lifetimeNanos;
+    }
 
     private static final class TxIdEntry {
-        private final StatsRequestType requestType;
         private final TransactionId txId;
 
-        public TxIdEntry(TransactionId txId, StatsRequestType requestType){
+        public TxIdEntry(TransactionId txId{
             this.txId = txId;
-            this.requestType = requestType;
         }
         public TransactionId getTxId() {
             return txId;
         }
-        public StatsRequestType getRequestType() {
-            return requestType;
-        }
         @Override
         public int hashCode() {
             final int prime = 31;
@@ -85,21 +83,26 @@ public class MultipartMessageManager {
 
         @Override
         public String toString() {
-            return "TxIdEntry [txId=" + txId + ", requestType=" + requestType + "]";
+            return "TxIdEntry [txId=" + txId + ']';
         }
     }
 
-    public void recordExpectedTableTransaction(TransactionId id, StatsRequestType type, Short tableId) {
-        recordExpectedTransaction(id, type);
-        txIdTotableIdMap.put(new TxIdEntry(id, null), Preconditions.checkNotNull(tableId));
+    public void recordExpectedTableTransaction(TransactionId id, Short tableId) {
+        recordExpectedTransaction(id);
+        txIdTotableIdMap.put(new TxIdEntry(id), Preconditions.checkNotNull(tableId));
     }
 
-    public Short isExpectedTableTransaction(TransactionAware transaction, Boolean more) {
+    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(), null);
+        final TxIdEntry key = new TxIdEntry(transaction.getTransactionId());
         if (more != null && more.booleanValue()) {
             return txIdTotableIdMap.get(key);
         } else {
@@ -107,13 +110,13 @@ public class MultipartMessageManager {
         }
     }
 
-    public void recordExpectedTransaction(TransactionId id, StatsRequestType type) {
-        TxIdEntry entry = new TxIdEntry(Preconditions.checkNotNull(id), Preconditions.checkNotNull(type));
+    public void recordExpectedTransaction(TransactionId id) {
+        TxIdEntry entry = new TxIdEntry(Preconditions.checkNotNull(id));
         txIdToRequestTypeMap.put(entry, getExpiryTime());
     }
 
-    public boolean isExpectedTransaction(TransactionAware transaction, Boolean more) {
-        TxIdEntry entry = new TxIdEntry(transaction.getTransactionId(), null);
+    private boolean isExpectedTransaction(TransactionAware transaction, Boolean more) {
+        final TxIdEntry entry = new TxIdEntry(transaction.getTransactionId());
         if (more != null && more.booleanValue()) {
             return txIdToRequestTypeMap.containsKey(entry);
         } else {
@@ -121,24 +124,20 @@ public class MultipartMessageManager {
         }
     }
 
-    private static Long getExpiryTime(){
-        return System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(
-                StatisticsProvider.STATS_COLLECTION_MILLIS*NUMBER_OF_WAIT_CYCLES);
+    public boolean isExpectedTransaction(TransactionAware transaction) {
+        Boolean more = null;
+        if (transaction instanceof MultipartTransactionAware) {
+            more = ((MultipartTransactionAware)transaction).isMoreReplies();
+        }
+
+        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();){