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 org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
16 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
17 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
18 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration;
19 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
20 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
21 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
23 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
24 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.SessionStatistics;
25 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
30 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
37 public class ConnectionContextImpl implements ConnectionContext {
39 private final ConnectionAdapter connectionAdapter;
40 private volatile CONNECTION_STATE connectionState;
41 private FeaturesReply featuresReply;
42 private NodeId nodeId;
43 private DeviceDisconnectedHandler deviceDisconnectedHandler;
44 private static final Logger LOG = LoggerFactory.getLogger(ConnectionContextImpl.class);
45 private OutboundQueueProvider outboundQueueProvider;
46 private OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration;
47 private HandshakeContext handshakeContext;
48 private DeviceInfo deviceInfo;
51 * @param connectionAdapter
53 public ConnectionContextImpl(final ConnectionAdapter connectionAdapter) {
54 this.connectionAdapter = connectionAdapter;
58 public ConnectionAdapter getConnectionAdapter() {
59 return connectionAdapter;
63 public OutboundQueue getOutboundQueueProvider() {
64 return this.outboundQueueProvider;
68 public void setOutboundQueueProvider(final OutboundQueueProvider outboundQueueProvider) {
69 this.outboundQueueProvider = outboundQueueProvider;
70 ((DeviceInfoImpl)this.deviceInfo).setOutboundQueueProvider(this.outboundQueueProvider);
74 public CONNECTION_STATE getConnectionState() {
75 return connectionState;
79 public NodeId getNodeId() {
84 public void setNodeId(final NodeId nodeId) {
89 public FeaturesReply getFeatures() {
94 public void setDeviceDisconnectedHandler(final DeviceDisconnectedHandler deviceDisconnectedHandler) {
95 this.deviceDisconnectedHandler = deviceDisconnectedHandler;
99 public void setFeatures(final FeaturesReply featuresReply) {
100 this.featuresReply = featuresReply;
104 public void closeConnection(final boolean propagate) {
105 if (Objects.isNull(nodeId)){
106 SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
108 SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_OFP);
110 final BigInteger datapathId = Objects.nonNull(featuresReply) ? featuresReply.getDatapathId() : BigInteger.ZERO;
111 if (LOG.isDebugEnabled()) {
112 LOG.debug("Actively closing connection: {}, datapathId: {}",
113 connectionAdapter.getRemoteAddress(), datapathId);
115 connectionState = ConnectionContext.CONNECTION_STATE.RIP;
117 unregisterOutboundQueue();
118 closeHandshakeContext();
120 if (getConnectionAdapter().isAlive()) {
121 getConnectionAdapter().disconnect();
125 propagateDeviceDisconnectedEvent();
129 private void closeHandshakeContext() {
130 LOG.debug("Trying closing handshake context for node {}", getSafeNodeIdForLOG());
131 if (handshakeContext != null) {
133 handshakeContext.close();
134 } catch (Exception e) {
135 LOG.error("handshake context closing failed:{} ", e);
137 handshakeContext = null;
143 public void onConnectionClosed() {
145 connectionState = ConnectionContext.CONNECTION_STATE.RIP;
148 SessionStatistics.countEvent(connectionAdapter.getRemoteAddress().toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
150 SessionStatistics.countEvent(nodeId.toString(), SessionStatistics.ConnectionStatus.CONNECTION_DISCONNECTED_BY_DEVICE);
153 final InetSocketAddress remoteAddress = connectionAdapter.getRemoteAddress();
154 final Short auxiliaryId;
155 if (null != getFeatures() && null != getFeatures().getAuxiliaryId()) {
156 auxiliaryId = getFeatures().getAuxiliaryId();
161 LOG.debug("disconnecting: node={}|auxId={}|connection state = {}",
164 getConnectionState());
166 unregisterOutboundQueue();
167 closeHandshakeContext();
168 propagateDeviceDisconnectedEvent();
171 private void propagateDeviceDisconnectedEvent() {
172 if (Objects.nonNull(deviceDisconnectedHandler)) {
173 final BigInteger datapathId = featuresReply != null ? featuresReply.getDatapathId() : BigInteger.ZERO;
174 if (LOG.isDebugEnabled()) {
175 LOG.debug("Propagating connection closed event: {}, datapathId:{}.",
176 connectionAdapter.getRemoteAddress(), datapathId);
178 deviceDisconnectedHandler.onDeviceDisconnected(this);
183 * This method returns safe nodeId for logging
184 * @return string value od nodeId or string "null"
187 public String getSafeNodeIdForLOG() {
188 return Objects.nonNull(nodeId) ? nodeId.getValue() : "null";
192 public void setOutboundQueueHandleRegistration(OutboundQueueHandlerRegistration<OutboundQueueProvider> outboundQueueHandlerRegistration) {
193 this.outboundQueueHandlerRegistration = outboundQueueHandlerRegistration;
196 private void unregisterOutboundQueue() {
197 if (LOG.isDebugEnabled()) {
198 LOG.debug("Trying unregister outbound queue handler registration for node {}", nodeId);
200 if (outboundQueueHandlerRegistration != null) {
201 outboundQueueHandlerRegistration.close();
202 outboundQueueHandlerRegistration = null;
207 public synchronized void changeStateToHandshaking() {
208 connectionState = CONNECTION_STATE.HANDSHAKING;
212 public synchronized void changeStateToTimeouting() {
213 connectionState = CONNECTION_STATE.TIMEOUTING;
217 public synchronized void changeStateToWorking() {
218 connectionState = CONNECTION_STATE.WORKING;
222 public DeviceInfo getDeviceInfo() {
223 return this.deviceInfo;
227 public void handshakeSuccessful() {
228 Preconditions.checkNotNull(nodeId, "Cannot create DeviceInfo if 'NodeId' is not set!");
229 Preconditions.checkNotNull(featuresReply, "Cannot create DeviceInfo if 'features' is not set!");
230 this.deviceInfo = new DeviceInfoImpl(
232 DeviceStateUtil.createNodeInstanceIdentifier(nodeId),
233 featuresReply.getVersion(),
234 featuresReply.getDatapathId(),
235 outboundQueueProvider);
239 public void setHandshakeContext(HandshakeContext handshakeContext) {
240 this.handshakeContext = handshakeContext;
244 public boolean equals(Object o) {
249 if (o == null || getClass() != o.getClass()) {
253 ConnectionContextImpl that = (ConnectionContextImpl) o;
255 if (!connectionAdapter.equals(that.connectionAdapter)) {
259 if (featuresReply != null ? !featuresReply.equals(that.featuresReply) : that.featuresReply != null) {
263 return nodeId != null ? nodeId.equals(that.nodeId) : that.nodeId == null;
268 public int hashCode() {
269 int result = connectionAdapter.hashCode();
270 result = 31 * result + (featuresReply != null ? featuresReply.hashCode() : 0);
271 result = 31 * result + (nodeId != null ? nodeId.hashCode() : 0);
275 private class DeviceInfoImpl implements DeviceInfo {
277 private final NodeId nodeId;
278 private final KeyedInstanceIdentifier<Node, NodeKey> nodeII;
279 private final Short version;
280 private final BigInteger datapathId;
281 private final ServiceGroupIdentifier serviceGroupIdentifier;
282 private OutboundQueue outboundQueueProvider;
286 final KeyedInstanceIdentifier<Node, NodeKey> nodeII,
288 final BigInteger datapathId,
289 final OutboundQueue outboundQueueProvider) {
290 this.nodeId = nodeId;
291 this.nodeII = nodeII;
292 this.version = version;
293 this.datapathId = datapathId;
294 this.outboundQueueProvider = outboundQueueProvider;
295 this.serviceGroupIdentifier = ServiceGroupIdentifier.create(this.nodeId.getValue());
299 public NodeId getNodeId() {
304 public KeyedInstanceIdentifier<Node, NodeKey> getNodeInstanceIdentifier() {
309 public short getVersion() {
314 public BigInteger getDatapathId() {
319 public ServiceGroupIdentifier getServiceIdentifier() {
320 return this.serviceGroupIdentifier;
324 public boolean equals(Object o) {
329 if (o == null || getClass() != o.getClass()) {
333 DeviceInfoImpl that = (DeviceInfoImpl) o;
335 return (nodeId.equals(that.nodeId) &&
336 nodeII.equals(that.nodeII) &&
337 version.equals(that.version) &&
338 datapathId.equals(that.datapathId));
343 public int hashCode() {
344 int result = nodeId.hashCode();
345 result = 31 * result + nodeII.hashCode();
346 result = 31 * result + version.hashCode();
347 result = 31 * result + datapathId.hashCode();
351 public void setOutboundQueueProvider(final OutboundQueue outboundQueueProvider) {
352 this.outboundQueueProvider = outboundQueueProvider;
356 public Long reserveXidForDeviceMessage() {
357 return outboundQueueProvider.reserveEntry();