Hooking up SessionManager to Conductor for ListenerMap
[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 java.util.Collection;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import java.util.concurrent.ConcurrentHashMap;
15
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;
25
26 /**
27  * @author mirehak
28  */
29 public class SessionManagerOFImpl implements SessionManager {
30
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;
35
36     private final ListenerRegistry<SessionListener> sessionListeners = new ListenerRegistry<>();
37
38     /**
39      * @return singleton instance
40      */
41     public static synchronized SessionManager getInstance() {
42         if (instance == null) {
43             instance = new SessionManagerOFImpl();
44         }
45         return instance;
46     }
47
48     private SessionManagerOFImpl() {
49         sessionLot = new ConcurrentHashMap<>();
50     }
51
52     @Override
53     public SessionContext getSessionContext(SwitchConnectionDistinguisher sessionKey) {
54         return sessionLot.get(sessionKey);
55     }
56
57     @Override
58     public void invalidateSessionContext(SwitchConnectionDistinguisher sessionKey) {
59         SessionContext context = getSessionContext(sessionKey);
60         if (context == null) {
61             LOG.warn("context for invalidation not found");
62         } else {
63             for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : context.getAuxiliaryConductors()) {
64                 invalidateAuxiliary(sessionKey, auxEntry.getKey());
65             }
66             context.getPrimaryConductor().disconnect();
67             context.setValid(false);
68             sessionLot.remove(sessionKey);
69             // TODO:: notify listeners
70         }
71     }
72
73     private void invalidateDeadSessionContext(SessionContext sessionContext) {
74         if (sessionContext == null) {
75             LOG.warn("context for invalidation not found");
76         } else {
77             for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : sessionContext
78                     .getAuxiliaryConductors()) {
79                 invalidateAuxiliary(sessionContext, auxEntry.getKey(), true);
80             }
81             sessionContext.setValid(false);
82             sessionLot.remove(sessionContext.getSessionKey(), sessionContext);
83             // TODO:: notify listeners
84         }
85     }
86
87     @Override
88     public void addSessionContext(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
89         sessionLot.put(sessionKey, context);
90
91         sessionNotifier.onSessionAdded(sessionKey, context);
92
93     }
94
95     @Override
96     public void invalidateAuxiliary(SwitchConnectionDistinguisher sessionKey,
97             SwitchConnectionDistinguisher connectionCookie) {
98         SessionContext context = getSessionContext(sessionKey);
99         invalidateAuxiliary(context, connectionCookie, true);
100     }
101
102     /**
103      * @param context
104      * @param connectionCookie
105      * @param disconnect
106      *            true if auxiliary connection is to be disconnected
107      */
108     private static void invalidateAuxiliary(SessionContext context, SwitchConnectionDistinguisher connectionCookie,
109             boolean disconnect) {
110         if (context == null) {
111             LOG.warn("context for invalidation not found");
112         } else {
113             ConnectionConductor auxiliaryConductor = context.removeAuxiliaryConductor(connectionCookie);
114             if (auxiliaryConductor == null) {
115                 LOG.warn("auxiliary conductor not found");
116             } else {
117                 if (disconnect) {
118                     auxiliaryConductor.disconnect();
119                 }
120             }
121         }
122     }
123
124     @Override
125     public void invalidateOnDisconnect(ConnectionConductor conductor) {
126         if (conductor.getAuxiliaryKey() == null) {
127             invalidateDeadSessionContext(conductor.getSessionContext());
128             // TODO:: notify listeners
129         } else {
130             invalidateAuxiliary(conductor.getSessionContext(), conductor.getAuxiliaryKey(), false);
131         }
132     }
133
134     /**
135      * @param listenerMapping
136      *            the listenerMapping to set
137      */
138     public void setListenerMapping(Map<Class<? extends DataObject>, Collection<IMDMessageListener>> listenerMapping) {
139         this.listenerMapping = listenerMapping;
140     }
141
142     @Override
143     public ListenerRegistration<SessionListener> registerSessionListener(SessionListener listener) {
144         return sessionListeners.register(listener);
145     }
146
147     private final SessionListener sessionNotifier = new SessionListener() {
148
149         @Override
150         public void onSessionAdded(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
151             for (ListenerRegistration<SessionListener> listener : sessionListeners) {
152                 try {
153                     listener.getInstance().onSessionAdded(sessionKey, context);
154                 } catch (Exception e) {
155                     LOG.error("Unhandled exeption occured while invoking onSessionAdded on listener", e);
156                 }
157             }
158         }
159     };
160
161     @Override
162     public Map<Class<? extends DataObject>, Collection<IMDMessageListener>> getListenerMapping() {
163         return this.listenerMapping;
164     }
165
166 }