dfe356bdb1efbbc5ec7d299025b6cf879d820b38
[controller.git] / opendaylight / md-sal / statistics-manager / src / main / java / org / opendaylight / controller / md / statistics / manager / MultipartMessageManager.java
1 /*
2  * Copyright IBM Corporation, 2013.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.statistics.manager;
9
10 import java.util.Date;
11 import java.util.Iterator;
12 import java.util.Map;
13 import java.util.concurrent.ConcurrentHashMap;
14
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;
17
18 /**
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
23  *
24  */
25 public class MultipartMessageManager {
26
27     /*
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.  
31      */
32     
33     private static Map<TxIdEntry,Date> txIdToRequestTypeMap = new ConcurrentHashMap<TxIdEntry,Date>();
34     /*
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.
37      */
38     private static Map<TxIdEntry,Short> txIdTotableIdMap = new ConcurrentHashMap<TxIdEntry,Short>();
39     
40     private final int NUMBER_OF_WAIT_CYCLES =2;
41
42     class TxIdEntry{
43         private final TransactionId txId;
44         private final NodeId nodeId;
45         private final StatsRequestType requestType;
46         
47         public TxIdEntry(NodeId nodeId, TransactionId txId, StatsRequestType requestType){
48             this.txId = txId;
49             this.nodeId = nodeId;
50             this.requestType = requestType;
51         }
52         public TransactionId getTxId() {
53             return txId;
54         }
55         public NodeId getNodeId() {
56             return nodeId;
57         }
58         public StatsRequestType getRequestType() {
59             return requestType;
60         }
61         @Override
62         public int hashCode() {
63             final int prime = 31;
64             int result = 1;
65             result = prime * result + getOuterType().hashCode();
66             result = prime * result + ((nodeId == null) ? 0 : nodeId.hashCode());
67             result = prime * result + ((txId == null) ? 0 : txId.hashCode());
68             return result;
69         }
70         @Override
71         public boolean equals(Object obj) {
72             if (this == obj) {
73                 return true;
74             }
75             if (obj == null) {
76                 return false;
77             }
78             if (!(obj instanceof TxIdEntry)) {
79                 return false;
80             }
81             TxIdEntry other = (TxIdEntry) obj;
82             if (!getOuterType().equals(other.getOuterType())) {
83                 return false;
84             }
85             if (nodeId == null) {
86                 if (other.nodeId != null) {
87                     return false;
88                 }
89             } else if (!nodeId.equals(other.nodeId)) {
90                 return false;
91             }
92             if (txId == null) {
93                 if (other.txId != null) {
94                     return false;
95                 }
96             } else if (!txId.equals(other.txId)) {
97                 return false;
98             }
99             return true;
100         }
101         private MultipartMessageManager getOuterType() {
102             return MultipartMessageManager.this;
103         }
104         @Override
105         public String toString() {
106             return "TxIdEntry [txId=" + txId + ", nodeId=" + nodeId + ", requestType=" + requestType + "]";
107         }
108     }
109
110     public MultipartMessageManager(){}
111     
112     public Short getTableIdForTxId(NodeId nodeId,TransactionId id){
113         
114         return txIdTotableIdMap.get(new TxIdEntry(nodeId,id,null));
115         
116     }
117     
118     public void setTxIdAndTableIdMapEntry(NodeId nodeId, TransactionId id,Short tableId){
119         
120         txIdTotableIdMap.put(new TxIdEntry(nodeId,id,null), tableId);
121     }
122     
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);
127         }else{
128             return txIdToRequestTypeMap.remove(entry)==null?false:true;
129         }
130     }
131     public void addTxIdToRequestTypeEntry (NodeId nodeId, TransactionId id,StatsRequestType type){
132         TxIdEntry entry = new TxIdEntry(nodeId,id,type);
133         txIdToRequestTypeMap.put(entry, getExpiryTime());
134     }
135     public boolean removeTxId(NodeId nodeId, TransactionId id){
136         TxIdEntry entry = new TxIdEntry(nodeId,id,null);
137         return txIdToRequestTypeMap.remove(entry)==null?false:true;
138     }
139     
140     private Date getExpiryTime(){
141         Date expires = new Date();
142         expires.setTime(expires.getTime()+StatisticsProvider.STATS_THREAD_EXECUTION_TIME*NUMBER_OF_WAIT_CYCLES);
143         return expires;
144     }
145
146     public enum StatsRequestType{
147         ALL_FLOW,
148         AGGR_FLOW,
149         ALL_PORT,
150         ALL_FLOW_TABLE,
151         ALL_QUEUE_STATS,
152         ALL_GROUP,
153         ALL_METER,
154         GROUP_DESC,
155         METER_CONFIG
156     }
157     
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)){
164                 it.remove();
165                 txIdTotableIdMap.remove(txIdEntry);
166             }            
167         }
168     }
169 }