Merge "additional fix fro BUG-782 unregistering switch providers"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / session / OFSessionUtil.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.math.BigInteger;
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.Map;
16
17 import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductor;
18 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
19 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
20 import org.opendaylight.openflowplugin.openflow.md.core.TranslatorKey;
21 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * @author mirehak
30  */
31 public abstract class OFSessionUtil {
32
33     private static final Logger LOG = LoggerFactory
34             .getLogger(OFSessionUtil.class);
35
36     /**
37      * @param connectionConductor
38      * @param features
39      * @param version
40      */
41     public static void registerSession(ConnectionConductor connectionConductor,
42             GetFeaturesOutput features, short version) {
43         SwitchConnectionDistinguisher sessionKey = createSwitchSessionKey(features
44                 .getDatapathId());
45         SessionContext sessionContext = getSessionManager().getSessionContext(sessionKey);
46         if (LOG.isDebugEnabled()) {
47             LOG.debug("registering sessionKey: {}", Arrays.toString(sessionKey.getId()));
48         }
49
50         if (features.getAuxiliaryId() == null || features.getAuxiliaryId() == 0) {
51             // handle primary
52             if (sessionContext != null) {
53                 LOG.warn("duplicate datapathId occured while registering new switch session: "
54                         + dumpDataPathId(features.getDatapathId()));
55                 getSessionManager().invalidateSessionContext(sessionKey);
56             }
57             // register new session context (based primary conductor)
58             SessionContextOFImpl context = new SessionContextOFImpl();
59             context.setPrimaryConductor(connectionConductor);
60             context.setFeatures(features);
61             context.setSessionKey(sessionKey);
62             connectionConductor.setSessionContext(context);
63             context.setValid(true);
64             getSessionManager().addSessionContext(sessionKey, context);
65         } else {
66             // handle auxiliary
67             if (sessionContext == null) {
68                 throw new IllegalStateException("unexpected auxiliary connection - primary connection missing: "
69                         + dumpDataPathId(features.getDatapathId()));
70             } else {
71                 // register auxiliary conductor into existing sessionContext
72                 SwitchConnectionDistinguisher auxiliaryKey = createConnectionCookie(features);
73                 if (sessionContext.getAuxiliaryConductor(auxiliaryKey) != null) {
74                     LOG.warn("duplicate datapathId+auxiliary occured while registering switch session: "
75                             + dumpDataPathId(features.getDatapathId())
76                             + " | "
77                             + features.getAuxiliaryId());
78                     getSessionManager().invalidateAuxiliary(sessionKey,
79                             auxiliaryKey);
80                 }
81
82                 sessionContext.addAuxiliaryConductor(auxiliaryKey,
83                         connectionConductor);
84                 connectionConductor.setSessionContext(sessionContext);
85                 connectionConductor.setConnectionCookie(auxiliaryKey);
86             }
87         }
88
89         // check registration result
90         SessionContext resulContext = getSessionManager().getSessionContext(sessionKey);
91         if (resulContext == null) {
92             throw new IllegalStateException("session context registration failed");
93         } else {
94             if (!resulContext.isValid()) {
95                 throw new IllegalStateException("registered session context is invalid");
96             }
97         }
98     }
99
100     /**
101      * @param datapathId
102      * @return readable version of datapathId (hex)
103      */
104     public static String dumpDataPathId(BigInteger datapathId) {
105         return datapathId.toString(16);
106     }
107
108     /**
109      * @param datapathId
110      * @return new session key
111      */
112     public static SwitchConnectionDistinguisher createSwitchSessionKey(
113             BigInteger datapathId) {
114         SwitchSessionKeyOFImpl key = new SwitchSessionKeyOFImpl();
115         key.setDatapathId(datapathId);
116         key.initId();
117         return key;
118     }
119
120     /**
121      * @param features
122      * @return connection cookie key
123      * @see #createConnectionCookie(BigInteger, short)
124      */
125     public static SwitchConnectionDistinguisher createConnectionCookie(
126             GetFeaturesOutput features) {
127         return createConnectionCookie(features.getDatapathId(),
128                 features.getAuxiliaryId());
129     }
130
131     /**
132      * @param datapathId
133      * @param auxiliaryId
134      * @return connection cookie key
135      */
136     public static SwitchConnectionDistinguisher createConnectionCookie(
137             BigInteger datapathId, short auxiliaryId) {
138         SwitchConnectionCookieOFImpl cookie = null;
139         if (auxiliaryId != 0) {
140             cookie = new SwitchConnectionCookieOFImpl();
141             cookie.setDatapathId(datapathId);
142             cookie.setAuxiliaryId(auxiliaryId);
143             cookie.initId();
144         }
145         return cookie;
146     }
147
148     /**
149      * @return session manager singleton instance
150      */
151     public static SessionManager getSessionManager() {
152         return SessionManagerOFImpl.getInstance();
153     }
154     
155     /**
156      * release session manager singleton instance
157      */
158     public static void releaseSessionManager() {
159         SessionManagerOFImpl.releaseInstance();
160     }
161     
162     /**
163     * @return session manager listener Map
164     */
165     public static Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getTranslatorMap() {
166         return getSessionManager().getTranslatorMapping();
167     }
168
169     /**
170      * @return pop listener Map
171      */
172     public static Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> getPopListenerMapping() {
173         return getSessionManager().getPopListenerMapping();
174     }
175
176 }