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
8 package org.opendaylight.openflowplugin.impl.connection.listener;
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import java.util.Objects;
14 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
15 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
16 import org.opendaylight.openflowplugin.impl.connection.HandshakeStepWrapper;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyOutput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class OpenflowProtocolListenerInitialImpl implements OpenflowProtocolListener {
33 private static final Logger LOG = LoggerFactory.getLogger(OpenflowProtocolListenerInitialImpl.class);
35 private final ConnectionContext connectionContext;
36 private final HandshakeContext handshakeContext;
41 * @param connectionContext - connection context
42 * @param handshakeContext - handshake context
44 public OpenflowProtocolListenerInitialImpl(final ConnectionContext connectionContext,
45 final HandshakeContext handshakeContext) {
46 this.connectionContext = connectionContext;
47 this.handshakeContext = handshakeContext;
51 public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) {
52 final var xid = echoRequestMessage.getXid();
53 LOG.debug("echo request received: {}", xid);
54 Futures.addCallback(connectionContext.getConnectionAdapter().echoReply(
55 new EchoReplyInputBuilder().setXid(xid).setData(echoRequestMessage.getData()).build()),
56 new FutureCallback<>() {
58 public void onSuccess(final RpcResult<EchoReplyOutput> result) {
59 LOG.debug("echo reply sent: {}", xid);
63 public void onFailure(final Throwable cause) {
64 LOG.debug("echo reply failed: {}", xid, cause);
66 }, MoreExecutors.directExecutor());
70 public void onErrorMessage(final ErrorMessage notification) {
71 LOG.debug("NOOP: Error message received during handshake phase: {}", notification);
75 public void onExperimenterMessage(final ExperimenterMessage notification) {
76 LOG.debug("NOOP: Experimenter message during handshake phase not supported: {}", notification);
80 public void onFlowRemovedMessage(final FlowRemovedMessage notification) {
81 LOG.debug("NOOP: Flow-removed message during handshake phase not supported: {}", notification);
85 public void onHelloMessage(final HelloMessage hello) {
86 LOG.debug("processing HELLO.xid: {} from device {}", hello.getXid(),
87 connectionContext.getConnectionAdapter().getRemoteAddress());
88 final ConnectionContext.CONNECTION_STATE connectionState = connectionContext.getConnectionState();
89 if (connectionState == null
90 || ConnectionContext.CONNECTION_STATE.HANDSHAKING.equals(connectionState)) {
91 synchronized (connectionContext) {
92 if (connectionContext.getConnectionState() == null) {
93 // got here before connection ready notification
94 connectionContext.changeStateToHandshaking();
97 if (checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING)) {
98 final HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
99 hello, handshakeContext.getHandshakeManager(), connectionContext.getConnectionAdapter());
100 // use up netty thread
101 handshakeStepWrapper.run();
103 LOG.debug("already out of handshake phase but still received hello message from device {}",
104 connectionContext.getConnectionAdapter().getRemoteAddress());
108 //TODO: consider disconnecting of bad behaving device
109 LOG.warn("Hello message received outside handshake phase:{} ", hello);
110 LOG.debug("already touched by onConnectionReady event from device {} (or finished handshake)",
111 connectionContext.getConnectionAdapter().getRemoteAddress());
116 public void onMultipartReplyMessage(final MultipartReplyMessage notification) {
117 LOG.debug("NOOP: Multipart-reply message during handshake phase not supported: {}", notification);
121 public void onPacketInMessage(final PacketInMessage notification) {
122 LOG.debug("NOOP: Packet-in message during handshake phase not supported: {}", notification);
126 public void onPortStatusMessage(final PortStatusMessage notification) {
127 connectionContext.handlePortStatusMessage(notification);
131 * Check state of the connection context.
133 * @param expectedState - the expected state
135 protected boolean checkState(final ConnectionContext.CONNECTION_STATE expectedState) {
136 boolean verdict = true;
137 if (!Objects.equals(connectionContext.getConnectionState(), expectedState)) {
139 LOG.info("Expected state: {}, actual state: {}", expectedState,
140 connectionContext.getConnectionState());