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