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