2 * Copyright IBM Corporation, 2013. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.md.statistics.manager;
10 import java.util.Iterator;
12 import java.util.concurrent.ConcurrentHashMap;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionAware;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
17 import com.google.common.base.Preconditions;
20 * Main responsibility of the class is to manage multipart response
21 * for multipart request. It also handles the flow aggregate request
22 * and response mapping.
23 * @author avishnoi@in.ibm.com
26 class MultipartMessageManager {
28 * Map for tx id and type of request, to keep track of all the request sent
29 * by Statistics Manager. Statistics Manager won't entertain any multipart
30 * response for which it didn't send the request.
32 private final Map<TxIdEntry,Long> txIdToRequestTypeMap = new ConcurrentHashMap<>();
34 * Map to keep track of the request tx id for flow table statistics request.
35 * Because flow table statistics multi part response do not contains the table id.
37 private final Map<TxIdEntry,Short> txIdTotableIdMap = new ConcurrentHashMap<>();
38 private final long lifetimeNanos;
40 public MultipartMessageManager(long lifetimeNanos) {
41 this.lifetimeNanos = lifetimeNanos;
44 private static final class TxIdEntry {
45 private final TransactionId txId;
47 public TxIdEntry(TransactionId txId) {
50 public TransactionId getTxId() {
54 public int hashCode() {
57 result = prime * result + ((txId == null) ? 0 : txId.hashCode());
61 public boolean equals(Object obj) {
68 if (!(obj instanceof TxIdEntry)) {
71 TxIdEntry other = (TxIdEntry) obj;
74 if (other.txId != null) {
77 } else if (!txId.equals(other.txId)) {
84 public String toString() {
85 return "TxIdEntry [txId=" + txId + ']';
89 public void recordExpectedTableTransaction(TransactionId id, Short tableId) {
90 recordExpectedTransaction(id);
91 txIdTotableIdMap.put(new TxIdEntry(id), Preconditions.checkNotNull(tableId));
94 public Short isExpectedTableTransaction(TransactionAware transaction, Boolean more) {
95 if (!isExpectedTransaction(transaction, more)) {
99 final TxIdEntry key = new TxIdEntry(transaction.getTransactionId());
100 if (more != null && more.booleanValue()) {
101 return txIdTotableIdMap.get(key);
103 return txIdTotableIdMap.remove(key);
107 public void recordExpectedTransaction(TransactionId id) {
108 TxIdEntry entry = new TxIdEntry(Preconditions.checkNotNull(id));
109 txIdToRequestTypeMap.put(entry, getExpiryTime());
112 public boolean isExpectedTransaction(TransactionAware transaction, Boolean more) {
113 TxIdEntry entry = new TxIdEntry(transaction.getTransactionId());
114 if (more != null && more.booleanValue()) {
115 return txIdToRequestTypeMap.containsKey(entry);
117 return txIdToRequestTypeMap.remove(entry) != null;
121 private Long getExpiryTime() {
122 return System.nanoTime() + lifetimeNanos;
125 public void cleanStaleTransactionIds() {
126 final long now = System.nanoTime();
128 for (Iterator<TxIdEntry> it = txIdToRequestTypeMap.keySet().iterator();it.hasNext();){
129 TxIdEntry txIdEntry = it.next();
131 Long expiryTime = txIdToRequestTypeMap.get(txIdEntry);
132 if(now > expiryTime){
134 txIdTotableIdMap.remove(txIdEntry);