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
9 package org.opendaylight.openflowplugin.impl.connection;
11 import java.math.BigInteger;
12 import java.net.InetSocketAddress;
13 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
14 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
15 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
16 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
17 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
18 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
19 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
20 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
29 public class ConnectionContextImpl implements ConnectionContext {
31 private final ConnectionAdapter connectionAdapter;
32 private CONNECTION_STATE connectionState;
33 private FeaturesReply featuresReply;
34 private NodeId nodeId;
35 private DeviceDisconnectedHandler deviceDisconnectedHandler;
36 private static final Logger LOG = LoggerFactory.getLogger(ConnectionContextImpl.class);
37 private OutboundQueueProvider outboundQueueProvider;
38 private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration;
39 private HandshakeContext handshakeContext;
42 * @param connectionAdapter
44 public ConnectionContextImpl(final ConnectionAdapter connectionAdapter) {
45 this.connectionAdapter = connectionAdapter;
49 public ConnectionAdapter getConnectionAdapter() {
50 return connectionAdapter;
54 public OutboundQueue getOutboundQueueProvider() {
55 return this.outboundQueueProvider;
59 public void setOutboundQueueProvider(final OutboundQueueProvider outboundQueueProvider) {
60 this.outboundQueueProvider = outboundQueueProvider;
64 public CONNECTION_STATE getConnectionState() {
65 return connectionState;
69 public NodeId getNodeId() {
74 public void setNodeId(final NodeId nodeId) {
79 public FeaturesReply getFeatures() {
84 public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
85 this.deviceDisconnectedHandler = deviceDisconnectedHandler;
89 public void setFeatures(final FeaturesReply featuresReply) {
90 this.featuresReply = featuresReply;
94 public void closeConnection(boolean propagate) {
96 SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
98 SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
100 final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
101 LOG.debug("Actively closing connection: {}, datapathId:{}.",
102 connectionAdapter.getRemoteAddress(), datapathId);
103 connectionState = ConnectionContext.CONNECTION_STATE.RIP;
105 unregisterOutboundQueue();
106 closeHandshakeContext();
108 if (getConnectionAdapter().isAlive()) {
109 getConnectionAdapter().disconnect();
113 propagateDeviceDisconnectedEvent();
117 private void closeHandshakeContext() {
118 if (handshakeContext != null) {
120 handshakeContext.close();
121 } catch (Exception e) {
122 LOG.info("handshake context closing failed: ", e);
124 handshakeContext = null;
130 public void onConnectionClosed() {
132 SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
134 SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
136 connectionState = ConnectionContext.CONNECTION_STATE.RIP;
138 final InetSocketAddress remoteAddress = connectionAdapter.getRemoteAddress();
139 final Short auxiliaryId;
140 if (null != getFeatures() && null != getFeatures().getAuxiliaryId()) {
141 auxiliaryId = getFeatures().getAuxiliaryId();
146 LOG.debug("disconnecting: node={}|auxId={}|connection state = {}",
149 getConnectionState());
151 unregisterOutboundQueue();
152 closeHandshakeContext();
153 propagateDeviceDisconnectedEvent();
156 private void propagateDeviceDisconnectedEvent() {
157 if (null != deviceDisconnectedHandler) {
158 final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
159 LOG.debug("Propagating connection closed event: {}, datapathId:{}.",
160 connectionAdapter.getRemoteAddress(), datapathId);
161 deviceDisconnectedHandler.onDeviceDisconnected(this);
166 public void setOutboundQueueHandleRegistration(OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration) {
167 this.outboundQueueHandlerRegistration = outboundQueueHandlerRegistration;
170 private void unregisterOutboundQueue() {
171 if (outboundQueueHandlerRegistration != null) {
172 outboundQueueHandlerRegistration.close();
173 outboundQueueHandlerRegistration = null;
178 public void changeStateToHandshaking() {
179 connectionState = CONNECTION_STATE.HANDSHAKING;
183 public void changeStateToTimeouting() {
184 connectionState = CONNECTION_STATE.TIMEOUTING;
188 public void changeStateToWorking() {
189 connectionState = CONNECTION_STATE.WORKING;
193 public void setHandshakeContext(HandshakeContext handshakeContext) {
194 this.handshakeContext = handshakeContext;