Fix of port-number model
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / session / SessionManagerOFImpl.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 com.google.common.util.concurrent.ListeningExecutorService;
12
13 import java.util.Arrays;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.concurrent.ConcurrentHashMap;
19
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
22 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
23 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
24 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
25 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionListener;
26 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SwitchSessionKeyOF;
27 import org.opendaylight.openflowplugin.api.statistics.MessageSpy;
28 import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
29 import org.opendaylight.openflowplugin.api.openflow.md.core.IMDMessageTranslator;
30 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
31 import org.opendaylight.openflowplugin.api.openflow.md.queue.PopListener;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.util.ListenerRegistry;
35 import org.opendaylight.yangtools.yang.binding.DataContainer;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * @author mirehak
42  */
43 public class SessionManagerOFImpl implements ConjunctSessionManager {
44
45     protected static final Logger LOG = LoggerFactory.getLogger(SessionManagerOFImpl.class);
46     private static SessionManagerOFImpl instance;
47     private ConcurrentHashMap<SwitchSessionKeyOF, SessionContext> sessionLot;
48     private Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> translatorMapping;
49     private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListenerMapping;
50
51     protected ListenerRegistry<SessionListener> sessionListeners;
52     private NotificationProviderService notificationProviderService;
53
54     private DataBroker dataBroker;
55     private ListeningExecutorService rpcPool;
56
57
58     /**
59      * @return singleton instance
60      */
61     public static ConjunctSessionManager getInstance() {
62         if (instance == null) {
63             synchronized (SessionContextOFImpl.class) {
64                 if (instance == null) {
65                     instance = new SessionManagerOFImpl();
66                 }
67             }
68         }
69         return instance;
70     }
71
72     /**
73      * close and release singleton instace
74      */
75     public static void releaseInstance() {
76         if (instance != null) {
77             synchronized (SessionManagerOFImpl.class) {
78                 if (instance != null) {
79                     instance.close();
80                     instance = null;
81                 }
82             }
83         }
84     }
85
86     private SessionManagerOFImpl() {
87         LOG.debug("singleton creating");
88         sessionLot = new ConcurrentHashMap<>();
89         sessionListeners = new ListenerRegistry<>();
90     }
91
92     @Override
93     public SessionContext getSessionContext(SwitchSessionKeyOF sessionKey) {
94         return sessionLot.get(sessionKey);
95     }
96
97     @Override
98     public void invalidateSessionContext(SwitchSessionKeyOF sessionKey) {
99         SessionContext context = getSessionContext(sessionKey);
100         if (context == null) {
101             LOG.warn("context for invalidation not found");
102         } else {
103             synchronized (context) {
104                 for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : context.getAuxiliaryConductors()) {
105                     invalidateAuxiliary(sessionKey, auxEntry.getKey());
106                 }
107                 context.getPrimaryConductor().disconnect();
108                 context.setValid(false);
109                 removeSessionContext(context);
110                 // TODO:: notify listeners
111             }
112         }
113     }
114
115     private void invalidateDeadSessionContext(SessionContext sessionContext) {
116         if (sessionContext == null) {
117             LOG.warn("context for invalidation not found");
118         } else {
119             synchronized (sessionContext) {
120                 for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : sessionContext
121                         .getAuxiliaryConductors()) {
122                     invalidateAuxiliary(sessionContext, auxEntry.getKey(), true);
123                 }
124                 sessionContext.setValid(false);
125                 removeSessionContext(sessionContext);
126                 // TODO:: notify listeners
127             }
128         }
129     }
130
131     private void removeSessionContext(SessionContext sessionContext) {
132         if (LOG.isDebugEnabled()) {
133             LOG.debug("removing session: {}", Arrays.toString(sessionContext.getSessionKey().getId()));
134         }
135         sessionLot.remove(sessionContext.getSessionKey(), sessionContext);
136         sessionNotifier.onSessionRemoved(sessionContext);
137     }
138
139     @Override
140     public void addSessionContext(SwitchSessionKeyOF sessionKey, SessionContext context) {
141         synchronized (context) {
142             sessionLot.put(sessionKey, context);
143             sessionNotifier.onSessionAdded(sessionKey, context);
144             context.setValid(true);
145         }
146     }
147
148     @Override
149     public void invalidateAuxiliary(SwitchSessionKeyOF sessionKey,
150                                     SwitchConnectionDistinguisher connectionCookie) {
151         SessionContext context = getSessionContext(sessionKey);
152         invalidateAuxiliary(context, connectionCookie, true);
153     }
154
155     /**
156      * @param context
157      * @param connectionCookie
158      * @param disconnect       true if auxiliary connection is to be disconnected
159      */
160     private static void invalidateAuxiliary(SessionContext context, SwitchConnectionDistinguisher connectionCookie,
161                                             boolean disconnect) {
162         if (context == null) {
163             LOG.warn("context for invalidation not found");
164         } else {
165             ConnectionConductor auxiliaryConductor = context.removeAuxiliaryConductor(connectionCookie);
166             if (auxiliaryConductor == null) {
167                 LOG.warn("auxiliary conductor not found");
168             } else {
169                 if (disconnect) {
170                     auxiliaryConductor.disconnect();
171                 }
172             }
173         }
174     }
175
176     @Override
177     public void invalidateOnDisconnect(ConnectionConductor conductor) {
178         if (conductor.getAuxiliaryKey() == null) {
179             invalidateDeadSessionContext(conductor.getSessionContext());
180             // TODO:: notify listeners
181         } else {
182             invalidateAuxiliary(conductor.getSessionContext(), conductor.getAuxiliaryKey(), false);
183         }
184     }
185
186     @Override
187     public void setTranslatorMapping(Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> translatorMapping) {
188         this.translatorMapping = translatorMapping;
189     }
190
191     @Override
192     public ListenerRegistration<SessionListener> registerSessionListener(SessionListener listener) {
193         LOG.debug("registerSessionListener");
194         return sessionListeners.register(listener);
195     }
196
197     private final SessionListener sessionNotifier = new SessionListener() {
198
199         @Override
200         public void onSessionAdded(SwitchSessionKeyOF sessionKey, SessionContext context) {
201             for (ListenerRegistration<SessionListener> listener : sessionListeners) {
202                 try {
203                     listener.getInstance().onSessionAdded(sessionKey, context);
204                 } catch (Exception e) {
205                     LOG.error("Unhandled exeption occured while invoking onSessionAdded on listener", e);
206                 }
207             }
208         }
209
210         @Override
211         public void onSessionRemoved(SessionContext context) {
212             for (ListenerRegistration<SessionListener> listener : sessionListeners) {
213                 try {
214                     listener.getInstance().onSessionRemoved(context);
215                 } catch (Exception e) {
216                     LOG.error("Unhandled exeption occured while invoking onSessionRemoved on listener", e);
217                 }
218             }
219         }
220     };
221     private MessageSpy<DataContainer> messageSpy;
222     private ExtensionConverterProvider extensionConverterProvider;
223
224
225     @Override
226     public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getTranslatorMapping() {
227         return this.translatorMapping;
228     }
229
230     @Override
231     public void setNotificationProviderService(
232             NotificationProviderService notificationProviderService) {
233         this.notificationProviderService = notificationProviderService;
234
235     }
236
237     @Override
238     public DataBroker getDataBroker() {
239         return dataBroker;
240     }
241
242     @Override
243     public void setDataBroker(DataBroker dataBroker) {
244         this.dataBroker = dataBroker;
245
246     }
247
248     @Override
249     public NotificationProviderService getNotificationProviderService() {
250         return notificationProviderService;
251     }
252
253     @Override
254     public Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> getPopListenerMapping() {
255         return popListenerMapping;
256     }
257
258     @Override
259     public void setPopListenerMapping(
260             Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListenerMapping) {
261         this.popListenerMapping = popListenerMapping;
262     }
263
264     @Override
265     public void close() {
266         LOG.debug("close");
267         synchronized (sessionLot) {
268             for (SessionContext sessionContext : sessionLot.values()) {
269                 sessionContext.getPrimaryConductor().disconnect();
270             }
271             // TODO: handle timeouted shutdown
272             rpcPool.shutdown();
273         }
274         
275         for (ListenerRegistration<SessionListener> listenerRegistration : sessionListeners) {
276             SessionListener listener = listenerRegistration.getInstance();
277             if (listener instanceof AutoCloseable) {
278                 try {
279                     ((AutoCloseable) listener).close();
280                 } catch (Exception e) {
281                     LOG.warn("closing of sessionListenerRegistration failed", e);
282                 }
283             }
284         }
285     }
286
287     @Override
288     public void setRpcPool(ListeningExecutorService rpcPool) {
289         this.rpcPool = rpcPool;
290     }
291
292     @Override
293     public ListeningExecutorService getRpcPool() {
294         return rpcPool;
295     }
296
297     @Override
298     public void setMessageSpy(MessageSpy<DataContainer> messageSpy) {
299         this.messageSpy = messageSpy;
300     }
301
302     @Override
303     public MessageSpy<DataContainer> getMessageSpy() {
304         return messageSpy;
305     }
306
307     @Override
308     public void setExtensionConverterProvider(
309             ExtensionConverterProvider extensionConverterProvider) {
310         this.extensionConverterProvider = extensionConverterProvider;
311     }
312
313     /**
314      * @return the extensionConverterProvider
315      */
316     @Override
317     public ExtensionConverterProvider getExtensionConverterProvider() {
318         return extensionConverterProvider;
319     }
320 }