Merge "BUG-1626: low cbench performance against dataStore"
[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.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(ConnectionConductor 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.setFeatures(features);
62             context.setSessionKey(sessionKey);
63             context.setSeed((int) System.currentTimeMillis());
64             connectionConductor.setSessionContext(context);
65             getSessionManager().addSessionContext(sessionKey, context);
66         } else {
67             // handle auxiliary
68             if (sessionContext == null) {
69                 throw new IllegalStateException("unexpected auxiliary connection - primary connection missing: "
70                         + dumpDataPathId(features.getDatapathId()));
71             } else {
72                 // register auxiliary conductor into existing sessionContext
73                 SwitchConnectionDistinguisher auxiliaryKey = createConnectionCookie(features, sessionContext.getSeed());
74                 if (sessionContext.getAuxiliaryConductor(auxiliaryKey) != null) {
75                     LOG.warn("duplicate datapathId+auxiliary occured while registering switch session: "
76                             + dumpDataPathId(features.getDatapathId())
77                             + " | "
78                             + features.getAuxiliaryId());
79                     getSessionManager().invalidateAuxiliary(sessionKey,
80                             auxiliaryKey);
81                 }
82
83                 sessionContext.addAuxiliaryConductor(auxiliaryKey,
84                         connectionConductor);
85                 connectionConductor.setSessionContext(sessionContext);
86                 connectionConductor.setConnectionCookie(auxiliaryKey);
87             }
88         }
89
90         // check registration result
91         SessionContext resulContext = getSessionManager().getSessionContext(sessionKey);
92         if (resulContext == null) {
93             throw new IllegalStateException("session context registration failed");
94         } else {
95             if (!resulContext.isValid()) {
96                 throw new IllegalStateException("registered session context is invalid");
97             }
98         }
99     }
100
101     /**
102      * @param datapathId
103      * @return readable version of datapathId (hex)
104      */
105     public static String dumpDataPathId(BigInteger datapathId) {
106         return datapathId.toString(16);
107     }
108
109     /**
110      * @param datapathId
111      * @return new session key
112      */
113     public static SwitchSessionKeyOF createSwitchSessionKey(
114             BigInteger datapathId) {
115         SwitchSessionKeyOF key = new SwitchSessionKeyOF();
116         key.setDatapathId(datapathId);
117         return key;
118     }
119
120     /**
121      * @param features
122      * @param seed 
123      * @return connection cookie key
124      * @see #createConnectionCookie(BigInteger,short, int)
125      */
126     public static SwitchConnectionDistinguisher createConnectionCookie(
127             GetFeaturesOutput features, int seed) {
128         return createConnectionCookie(features.getDatapathId(),
129                 features.getAuxiliaryId(), seed);
130     }
131
132     /**
133      * @param datapathId
134      * @param auxiliaryId
135      * @param seed 
136      * @return connection cookie key
137      */
138     public static SwitchConnectionDistinguisher createConnectionCookie(
139             BigInteger datapathId, short auxiliaryId, int seed) {
140         SwitchConnectionCookieOFImpl cookie = null;
141         cookie = new SwitchConnectionCookieOFImpl();
142         cookie.setAuxiliaryId(auxiliaryId);
143         cookie.init(datapathId.intValue() + seed);
144         return cookie;
145     }
146
147     /**
148      * @return session manager singleton instance
149      */
150     public static SessionManager getSessionManager() {
151         return SessionManagerOFImpl.getInstance();
152     }
153     
154     /**
155      * release session manager singleton instance
156      */
157     public static void releaseSessionManager() {
158         SessionManagerOFImpl.releaseInstance();
159     }
160     
161     /**
162     * @return session manager listener Map
163     */
164     public static Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getTranslatorMap() {
165         return getSessionManager().getTranslatorMapping();
166     }
167
168     /**
169      * @return pop listener Map
170      */
171     public static Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> getPopListenerMapping() {
172         return getSessionManager().getPopListenerMapping();
173     }
174
175     /**
176      * @return extension converters provider
177      */
178     public static ExtensionConverterProvider getExtensionConvertorProvider() {
179         return getSessionManager().getExtensionConverterProvider();
180     }
181
182 }