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.Objects;
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Executors;
18 import java.util.concurrent.Future;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.TimeoutException;
21 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
22 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
23 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
24 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
25 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
26 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
27 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
29 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
30 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
31 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
36 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
43 public class ConnectionContextImpl implements ConnectionContext {
45 private final ConnectionAdapter connectionAdapter;
46 private volatile CONNECTION_STATE connectionState;
47 private FeaturesReply featuresReply;
48 private NodeId nodeId;
49 private DeviceDisconnectedHandler deviceDisconnectedHandler;
50 private static final Logger LOG = LoggerFactory.getLogger(ConnectionContextImpl.class);
51 private OutboundQueueProvider outboundQueueProvider;
52 private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration;
53 private HandshakeContext handshakeContext;
54 private DeviceInfo deviceInfo;
57 * @param connectionAdapter
59 public ConnectionContextImpl(final ConnectionAdapter connectionAdapter) {
60 this.connectionAdapter = connectionAdapter;
64 public ConnectionAdapter getConnectionAdapter() {
65 return connectionAdapter;
69 public OutboundQueue getOutboundQueueProvider() {
70 return this.outboundQueueProvider;
74 public void setOutboundQueueProvider(final OutboundQueueProvider outboundQueueProvider) {
75 this.outboundQueueProvider = outboundQueueProvider;
76 ((DeviceInfoImpl)this.deviceInfo).setOutboundQueueProvider(this.outboundQueueProvider);
80 public CONNECTION_STATE getConnectionState() {
81 return connectionState;
85 public NodeId getNodeId() {
90 public void setNodeId(final NodeId nodeId) {
95 public FeaturesReply getFeatures() {
100 public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
101 this.deviceDisconnectedHandler = deviceDisconnectedHandler;
105 public void setFeatures(final FeaturesReply featuresReply) {
106 this.featuresReply = featuresReply;
110 public void closeConnection(final boolean propagate) {
112 SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
114 SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
116 final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
117 LOG.debug("Actively closing connection: {}, datapathId: {}",
118 connectionAdapter.getRemoteAddress(), datapathId);
119 connectionState = ConnectionContext.CONNECTION_STATE.RIP;
121 Future<Void> future = Executors.newSingleThreadExecutor().submit(new Callable<Void>() {
123 public Void call() throws Exception {
124 unregisterOutboundQueue();
129 future.get(1, TimeUnit.SECONDS);
130 LOG.info("Unregister outbound queue successful.");
131 } catch (InterruptedException | TimeoutException | ExecutionException e) {
132 LOG.warn("Unregister outbound queue throws exception for node {} ", getSafeNodeIdForLOG());
133 LOG.trace("Unregister outbound queue throws exception for node {} ", getSafeNodeIdForLOG(), e);
136 closeHandshakeContext();
138 if (getConnectionAdapter().isAlive()) {
139 getConnectionAdapter().disconnect();
143 LOG.debug("Propagating device disconnect for node {}", getSafeNodeIdForLOG());
144 propagateDeviceDisconnectedEvent();
146 LOG.debug("Close connection without propagating for node {}", getSafeNodeIdForLOG());
150 private void closeHandshakeContext() {
151 LOG.debug("Trying closing handshake context for node {}", getSafeNodeIdForLOG());
152 if (handshakeContext != null) {
154 handshakeContext.close();
155 } catch (Exception e) {
156 LOG.error("handshake context closing failed:{} ", e);
158 handshakeContext = null;
164 public void onConnectionClosed() {
166 SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
168 SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
170 connectionState = ConnectionContext.CONNECTION_STATE.RIP;
172 final InetSocketAddress remoteAddress = connectionAdapter.getRemoteAddress();
173 final Short auxiliaryId;
174 if (null != getFeatures() && null != getFeatures().getAuxiliaryId()) {
175 auxiliaryId = getFeatures().getAuxiliaryId();
180 LOG.debug("disconnecting: node={}|auxId={}|connection state = {}",
183 getConnectionState());
185 unregisterOutboundQueue();
186 closeHandshakeContext();
187 propagateDeviceDisconnectedEvent();
190 private void propagateDeviceDisconnectedEvent() {
191 if (null != deviceDisconnectedHandler) {
192 final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
193 LOG.debug("Propagating connection closed event: {}, datapathId:{}.",
194 connectionAdapter.getRemoteAddress(), datapathId);
195 deviceDisconnectedHandler.onDeviceDisconnected(this);
200 * This method returns safe nodeId for logging
201 * @return string value od nodeId or string "null"
203 private String getSafeNodeIdForLOG() {
204 return null == nodeId ? "null" : nodeId.getValue();
208 public void setOutboundQueueHandleRegistration(OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration) {
209 this.outboundQueueHandlerRegistration = outboundQueueHandlerRegistration;
212 private void unregisterOutboundQueue() {
213 LOG.debug("Trying unregister outbound queue handler registration for node {}", nodeId);
214 if (outboundQueueHandlerRegistration != null) {
215 outboundQueueHandlerRegistration.close();
216 outboundQueueHandlerRegistration = null;
221 public synchronized void changeStateToHandshaking() {
222 connectionState = CONNECTION_STATE.HANDSHAKING;
226 public synchronized void changeStateToTimeouting() {
227 connectionState = CONNECTION_STATE.TIMEOUTING;
231 public synchronized void changeStateToWorking() {
232 connectionState = CONNECTION_STATE.WORKING;
236 public DeviceInfo getDeviceInfo() {
237 return this.deviceInfo;
241 public void handshakeSuccessful() {
242 Preconditions.checkNotNull(nodeId, "Cannot create DeviceInfo if 'NodeId' is not set!");
243 Preconditions.checkNotNull(featuresReply, "Cannot create DeviceInfo if 'features' is not set!");
244 this.deviceInfo = new DeviceInfoImpl(
246 DeviceStateUtil.createNodeInstanceIdentifier(nodeId),
247 featuresReply.getVersion(),
248 featuresReply.getDatapathId(),
249 outboundQueueProvider);
253 public void setHandshakeContext(HandshakeContext handshakeContext) {
254 this.handshakeContext = handshakeContext;
258 public boolean equals(Object o) {
263 if (o == null || getClass() != o.getClass()) {
267 ConnectionContextImpl that = (ConnectionContextImpl) o;
269 if (!connectionAdapter.equals(that.connectionAdapter)) {
273 if (featuresReply != null ? !featuresReply.equals(that.featuresReply) : that.featuresReply != null) {
277 return nodeId != null ? nodeId.equals(that.nodeId) : that.nodeId == null;
282 public int hashCode() {
283 int result = connectionAdapter.hashCode();
284 result = 31 * result + (featuresReply != null ? featuresReply.hashCode() : 0);
285 result = 31 * result + (nodeId != null ? nodeId.hashCode() : 0);
289 private class DeviceInfoImpl implements DeviceInfo {
291 private final NodeId nodeId;
292 private final KeyedInstanceIdentifier<Node, NodeKey> nodeII;
293 private final Short version;
294 private final BigInteger datapathId;
295 private final ServiceGroupIdentifier serviceGroupIdentifier;
296 private OutboundQueue outboundQueueProvider;
300 final KeyedInstanceIdentifier<Node, NodeKey> nodeII,
302 final BigInteger datapathId,
303 final OutboundQueue outboundQueueProvider) {
304 this.nodeId = nodeId;
305 this.nodeII = nodeII;
306 this.version = version;
307 this.datapathId = datapathId;
308 this.outboundQueueProvider = outboundQueueProvider;
309 this.serviceGroupIdentifier = ServiceGroupIdentifier.create(this.nodeId.getValue());
313 public NodeId getNodeId() {
318 public KeyedInstanceIdentifier<Node, NodeKey> getNodeInstanceIdentifier() {
323 public Short getVersion() {
328 public BigInteger getDatapathId() {
333 public ServiceGroupIdentifier getServiceIdentifier() {
334 return this.serviceGroupIdentifier;
338 public boolean equals(Object o) {
343 if (o == null || getClass() != o.getClass()) {
347 DeviceInfoImpl that = (DeviceInfoImpl) o;
349 return (nodeId.equals(that.nodeId) &&
350 nodeII.equals(that.nodeII) &&
351 version.equals(that.version) &&
352 datapathId.equals(that.datapathId));
357 public int hashCode() {
358 int result = nodeId.hashCode();
359 result = 31 * result + nodeII.hashCode();
360 result = 31 * result + version.hashCode();
361 result = 31 * result + datapathId.hashCode();
365 public void setOutboundQueueProvider(final OutboundQueue outboundQueueProvider) {
366 this.outboundQueueProvider = outboundQueueProvider;
370 public Long reserveXidForDeviceMessage() {
371 return outboundQueueProvider.reserveEntry();