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;
13 import java.util.concurrent.TimeUnit;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
19 * Main responsibility of the class is to manage multipart response
20 * for multipart request. It also handles the flow aggregate request
21 * and response mapping.
22 * @author avishnoi@in.ibm.com
25 public 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<>();
39 private static final int NUMBER_OF_WAIT_CYCLES =2;
41 private static final class TxIdEntry {
42 private final TransactionId txId;
43 private final NodeId nodeId;
44 private final StatsRequestType requestType;
46 public TxIdEntry(NodeId nodeId, TransactionId txId, StatsRequestType requestType){
49 this.requestType = requestType;
51 public TransactionId getTxId() {
54 public NodeId getNodeId() {
57 public StatsRequestType getRequestType() {
61 public int hashCode() {
64 result = prime * result + ((nodeId == null) ? 0 : nodeId.hashCode());
65 result = prime * result + ((txId == null) ? 0 : txId.hashCode());
69 public boolean equals(Object obj) {
76 if (!(obj instanceof TxIdEntry)) {
79 TxIdEntry other = (TxIdEntry) obj;
82 if (other.nodeId != null) {
85 } else if (!nodeId.equals(other.nodeId)) {
89 if (other.txId != null) {
92 } else if (!txId.equals(other.txId)) {
99 public String toString() {
100 return "TxIdEntry [txId=" + txId + ", nodeId=" + nodeId + ", requestType=" + requestType + "]";
104 public Short getTableIdForTxId(NodeId nodeId,TransactionId id){
105 return txIdTotableIdMap.get(new TxIdEntry(nodeId,id,null));
108 public void setTxIdAndTableIdMapEntry(NodeId nodeId, TransactionId id,Short tableId){
111 txIdTotableIdMap.put(new TxIdEntry(nodeId,id,null), tableId);
114 public boolean isRequestTxIdExist(NodeId nodeId, TransactionId id, Boolean moreRepliesToFollow){
115 TxIdEntry entry = new TxIdEntry(nodeId,id,null);
116 if(moreRepliesToFollow.booleanValue()){
117 return txIdToRequestTypeMap.containsKey(entry);
119 return txIdToRequestTypeMap.remove(entry) != null;
123 public void addTxIdToRequestTypeEntry (NodeId nodeId, TransactionId id,StatsRequestType type){
126 TxIdEntry entry = new TxIdEntry(nodeId,id,type);
127 txIdToRequestTypeMap.put(entry, getExpiryTime());
130 private static Long getExpiryTime(){
131 return System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(
132 StatisticsProvider.STATS_THREAD_EXECUTION_TIME*NUMBER_OF_WAIT_CYCLES);
135 public enum StatsRequestType{
147 public void cleanStaleTransactionIds(){
148 final long now = System.nanoTime();
150 for (Iterator<TxIdEntry> it = txIdToRequestTypeMap.keySet().iterator();it.hasNext();){
151 TxIdEntry txIdEntry = it.next();
153 Long expiryTime = txIdToRequestTypeMap.get(txIdEntry);
154 if(now > expiryTime){
156 txIdTotableIdMap.remove(txIdEntry);