fix for BUG-709 - connectionCookie generate, use
[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().expireAfterWrite(10000, TimeUnit.MILLISECONDS).concurrencyLevel(1).build();
48     private CompositeObjectRegistration<ModelDrivenSwitch> providerRegistration;
49     private int seed;
50     
51
52     /**
53      * default ctor
54      */
55     public SessionContextOFImpl() {
56         auxiliaryConductors = new ConcurrentHashMap<>();
57         mdService = new MessageDispatchServiceImpl(this);
58         xid = new AtomicLong();
59         this.physicalPorts = new HashMap<Long, PortGrouping>();
60         this.portBandwidth = new HashMap<Long, Boolean>();
61     }
62
63     @Override
64     public ConnectionConductor getPrimaryConductor() {
65         return primaryConductor;
66     }
67
68     @Override
69     public ConnectionConductor getAuxiliaryConductor(
70             SwitchConnectionDistinguisher auxiliaryKey) {
71         return auxiliaryConductors.get(auxiliaryKey);
72     }
73
74     @Override
75     public void addAuxiliaryConductor(
76             SwitchConnectionDistinguisher auxiliaryKey,
77             ConnectionConductor conductor) {
78         auxiliaryConductors.put(auxiliaryKey, conductor);
79     }
80
81     @Override
82     public Set<Entry<SwitchConnectionDistinguisher, ConnectionConductor>> getAuxiliaryConductors() {
83         return Collections.unmodifiableSet(auxiliaryConductors.entrySet());
84     }
85     
86     @Override
87     public Cache<TransactionKey, Object> getbulkTransactionCache() {
88         return bulkTransactionCache;
89     }
90
91     @Override
92     public GetFeaturesOutput getFeatures() {
93         return features;
94     }
95
96     /**
97      * @param features
98      *            the features to set
99      */
100     public void setFeatures(GetFeaturesOutput features) {
101         this.features = features;
102     }
103
104     /**
105      * @param primaryConductor
106      *            the primaryConductor to set
107      */
108     public void setPrimaryConductor(ConnectionConductor primaryConductor) {
109         this.primaryConductor = primaryConductor;
110     }
111
112     @Override
113     public ConnectionConductor removeAuxiliaryConductor(
114             SwitchConnectionDistinguisher connectionCookie) {
115         return auxiliaryConductors.remove(connectionCookie);
116     }
117
118     @Override
119     public boolean isValid() {
120         return valid;
121     }
122
123     @Override
124     public void setValid(boolean valid) {
125         this.valid = valid;
126     }
127
128     /**
129      * @param sessionKey the sessionKey to set
130      */
131     public void setSessionKey(SwitchSessionKeyOF sessionKey) {
132         this.sessionKey = sessionKey;
133     }
134     
135     /**
136      * @param seed the seed to set
137      */
138     public void setSeed(int seed) {
139         this.seed = seed;
140     }
141
142     @Override
143     public SwitchSessionKeyOF getSessionKey() {
144         return sessionKey;
145     }
146
147     @Override
148     public IMessageDispatchService getMessageDispatchService() {
149         return mdService;
150     }
151
152     @Override
153     public Long getNextXid() {
154         return xid.incrementAndGet();
155     }
156
157     @Override
158     public Map<Long, PortGrouping> getPhysicalPorts() {
159         return this.physicalPorts;
160     }
161     
162     @Override
163     public Map<Long, Boolean> getPortsBandwidth() {
164         return this.portBandwidth;
165     }
166
167     @Override
168     public Set<Long> getPorts() {
169         return this.physicalPorts.keySet();
170     }
171
172     @Override
173     public PortGrouping getPhysicalPort(Long portNumber) {
174         return this.physicalPorts.get(portNumber);
175     }
176
177     @Override
178     public Boolean getPortBandwidth(Long portNumber) {
179         return this.portBandwidth.get(portNumber);
180     }
181
182     @Override
183     public boolean isPortEnabled(long portNumber) {
184         return isPortEnabled(physicalPorts.get(portNumber));
185     }
186
187     @Override
188     public boolean isPortEnabled(PortGrouping port) {
189         if (port == null) {
190             return false;
191         }
192         if (port.getConfig().isPortDown()) {
193             return false;
194         }
195         if (port.getState().isLinkDown()) {
196             return false;
197         }
198         if (port.getState().isBlocked()) {
199             return false;
200         }
201         return true;
202     }
203
204     @Override
205     public List<PortGrouping> getEnabledPorts() {
206         List<PortGrouping> result = new ArrayList<PortGrouping>();
207         synchronized (this.physicalPorts) {
208             for (PortGrouping port : physicalPorts.values()) {
209                 if (isPortEnabled(port)) {
210                     result.add(port);
211                 }
212             }
213         }
214         return result;
215     }
216     
217     @Override
218     public void setProviderRegistration(
219             CompositeObjectRegistration<ModelDrivenSwitch> providerRegistration) {
220                 this.providerRegistration = providerRegistration;
221     }
222     
223     @Override
224     public CompositeObjectRegistration<ModelDrivenSwitch> getProviderRegistration() {
225         return providerRegistration;
226     }
227     
228     @Override
229     public int getSeed() {
230         return seed;
231     }
232 }