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.openflowplugin.openflow.md.core.ConnectionConductor;
17 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageListener;
18 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
26 public class SessionManagerOFImpl implements SessionManager {
28 private static final Logger LOG = LoggerFactory.getLogger(SessionManagerOFImpl.class);
29 private static SessionManagerOFImpl instance;
30 private ConcurrentHashMap<SwitchConnectionDistinguisher, SessionContext> sessionLot;
31 private Map<Class<? extends DataObject>, Collection<IMDMessageListener>> listenerMapping;
34 * @return singleton instance
36 public static synchronized SessionManager getInstance() {
37 if (instance == null) {
38 instance = new SessionManagerOFImpl();
43 private SessionManagerOFImpl() {
44 sessionLot = new ConcurrentHashMap<>();
48 public SessionContext getSessionContext(SwitchConnectionDistinguisher sessionKey) {
49 return sessionLot.get(sessionKey);
53 public void invalidateSessionContext(SwitchConnectionDistinguisher sessionKey) {
54 SessionContext context = getSessionContext(sessionKey);
55 if (context == null) {
56 LOG.warn("context for invalidation not found");
58 for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : context.getAuxiliaryConductors()) {
59 invalidateAuxiliary(sessionKey, auxEntry.getKey());
61 context.getPrimaryConductor().disconnect();
62 context.setValid(false);
63 sessionLot.remove(sessionKey);
64 // TODO:: notify listeners
68 private void invalidateDeadSessionContext(SessionContext sessionContext) {
69 if (sessionContext == null) {
70 LOG.warn("context for invalidation not found");
72 for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : sessionContext
73 .getAuxiliaryConductors()) {
74 invalidateAuxiliary(sessionContext, auxEntry.getKey(), true);
76 sessionContext.setValid(false);
77 sessionLot.remove(sessionContext.getSessionKey(), sessionContext);
78 // TODO:: notify listeners
83 public void addSessionContext(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
84 sessionLot.put(sessionKey, context);
85 // TODO:: notify listeners
89 public void invalidateAuxiliary(SwitchConnectionDistinguisher sessionKey,
90 SwitchConnectionDistinguisher connectionCookie) {
91 SessionContext context = getSessionContext(sessionKey);
92 invalidateAuxiliary(context, connectionCookie, true);
97 * @param connectionCookie
99 * true if auxiliary connection is to be disconnected
101 private static void invalidateAuxiliary(SessionContext context, SwitchConnectionDistinguisher connectionCookie,
102 boolean disconnect) {
103 if (context == null) {
104 LOG.warn("context for invalidation not found");
106 ConnectionConductor auxiliaryConductor = context.removeAuxiliaryConductor(connectionCookie);
107 if (auxiliaryConductor == null) {
108 LOG.warn("auxiliary conductor not found");
111 auxiliaryConductor.disconnect();
118 public void invalidateOnDisconnect(ConnectionConductor conductor) {
119 if (conductor.getAuxiliaryKey() == null) {
120 invalidateDeadSessionContext(conductor.getSessionContext());
121 // TODO:: notify listeners
123 invalidateAuxiliary(conductor.getSessionContext(), conductor.getAuxiliaryKey(), false);
128 * @param listenerMapping
129 * the listenerMapping to set
131 public void setListenerMapping(Map<Class<? extends DataObject>, Collection<IMDMessageListener>> listenerMapping) {
132 this.listenerMapping = listenerMapping;