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.Collection;
13 import java.util.Map.Entry;
14 import java.util.concurrent.ConcurrentHashMap;
16 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
17 import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductor;
18 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageListener;
19 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
29 public class SessionManagerOFImpl implements SessionManager {
31 private static final Logger LOG = LoggerFactory.getLogger(SessionManagerOFImpl.class);
32 private static SessionManagerOFImpl instance;
33 private ConcurrentHashMap<SwitchConnectionDistinguisher, SessionContext> sessionLot;
34 private Map<Class<? extends DataObject>, Collection<IMDMessageListener>> listenerMapping;
36 private final ListenerRegistry<SessionListener> sessionListeners = new ListenerRegistry<>();
39 * @return singleton instance
41 public static synchronized SessionManager getInstance() {
42 if (instance == null) {
43 instance = new SessionManagerOFImpl();
48 private SessionManagerOFImpl() {
49 sessionLot = new ConcurrentHashMap<>();
53 public SessionContext getSessionContext(SwitchConnectionDistinguisher sessionKey) {
54 return sessionLot.get(sessionKey);
58 public void invalidateSessionContext(SwitchConnectionDistinguisher sessionKey) {
59 SessionContext context = getSessionContext(sessionKey);
60 if (context == null) {
61 LOG.warn("context for invalidation not found");
63 for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : context.getAuxiliaryConductors()) {
64 invalidateAuxiliary(sessionKey, auxEntry.getKey());
66 context.getPrimaryConductor().disconnect();
67 context.setValid(false);
68 sessionLot.remove(sessionKey);
69 // TODO:: notify listeners
73 private void invalidateDeadSessionContext(SessionContext sessionContext) {
74 if (sessionContext == null) {
75 LOG.warn("context for invalidation not found");
77 for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : sessionContext
78 .getAuxiliaryConductors()) {
79 invalidateAuxiliary(sessionContext, auxEntry.getKey(), true);
81 sessionContext.setValid(false);
82 sessionLot.remove(sessionContext.getSessionKey(), sessionContext);
83 // TODO:: notify listeners
88 public void addSessionContext(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
89 sessionLot.put(sessionKey, context);
91 sessionNotifier.onSessionAdded(sessionKey, context);
96 public void invalidateAuxiliary(SwitchConnectionDistinguisher sessionKey,
97 SwitchConnectionDistinguisher connectionCookie) {
98 SessionContext context = getSessionContext(sessionKey);
99 invalidateAuxiliary(context, connectionCookie, true);
104 * @param connectionCookie
106 * true if auxiliary connection is to be disconnected
108 private static void invalidateAuxiliary(SessionContext context, SwitchConnectionDistinguisher connectionCookie,
109 boolean disconnect) {
110 if (context == null) {
111 LOG.warn("context for invalidation not found");
113 ConnectionConductor auxiliaryConductor = context.removeAuxiliaryConductor(connectionCookie);
114 if (auxiliaryConductor == null) {
115 LOG.warn("auxiliary conductor not found");
118 auxiliaryConductor.disconnect();
125 public void invalidateOnDisconnect(ConnectionConductor conductor) {
126 if (conductor.getAuxiliaryKey() == null) {
127 invalidateDeadSessionContext(conductor.getSessionContext());
128 // TODO:: notify listeners
130 invalidateAuxiliary(conductor.getSessionContext(), conductor.getAuxiliaryKey(), false);
135 * @param listenerMapping
136 * the listenerMapping to set
138 public void setListenerMapping(Map<Class<? extends DataObject>, Collection<IMDMessageListener>> listenerMapping) {
139 this.listenerMapping = listenerMapping;
143 public ListenerRegistration<SessionListener> registerSessionListener(SessionListener listener) {
144 return sessionListeners.register(listener);
147 private final SessionListener sessionNotifier = new SessionListener() {
150 public void onSessionAdded(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
151 for (ListenerRegistration<SessionListener> listener : sessionListeners) {
153 listener.getInstance().onSessionAdded(sessionKey, context);
154 } catch (Exception e) {
155 LOG.error("Unhandled exeption occured while invoking onSessionAdded on listener", e);
162 public Map<Class<? extends DataObject>, Collection<IMDMessageListener>> getListenerMapping() {
163 return this.listenerMapping;