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