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 java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.List;
16 import java.util.Map.Entry;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.atomic.AtomicLong;
21 import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductor;
22 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Port;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatus;
30 public class SessionContextOFImpl implements SessionContext {
32 private GetFeaturesOutput features;
33 private ConnectionConductor primaryConductor;
34 private ConcurrentHashMap<SwitchConnectionDistinguisher, ConnectionConductor> auxiliaryConductors;
35 private boolean valid;
36 private SwitchConnectionDistinguisher sessionKey;
37 private IMessageDispatchService mdService;
38 private final AtomicLong xid;
39 private final Map<Long, Port> physicalPorts;
40 private final Map<Long, Boolean> portBandwidth;
45 public SessionContextOFImpl() {
46 auxiliaryConductors = new ConcurrentHashMap<>();
47 mdService = new MessageDispatchServiceImpl(this);
48 xid = new AtomicLong();
49 this.physicalPorts = new HashMap<Long, Port>();
50 this.portBandwidth = new HashMap<Long, Boolean>();
54 public ConnectionConductor getPrimaryConductor() {
55 return primaryConductor;
59 public ConnectionConductor getAuxiliaryConductor(
60 SwitchConnectionDistinguisher auxiliaryKey) {
61 return auxiliaryConductors.get(auxiliaryKey);
65 public void addAuxiliaryConductor(
66 SwitchConnectionDistinguisher auxiliaryKey,
67 ConnectionConductor conductor) {
68 auxiliaryConductors.put(auxiliaryKey, conductor);
72 public Set<Entry<SwitchConnectionDistinguisher, ConnectionConductor>> getAuxiliaryConductors() {
73 return Collections.unmodifiableSet(auxiliaryConductors.entrySet());
77 public GetFeaturesOutput getFeatures() {
85 public void setFeatures(GetFeaturesOutput features) {
86 this.features = features;
90 * @param primaryConductor
91 * the primaryConductor to set
93 public void setPrimaryConductor(ConnectionConductor primaryConductor) {
94 this.primaryConductor = primaryConductor;
98 public ConnectionConductor removeAuxiliaryConductor(
99 SwitchConnectionDistinguisher connectionCookie) {
100 return auxiliaryConductors.remove(connectionCookie);
104 public boolean isValid() {
109 public void setValid(boolean valid) {
114 * @param sessionKey the sessionKey to set
116 public void setSessionKey(SwitchConnectionDistinguisher sessionKey) {
117 this.sessionKey = sessionKey;
121 public SwitchConnectionDistinguisher getSessionKey() {
126 public IMessageDispatchService getMessageDispatchService() {
131 public Long getNextXid() {
132 return xid.incrementAndGet();
136 public Map<Long, Port> getPhysicalPorts() {
137 return this.physicalPorts;
141 public Set<Long> getPorts() {
142 return this.physicalPorts.keySet();
146 public Port getPhysicalPort(Long portNumber) {
147 return this.physicalPorts.get(portNumber);
151 public Boolean getPortBandwidth(Long portNumber) {
152 return this.portBandwidth.get(portNumber);
156 public boolean isPortEnabled(long portNumber) {
157 return isPortEnabled(physicalPorts.get(portNumber));
161 public boolean isPortEnabled(Port port) {
165 if (port.getConfig().isPortDown()) {
168 if (port.getState().isLinkDown()) {
171 if (port.getState().isBlocked()) {
178 public List<Port> getEnabledPorts() {
179 List<Port> result = new ArrayList<Port>();
180 synchronized (this.physicalPorts) {
181 for (Port port : physicalPorts.values()) {
182 if (isPortEnabled(port)) {
191 public void processPortStatusMsg(PortStatus msg) {
193 if (msg.getReason().getIntValue() == 2) {
194 updatePhysicalPort(port);
195 } else if (msg.getReason().getIntValue() == 0) {
196 updatePhysicalPort(port);
197 } else if (msg.getReason().getIntValue() == 1) {
198 deletePhysicalPort(port);
203 private void updatePhysicalPort(Port port) {
204 Long portNumber = port.getPortNo();
205 physicalPorts.put(portNumber, port);
208 ( (port.getCurrentFeatures().is_100gbFd())
209 |(port.getCurrentFeatures().is_100mbFd()) | (port.getCurrentFeatures().is_100mbHd())
210 | (port.getCurrentFeatures().is_10gbFd()) | (port.getCurrentFeatures().is_10mbFd())
211 | (port.getCurrentFeatures().is_10mbHd()) | (port.getCurrentFeatures().is_1gbFd())
212 | (port.getCurrentFeatures().is_1gbHd()) | (port.getCurrentFeatures().is_1tbFd())
213 | (port.getCurrentFeatures().is_40gbFd()) | (port.getCurrentFeatures().isAutoneg())
214 | (port.getCurrentFeatures().isCopper()) | (port.getCurrentFeatures().isFiber())
215 | (port.getCurrentFeatures().isOther()) | (port.getCurrentFeatures().isPause())
216 | (port.getCurrentFeatures().isPauseAsym()) ) );
219 private void deletePhysicalPort(Port port) {
220 Long portNumber = port.getPortNo();
221 physicalPorts.remove(portNumber);
222 portBandwidth.remove(portNumber);