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 java.util.concurrent.Callable;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Executors;
16 import java.util.concurrent.Future;
17 import java.util.concurrent.TimeUnit;
18 import java.util.concurrent.TimeoutException;
20 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
21 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
22 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
23 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
24 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
25 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
26 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
27 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
36 public class ConnectionContextImpl implements ConnectionContext {
38 private final ConnectionAdapter connectionAdapter;
39 private volatile CONNECTION_STATE connectionState;
40 private FeaturesReply featuresReply;
41 private NodeId nodeId;
42 private DeviceDisconnectedHandler deviceDisconnectedHandler;
43 private static final Logger LOG = LoggerFactory.getLogger(ConnectionContextImpl.class);
44 private OutboundQueueProvider outboundQueueProvider;
45 private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration;
46 private HandshakeContext handshakeContext;
49 * @param connectionAdapter
51 public ConnectionContextImpl(final ConnectionAdapter connectionAdapter) {
52 this.connectionAdapter = connectionAdapter;
56 public ConnectionAdapter getConnectionAdapter() {
57 return connectionAdapter;
61 public OutboundQueue getOutboundQueueProvider() {
62 return this.outboundQueueProvider;
66 public void setOutboundQueueProvider(final OutboundQueueProvider outboundQueueProvider) {
67 this.outboundQueueProvider = outboundQueueProvider;
71 public CONNECTION_STATE getConnectionState() {
72 return connectionState;
76 public NodeId getNodeId() {
81 public void setNodeId(final NodeId nodeId) {
86 public FeaturesReply getFeatures() {
91 public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
92 this.deviceDisconnectedHandler = deviceDisconnectedHandler;
96 public void setFeatures(final FeaturesReply featuresReply) {
97 this.featuresReply = featuresReply;
101 public void closeConnection(final boolean propagate) {
103 SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
105 SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
107 final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
108 LOG.debug("Actively closing connection: {}, datapathId:{}.",
109 connectionAdapter.getRemoteAddress(), datapathId);
110 connectionState = ConnectionContext.CONNECTION_STATE.RIP;
112 Future<Void> future = Executors.newSingleThreadExecutor().submit(new Callable<Void>() {
114 public Void call() throws Exception {
115 unregisterOutboundQueue();
120 LOG.debug("Waiting 1s for unregistering outbound queue.");
121 future.get(1, TimeUnit.SECONDS);
122 LOG.info("Unregistering outbound queue successful.");
123 } catch (InterruptedException e) {
124 LOG.warn("Unregistering outbound queue was interrupted for node {}", nodeId);
125 } catch (ExecutionException e) {
126 LOG.warn("Unregistering outbound queue throws exception for node {}", nodeId, e);
127 } catch (TimeoutException e) {
128 LOG.warn("Unregistering outbound queue took longer than 1 seconds for node {}", nodeId);
131 closeHandshakeContext();
133 if (getConnectionAdapter().isAlive()) {
134 getConnectionAdapter().disconnect();
138 LOG.debug("Propagating device disconnect for node {}", nodeId);
139 propagateDeviceDisconnectedEvent();
141 LOG.debug("Close connection without propagating for node {}", nodeId);
145 private void closeHandshakeContext() {
146 LOG.debug("Trying closing handshake context for node {}", nodeId);
147 if (handshakeContext != null) {
149 handshakeContext.close();
150 } catch (Exception e) {
151 LOG.error("handshake context closing failed:{} ", e);
153 handshakeContext = null;
159 public void onConnectionClosed() {
161 SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
163 SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
165 connectionState = ConnectionContext.CONNECTION_STATE.RIP;
167 final InetSocketAddress remoteAddress = connectionAdapter.getRemoteAddress();
168 final Short auxiliaryId;
169 if (null != getFeatures() && null != getFeatures().getAuxiliaryId()) {
170 auxiliaryId = getFeatures().getAuxiliaryId();
175 LOG.debug("disconnecting: node={}|auxId={}|connection state = {}",
178 getConnectionState());
180 unregisterOutboundQueue();
181 closeHandshakeContext();
182 propagateDeviceDisconnectedEvent();
185 private void propagateDeviceDisconnectedEvent() {
186 if (null != deviceDisconnectedHandler) {
187 final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
188 LOG.debug("Propagating connection closed event: {}, datapathId:{}.",
189 connectionAdapter.getRemoteAddress(), datapathId);
190 deviceDisconnectedHandler.onDeviceDisconnected(this);
195 public void setOutboundQueueHandleRegistration(OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration) {
196 this.outboundQueueHandlerRegistration = outboundQueueHandlerRegistration;
199 private void unregisterOutboundQueue() {
200 LOG.debug("Trying unregister outbound queue handler registration for node {}", nodeId);
201 if (outboundQueueHandlerRegistration != null) {
202 outboundQueueHandlerRegistration.close();
203 outboundQueueHandlerRegistration = null;
208 public synchronized void changeStateToHandshaking() {
209 connectionState = CONNECTION_STATE.HANDSHAKING;
213 public synchronized void changeStateToTimeouting() {
214 connectionState = CONNECTION_STATE.TIMEOUTING;
218 public synchronized void changeStateToWorking() {
219 connectionState = CONNECTION_STATE.WORKING;
223 public void setHandshakeContext(HandshakeContext handshakeContext) {
224 this.handshakeContext = handshakeContext;