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.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;
37 public class SessionContextOFImpl implements SessionContext {
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;
51 private ControllerRole roleOnDevice = ControllerRole.OFPCRROLEEQUAL;
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>();
66 public ConnectionConductor getPrimaryConductor() {
67 return primaryConductor;
71 public ConnectionConductor getAuxiliaryConductor(
72 SwitchConnectionDistinguisher auxiliaryKey) {
73 return auxiliaryConductors.get(auxiliaryKey);
77 public void addAuxiliaryConductor(
78 SwitchConnectionDistinguisher auxiliaryKey,
79 ConnectionConductor conductor) {
80 auxiliaryConductors.put(auxiliaryKey, conductor);
84 public Set<Entry<SwitchConnectionDistinguisher, ConnectionConductor>> getAuxiliaryConductors() {
85 return Collections.unmodifiableSet(auxiliaryConductors.entrySet());
89 public GetFeaturesOutput getFeatures() {
97 public void setFeatures(GetFeaturesOutput features) {
98 this.features = features;
102 * @param primaryConductor
103 * the primaryConductor to set
105 public void setPrimaryConductor(ConnectionConductor primaryConductor) {
106 this.primaryConductor = primaryConductor;
110 public ConnectionConductor removeAuxiliaryConductor(
111 SwitchConnectionDistinguisher connectionCookie) {
112 return auxiliaryConductors.remove(connectionCookie);
116 public boolean isValid() {
121 public void setValid(boolean valid) {
126 * @param sessionKey the sessionKey to set
128 public void setSessionKey(SwitchSessionKeyOF sessionKey) {
129 this.sessionKey = sessionKey;
133 * @param seed the seed to set
135 public void setSeed(int seed) {
140 public SwitchSessionKeyOF getSessionKey() {
145 public IMessageDispatchService getMessageDispatchService() {
150 public Long getNextXid() {
151 return xid.incrementAndGet();
155 public Map<Long, PortGrouping> getPhysicalPorts() {
156 return this.physicalPorts;
160 public Map<Long, Boolean> getPortsBandwidth() {
161 return this.portBandwidth;
165 public Set<Long> getPorts() {
166 return this.physicalPorts.keySet();
170 public PortGrouping getPhysicalPort(Long portNumber) {
171 return this.physicalPorts.get(portNumber);
175 public Boolean getPortBandwidth(Long portNumber) {
176 return this.portBandwidth.get(portNumber);
180 public boolean isPortEnabled(long portNumber) {
181 return isPortEnabled(physicalPorts.get(portNumber));
185 public boolean isPortEnabled(PortGrouping port) {
189 if (port.getConfig().isPortDown()) {
192 if (port.getState().isLinkDown()) {
195 if (port.getState().isBlocked()) {
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)) {
215 public void setProviderRegistration(
216 CompositeObjectRegistration<ModelDrivenSwitch> providerRegistration) {
217 this.providerRegistration = providerRegistration;
221 public CompositeObjectRegistration<ModelDrivenSwitch> getProviderRegistration() {
222 return providerRegistration;
226 public int getSeed() {
231 * @param notificationEnqueuer the notificationEnqueuer to set
233 public void setNotificationEnqueuer(
234 NotificationEnqueuer notificationEnqueuer) {
235 this.notificationEnqueuer = notificationEnqueuer;
239 public NotificationEnqueuer getNotificationEnqueuer() {
240 return notificationEnqueuer;
244 * @return the roleOnDevice
247 public ControllerRole getRoleOnDevice() {
252 * @param roleOnDevice the roleOnDevice to set
255 public void setRoleOnDevice(ControllerRole roleOnDevice) {
256 Preconditions.checkNotNull("Proposed controller role can not be empty.", roleOnDevice);
257 this.roleOnDevice = roleOnDevice;