2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.openflow.md.core.session;
11 import java.math.BigInteger;
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.List;
17 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
18 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SwitchSessionKeyOF;
19 import org.opendaylight.openflowplugin.openflow.md.core.ConnectionConductorImpl;
20 import org.opendaylight.openflowplugin.api.openflow.md.core.IMDMessageTranslator;
21 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
22 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
23 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
24 import org.opendaylight.openflowplugin.api.openflow.md.queue.PopListener;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
34 public abstract class OFSessionUtil {
36 private static final Logger LOG = LoggerFactory
37 .getLogger(OFSessionUtil.class);
40 * @param connectionConductor
44 public static void registerSession(ConnectionConductorImpl connectionConductor,
45 GetFeaturesOutput features, short version) {
46 SwitchSessionKeyOF sessionKey = createSwitchSessionKey(features
48 SessionContext sessionContext = getSessionManager().getSessionContext(sessionKey);
49 if (LOG.isDebugEnabled()) {
50 LOG.debug("registering sessionKey: {}", Arrays.toString(sessionKey.getId()));
53 if (features.getAuxiliaryId() == null || features.getAuxiliaryId() == 0) {
55 if (sessionContext != null) {
56 LOG.warn("duplicate datapathId occured while registering new switch session: "
57 + dumpDataPathId(features.getDatapathId()));
58 getSessionManager().invalidateSessionContext(sessionKey);
60 // register new session context (based primary conductor)
61 SessionContextOFImpl context = new SessionContextOFImpl();
62 context.setPrimaryConductor(connectionConductor);
63 context.setNotificationEnqueuer(connectionConductor);
64 context.setFeatures(features);
65 context.setSessionKey(sessionKey);
66 context.setSeed((int) System.currentTimeMillis());
67 connectionConductor.setSessionContext(context);
68 getSessionManager().addSessionContext(sessionKey, context);
71 if (sessionContext == null) {
72 throw new IllegalStateException("unexpected auxiliary connection - primary connection missing: "
73 + dumpDataPathId(features.getDatapathId()));
75 // register auxiliary conductor into existing sessionContext
76 SwitchConnectionDistinguisher auxiliaryKey = createConnectionCookie(features, sessionContext.getSeed());
77 if (sessionContext.getAuxiliaryConductor(auxiliaryKey) != null) {
78 LOG.warn("duplicate datapathId+auxiliary occured while registering switch session: "
79 + dumpDataPathId(features.getDatapathId())
81 + features.getAuxiliaryId());
82 getSessionManager().invalidateAuxiliary(sessionKey,
86 sessionContext.addAuxiliaryConductor(auxiliaryKey,
88 connectionConductor.setSessionContext(sessionContext);
89 connectionConductor.setConnectionCookie(auxiliaryKey);
93 // check registration result
94 SessionContext resulContext = getSessionManager().getSessionContext(sessionKey);
95 if (resulContext == null) {
96 throw new IllegalStateException("session context registration failed");
98 if (!resulContext.isValid()) {
99 throw new IllegalStateException("registered session context is invalid");
106 * @return readable version of datapathId (hex)
108 public static String dumpDataPathId(BigInteger datapathId) {
109 return datapathId.toString(16);
114 * @return new session key
116 public static SwitchSessionKeyOF createSwitchSessionKey(
117 BigInteger datapathId) {
118 SwitchSessionKeyOF key = new SwitchSessionKeyOF();
119 key.setDatapathId(datapathId);
126 * @return connection cookie key
127 * @see #createConnectionCookie(BigInteger,short, int)
129 public static SwitchConnectionDistinguisher createConnectionCookie(
130 GetFeaturesOutput features, int seed) {
131 return createConnectionCookie(features.getDatapathId(),
132 features.getAuxiliaryId(), seed);
139 * @return connection cookie key
141 public static SwitchConnectionDistinguisher createConnectionCookie(
142 BigInteger datapathId, short auxiliaryId, int seed) {
143 SwitchConnectionCookieOFImpl cookie = null;
144 cookie = new SwitchConnectionCookieOFImpl();
145 cookie.setAuxiliaryId(auxiliaryId);
146 cookie.init(datapathId.intValue() + seed);
151 * @return session manager singleton instance
153 public static ConjunctSessionManager getSessionManager() {
154 return SessionManagerOFImpl.getInstance();
158 * release session manager singleton instance
160 public static void releaseSessionManager() {
161 SessionManagerOFImpl.releaseInstance();
165 * @return session manager listener Map
167 public static Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getTranslatorMap() {
168 return getSessionManager().getTranslatorMapping();
172 * @return pop listener Map
174 public static Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> getPopListenerMapping() {
175 return getSessionManager().getPopListenerMapping();
179 * @return extension converters provider
181 public static ExtensionConverterProvider getExtensionConvertorProvider() {
182 return getSessionManager().getExtensionConverterProvider();
186 * @return collection of all sessions
188 public static Collection<SessionContext> getAllSessions() {
189 return getSessionManager().getAllSessions();