763804ee033961688d44af7f804a35789a19e52a
[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.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;
30
31 /**
32  * @author mirehak
33  */
34 public abstract class OFSessionUtil {
35
36     private static final Logger LOG = LoggerFactory
37             .getLogger(OFSessionUtil.class);
38
39     /**
40      * @param connectionConductor
41      * @param features
42      * @param version
43      */
44     public static void registerSession(ConnectionConductorImpl connectionConductor,
45             GetFeaturesOutput features, short version) {
46         SwitchSessionKeyOF sessionKey = createSwitchSessionKey(features
47                 .getDatapathId());
48         SessionContext sessionContext = getSessionManager().getSessionContext(sessionKey);
49         if (LOG.isDebugEnabled()) {
50             LOG.debug("registering sessionKey: {}", Arrays.toString(sessionKey.getId()));
51         }
52
53         if (features.getAuxiliaryId() == null || features.getAuxiliaryId() == 0) {
54             // handle primary
55             if (sessionContext != null) {
56                 LOG.warn("duplicate datapathId occured while registering new switch session: "
57                         + dumpDataPathId(features.getDatapathId()));
58                 getSessionManager().invalidateSessionContext(sessionKey);
59             }
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);
69         } else {
70             // handle auxiliary
71             if (sessionContext == null) {
72                 throw new IllegalStateException("unexpected auxiliary connection - primary connection missing: "
73                         + dumpDataPathId(features.getDatapathId()));
74             } else {
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())
80                             + " | "
81                             + features.getAuxiliaryId());
82                     getSessionManager().invalidateAuxiliary(sessionKey,
83                             auxiliaryKey);
84                 }
85
86                 sessionContext.addAuxiliaryConductor(auxiliaryKey,
87                         connectionConductor);
88                 connectionConductor.setSessionContext(sessionContext);
89                 connectionConductor.setConnectionCookie(auxiliaryKey);
90             }
91         }
92
93         // check registration result
94         SessionContext resulContext = getSessionManager().getSessionContext(sessionKey);
95         if (resulContext == null) {
96             throw new IllegalStateException("session context registration failed");
97         } else {
98             if (!resulContext.isValid()) {
99                 throw new IllegalStateException("registered session context is invalid");
100             }
101         }
102     }
103
104     /**
105      * @param datapathId
106      * @return readable version of datapathId (hex)
107      */
108     public static String dumpDataPathId(BigInteger datapathId) {
109         return datapathId.toString(16);
110     }
111
112     /**
113      * @param datapathId
114      * @return new session key
115      */
116     public static SwitchSessionKeyOF createSwitchSessionKey(
117             BigInteger datapathId) {
118         SwitchSessionKeyOF key = new SwitchSessionKeyOF();
119         key.setDatapathId(datapathId);
120         return key;
121     }
122
123     /**
124      * @param features
125      * @param seed 
126      * @return connection cookie key
127      * @see #createConnectionCookie(BigInteger,short, int)
128      */
129     public static SwitchConnectionDistinguisher createConnectionCookie(
130             GetFeaturesOutput features, int seed) {
131         return createConnectionCookie(features.getDatapathId(),
132                 features.getAuxiliaryId(), seed);
133     }
134
135     /**
136      * @param datapathId
137      * @param auxiliaryId
138      * @param seed 
139      * @return connection cookie key
140      */
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);
147         return cookie;
148     }
149
150     /**
151      * @return session manager singleton instance
152      */
153     public static ConjunctSessionManager getSessionManager() {
154         return SessionManagerOFImpl.getInstance();
155     }
156     
157     /**
158      * release session manager singleton instance
159      */
160     public static void releaseSessionManager() {
161         SessionManagerOFImpl.releaseInstance();
162     }
163     
164     /**
165     * @return session manager listener Map
166     */
167     public static Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, List<DataObject>>>> getTranslatorMap() {
168         return getSessionManager().getTranslatorMapping();
169     }
170
171     /**
172      * @return pop listener Map
173      */
174     public static Map<Class<? extends DataObject>, Collection<PopListener<DataObject>>> getPopListenerMapping() {
175         return getSessionManager().getPopListenerMapping();
176     }
177
178     /**
179      * @return extension converters provider
180      */
181     public static ExtensionConverterProvider getExtensionConvertorProvider() {
182         return getSessionManager().getExtensionConverterProvider();
183     }
184
185 }