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