112b2c7598d915925939dbfe34c85d26272106b8
[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.yang.binding.DataObject;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * @author mirehak
25  */
26 public class SessionManagerOFImpl implements SessionManager {
27
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;
32
33     /**
34      * @return singleton instance
35      */
36     public static synchronized SessionManager getInstance() {
37         if (instance == null) {
38             instance = new SessionManagerOFImpl();
39         }
40         return instance;
41     }
42
43     private SessionManagerOFImpl() {
44         sessionLot = new ConcurrentHashMap<>();
45     }
46
47     @Override
48     public SessionContext getSessionContext(SwitchConnectionDistinguisher sessionKey) {
49         return sessionLot.get(sessionKey);
50     }
51
52     @Override
53     public void invalidateSessionContext(SwitchConnectionDistinguisher sessionKey) {
54         SessionContext context = getSessionContext(sessionKey);
55         if (context == null) {
56             LOG.warn("context for invalidation not found");
57         } else {
58             for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : context.getAuxiliaryConductors()) {
59                 invalidateAuxiliary(sessionKey, auxEntry.getKey());
60             }
61             context.getPrimaryConductor().disconnect();
62             context.setValid(false);
63             sessionLot.remove(sessionKey);
64             // TODO:: notify listeners
65         }
66     }
67
68     private void invalidateDeadSessionContext(SessionContext sessionContext) {
69         if (sessionContext == null) {
70             LOG.warn("context for invalidation not found");
71         } else {
72             for (Entry<SwitchConnectionDistinguisher, ConnectionConductor> auxEntry : sessionContext
73                     .getAuxiliaryConductors()) {
74                 invalidateAuxiliary(sessionContext, auxEntry.getKey(), true);
75             }
76             sessionContext.setValid(false);
77             sessionLot.remove(sessionContext.getSessionKey(), sessionContext);
78             // TODO:: notify listeners
79         }
80     }
81
82     @Override
83     public void addSessionContext(SwitchConnectionDistinguisher sessionKey, SessionContext context) {
84         sessionLot.put(sessionKey, context);
85         // TODO:: notify listeners
86     }
87
88     @Override
89     public void invalidateAuxiliary(SwitchConnectionDistinguisher sessionKey,
90             SwitchConnectionDistinguisher connectionCookie) {
91         SessionContext context = getSessionContext(sessionKey);
92         invalidateAuxiliary(context, connectionCookie, true);
93     }
94
95     /**
96      * @param context
97      * @param connectionCookie
98      * @param disconnect
99      *            true if auxiliary connection is to be disconnected
100      */
101     private static void invalidateAuxiliary(SessionContext context, SwitchConnectionDistinguisher connectionCookie,
102             boolean disconnect) {
103         if (context == null) {
104             LOG.warn("context for invalidation not found");
105         } else {
106             ConnectionConductor auxiliaryConductor = context.removeAuxiliaryConductor(connectionCookie);
107             if (auxiliaryConductor == null) {
108                 LOG.warn("auxiliary conductor not found");
109             } else {
110                 if (disconnect) {
111                     auxiliaryConductor.disconnect();
112                 }
113             }
114         }
115     }
116
117     @Override
118     public void invalidateOnDisconnect(ConnectionConductor conductor) {
119         if (conductor.getAuxiliaryKey() == null) {
120             invalidateDeadSessionContext(conductor.getSessionContext());
121             // TODO:: notify listeners
122         } else {
123             invalidateAuxiliary(conductor.getSessionContext(), conductor.getAuxiliaryKey(), false);
124         }
125     }
126
127     /**
128      * @param listenerMapping
129      *            the listenerMapping to set
130      */
131     public void setListenerMapping(Map<Class<? extends DataObject>, Collection<IMDMessageListener>> listenerMapping) {
132         this.listenerMapping = listenerMapping;
133     }
134 }