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