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.Date;
11 import java.util.Iterator;
13 import java.util.concurrent.ConcurrentHashMap;
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.
33 private static Map<TxIdEntry,Date> txIdToRequestTypeMap = new ConcurrentHashMap<TxIdEntry,Date>();
35 * Map to keep track of the request tx id for flow table statistics request.
36 * Because flow table statistics multi part response do not contains the table id.
38 private static Map<TxIdEntry,Short> txIdTotableIdMap = new ConcurrentHashMap<TxIdEntry,Short>();
40 private final int NUMBER_OF_WAIT_CYCLES =2;
43 private final TransactionId txId;
44 private final NodeId nodeId;
45 private final StatsRequestType requestType;
47 public TxIdEntry(NodeId nodeId, TransactionId txId, StatsRequestType requestType){
50 this.requestType = requestType;
52 public TransactionId getTxId() {
55 public NodeId getNodeId() {
58 public StatsRequestType getRequestType() {
62 public int hashCode() {
65 result = prime * result + getOuterType().hashCode();
66 result = prime * result + ((nodeId == null) ? 0 : nodeId.hashCode());
67 result = prime * result + ((txId == null) ? 0 : txId.hashCode());
71 public boolean equals(Object obj) {
78 if (!(obj instanceof TxIdEntry)) {
81 TxIdEntry other = (TxIdEntry) obj;
82 if (!getOuterType().equals(other.getOuterType())) {
86 if (other.nodeId != null) {
89 } else if (!nodeId.equals(other.nodeId)) {
93 if (other.txId != null) {
96 } else if (!txId.equals(other.txId)) {
101 private MultipartMessageManager getOuterType() {
102 return MultipartMessageManager.this;
105 public String toString() {
106 return "TxIdEntry [txId=" + txId + ", nodeId=" + nodeId + ", requestType=" + requestType + "]";
110 public MultipartMessageManager(){}
112 public Short getTableIdForTxId(NodeId nodeId,TransactionId id){
114 return txIdTotableIdMap.get(new TxIdEntry(nodeId,id,null));
118 public void setTxIdAndTableIdMapEntry(NodeId nodeId, TransactionId id,Short tableId){
120 txIdTotableIdMap.put(new TxIdEntry(nodeId,id,null), tableId);
123 public boolean isRequestTxIdExist(NodeId nodeId, TransactionId id, Boolean moreRepliesToFollow){
124 TxIdEntry entry = new TxIdEntry(nodeId,id,null);
125 if(moreRepliesToFollow.booleanValue()){
126 return txIdToRequestTypeMap.containsKey(entry);
128 return txIdToRequestTypeMap.remove(entry)==null?false:true;
131 public void addTxIdToRequestTypeEntry (NodeId nodeId, TransactionId id,StatsRequestType type){
132 TxIdEntry entry = new TxIdEntry(nodeId,id,type);
133 txIdToRequestTypeMap.put(entry, getExpiryTime());
135 public boolean removeTxId(NodeId nodeId, TransactionId id){
136 TxIdEntry entry = new TxIdEntry(nodeId,id,null);
137 return txIdToRequestTypeMap.remove(entry)==null?false:true;
140 private Date getExpiryTime(){
141 Date expires = new Date();
142 expires.setTime(expires.getTime()+StatisticsProvider.STATS_THREAD_EXECUTION_TIME*NUMBER_OF_WAIT_CYCLES);
146 public enum StatsRequestType{
158 public void cleanStaleTransactionIds(){
159 for (Iterator<TxIdEntry> it = txIdToRequestTypeMap.keySet().iterator();it.hasNext();){
160 TxIdEntry txIdEntry = it.next();
161 Date now = new Date();
162 Date expiryTime = txIdToRequestTypeMap.get(txIdEntry);
163 if(now.after(expiryTime)){
165 txIdTotableIdMap.remove(txIdEntry);