Merge "Drop the odlparent.netty property"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / listener / OpenflowProtocolListenerInitialImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.connection.listener;
9
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.impl.connection.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;
26
27 /**
28  *
29  */
30 public class OpenflowProtocolListenerInitialImpl implements OpenflowProtocolListener {
31
32     private static final Logger LOG = LoggerFactory.getLogger(OpenflowProtocolListenerInitialImpl.class);
33
34     private final ConnectionContext connectionContext;
35     private final HandshakeContext handshakeContext;
36
37     /**
38      * @param connectionContext
39      * @param handshakeContext
40      */
41     public OpenflowProtocolListenerInitialImpl(final ConnectionContext connectionContext,
42                                                final HandshakeContext handshakeContext) {
43         this.connectionContext = connectionContext;
44         this.handshakeContext = handshakeContext;
45     }
46
47     @Override
48     public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) {
49         if (LOG.isDebugEnabled()) {
50             LOG.debug("echo request received: {}", echoRequestMessage.getXid());
51         }
52         EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
53         builder.setVersion(echoRequestMessage.getVersion());
54         builder.setXid(echoRequestMessage.getXid());
55         builder.setData(echoRequestMessage.getData());
56
57         connectionContext.getConnectionAdapter().echoReply(builder.build());
58     }
59
60     @Override
61     public void onErrorMessage(final ErrorMessage notification) {
62         LOG.debug("NOOP: Error message received during handshake phase: {}", notification);
63     }
64
65     @Override
66     public void onExperimenterMessage(final ExperimenterMessage notification) {
67         LOG.debug("NOOP: Experimenter message during handshake phase not supported: {}", notification);
68     }
69
70     @Override
71     public void onFlowRemovedMessage(final FlowRemovedMessage notification) {
72         LOG.debug("NOOP: Flow-removed message during handshake phase not supported: {}", notification);
73     }
74
75     @Override
76     public void onHelloMessage(final HelloMessage hello) {
77         LOG.debug("processing HELLO.xid: {} from device {}", hello.getXid(),
78                 connectionContext.getConnectionAdapter().getRemoteAddress());
79         final ConnectionContext.CONNECTION_STATE connectionState = connectionContext.getConnectionState();
80         if (connectionState == null
81                 || ConnectionContext.CONNECTION_STATE.HANDSHAKING.equals(connectionState)) {
82             synchronized (connectionContext) {
83                 if (connectionContext.getConnectionState() == null) {
84                     // got here before connection ready notification
85                     connectionContext.changeStateToHandshaking();
86                 }
87
88                 if (checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING)) {
89                     final HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
90                             hello, handshakeContext.getHandshakeManager(), connectionContext.getConnectionAdapter());
91                     // use up netty thread
92                     handshakeStepWrapper.run();
93                 } else {
94                     LOG.debug("already out of handshake phase but still received hello message from device {}",
95                             connectionContext.getConnectionAdapter().getRemoteAddress());
96                 }
97             }
98         } else {
99             //TODO: consider disconnecting of bad behaving device
100             LOG.warn("Hello message received outside handshake phase:{} ", hello);
101             LOG.debug("already touched by onConnectionReady event from device {} (or finished handshake)",
102                     connectionContext.getConnectionAdapter().getRemoteAddress());
103         }
104     }
105
106     @Override
107     public void onMultipartReplyMessage(final MultipartReplyMessage notification) {
108         LOG.debug("NOOP: Multipart-reply message during handshake phase not supported: {}", notification);
109     }
110
111     @Override
112     public void onPacketInMessage(final PacketInMessage notification) {
113         LOG.debug("NOOP: Packet-in message during handshake phase not supported: {}", notification);
114     }
115
116     @Override
117     public void onPortStatusMessage(final PortStatusMessage notification) {
118         connectionContext.handlePortStatusMessage(notification);
119     }
120
121     /**
122      * @param expectedState
123      */
124     protected boolean checkState(final ConnectionContext.CONNECTION_STATE expectedState) {
125         boolean verdict = true;
126         if (! Objects.equal(connectionContext.getConnectionState(), expectedState)) {
127             verdict = false;
128             LOG.info("Expected state: {}, actual state: {}", expectedState,
129                     connectionContext.getConnectionState());
130         }
131
132         return verdict;
133     }
134 }