Merge "BUG-4117: add support for meter and group old Notif"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / ConnectionManagerImpl.java
1 /**
2  * Copyright (c) 2015 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.impl.connection;
10
11 import java.net.InetAddress;
12 import java.util.concurrent.LinkedBlockingQueue;
13 import java.util.concurrent.TimeUnit;
14 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
15 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
16 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
17 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionManager;
18 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
20 import org.opendaylight.openflowplugin.api.openflow.md.core.ConnectionConductor;
21 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeListener;
22 import org.opendaylight.openflowplugin.api.openflow.md.core.HandshakeManager;
23 import org.opendaylight.openflowplugin.impl.connection.listener.ConnectionReadyListenerImpl;
24 import org.opendaylight.openflowplugin.impl.connection.listener.HandshakeListenerImpl;
25 import org.opendaylight.openflowplugin.impl.connection.listener.OpenflowProtocolListenerInitialImpl;
26 import org.opendaylight.openflowplugin.impl.connection.listener.SystemNotificationsListenerImpl;
27 import org.opendaylight.openflowplugin.openflow.md.core.ErrorHandlerSimpleImpl;
28 import org.opendaylight.openflowplugin.openflow.md.core.HandshakeManagerImpl;
29 import org.opendaylight.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  *
37  */
38 public class ConnectionManagerImpl implements ConnectionManager {
39
40     private static final Logger LOG = LoggerFactory.getLogger(ConnectionManagerImpl.class);
41     private static final int HELLO_LIMIT = 20;
42     private final boolean bitmapNegotiationEnabled = true;
43     private DeviceConnectedHandler deviceConnectedHandler;
44     private final long echoReplyTimeout;
45
46     public ConnectionManagerImpl(long echoReplyTimeout) {
47         this.echoReplyTimeout = echoReplyTimeout;
48     }
49
50
51     @Override
52     public void onSwitchConnected(final ConnectionAdapter connectionAdapter) {
53
54         LOG.trace("preparing handshake: {}", connectionAdapter.getRemoteAddress());
55
56         final int handshakeThreadLimit = 1;
57         final ThreadPoolLoggingExecutor handshakePool = createHandshakePool(
58                 connectionAdapter.getRemoteAddress().toString(), handshakeThreadLimit);
59
60         LOG.trace("prepare connection context");
61         final ConnectionContext connectionContext = new ConnectionContextImpl(connectionAdapter);
62
63         HandshakeListener handshakeListener = new HandshakeListenerImpl(connectionContext, deviceConnectedHandler);
64         final HandshakeManager handshakeManager = createHandshakeManager(connectionAdapter, handshakeListener);
65
66         LOG.trace("prepare handshake context");
67         HandshakeContext handshakeContext = new HandshakeContextImpl(handshakePool, handshakeManager);
68         handshakeListener.setHandshakeContext(handshakeContext);
69         connectionContext.setHandshakeContext(handshakeContext);
70
71         LOG.trace("prepare connection listeners");
72         final ConnectionReadyListener connectionReadyListener = new ConnectionReadyListenerImpl(
73                 connectionContext, handshakeContext);
74         connectionAdapter.setConnectionReadyListener(connectionReadyListener);
75
76         final OpenflowProtocolListener ofMessageListener =
77                 new OpenflowProtocolListenerInitialImpl(connectionContext, handshakeContext);
78         connectionAdapter.setMessageListener(ofMessageListener);
79
80         final SystemNotificationsListener systemListener = new SystemNotificationsListenerImpl(connectionContext, echoReplyTimeout);
81         connectionAdapter.setSystemListener(systemListener);
82
83         LOG.trace("connection ballet finished");
84     }
85
86     /**
87      * @param connectionIdentifier
88      * @param handshakeThreadLimit
89      * @return
90      */
91     private static ThreadPoolLoggingExecutor createHandshakePool(
92             final String connectionIdentifier, final int handshakeThreadLimit) {
93         return new ThreadPoolLoggingExecutor(handshakeThreadLimit,
94                 handshakeThreadLimit, 0L, TimeUnit.MILLISECONDS,
95                 new LinkedBlockingQueue<>(HELLO_LIMIT), "OFHandshake-" + connectionIdentifier);
96     }
97
98     /**
99      * @param connectionAdapter
100      * @param handshakeListener
101      * @return
102      */
103     private HandshakeManager createHandshakeManager(final ConnectionAdapter connectionAdapter,
104                                                     final HandshakeListener handshakeListener) {
105         HandshakeManagerImpl handshakeManager = new HandshakeManagerImpl(connectionAdapter,
106                 ConnectionConductor.versionOrder.get(0),
107                 ConnectionConductor.versionOrder);
108         handshakeManager.setUseVersionBitmap(isBitmapNegotiationEnabled());
109         handshakeManager.setHandshakeListener(handshakeListener);
110         handshakeManager.setErrorHandler(new ErrorHandlerSimpleImpl());
111
112         return handshakeManager;
113     }
114
115     /**
116      * @return parameter dedicated to hello message content
117      */
118     public boolean isBitmapNegotiationEnabled() {
119         return bitmapNegotiationEnabled;
120     }
121
122
123     @Override
124     public boolean accept(final InetAddress switchAddress) {
125         // TODO add connection accept logic based on address
126         return true;
127     }
128
129     @Override
130     public void setDeviceConnectedHandler(final DeviceConnectedHandler deviceConnectedHandler) {
131         this.deviceConnectedHandler = deviceConnectedHandler;
132     }
133 }