BUG-1006 - limiting bulkTransactionCache
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / session / SessionContextOFImpl.java
1 /**
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  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
9 package org.opendaylight.openflowplugin.openflow.md.core.session;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.atomic.AtomicLong;
20
21 import com.google.common.cache.Cache;
22 import com.google.common.cache.CacheBuilder;
23
24 import java.util.concurrent.TimeUnit;
25
26 import org.opendaylight.openflowplugin.openflow.md.ModelDrivenSwitch;
27 import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductor;
28 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
31 import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
32
33 /**
34  * @author mirehak
35  */
36 public class SessionContextOFImpl implements SessionContext {
37
38     private GetFeaturesOutput features;
39     private ConnectionConductor primaryConductor;
40     private ConcurrentHashMap<SwitchConnectionDistinguisher, ConnectionConductor> auxiliaryConductors;
41     private boolean valid;
42     private SwitchSessionKeyOF sessionKey;
43     private IMessageDispatchService mdService;
44     private final AtomicLong xid;
45     private final Map<Long, PortGrouping> physicalPorts;
46     private final Map<Long, Boolean> portBandwidth;
47     private Cache<TransactionKey, Object> bulkTransactionCache = CacheBuilder.newBuilder()
48             .softValues().maximumSize(1000).expireAfterWrite(2000, TimeUnit.MILLISECONDS).concurrencyLevel(1).build();
49     private CompositeObjectRegistration<ModelDrivenSwitch> providerRegistration;
50     private int seed;
51     
52
53     /**
54      * default ctor
55      */
56     public SessionContextOFImpl() {
57         auxiliaryConductors = new ConcurrentHashMap<>();
58         mdService = new MessageDispatchServiceImpl(this);
59         xid = new AtomicLong();
60         this.physicalPorts = new HashMap<Long, PortGrouping>();
61         this.portBandwidth = new HashMap<Long, Boolean>();
62     }
63
64     @Override
65     public ConnectionConductor getPrimaryConductor() {
66         return primaryConductor;
67     }
68
69     @Override
70     public ConnectionConductor getAuxiliaryConductor(
71             SwitchConnectionDistinguisher auxiliaryKey) {
72         return auxiliaryConductors.get(auxiliaryKey);
73     }
74
75     @Override
76     public void addAuxiliaryConductor(
77             SwitchConnectionDistinguisher auxiliaryKey,
78             ConnectionConductor conductor) {
79         auxiliaryConductors.put(auxiliaryKey, conductor);
80     }
81
82     @Override
83     public Set<Entry<SwitchConnectionDistinguisher, ConnectionConductor>> getAuxiliaryConductors() {
84         return Collections.unmodifiableSet(auxiliaryConductors.entrySet());
85     }
86     
87     @Override
88     public Cache<TransactionKey, Object> getbulkTransactionCache() {
89         return bulkTransactionCache;
90     }
91
92     @Override
93     public GetFeaturesOutput getFeatures() {
94         return features;
95     }
96
97     /**
98      * @param features
99      *            the features to set
100      */
101     public void setFeatures(GetFeaturesOutput features) {
102         this.features = features;
103     }
104
105     /**
106      * @param primaryConductor
107      *            the primaryConductor to set
108      */
109     public void setPrimaryConductor(ConnectionConductor primaryConductor) {
110         this.primaryConductor = primaryConductor;
111     }
112
113     @Override
114     public ConnectionConductor removeAuxiliaryConductor(
115             SwitchConnectionDistinguisher connectionCookie) {
116         return auxiliaryConductors.remove(connectionCookie);
117     }
118
119     @Override
120     public boolean isValid() {
121         return valid;
122     }
123
124     @Override
125     public void setValid(boolean valid) {
126         this.valid = valid;
127     }
128
129     /**
130      * @param sessionKey the sessionKey to set
131      */
132     public void setSessionKey(SwitchSessionKeyOF sessionKey) {
133         this.sessionKey = sessionKey;
134     }
135     
136     /**
137      * @param seed the seed to set
138      */
139     public void setSeed(int seed) {
140         this.seed = seed;
141     }
142
143     @Override
144     public SwitchSessionKeyOF getSessionKey() {
145         return sessionKey;
146     }
147
148     @Override
149     public IMessageDispatchService getMessageDispatchService() {
150         return mdService;
151     }
152
153     @Override
154     public Long getNextXid() {
155         return xid.incrementAndGet();
156     }
157
158     @Override
159     public Map<Long, PortGrouping> getPhysicalPorts() {
160         return this.physicalPorts;
161     }
162     
163     @Override
164     public Map<Long, Boolean> getPortsBandwidth() {
165         return this.portBandwidth;
166     }
167
168     @Override
169     public Set<Long> getPorts() {
170         return this.physicalPorts.keySet();
171     }
172
173     @Override
174     public PortGrouping getPhysicalPort(Long portNumber) {
175         return this.physicalPorts.get(portNumber);
176     }
177
178     @Override
179     public Boolean getPortBandwidth(Long portNumber) {
180         return this.portBandwidth.get(portNumber);
181     }
182
183     @Override
184     public boolean isPortEnabled(long portNumber) {
185         return isPortEnabled(physicalPorts.get(portNumber));
186     }
187
188     @Override
189     public boolean isPortEnabled(PortGrouping port) {
190         if (port == null) {
191             return false;
192         }
193         if (port.getConfig().isPortDown()) {
194             return false;
195         }
196         if (port.getState().isLinkDown()) {
197             return false;
198         }
199         if (port.getState().isBlocked()) {
200             return false;
201         }
202         return true;
203     }
204
205     @Override
206     public List<PortGrouping> getEnabledPorts() {
207         List<PortGrouping> result = new ArrayList<PortGrouping>();
208         synchronized (this.physicalPorts) {
209             for (PortGrouping port : physicalPorts.values()) {
210                 if (isPortEnabled(port)) {
211                     result.add(port);
212                 }
213             }
214         }
215         return result;
216     }
217     
218     @Override
219     public void setProviderRegistration(
220             CompositeObjectRegistration<ModelDrivenSwitch> providerRegistration) {
221                 this.providerRegistration = providerRegistration;
222     }
223     
224     @Override
225     public CompositeObjectRegistration<ModelDrivenSwitch> getProviderRegistration() {
226         return providerRegistration;
227     }
228     
229     @Override
230     public int getSeed() {
231         return seed;
232     }
233 }