2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.openflow.md.core.session;
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;
17 import java.util.Map.Entry;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.atomic.AtomicLong;
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;
36 public class SessionContextOFImpl implements SessionContext {
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;
50 private ControllerRole roleOnDevice = ControllerRole.OFPCRROLEEQUAL;
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>();
65 public ConnectionConductor getPrimaryConductor() {
66 return primaryConductor;
70 public ConnectionConductor getAuxiliaryConductor(
71 SwitchConnectionDistinguisher auxiliaryKey) {
72 return auxiliaryConductors.get(auxiliaryKey);
76 public void addAuxiliaryConductor(
77 SwitchConnectionDistinguisher auxiliaryKey,
78 ConnectionConductor conductor) {
79 auxiliaryConductors.put(auxiliaryKey, conductor);
83 public Set<Entry<SwitchConnectionDistinguisher, ConnectionConductor>> getAuxiliaryConductors() {
84 return Collections.unmodifiableSet(auxiliaryConductors.entrySet());
88 public GetFeaturesOutput getFeatures() {
96 public void setFeatures(GetFeaturesOutput features) {
97 this.features = features;
101 * @param primaryConductor
102 * the primaryConductor to set
104 public void setPrimaryConductor(ConnectionConductor primaryConductor) {
105 this.primaryConductor = primaryConductor;
109 public ConnectionConductor removeAuxiliaryConductor(
110 SwitchConnectionDistinguisher connectionCookie) {
111 return auxiliaryConductors.remove(connectionCookie);
115 public boolean isValid() {
120 public void setValid(boolean valid) {
125 * @param sessionKey the sessionKey to set
127 public void setSessionKey(SwitchSessionKeyOF sessionKey) {
128 this.sessionKey = sessionKey;
132 * @param seed the seed to set
134 public void setSeed(int seed) {
139 public SwitchSessionKeyOF getSessionKey() {
144 public IMessageDispatchService getMessageDispatchService() {
149 public Long getNextXid() {
150 return xid.incrementAndGet();
154 public Map<Long, PortGrouping> getPhysicalPorts() {
155 return this.physicalPorts;
159 public Map<Long, Boolean> getPortsBandwidth() {
160 return this.portBandwidth;
164 public Set<Long> getPorts() {
165 return this.physicalPorts.keySet();
169 public PortGrouping getPhysicalPort(Long portNumber) {
170 return this.physicalPorts.get(portNumber);
174 public Boolean getPortBandwidth(Long portNumber) {
175 return this.portBandwidth.get(portNumber);
179 public boolean isPortEnabled(long portNumber) {
180 return isPortEnabled(physicalPorts.get(portNumber));
184 public boolean isPortEnabled(PortGrouping port) {
188 if (port.getConfig().isPortDown()) {
191 if (port.getState().isLinkDown()) {
194 if (port.getState().isBlocked()) {
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)) {
214 public void setProviderRegistration(ModelDrivenSwitchRegistration providerRegistration) {
215 this.providerRegistration = providerRegistration;
219 public ModelDrivenSwitchRegistration getProviderRegistration() {
220 return providerRegistration;
224 public int getSeed() {
229 * @param notificationEnqueuer the notificationEnqueuer to set
231 public void setNotificationEnqueuer(
232 NotificationEnqueuer notificationEnqueuer) {
233 this.notificationEnqueuer = notificationEnqueuer;
237 public NotificationEnqueuer getNotificationEnqueuer() {
238 return notificationEnqueuer;
242 * @return the roleOnDevice
245 public ControllerRole getRoleOnDevice() {
250 * @param roleOnDevice the roleOnDevice to set
253 public void setRoleOnDevice(ControllerRole roleOnDevice) {
254 Preconditions.checkNotNull("Proposed controller role can not be empty.", roleOnDevice);
255 this.roleOnDevice = roleOnDevice;