BUG-2091: notification NodeRemoved and processing queue
[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.ModelDrivenSwitch;
22 import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductor;
23 import org.opendaylight.openflowplugin.openflow.md.core.NotificationEnqueuer;
24 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
27 import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
28
29 /**
30  * @author mirehak
31  */
32 public class SessionContextOFImpl implements SessionContext {
33
34     private GetFeaturesOutput features;
35     private ConnectionConductor primaryConductor;
36     private NotificationEnqueuer notificationEnqueuer;
37     private ConcurrentHashMap<SwitchConnectionDistinguisher, ConnectionConductor> auxiliaryConductors;
38     private boolean valid;
39     private SwitchSessionKeyOF sessionKey;
40     private IMessageDispatchService mdService;
41     private final AtomicLong xid;
42     private final Map<Long, PortGrouping> physicalPorts;
43     private final Map<Long, Boolean> portBandwidth;
44     private CompositeObjectRegistration<ModelDrivenSwitch> providerRegistration;
45     private int seed;
46     
47
48     /**
49      * default ctor
50      */
51     public SessionContextOFImpl() {
52         auxiliaryConductors = new ConcurrentHashMap<>();
53         mdService = new MessageDispatchServiceImpl(this);
54         xid = new AtomicLong();
55         this.physicalPorts = new HashMap<Long, PortGrouping>();
56         this.portBandwidth = new HashMap<Long, Boolean>();
57     }
58
59     @Override
60     public ConnectionConductor getPrimaryConductor() {
61         return primaryConductor;
62     }
63
64     @Override
65     public ConnectionConductor getAuxiliaryConductor(
66             SwitchConnectionDistinguisher auxiliaryKey) {
67         return auxiliaryConductors.get(auxiliaryKey);
68     }
69
70     @Override
71     public void addAuxiliaryConductor(
72             SwitchConnectionDistinguisher auxiliaryKey,
73             ConnectionConductor conductor) {
74         auxiliaryConductors.put(auxiliaryKey, conductor);
75     }
76
77     @Override
78     public Set<Entry<SwitchConnectionDistinguisher, ConnectionConductor>> getAuxiliaryConductors() {
79         return Collections.unmodifiableSet(auxiliaryConductors.entrySet());
80     }
81     
82     @Override
83     public GetFeaturesOutput getFeatures() {
84         return features;
85     }
86
87     /**
88      * @param features
89      *            the features to set
90      */
91     public void setFeatures(GetFeaturesOutput features) {
92         this.features = features;
93     }
94
95     /**
96      * @param primaryConductor
97      *            the primaryConductor to set
98      */
99     public void setPrimaryConductor(ConnectionConductor primaryConductor) {
100         this.primaryConductor = primaryConductor;
101     }
102
103     @Override
104     public ConnectionConductor removeAuxiliaryConductor(
105             SwitchConnectionDistinguisher connectionCookie) {
106         return auxiliaryConductors.remove(connectionCookie);
107     }
108
109     @Override
110     public boolean isValid() {
111         return valid;
112     }
113
114     @Override
115     public void setValid(boolean valid) {
116         this.valid = valid;
117     }
118
119     /**
120      * @param sessionKey the sessionKey to set
121      */
122     public void setSessionKey(SwitchSessionKeyOF sessionKey) {
123         this.sessionKey = sessionKey;
124     }
125     
126     /**
127      * @param seed the seed to set
128      */
129     public void setSeed(int seed) {
130         this.seed = seed;
131     }
132
133     @Override
134     public SwitchSessionKeyOF getSessionKey() {
135         return sessionKey;
136     }
137
138     @Override
139     public IMessageDispatchService getMessageDispatchService() {
140         return mdService;
141     }
142
143     @Override
144     public Long getNextXid() {
145         return xid.incrementAndGet();
146     }
147
148     @Override
149     public Map<Long, PortGrouping> getPhysicalPorts() {
150         return this.physicalPorts;
151     }
152     
153     @Override
154     public Map<Long, Boolean> getPortsBandwidth() {
155         return this.portBandwidth;
156     }
157
158     @Override
159     public Set<Long> getPorts() {
160         return this.physicalPorts.keySet();
161     }
162
163     @Override
164     public PortGrouping getPhysicalPort(Long portNumber) {
165         return this.physicalPorts.get(portNumber);
166     }
167
168     @Override
169     public Boolean getPortBandwidth(Long portNumber) {
170         return this.portBandwidth.get(portNumber);
171     }
172
173     @Override
174     public boolean isPortEnabled(long portNumber) {
175         return isPortEnabled(physicalPorts.get(portNumber));
176     }
177
178     @Override
179     public boolean isPortEnabled(PortGrouping port) {
180         if (port == null) {
181             return false;
182         }
183         if (port.getConfig().isPortDown()) {
184             return false;
185         }
186         if (port.getState().isLinkDown()) {
187             return false;
188         }
189         if (port.getState().isBlocked()) {
190             return false;
191         }
192         return true;
193     }
194
195     @Override
196     public List<PortGrouping> getEnabledPorts() {
197         List<PortGrouping> result = new ArrayList<PortGrouping>();
198         synchronized (this.physicalPorts) {
199             for (PortGrouping port : physicalPorts.values()) {
200                 if (isPortEnabled(port)) {
201                     result.add(port);
202                 }
203             }
204         }
205         return result;
206     }
207     
208     @Override
209     public void setProviderRegistration(
210             CompositeObjectRegistration<ModelDrivenSwitch> providerRegistration) {
211                 this.providerRegistration = providerRegistration;
212     }
213     
214     @Override
215     public CompositeObjectRegistration<ModelDrivenSwitch> getProviderRegistration() {
216         return providerRegistration;
217     }
218     
219     @Override
220     public int getSeed() {
221         return seed;
222     }
223     
224     /**
225      * @param notificationEnqueuer the notificationEnqueuer to set
226      */
227     public void setNotificationEnqueuer(
228             NotificationEnqueuer notificationEnqueuer) {
229         this.notificationEnqueuer = notificationEnqueuer;
230     }
231     
232     @Override
233     public NotificationEnqueuer getNotificationEnqueuer() {
234         return notificationEnqueuer;
235     }
236 }