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.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;
32 public abstract class OFSessionUtil {
34 private static final Logger LOG = LoggerFactory
35 .getLogger(OFSessionUtil.class);
38 * @param connectionConductor
42 public static void registerSession(ConnectionConductor connectionConductor,
43 GetFeaturesOutput features, short version) {
44 SwitchSessionKeyOF sessionKey = createSwitchSessionKey(features
46 SessionContext sessionContext = getSessionManager().getSessionContext(sessionKey);
47 if (LOG.isDebugEnabled()) {
48 LOG.debug("registering sessionKey: {}", Arrays.toString(sessionKey.getId()));
51 if (features.getAuxiliaryId() == null || features.getAuxiliaryId() == 0) {
53 if (sessionContext != null) {
54 LOG.warn("duplicate datapathId occured while registering new switch session: "
55 + dumpDataPathId(features.getDatapathId()));
56 getSessionManager().invalidateSessionContext(sessionKey);
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);
68 if (sessionContext == null) {
69 throw new IllegalStateException("unexpected auxiliary connection - primary connection missing: "
70 + dumpDataPathId(features.getDatapathId()));
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())
78 + features.getAuxiliaryId());
79 getSessionManager().invalidateAuxiliary(sessionKey,
83 sessionContext.addAuxiliaryConductor(auxiliaryKey,
85 connectionConductor.setSessionContext(sessionContext);
86 connectionConductor.setConnectionCookie(auxiliaryKey);
90 // check registration result
91 SessionContext resulContext = getSessionManager().getSessionContext(sessionKey);
92 if (resulContext == null) {
93 throw new IllegalStateException("session context registration failed");
95 if (!resulContext.isValid()) {
96 throw new IllegalStateException("registered session context is invalid");
103 * @return readable version of datapathId (hex)
105 public static String dumpDataPathId(BigInteger datapathId) {
106 return datapathId.toString(16);
111 * @return new session key
113 public static SwitchSessionKeyOF createSwitchSessionKey(
114 BigInteger datapathId) {
115 SwitchSessionKeyOF key = new SwitchSessionKeyOF();
116 key.setDatapathId(datapathId);
123 * @return connection cookie key
124 * @see #createConnectionCookie(BigInteger,short, int)
126 public static SwitchConnectionDistinguisher createConnectionCookie(
127 GetFeaturesOutput features, int seed) {
128 return createConnectionCookie(features.getDatapathId(),
129 features.getAuxiliaryId(), seed);
136 * @return connection cookie key
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);
148 * @return session manager singleton instance
150 public static SessionManager getSessionManager() {
151 return SessionManagerOFImpl.getInstance();
155 * release session manager singleton instance
157 public static void releaseSessionManager() {
158 SessionManagerOFImpl.releaseInstance();
162 * @return session manager listener Map
164 public static Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getTranslatorMap() {
165 return getSessionManager().getTranslatorMapping();
169 * @return pop listener Map
171 public static Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> getPopListenerMapping() {
172 return getSessionManager().getPopListenerMapping();
176 * @return extension converters provider
178 public static ExtensionConverterProvider getExtensionConvertorProvider() {
179 return getSessionManager().getExtensionConverterProvider();