83f1df12dab12d87f3d55212c7456e1cc3ed1a40
[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.ConnectionConductorImpl;
18 import org.opendaylight.openflowplugin.openflow.md.core.IMDMessageTranslator;
19 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
20 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
21 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterProvider;
22 import org.opendaylight.openflowplugin.openflow.md.queue.PopListener;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * @author mirehak
31  */
32 public abstract class OFSessionUtil {
33
34     private static final Logger LOG = LoggerFactory
35             .getLogger(OFSessionUtil.class);
36
37     /**
38      * @param connectionConductor
39      * @param features
40      * @param version
41      */
42     public static void registerSession(ConnectionConductorImpl connectionConductor,
43             GetFeaturesOutput features, short version) {
44         SwitchSessionKeyOF sessionKey = createSwitchSessionKey(features
45                 .getDatapathId());
46         SessionContext sessionContext = getSessionManager().getSessionContext(sessionKey);
47         if (LOG.isDebugEnabled()) {
48             LOG.debug("registering sessionKey: {}", Arrays.toString(sessionKey.getId()));
49         }
50
51         if (features.getAuxiliaryId() == null || features.getAuxiliaryId() == 0) {
52             // handle primary
53             if (sessionContext != null) {
54                 LOG.warn("duplicate datapathId occured while registering new switch session: "
55                         + dumpDataPathId(features.getDatapathId()));
56                 getSessionManager().invalidateSessionContext(sessionKey);
57             }
58             // register new session context (based primary conductor)
59             SessionContextOFImpl context = new SessionContextOFImpl();
60             context.setPrimaryConductor(connectionConductor);
61             context.setNotificationEnqueuer(connectionConductor);
62             context.setFeatures(features);
63             context.setSessionKey(sessionKey);
64             context.setSeed((int) System.currentTimeMillis());
65             connectionConductor.setSessionContext(context);
66             getSessionManager().addSessionContext(sessionKey, context);
67         } else {
68             // handle auxiliary
69             if (sessionContext == null) {
70                 throw new IllegalStateException("unexpected auxiliary connection - primary connection missing: "
71                         + dumpDataPathId(features.getDatapathId()));
72             } else {
73                 // register auxiliary conductor into existing sessionContext
74                 SwitchConnectionDistinguisher auxiliaryKey = createConnectionCookie(features, sessionContext.getSeed());
75                 if (sessionContext.getAuxiliaryConductor(auxiliaryKey) != null) {
76                     LOG.warn("duplicate datapathId+auxiliary occured while registering switch session: "
77                             + dumpDataPathId(features.getDatapathId())
78                             + " | "
79                             + features.getAuxiliaryId());
80                     getSessionManager().invalidateAuxiliary(sessionKey,
81                             auxiliaryKey);
82                 }
83
84                 sessionContext.addAuxiliaryConductor(auxiliaryKey,
85                         connectionConductor);
86                 connectionConductor.setSessionContext(sessionContext);
87                 connectionConductor.setConnectionCookie(auxiliaryKey);
88             }
89         }
90
91         // check registration result
92         SessionContext resulContext = getSessionManager().getSessionContext(sessionKey);
93         if (resulContext == null) {
94             throw new IllegalStateException("session context registration failed");
95         } else {
96             if (!resulContext.isValid()) {
97                 throw new IllegalStateException("registered session context is invalid");
98             }
99         }
100     }
101
102     /**
103      * @param datapathId
104      * @return readable version of datapathId (hex)
105      */
106     public static String dumpDataPathId(BigInteger datapathId) {
107         return datapathId.toString(16);
108     }
109
110     /**
111      * @param datapathId
112      * @return new session key
113      */
114     public static SwitchSessionKeyOF createSwitchSessionKey(
115             BigInteger datapathId) {
116         SwitchSessionKeyOF key = new SwitchSessionKeyOF();
117         key.setDatapathId(datapathId);
118         return key;
119     }
120
121     /**
122      * @param features
123      * @param seed 
124      * @return connection cookie key
125      * @see #createConnectionCookie(BigInteger,short, int)
126      */
127     public static SwitchConnectionDistinguisher createConnectionCookie(
128             GetFeaturesOutput features, int seed) {
129         return createConnectionCookie(features.getDatapathId(),
130                 features.getAuxiliaryId(), seed);
131     }
132
133     /**
134      * @param datapathId
135      * @param auxiliaryId
136      * @param seed 
137      * @return connection cookie key
138      */
139     public static SwitchConnectionDistinguisher createConnectionCookie(
140             BigInteger datapathId, short auxiliaryId, int seed) {
141         SwitchConnectionCookieOFImpl cookie = null;
142         cookie = new SwitchConnectionCookieOFImpl();
143         cookie.setAuxiliaryId(auxiliaryId);
144         cookie.init(datapathId.intValue() + seed);
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     /**
177      * @return extension converters provider
178      */
179     public static ExtensionConverterProvider getExtensionConvertorProvider() {
180         return getSessionManager().getExtensionConverterProvider();
181     }
182
183 }