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.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;
31 public abstract class OFSessionUtil {
33 private static final Logger LOG = LoggerFactory
34 .getLogger(OFSessionUtil.class);
37 * @param connectionConductor
41 public static void registerSession(ConnectionConductor connectionConductor,
42 GetFeaturesOutput features, short version) {
43 SwitchConnectionDistinguisher sessionKey = createSwitchSessionKey(features
45 SessionContext sessionContext = getSessionManager().getSessionContext(sessionKey);
46 if (LOG.isDebugEnabled()) {
47 LOG.debug("registering sessionKey: {}", Arrays.toString(sessionKey.getId()));
50 if (features.getAuxiliaryId() == null || features.getAuxiliaryId() == 0) {
52 if (sessionContext != null) {
53 LOG.warn("duplicate datapathId occured while registering new switch session: "
54 + dumpDataPathId(features.getDatapathId()));
55 getSessionManager().invalidateSessionContext(sessionKey);
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);
67 if (sessionContext == null) {
68 throw new IllegalStateException("unexpected auxiliary connection - primary connection missing: "
69 + dumpDataPathId(features.getDatapathId()));
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())
77 + features.getAuxiliaryId());
78 getSessionManager().invalidateAuxiliary(sessionKey,
82 sessionContext.addAuxiliaryConductor(auxiliaryKey,
84 connectionConductor.setSessionContext(sessionContext);
85 connectionConductor.setConnectionCookie(auxiliaryKey);
89 // check registration result
90 SessionContext resulContext = getSessionManager().getSessionContext(sessionKey);
91 if (resulContext == null) {
92 throw new IllegalStateException("session context registration failed");
94 if (!resulContext.isValid()) {
95 throw new IllegalStateException("registered session context is invalid");
102 * @return readable version of datapathId (hex)
104 public static String dumpDataPathId(BigInteger datapathId) {
105 return datapathId.toString(16);
110 * @return new session key
112 public static SwitchConnectionDistinguisher createSwitchSessionKey(
113 BigInteger datapathId) {
114 SwitchSessionKeyOFImpl key = new SwitchSessionKeyOFImpl();
115 key.setDatapathId(datapathId);
122 * @return connection cookie key
123 * @see #createConnectionCookie(BigInteger, short)
125 public static SwitchConnectionDistinguisher createConnectionCookie(
126 GetFeaturesOutput features) {
127 return createConnectionCookie(features.getDatapathId(),
128 features.getAuxiliaryId());
134 * @return connection cookie key
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);
149 * @return session manager singleton instance
151 public static SessionManager getSessionManager() {
152 return SessionManagerOFImpl.getInstance();
156 * release session manager singleton instance
158 public static void releaseSessionManager() {
159 SessionManagerOFImpl.releaseInstance();
163 * @return session manager listener Map
165 public static Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getTranslatorMap() {
166 return getSessionManager().getTranslatorMapping();
170 * @return pop listener Map
172 public static Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> getPopListenerMapping() {
173 return getSessionManager().getPopListenerMapping();