BUG-542 - adding overall statictics
[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.Arrays;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.concurrent.ConcurrentHashMap;
17
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;
33
34 import com.google.common.util.concurrent.ListeningExecutorService;
35
36 /**
37  * @author mirehak
38  */
39 public class SessionManagerOFImpl implements SessionManager {
40
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;
46
47     protected ListenerRegistry<SessionListener> sessionListeners;
48     private NotificationProviderService notificationProviderService;
49
50     private DataProviderService dataProviderService;
51     private ListeningExecutorService rpcPool;
52     
53
54     /**
55      * @return singleton instance
56      */
57     public static synchronized SessionManager getInstance() {
58         if (instance == null) {
59             instance = new SessionManagerOFImpl();
60         }
61         return instance;
62     }
63     
64     /**
65      * close and release singleton instace
66      */
67     public static synchronized void releaseInstance() {
68         instance.close();
69         instance = null;
70     }
71
72     private SessionManagerOFImpl() {
73         LOG.debug("singleton creating");
74         sessionLot = new ConcurrentHashMap<>();
75         sessionListeners = new ListenerRegistry<>();
76     }
77
78     @Override
79     public SessionContext getSessionContext(SwitchSessionKeyOF sessionKey) {
80         return sessionLot.get(sessionKey);
81     }
82
83     @Override
84     public void invalidateSessionContext(SwitchSessionKeyOF sessionKey) {
85         SessionContext context = getSessionContext(sessionKey);
86         if (context == null) {
87             LOG.warn("context for invalidation not found");
88         } else {
89             for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : context.getAuxiliaryConductors()) {
90                 invalidateAuxiliary(sessionKey, auxEntry.getKey());
91             }
92             context.getPrimaryConductor().disconnect();
93             context.setValid(false);
94             removeSessionContext(context);
95             // TODO:: notify listeners
96         }
97     }
98
99     private void invalidateDeadSessionContext(SessionContext sessionContext) {
100         if (sessionContext == null) {
101             LOG.warn("context for invalidation not found");
102         } else {
103             for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : sessionContext
104                     .getAuxiliaryConductors()) {
105                 invalidateAuxiliary(sessionContext, auxEntry.getKey(), true);
106             }
107             sessionContext.setValid(false);
108             removeSessionContext(sessionContext);
109             // TODO:: notify listeners
110         }
111     }
112
113     private void removeSessionContext(SessionContext sessionContext) {
114         if (LOG.isDebugEnabled()) {
115             LOG.debug("removing session: {}", Arrays.toString(sessionContext.getSessionKey().getId()));
116         }
117         sessionLot.remove(sessionContext.getSessionKey(), sessionContext);
118         sessionNotifier.onSessionRemoved(sessionContext);
119     }
120
121     @Override
122     public void addSessionContext(SwitchSessionKeyOF sessionKey, SessionContext context) {
123         sessionLot.put(sessionKey, context);
124
125         sessionNotifier.onSessionAdded(sessionKey, context);
126
127     }
128
129     @Override
130     public void invalidateAuxiliary(SwitchSessionKeyOF sessionKey,
131             SwitchConnectionDistinguisher connectionCookie) {
132         SessionContext context = getSessionContext(sessionKey);
133         invalidateAuxiliary(context, connectionCookie, true);
134     }
135
136     /**
137      * @param context
138      * @param connectionCookie
139      * @param disconnect
140      *            true if auxiliary connection is to be disconnected
141      */
142     private static void invalidateAuxiliary(SessionContext context, SwitchConnectionDistinguisher connectionCookie,
143             boolean disconnect) {
144         if (context == null) {
145             LOG.warn("context for invalidation not found");
146         } else {
147             ConnectionConductor auxiliaryConductor = context.removeAuxiliaryConductor(connectionCookie);
148             if (auxiliaryConductor == null) {
149                 LOG.warn("auxiliary conductor not found");
150             } else {
151                 if (disconnect) {
152                     auxiliaryConductor.disconnect();
153                 }
154             }
155         }
156     }
157
158     @Override
159     public void invalidateOnDisconnect(ConnectionConductor conductor) {
160         if (conductor.getAuxiliaryKey() == null) {
161             invalidateDeadSessionContext(conductor.getSessionContext());
162             // TODO:: notify listeners
163         } else {
164             invalidateAuxiliary(conductor.getSessionContext(), conductor.getAuxiliaryKey(), false);
165         }
166     }
167
168     @Override
169     public void setTranslatorMapping(Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> translatorMapping) {
170         this.translatorMapping = translatorMapping;
171     }
172
173     @Override
174     public ListenerRegistration<SessionListener> registerSessionListener(SessionListener listener) {
175         LOG.debug("registerSessionListener");
176         return sessionListeners.register(listener);
177     }
178
179     private final SessionListener sessionNotifier = new SessionListener() {
180
181         @Override
182         public void onSessionAdded(SwitchSessionKeyOF sessionKey, SessionContext context) {
183             for (ListenerRegistration<SessionListener> listener : sessionListeners) {
184                 try {
185                     listener.getInstance().onSessionAdded(sessionKey, context);
186                 } catch (Exception e) {
187                     LOG.error("Unhandled exeption occured while invoking onSessionAdded on listener", e);
188                 }
189             }
190         }
191
192         @Override
193         public void onSessionRemoved(SessionContext context) {
194             for (ListenerRegistration<SessionListener> listener : sessionListeners) {
195                 try {
196                     listener.getInstance().onSessionRemoved(context);
197                 } catch (Exception e) {
198                     LOG.error("Unhandled exeption occured while invoking onSessionRemoved on listener", e);
199                 }
200             }
201         }
202     };
203     private MessageSpy<DataContainer> messageSpy;
204     
205
206     @Override
207     public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getTranslatorMapping() {
208         return this.translatorMapping;
209     }
210
211     @Override
212     public void setNotificationProviderService(
213             NotificationProviderService notificationProviderService) {
214         this.notificationProviderService = notificationProviderService;
215
216     }
217
218     @Override
219     public DataProviderService getDataProviderService() {
220         return dataProviderService;
221     }
222
223     @Override
224     public void setDataProviderService(DataProviderService dataServiceProvider) {
225         this.dataProviderService = dataServiceProvider;
226
227     }
228
229     @Override
230     public NotificationProviderService getNotificationProviderService() {
231         return notificationProviderService;
232     }
233
234     @Override
235     public Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> getPopListenerMapping() {
236         return popListenerMapping;
237     }
238
239     @Override
240     public void setPopListenerMapping(
241             Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> popListenerMapping) {
242         this.popListenerMapping = popListenerMapping;
243     }
244     
245     @Override
246     public void close() {
247         LOG.debug("close");
248         sessionListeners = null;
249         synchronized (sessionLot) {
250             for (SessionContext sessionContext : sessionLot.values()) {
251                 sessionContext.getPrimaryConductor().disconnect();
252             }
253             // TODO: handle timeouted shutdown
254             rpcPool.shutdown();
255         }
256     }
257
258     @Override
259     public void setRpcPool(ListeningExecutorService rpcPool) {
260         this.rpcPool = rpcPool;
261     }
262     
263     @Override
264     public ListeningExecutorService getRpcPool() {
265         return rpcPool;
266     }
267     
268     @Override
269     public void setMessageSpy(MessageSpy<DataContainer> messageSpy) {
270         this.messageSpy = messageSpy;
271     }
272     
273     @Override
274     public MessageSpy<DataContainer> getMessageSpy() {
275         return messageSpy;
276     }
277 }