bug 2446 - High priority (control) queue stop reading from channel if is full
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / queue / WrapperQueueImpl.java
1 package org.opendaylight.openflowplugin.openflow.md.queue;
2
3 import java.util.Collection;
4 import java.util.Iterator;
5 import java.util.Queue;
6
7 import org.opendaylight.openflowplugin.api.openflow.md.queue.QueueItem;
8 import org.opendaylight.openflowplugin.api.openflow.md.queue.WaterMarkListener;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11
12 import com.google.common.base.Preconditions;
13
14 public class WrapperQueueImpl<E> implements Queue<E> {
15
16     protected static final Logger LOG = LoggerFactory
17             .getLogger(WrapperQueueImpl.class);
18
19     private int lowWaterMark;
20     private int highWaterMark;
21
22     private WaterMarkListener queueListenerMark;
23
24     private Queue<E> queueDefault;
25
26     private boolean flooded;
27
28     /**
29      * @param capacity
30      * @param queueZipper
31      */
32     public WrapperQueueImpl(int capacity, Queue<E> queueDefault,
33             WaterMarkListener queueListenerMark) {
34         this.queueListenerMark = queueListenerMark;
35         this.queueDefault = Preconditions.checkNotNull(queueDefault);
36
37         this.highWaterMark = (int) (capacity * 0.8);
38         this.lowWaterMark = (int) (capacity * 0.65);
39     }
40
41     /**
42      * Marking checks size of {@link #queueDefault} and on the basis of this is
43      * set autoRead
44      */
45     private void marking() {
46         if (queueDefault.size() >= highWaterMark && !flooded) {
47             queueListenerMark.onHighWaterMark();
48             flooded = true;
49         } else if (queueDefault.size() <= lowWaterMark && flooded) {
50             queueListenerMark.onLowWaterMark();
51             flooded = false;
52         }
53     }
54
55     /**
56      * @return true if flooded
57      */
58     public boolean isFlooded() {
59         return flooded;
60     }
61
62     /**
63      * poll {@link QueueItem} and call {@link #marking()} for check marks and
64      * set autoRead if it need it
65      * 
66      * @return polled item
67      */
68     public E poll() {
69         E nextQueueItem = queueDefault.poll();
70         marking();
71         return nextQueueItem;
72     }
73
74     public boolean add(E e) {
75         return queueDefault.add(e);
76     }
77
78     public int size() {
79         return queueDefault.size();
80     }
81
82     public boolean isEmpty() {
83         return queueDefault.isEmpty();
84     }
85
86     public boolean contains(Object o) {
87         return queueDefault.contains(o);
88     }
89
90     public boolean offer(E e) {
91         boolean enqueueResult = queueDefault.offer(e);
92         marking();
93         return enqueueResult;
94     }
95
96     public Iterator<E> iterator() {
97         return queueDefault.iterator();
98     }
99
100     public E remove() {
101         return queueDefault.remove();
102     }
103
104     public Object[] toArray() {
105         return queueDefault.toArray();
106     }
107
108     public E element() {
109         return queueDefault.element();
110     }
111
112     public E peek() {
113         return queueDefault.peek();
114     }
115
116     public <T> T[] toArray(T[] a) {
117         return queueDefault.toArray(a);
118     }
119
120     public boolean remove(Object o) {
121         return queueDefault.remove(o);
122     }
123
124     public boolean containsAll(Collection<?> c) {
125         return queueDefault.containsAll(c);
126     }
127
128     public boolean addAll(Collection<? extends E> c) {
129         return queueDefault.addAll(c);
130     }
131
132     public boolean removeAll(Collection<?> c) {
133         return queueDefault.removeAll(c);
134     }
135
136     public boolean retainAll(Collection<?> c) {
137         return queueDefault.retainAll(c);
138     }
139
140     public void clear() {
141         queueDefault.clear();
142     }
143
144     public boolean equals(Object o) {
145         return queueDefault.equals(o);
146     }
147
148     public int hashCode() {
149         return queueDefault.hashCode();
150     }
151
152 }