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 com.google.common.base.Preconditions;
12 import java.math.BigInteger;
13 import java.net.InetSocketAddress;
14 import java.util.concurrent.Callable;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.Future;
18 import java.util.concurrent.TimeUnit;
19 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.DeviceInfo;
27 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
28 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
29 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
34 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
41 public class ConnectionContextImpl implements ConnectionContext {
43 private final ConnectionAdapter connectionAdapter;
44 private volatile CONNECTION_STATE connectionState;
45 private FeaturesReply featuresReply;
46 private NodeId nodeId;
47 private DeviceDisconnectedHandler deviceDisconnectedHandler;
48 private static final Logger LOG = LoggerFactory.getLogger(ConnectionContextImpl.class);
49 private OutboundQueueProvider outboundQueueProvider;
50 private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration;
51 private HandshakeContext handshakeContext;
52 private DeviceInfo deviceInfo;
55 * @param connectionAdapter
57 public ConnectionContextImpl(final ConnectionAdapter connectionAdapter) {
58 this.connectionAdapter = connectionAdapter;
62 public ConnectionAdapter getConnectionAdapter() {
63 return connectionAdapter;
67 public OutboundQueue getOutboundQueueProvider() {
68 return this.outboundQueueProvider;
72 public void setOutboundQueueProvider(final OutboundQueueProvider outboundQueueProvider) {
73 this.outboundQueueProvider = outboundQueueProvider;
77 public CONNECTION_STATE getConnectionState() {
78 return connectionState;
82 public NodeId getNodeId() {
87 public void setNodeId(final NodeId nodeId) {
92 public FeaturesReply getFeatures() {
97 public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
98 this.deviceDisconnectedHandler = deviceDisconnectedHandler;
102 public void setFeatures(final FeaturesReply featuresReply) {
103 this.featuresReply = featuresReply;
107 public void closeConnection(final boolean propagate) {
109 SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
111 SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
113 final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
114 LOG.debug("Actively closing connection: {}, datapathId:{}.",
115 connectionAdapter.getRemoteAddress(), datapathId);
116 connectionState = ConnectionContext.CONNECTION_STATE.RIP;
118 Future<Void> future = Executors.newSingleThreadExecutor().submit(new Callable<Void>() {
120 public Void call() throws Exception {
121 unregisterOutboundQueue();
126 LOG.debug("Waiting 1s for unregistering outbound queue.");
127 future.get(1, TimeUnit.SECONDS);
128 LOG.info("Unregistering outbound queue successful.");
129 } catch (InterruptedException e) {
130 LOG.warn("Unregistering outbound queue was interrupted for node {}", nodeId);
131 } catch (ExecutionException e) {
132 LOG.warn("Unregistering outbound queue throws exception for node {}", nodeId, e);
133 } catch (TimeoutException e) {
134 LOG.warn("Unregistering outbound queue took longer than 1 seconds for node {}", nodeId);
137 closeHandshakeContext();
139 if (getConnectionAdapter().isAlive()) {
140 getConnectionAdapter().disconnect();
144 LOG.debug("Propagating device disconnect for node {}", nodeId);
145 propagateDeviceDisconnectedEvent();
147 LOG.debug("Close connection without propagating for node {}", nodeId);
151 private void closeHandshakeContext() {
152 LOG.debug("Trying closing handshake context for node {}", nodeId);
153 if (handshakeContext != null) {
155 handshakeContext.close();
156 } catch (Exception e) {
157 LOG.error("handshake context closing failed:{} ", e);
159 handshakeContext = null;
165 public void onConnectionClosed() {
167 SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
169 SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
171 connectionState = ConnectionContext.CONNECTION_STATE.RIP;
173 final InetSocketAddress remoteAddress = connectionAdapter.getRemoteAddress();
174 final Short auxiliaryId;
175 if (null != getFeatures() && null != getFeatures().getAuxiliaryId()) {
176 auxiliaryId = getFeatures().getAuxiliaryId();
181 LOG.debug("disconnecting: node={}|auxId={}|connection state = {}",
184 getConnectionState());
186 unregisterOutboundQueue();
187 closeHandshakeContext();
188 propagateDeviceDisconnectedEvent();
191 private void propagateDeviceDisconnectedEvent() {
192 if (null != deviceDisconnectedHandler) {
193 final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
194 LOG.debug("Propagating connection closed event: {}, datapathId:{}.",
195 connectionAdapter.getRemoteAddress(), datapathId);
196 deviceDisconnectedHandler.onDeviceDisconnected(this);
201 public void setOutboundQueueHandleRegistration(OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration) {
202 this.outboundQueueHandlerRegistration = outboundQueueHandlerRegistration;
205 private void unregisterOutboundQueue() {
206 LOG.debug("Trying unregister outbound queue handler registration for node {}", nodeId);
207 if (outboundQueueHandlerRegistration != null) {
208 outboundQueueHandlerRegistration.close();
209 outboundQueueHandlerRegistration = null;
214 public synchronized void changeStateToHandshaking() {
215 connectionState = CONNECTION_STATE.HANDSHAKING;
219 public synchronized void changeStateToTimeouting() {
220 connectionState = CONNECTION_STATE.TIMEOUTING;
224 public synchronized void changeStateToWorking() {
225 connectionState = CONNECTION_STATE.WORKING;
229 public DeviceInfo getDeviceInfo() {
230 return this.deviceInfo;
234 public void handshakeSuccessful() {
235 Preconditions.checkNotNull(nodeId, "Cannot create DeviceInfo if 'NodeId' is not set!");
236 Preconditions.checkNotNull(featuresReply, "Cannot create DeviceInfo if 'features' is not set!");
237 this.deviceInfo = new DeviceInfoImpl(
239 DeviceStateUtil.createNodeInstanceIdentifier(nodeId),
240 featuresReply.getVersion(),
241 featuresReply.getDatapathId());
245 public void setHandshakeContext(HandshakeContext handshakeContext) {
246 this.handshakeContext = handshakeContext;
250 private class DeviceInfoImpl implements DeviceInfo {
252 final private NodeId nodeId;
253 final private KeyedInstanceIdentifier<Node, NodeKey> nodeII;
254 final private Short version;
255 final private BigInteger datapathId;
259 final KeyedInstanceIdentifier<Node, NodeKey> nodeII,
261 final BigInteger datapathId) {
262 this.nodeId = nodeId;
263 this.nodeII = nodeII;
264 this.version = version;
265 this.datapathId = datapathId;
269 public NodeId getNodeId() {
274 public KeyedInstanceIdentifier<Node, NodeKey> getNodeInstanceIdentifier() {
279 public Short getVersion() {
284 public BigInteger getDatapathId() {
289 public boolean equals(Object o) {
290 if (this == o) return true;
291 if (o == null || getClass() != o.getClass()) return false;
293 DeviceInfoImpl that = (DeviceInfoImpl) o;
295 if (!nodeId.equals(that.nodeId)) return false;
296 if (!nodeII.equals(that.nodeII)) return false;
297 if (!version.equals(that.version)) return false;
298 return datapathId.equals(that.datapathId);
303 public int hashCode() {
304 int result = nodeId.hashCode();
305 result = 31 * result + nodeII.hashCode();
306 result = 31 * result + version.hashCode();
307 result = 31 * result + datapathId.hashCode();