Merge "changed pom structure, removed dependency on controller parent pom"
[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 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatus;
26
27 /**
28  * @author mirehak
29  */
30 public class SessionContextOFImpl implements SessionContext {
31
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;
41
42     /**
43      * default ctor
44      */
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>();
51     }
52
53     @Override
54     public ConnectionConductor getPrimaryConductor() {
55         return primaryConductor;
56     }
57
58     @Override
59     public ConnectionConductor getAuxiliaryConductor(
60             SwitchConnectionDistinguisher auxiliaryKey) {
61         return auxiliaryConductors.get(auxiliaryKey);
62     }
63
64     @Override
65     public void addAuxiliaryConductor(
66             SwitchConnectionDistinguisher auxiliaryKey,
67             ConnectionConductor conductor) {
68         auxiliaryConductors.put(auxiliaryKey, conductor);
69     }
70
71     @Override
72     public Set<Entry<SwitchConnectionDistinguisher, ConnectionConductor>> getAuxiliaryConductors() {
73         return Collections.unmodifiableSet(auxiliaryConductors.entrySet());
74     }
75
76     @Override
77     public GetFeaturesOutput getFeatures() {
78         return features;
79     }
80
81     /**
82      * @param features
83      *            the features to set
84      */
85     public void setFeatures(GetFeaturesOutput features) {
86         this.features = features;
87     }
88
89     /**
90      * @param primaryConductor
91      *            the primaryConductor to set
92      */
93     public void setPrimaryConductor(ConnectionConductor primaryConductor) {
94         this.primaryConductor = primaryConductor;
95     }
96
97     @Override
98     public ConnectionConductor removeAuxiliaryConductor(
99             SwitchConnectionDistinguisher connectionCookie) {
100         return auxiliaryConductors.remove(connectionCookie);
101     }
102
103     @Override
104     public boolean isValid() {
105         return valid;
106     }
107
108     @Override
109     public void setValid(boolean valid) {
110         this.valid = valid;
111     }
112
113     /**
114      * @param sessionKey the sessionKey to set
115      */
116     public void setSessionKey(SwitchConnectionDistinguisher sessionKey) {
117         this.sessionKey = sessionKey;
118     }
119
120     @Override
121     public SwitchConnectionDistinguisher getSessionKey() {
122         return sessionKey;
123     }
124
125     @Override
126     public IMessageDispatchService getMessageDispatchService() {
127         return mdService;
128     }
129
130     @Override
131     public Long getNextXid() {
132         return xid.incrementAndGet();
133     }
134
135     @Override
136     public Map<Long, Port> getPhysicalPorts() {
137         return this.physicalPorts;
138     }
139
140     @Override
141     public Set<Long> getPorts() {
142         return this.physicalPorts.keySet();
143     }
144
145     @Override
146     public Port getPhysicalPort(Long portNumber) {
147         return this.physicalPorts.get(portNumber);
148     }
149
150     @Override
151     public Boolean getPortBandwidth(Long portNumber) {
152         return this.portBandwidth.get(portNumber);
153     }
154
155     @Override
156     public boolean isPortEnabled(long portNumber) {
157         return isPortEnabled(physicalPorts.get(portNumber));
158     }
159
160     @Override
161     public boolean isPortEnabled(Port port) {
162         if (port == null) {
163             return false;
164         }
165         if (port.getConfig().isPortDown()) {
166             return false;
167         }
168         if (port.getState().isLinkDown()) {
169             return false;
170         }
171         if (port.getState().isBlocked()) {
172             return false;
173         }
174         return true;
175     }
176
177     @Override
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)) {
183                     result.add(port);
184                 }
185             }
186         }
187         return result;
188     }
189
190     @Override
191     public void processPortStatusMsg(PortStatus msg) {
192         Port port = 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);
199         }
200
201     }
202
203     private void updatePhysicalPort(Port port) {
204         Long portNumber = port.getPortNo();
205         physicalPorts.put(portNumber, port);
206         portBandwidth
207                 .put(portNumber,
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()) ) );
217     }
218
219     private void deletePhysicalPort(Port port) {
220         Long portNumber = port.getPortNo();
221         physicalPorts.remove(portNumber);
222         portBandwidth.remove(portNumber);
223     }
224 }