Eliminate the use of CompositeObjectRegistration
[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.ModelDrivenSwitchRegistration;
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
33 /**
34  * @author mirehak
35  */
36 public class SessionContextOFImpl implements SessionContext {
37
38     private GetFeaturesOutput features;
39     private ConnectionConductor primaryConductor;
40     private NotificationEnqueuer notificationEnqueuer;
41     private ConcurrentHashMap<SwitchConnectionDistinguisher, ConnectionConductor> auxiliaryConductors;
42     private boolean valid;
43     private SwitchSessionKeyOF sessionKey;
44     private IMessageDispatchService mdService;
45     private final AtomicLong xid;
46     private final Map<Long, PortGrouping> physicalPorts;
47     private final Map<Long, Boolean> portBandwidth;
48     private ModelDrivenSwitchRegistration providerRegistration;
49     private int seed;
50     private ControllerRole roleOnDevice = ControllerRole.OFPCRROLEEQUAL;
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 GetFeaturesOutput getFeatures() {
89         return features;
90     }
91
92     /**
93      * @param features
94      *            the features to set
95      */
96     public void setFeatures(GetFeaturesOutput features) {
97         this.features = features;
98     }
99
100     /**
101      * @param primaryConductor
102      *            the primaryConductor to set
103      */
104     public void setPrimaryConductor(ConnectionConductor primaryConductor) {
105         this.primaryConductor = primaryConductor;
106     }
107
108     @Override
109     public ConnectionConductor removeAuxiliaryConductor(
110             SwitchConnectionDistinguisher connectionCookie) {
111         return auxiliaryConductors.remove(connectionCookie);
112     }
113
114     @Override
115     public boolean isValid() {
116         return valid;
117     }
118
119     @Override
120     public void setValid(boolean valid) {
121         this.valid = valid;
122     }
123
124     /**
125      * @param sessionKey the sessionKey to set
126      */
127     public void setSessionKey(SwitchSessionKeyOF sessionKey) {
128         this.sessionKey = sessionKey;
129     }
130
131     /**
132      * @param seed the seed to set
133      */
134     public void setSeed(int seed) {
135         this.seed = seed;
136     }
137
138     @Override
139     public SwitchSessionKeyOF getSessionKey() {
140         return sessionKey;
141     }
142
143     @Override
144     public IMessageDispatchService getMessageDispatchService() {
145         return mdService;
146     }
147
148     @Override
149     public Long getNextXid() {
150         return xid.incrementAndGet();
151     }
152
153     @Override
154     public Map<Long, PortGrouping> getPhysicalPorts() {
155         return this.physicalPorts;
156     }
157
158     @Override
159     public Map<Long, Boolean> getPortsBandwidth() {
160         return this.portBandwidth;
161     }
162
163     @Override
164     public Set<Long> getPorts() {
165         return this.physicalPorts.keySet();
166     }
167
168     @Override
169     public PortGrouping getPhysicalPort(Long portNumber) {
170         return this.physicalPorts.get(portNumber);
171     }
172
173     @Override
174     public Boolean getPortBandwidth(Long portNumber) {
175         return this.portBandwidth.get(portNumber);
176     }
177
178     @Override
179     public boolean isPortEnabled(long portNumber) {
180         return isPortEnabled(physicalPorts.get(portNumber));
181     }
182
183     @Override
184     public boolean isPortEnabled(PortGrouping port) {
185         if (port == null) {
186             return false;
187         }
188         if (port.getConfig().isPortDown()) {
189             return false;
190         }
191         if (port.getState().isLinkDown()) {
192             return false;
193         }
194         if (port.getState().isBlocked()) {
195             return false;
196         }
197         return true;
198     }
199
200     @Override
201     public List<PortGrouping> getEnabledPorts() {
202         List<PortGrouping> result = new ArrayList<PortGrouping>();
203         synchronized (this.physicalPorts) {
204             for (PortGrouping port : physicalPorts.values()) {
205                 if (isPortEnabled(port)) {
206                     result.add(port);
207                 }
208             }
209         }
210         return result;
211     }
212
213     @Override
214     public void setProviderRegistration(ModelDrivenSwitchRegistration providerRegistration) {
215         this.providerRegistration = providerRegistration;
216     }
217
218     @Override
219     public ModelDrivenSwitchRegistration getProviderRegistration() {
220         return providerRegistration;
221     }
222
223     @Override
224     public int getSeed() {
225         return seed;
226     }
227
228     /**
229      * @param notificationEnqueuer the notificationEnqueuer to set
230      */
231     public void setNotificationEnqueuer(
232             NotificationEnqueuer notificationEnqueuer) {
233         this.notificationEnqueuer = notificationEnqueuer;
234     }
235
236     @Override
237     public NotificationEnqueuer getNotificationEnqueuer() {
238         return notificationEnqueuer;
239     }
240
241     /**
242      * @return the roleOnDevice
243      */
244     @Override
245     public ControllerRole getRoleOnDevice() {
246         return roleOnDevice;
247     }
248
249     /**
250      * @param roleOnDevice the roleOnDevice to set
251      */
252     @Override
253     public void setRoleOnDevice(ControllerRole roleOnDevice) {
254         Preconditions.checkNotNull("Proposed controller role can not be empty.", roleOnDevice);
255         this.roleOnDevice = roleOnDevice;
256     }
257 }