2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.openflow.md.core.session;
11 import java.util.Arrays;
12 import java.util.Collection;
13 import java.util.List;
15 import java.util.Map.Entry;
16 import java.util.concurrent.ConcurrentHashMap;
18 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
19 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
20 import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductor;
21 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
22 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
23 import org.opendaylight.openflowplugin.openflow.md.core.TranslatorKey;
24 import org.opendaylight.openflowplugin.openflow.md.queue.MessageSpy;
25 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
29 import org.opendaylight.yangtools.yang.binding.DataContainer;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 import com.google.common.util.concurrent.ListeningExecutorService;
39 public class SessionManagerOFImpl implements SessionManager {
41 protected static final Logger LOG = LoggerFactory.getLogger(SessionManagerOFImpl.class);
42 private static SessionManagerOFImpl instance;
43 private ConcurrentHashMap<SwitchSessionKeyOF, SessionContext> sessionLot;
44 private Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> translatorMapping;
45 private Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListenerMapping;
47 protected ListenerRegistry<SessionListener> sessionListeners;
48 private NotificationProviderService notificationProviderService;
50 private DataProviderService dataProviderService;
51 private ListeningExecutorService rpcPool;
55 * @return singleton instance
57 public static synchronized SessionManager getInstance() {
58 if (instance == null) {
59 instance = new SessionManagerOFImpl();
65 * close and release singleton instace
67 public static synchronized void releaseInstance() {
72 private SessionManagerOFImpl() {
73 LOG.debug("singleton creating");
74 sessionLot = new ConcurrentHashMap<>();
75 sessionListeners = new ListenerRegistry<>();
79 public SessionContext getSessionContext(SwitchSessionKeyOF sessionKey) {
80 return sessionLot.get(sessionKey);
84 public void invalidateSessionContext(SwitchSessionKeyOF sessionKey) {
85 SessionContext context = getSessionContext(sessionKey);
86 if (context == null) {
87 LOG.warn("context for invalidation not found");
89 for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : context.getAuxiliaryConductors()) {
90 invalidateAuxiliary(sessionKey, auxEntry.getKey());
92 context.getPrimaryConductor().disconnect();
93 context.setValid(false);
94 removeSessionContext(context);
95 // TODO:: notify listeners
99 private void invalidateDeadSessionContext(SessionContext sessionContext) {
100 if (sessionContext == null) {
101 LOG.warn("context for invalidation not found");
103 for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : sessionContext
104 .getAuxiliaryConductors()) {
105 invalidateAuxiliary(sessionContext, auxEntry.getKey(), true);
107 sessionContext.setValid(false);
108 removeSessionContext(sessionContext);
109 // TODO:: notify listeners
113 private void removeSessionContext(SessionContext sessionContext) {
114 if (LOG.isDebugEnabled()) {
115 LOG.debug("removing session: {}", Arrays.toString(sessionContext.getSessionKey().getId()));
117 sessionLot.remove(sessionContext.getSessionKey(), sessionContext);
118 sessionNotifier.onSessionRemoved(sessionContext);
122 public void addSessionContext(SwitchSessionKeyOF sessionKey, SessionContext context) {
123 sessionLot.put(sessionKey, context);
125 sessionNotifier.onSessionAdded(sessionKey, context);
130 public void invalidateAuxiliary(SwitchSessionKeyOF sessionKey,
131 SwitchConnectionDistinguisher connectionCookie) {
132 SessionContext context = getSessionContext(sessionKey);
133 invalidateAuxiliary(context, connectionCookie, true);
138 * @param connectionCookie
140 * true if auxiliary connection is to be disconnected
142 private static void invalidateAuxiliary(SessionContext context, SwitchConnectionDistinguisher connectionCookie,
143 boolean disconnect) {
144 if (context == null) {
145 LOG.warn("context for invalidation not found");
147 ConnectionConductor auxiliaryConductor = context.removeAuxiliaryConductor(connectionCookie);
148 if (auxiliaryConductor == null) {
149 LOG.warn("auxiliary conductor not found");
152 auxiliaryConductor.disconnect();
159 public void invalidateOnDisconnect(ConnectionConductor conductor) {
160 if (conductor.getAuxiliaryKey() == null) {
161 invalidateDeadSessionContext(conductor.getSessionContext());
162 // TODO:: notify listeners
164 invalidateAuxiliary(conductor.getSessionContext(), conductor.getAuxiliaryKey(), false);
169 public void setTranslatorMapping(Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> translatorMapping) {
170 this.translatorMapping = translatorMapping;
174 public ListenerRegistration<SessionListener> registerSessionListener(SessionListener listener) {
175 LOG.debug("registerSessionListener");
176 return sessionListeners.register(listener);
179 private final SessionListener sessionNotifier = new SessionListener() {
182 public void onSessionAdded(SwitchSessionKeyOF sessionKey, SessionContext context) {
183 for (ListenerRegistration<SessionListener> listener : sessionListeners) {
185 listener.getInstance().onSessionAdded(sessionKey, context);
186 } catch (Exception e) {
187 LOG.error("Unhandled exeption occured while invoking onSessionAdded on listener", e);
193 public void onSessionRemoved(SessionContext context) {
194 for (ListenerRegistration<SessionListener> listener : sessionListeners) {
196 listener.getInstance().onSessionRemoved(context);
197 } catch (Exception e) {
198 LOG.error("Unhandled exeption occured while invoking onSessionRemoved on listener", e);
203 private MessageSpy<DataContainer> messageSpy;
207 public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getTranslatorMapping() {
208 return this.translatorMapping;
212 public void setNotificationProviderService(
213 NotificationProviderService notificationProviderService) {
214 this.notificationProviderService = notificationProviderService;
219 public DataProviderService getDataProviderService() {
220 return dataProviderService;
224 public void setDataProviderService(DataProviderService dataServiceProvider) {
225 this.dataProviderService = dataServiceProvider;
230 public NotificationProviderService getNotificationProviderService() {
231 return notificationProviderService;
235 public Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> getPopListenerMapping() {
236 return popListenerMapping;
240 public void setPopListenerMapping(
241 Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListenerMapping) {
242 this.popListenerMapping = popListenerMapping;
246 public void close() {
248 sessionListeners = null;
249 synchronized (sessionLot) {
250 for (SessionContext sessionContext : sessionLot.values()) {
251 sessionContext.getPrimaryConductor().disconnect();
253 // TODO: handle timeouted shutdown
259 public void setRpcPool(ListeningExecutorService rpcPool) {
260 this.rpcPool = rpcPool;
264 public ListeningExecutorService getRpcPool() {
269 public void setMessageSpy(MessageSpy<DataContainer> messageSpy) {
270 this.messageSpy = messageSpy;
274 public MessageSpy<DataContainer> getMessageSpy() {