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.base.Objects;
11 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
12 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
13 import org.opendaylight.openflowplugin.openflow.md.core.HandshakeStepWrapper;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
30 public class OpenflowProtocolListenerInitialImpl implements OpenflowProtocolListener {
32 private static final Logger LOG = LoggerFactory.getLogger(OpenflowProtocolListenerInitialImpl.class);
34 private final ConnectionContext connectionContext;
35 private final HandshakeContext handshakeContext;
38 * @param connectionContext
39 * @param handshakeContext
41 public OpenflowProtocolListenerInitialImpl(final ConnectionContext connectionContext,
42 final HandshakeContext handshakeContext) {
43 this.connectionContext = connectionContext;
44 this.handshakeContext = handshakeContext;
48 public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) {
49 LOG.debug("echo request received: {}", echoRequestMessage.getXid());
50 EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
51 builder.setVersion(echoRequestMessage.getVersion());
52 builder.setXid(echoRequestMessage.getXid());
53 builder.setData(echoRequestMessage.getData());
55 connectionContext.getConnectionAdapter().echoReply(builder.build());
59 public void onErrorMessage(final ErrorMessage notification) {
60 LOG.warn("NOOP: Error message received during handshake phase: {}", notification);
64 public void onExperimenterMessage(final ExperimenterMessage notification) {
65 LOG.info("NOOP: Experimenter message during handshake phase not supported: {}", notification);
69 public void onFlowRemovedMessage(final FlowRemovedMessage notification) {
70 LOG.info("NOOP: Flow-removed message during handshake phase not supported: {}", notification);
74 public void onHelloMessage(final HelloMessage hello) {
75 LOG.debug("processing HELLO.xid: {} from device {}", hello.getXid(), connectionContext.getConnectionAdapter().getRemoteAddress());
76 final ConnectionContext.CONNECTION_STATE connectionState = connectionContext.getConnectionState();
77 if (connectionState == null
78 || ConnectionContext.CONNECTION_STATE.HANDSHAKING.equals(connectionState)) {
79 synchronized (connectionContext) {
80 if (connectionContext.getConnectionState() == null) {
81 // got here before connection ready notification
82 connectionContext.changeStateToHandshaking();
85 if (checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING)) {
86 final HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
87 hello, handshakeContext.getHandshakeManager(), connectionContext.getConnectionAdapter());
88 // use up netty thread
89 handshakeStepWrapper.run();
91 LOG.debug("already out of handshake phase but still received hello message from device {}", connectionContext.getConnectionAdapter().getRemoteAddress());
95 //TODO: consider disconnecting of bad behaving device
96 LOG.warn("Hello message received outside handshake phase: ", hello);
97 LOG.debug("already touched by onConnectionReady event from device {} (or finished handshake)", connectionContext.getConnectionAdapter().getRemoteAddress());
102 public void onMultipartReplyMessage(final MultipartReplyMessage notification) {
103 LOG.info("NOOP: Multipart-reply message during handshake phase not supported: {}", notification);
107 public void onPacketInMessage(final PacketInMessage notification) {
108 LOG.info("NOOP: Packet-in message during handshake phase not supported: {}", notification);
112 public void onPortStatusMessage(final PortStatusMessage notification) {
113 LOG.info("NOOP: Port-status message during handshake phase not supported: {}", notification);
117 * @param expectedState
119 protected boolean checkState(final ConnectionContext.CONNECTION_STATE expectedState) {
120 boolean verdict = true;
121 if (! Objects.equal(connectionContext.getConnectionState(), expectedState)) {
123 LOG.info("Expected state: {}, actual state: {}", expectedState,
124 connectionContext.getConnectionState());