Merge "Wiring message processing to deviceContext"
[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.openflow.md.core.HandshakeStepWrapper;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.*;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  *
20  */
21 public class OpenflowProtocolListenerInitialImpl implements OpenflowProtocolListener {
22
23     private static final Logger LOG = LoggerFactory.getLogger(OpenflowProtocolListenerInitialImpl.class);
24
25     private final ConnectionContext connectionContext;
26     private final HandshakeContext handshakeContext;
27
28     /**
29      * @param connectionContext
30      * @param handshakeContext
31      */
32     public OpenflowProtocolListenerInitialImpl(final ConnectionContext connectionContext,
33                                                final HandshakeContext handshakeContext) {
34         this.connectionContext = connectionContext;
35         this.handshakeContext = handshakeContext;
36     }
37
38     @Override
39     public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) {
40         LOG.debug("echo request received: {}", echoRequestMessage.getXid());
41         EchoReplyInputBuilder builder = new EchoReplyInputBuilder();
42         builder.setVersion(echoRequestMessage.getVersion());
43         builder.setXid(echoRequestMessage.getXid());
44         builder.setData(echoRequestMessage.getData());
45
46         connectionContext.getConnectionAdapter().echoReply(builder.build());
47     }
48
49     @Override
50     public void onErrorMessage(final ErrorMessage notification) {
51         // TODO: log
52     }
53
54     @Override
55     public void onExperimenterMessage(final ExperimenterMessage notification) {
56         // NOOP
57
58     }
59
60     @Override
61     public void onFlowRemovedMessage(final FlowRemovedMessage notification) {
62         // NOOP
63     }
64
65     @Override
66     public void onHelloMessage(final HelloMessage hello) {
67         LOG.debug("processing HELLO.xid: {}", hello.getXid());
68         if (connectionContext.getConnectionState() == null) {
69             connectionContext.setConnectionState(ConnectionContext.CONNECTION_STATE.HANDSHAKING);
70         }
71
72         if (checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING)) {
73             final HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
74                     hello, handshakeContext.getHandshakeManager(), connectionContext.getConnectionAdapter());
75             handshakeContext.getHandshakePool().submit(handshakeStepWrapper);
76         } else {
77             //TODO: consider disconnecting of bad behaving device
78         }
79
80     }
81
82     @Override
83     public void onMultipartReplyMessage(final MultipartReplyMessage notification) {
84         // NOOP
85     }
86
87     @Override
88     public void onPacketInMessage(final PacketInMessage notification) {
89         // NOOP
90     }
91
92     @Override
93     public void onPortStatusMessage(final PortStatusMessage notification) {
94         // NOOP
95     }
96
97     /**
98      * @param expectedState
99      */
100     protected boolean checkState(final ConnectionContext.CONNECTION_STATE expectedState) {
101         boolean verdict = true;
102         if (! Objects.equal(connectionContext.getConnectionState(), expectedState)) {
103             verdict = false;
104             LOG.info("Expected state: {}, actual state: {}", expectedState,
105                     connectionContext.getConnectionState());
106         }
107
108         return verdict;
109     }
110 }