2 * Copyright (c) 2015 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
8 package org.opendaylight.openflowplugin.impl.connection;
10 import java.math.BigInteger;
11 import java.net.InetSocketAddress;
12 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
13 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
14 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
15 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
16 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
17 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
18 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
19 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
28 public class ConnectionContextImpl implements ConnectionContext {
30 private final ConnectionAdapter connectionAdapter;
31 private CONNECTION_STATE connectionState;
32 private FeaturesReply featuresReply;
33 private NodeId nodeId;
34 private DeviceDisconnectedHandler deviceDisconnectedHandler;
35 private static final Logger LOG = LoggerFactory.getLogger(ConnectionContextImpl.class);
36 private OutboundQueueProvider outboundQueueProvider;
37 private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration;
38 private HandshakeContext handshakeContext;
41 * @param connectionAdapter
43 public ConnectionContextImpl(final ConnectionAdapter connectionAdapter) {
44 this.connectionAdapter = connectionAdapter;
48 public ConnectionAdapter getConnectionAdapter() {
49 return connectionAdapter;
53 public OutboundQueue getOutboundQueueProvider() {
54 return this.outboundQueueProvider;
58 public void setOutboundQueueProvider(final OutboundQueueProvider outboundQueueProvider) {
59 this.outboundQueueProvider = outboundQueueProvider;
63 public CONNECTION_STATE getConnectionState() {
64 return connectionState;
68 public NodeId getNodeId() {
73 public void setNodeId(final NodeId nodeId) {
78 public FeaturesReply getFeatures() {
83 public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
84 this.deviceDisconnectedHandler = deviceDisconnectedHandler;
88 public void setFeatures(final FeaturesReply featuresReply) {
89 this.featuresReply = featuresReply;
93 public void closeConnection(boolean propagate) {
95 SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
97 SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
99 final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
100 LOG.debug("Actively closing connection: {}, datapathId:{}.",
101 connectionAdapter.getRemoteAddress(), datapathId);
102 connectionState = ConnectionContext.CONNECTION_STATE.RIP;
104 unregisterOutboundQueue();
105 closeHandshakeContext();
107 if (getConnectionAdapter().isAlive()) {
108 getConnectionAdapter().disconnect();
112 propagateDeviceDisconnectedEvent();
116 private void closeHandshakeContext() {
117 if (handshakeContext != null) {
119 handshakeContext.close();
120 } catch (Exception e) {
121 LOG.info("handshake context closing failed: ", e);
123 handshakeContext = null;
129 public void onConnectionClosed() {
131 SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
133 SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
135 connectionState = ConnectionContext.CONNECTION_STATE.RIP;
137 final InetSocketAddress remoteAddress = connectionAdapter.getRemoteAddress();
138 final Short auxiliaryId;
139 if (null != getFeatures() && null != getFeatures().getAuxiliaryId()) {
140 auxiliaryId = getFeatures().getAuxiliaryId();
145 LOG.debug("disconnecting: node={}|auxId={}|connection state = {}",
148 getConnectionState());
150 unregisterOutboundQueue();
151 closeHandshakeContext();
152 propagateDeviceDisconnectedEvent();
155 private void propagateDeviceDisconnectedEvent() {
156 if (null != deviceDisconnectedHandler) {
157 final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
158 LOG.debug("Propagating connection closed event: {}, datapathId:{}.",
159 connectionAdapter.getRemoteAddress(), datapathId);
160 deviceDisconnectedHandler.onDeviceDisconnected(this);
165 public void setOutboundQueueHandleRegistration(OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration) {
166 this.outboundQueueHandlerRegistration = outboundQueueHandlerRegistration;
169 private void unregisterOutboundQueue() {
170 if (outboundQueueHandlerRegistration != null) {
171 outboundQueueHandlerRegistration.close();
172 outboundQueueHandlerRegistration = null;
177 public void changeStateToHandshaking() {
178 connectionState = CONNECTION_STATE.HANDSHAKING;
182 public void changeStateToTimeouting() {
183 connectionState = CONNECTION_STATE.TIMEOUTING;
187 public void changeStateToWorking() {
188 connectionState = CONNECTION_STATE.WORKING;
192 public void setHandshakeContext(HandshakeContext handshakeContext) {
193 this.handshakeContext = handshakeContext;