fix port update - honor message version
[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 java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Map.Entry;
17 import java.util.Set;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.atomic.AtomicLong;
20
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
26 /**
27  * @author mirehak
28  */
29 public class SessionContextOFImpl implements SessionContext {
30
31     private GetFeaturesOutput features;
32     private ConnectionConductor primaryConductor;
33     private ConcurrentHashMap<SwitchConnectionDistinguisher, ConnectionConductor> auxiliaryConductors;
34     private boolean valid;
35     private SwitchConnectionDistinguisher sessionKey;
36     private IMessageDispatchService mdService;
37     private final AtomicLong xid;
38     private final Map<Long, Port> physicalPorts;
39     private final Map<Long, Boolean> portBandwidth;
40
41     /**
42      * default ctor
43      */
44     public SessionContextOFImpl() {
45         auxiliaryConductors = new ConcurrentHashMap<>();
46         mdService = new MessageDispatchServiceImpl(this);
47         xid = new AtomicLong();
48         this.physicalPorts = new HashMap<Long, Port>();
49         this.portBandwidth = new HashMap<Long, Boolean>();
50     }
51
52     @Override
53     public ConnectionConductor getPrimaryConductor() {
54         return primaryConductor;
55     }
56
57     @Override
58     public ConnectionConductor getAuxiliaryConductor(
59             SwitchConnectionDistinguisher auxiliaryKey) {
60         return auxiliaryConductors.get(auxiliaryKey);
61     }
62
63     @Override
64     public void addAuxiliaryConductor(
65             SwitchConnectionDistinguisher auxiliaryKey,
66             ConnectionConductor conductor) {
67         auxiliaryConductors.put(auxiliaryKey, conductor);
68     }
69
70     @Override
71     public Set<Entry<SwitchConnectionDistinguisher, ConnectionConductor>> getAuxiliaryConductors() {
72         return Collections.unmodifiableSet(auxiliaryConductors.entrySet());
73     }
74
75     @Override
76     public GetFeaturesOutput getFeatures() {
77         return features;
78     }
79
80     /**
81      * @param features
82      *            the features to set
83      */
84     public void setFeatures(GetFeaturesOutput features) {
85         this.features = features;
86     }
87
88     /**
89      * @param primaryConductor
90      *            the primaryConductor to set
91      */
92     public void setPrimaryConductor(ConnectionConductor primaryConductor) {
93         this.primaryConductor = primaryConductor;
94     }
95
96     @Override
97     public ConnectionConductor removeAuxiliaryConductor(
98             SwitchConnectionDistinguisher connectionCookie) {
99         return auxiliaryConductors.remove(connectionCookie);
100     }
101
102     @Override
103     public boolean isValid() {
104         return valid;
105     }
106
107     @Override
108     public void setValid(boolean valid) {
109         this.valid = valid;
110     }
111
112     /**
113      * @param sessionKey the sessionKey to set
114      */
115     public void setSessionKey(SwitchConnectionDistinguisher sessionKey) {
116         this.sessionKey = sessionKey;
117     }
118
119     @Override
120     public SwitchConnectionDistinguisher getSessionKey() {
121         return sessionKey;
122     }
123
124     @Override
125     public IMessageDispatchService getMessageDispatchService() {
126         return mdService;
127     }
128
129     @Override
130     public Long getNextXid() {
131         return xid.incrementAndGet();
132     }
133
134     @Override
135     public Map<Long, Port> getPhysicalPorts() {
136         return this.physicalPorts;
137     }
138     
139     @Override
140     public Map<Long, Boolean> getPortsBandwidth() {
141         return this.portBandwidth;
142     }
143
144     @Override
145     public Set<Long> getPorts() {
146         return this.physicalPorts.keySet();
147     }
148
149     @Override
150     public Port getPhysicalPort(Long portNumber) {
151         return this.physicalPorts.get(portNumber);
152     }
153
154     @Override
155     public Boolean getPortBandwidth(Long portNumber) {
156         return this.portBandwidth.get(portNumber);
157     }
158
159     @Override
160     public boolean isPortEnabled(long portNumber) {
161         return isPortEnabled(physicalPorts.get(portNumber));
162     }
163
164     @Override
165     public boolean isPortEnabled(Port port) {
166         if (port == null) {
167             return false;
168         }
169         if (port.getConfig().isPortDown()) {
170             return false;
171         }
172         if (port.getState().isLinkDown()) {
173             return false;
174         }
175         if (port.getState().isBlocked()) {
176             return false;
177         }
178         return true;
179     }
180
181     @Override
182     public List<Port> getEnabledPorts() {
183         List<Port> result = new ArrayList<Port>();
184         synchronized (this.physicalPorts) {
185             for (Port port : physicalPorts.values()) {
186                 if (isPortEnabled(port)) {
187                     result.add(port);
188                 }
189             }
190         }
191         return result;
192     }
193 }