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 com.google.common.cache.Cache;
22 import com.google.common.cache.CacheBuilder;
24 import java.util.concurrent.TimeUnit;
26 import org.opendaylight.openflowplugin.openflow.md.ModelDrivenSwitch;
27 import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductor;
28 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
31 import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
36 public class SessionContextOFImpl implements SessionContext {
38 private GetFeaturesOutput features;
39 private ConnectionConductor primaryConductor;
40 private ConcurrentHashMap<SwitchConnectionDistinguisher, ConnectionConductor> auxiliaryConductors;
41 private boolean valid;
42 private SwitchSessionKeyOF sessionKey;
43 private IMessageDispatchService mdService;
44 private final AtomicLong xid;
45 private final Map<Long, PortGrouping> physicalPorts;
46 private final Map<Long, Boolean> portBandwidth;
47 private Cache<TransactionKey, Object> bulkTransactionCache = CacheBuilder.newBuilder()
48 .softValues().maximumSize(1000).expireAfterWrite(2000, TimeUnit.MILLISECONDS).concurrencyLevel(1).build();
49 private CompositeObjectRegistration<ModelDrivenSwitch> providerRegistration;
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 Cache<TransactionKey, Object> getbulkTransactionCache() {
89 return bulkTransactionCache;
93 public GetFeaturesOutput getFeatures() {
101 public void setFeatures(GetFeaturesOutput features) {
102 this.features = features;
106 * @param primaryConductor
107 * the primaryConductor to set
109 public void setPrimaryConductor(ConnectionConductor primaryConductor) {
110 this.primaryConductor = primaryConductor;
114 public ConnectionConductor removeAuxiliaryConductor(
115 SwitchConnectionDistinguisher connectionCookie) {
116 return auxiliaryConductors.remove(connectionCookie);
120 public boolean isValid() {
125 public void setValid(boolean valid) {
130 * @param sessionKey the sessionKey to set
132 public void setSessionKey(SwitchSessionKeyOF sessionKey) {
133 this.sessionKey = sessionKey;
137 * @param seed the seed to set
139 public void setSeed(int seed) {
144 public SwitchSessionKeyOF getSessionKey() {
149 public IMessageDispatchService getMessageDispatchService() {
154 public Long getNextXid() {
155 return xid.incrementAndGet();
159 public Map<Long, PortGrouping> getPhysicalPorts() {
160 return this.physicalPorts;
164 public Map<Long, Boolean> getPortsBandwidth() {
165 return this.portBandwidth;
169 public Set<Long> getPorts() {
170 return this.physicalPorts.keySet();
174 public PortGrouping getPhysicalPort(Long portNumber) {
175 return this.physicalPorts.get(portNumber);
179 public Boolean getPortBandwidth(Long portNumber) {
180 return this.portBandwidth.get(portNumber);
184 public boolean isPortEnabled(long portNumber) {
185 return isPortEnabled(physicalPorts.get(portNumber));
189 public boolean isPortEnabled(PortGrouping port) {
193 if (port.getConfig().isPortDown()) {
196 if (port.getState().isLinkDown()) {
199 if (port.getState().isBlocked()) {
206 public List<PortGrouping> getEnabledPorts() {
207 List<PortGrouping> result = new ArrayList<PortGrouping>();
208 synchronized (this.physicalPorts) {
209 for (PortGrouping port : physicalPorts.values()) {
210 if (isPortEnabled(port)) {
219 public void setProviderRegistration(
220 CompositeObjectRegistration<ModelDrivenSwitch> providerRegistration) {
221 this.providerRegistration = providerRegistration;
225 public CompositeObjectRegistration<ModelDrivenSwitch> getProviderRegistration() {
226 return providerRegistration;
230 public int getSeed() {