Ditch use of SystemNotificationsListener
[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.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;
31
32 public class OpenflowProtocolListenerInitialImpl implements OpenflowProtocolListener {
33     private static final Logger LOG = LoggerFactory.getLogger(OpenflowProtocolListenerInitialImpl.class);
34
35     private final ConnectionContext connectionContext;
36     private final HandshakeContext handshakeContext;
37
38     /**
39      * Constructor.
40      *
41      * @param connectionContext - connection context
42      * @param handshakeContext - handshake context
43      */
44     public OpenflowProtocolListenerInitialImpl(final ConnectionContext connectionContext,
45                                                final HandshakeContext handshakeContext) {
46         this.connectionContext = connectionContext;
47         this.handshakeContext = handshakeContext;
48     }
49
50     @Override
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<>() {
57                 @Override
58                 public void onSuccess(final RpcResult<EchoReplyOutput> result) {
59                     LOG.debug("echo reply sent: {}", xid);
60                 }
61
62                 @Override
63                 public void onFailure(final Throwable cause) {
64                     LOG.debug("echo reply failed: {}", xid, cause);
65                 }
66             }, MoreExecutors.directExecutor());
67     }
68
69     @Override
70     public void onErrorMessage(final ErrorMessage notification) {
71         LOG.debug("NOOP: Error message received during handshake phase: {}", notification);
72     }
73
74     @Override
75     public void onExperimenterMessage(final ExperimenterMessage notification) {
76         LOG.debug("NOOP: Experimenter message during handshake phase not supported: {}", notification);
77     }
78
79     @Override
80     public void onFlowRemovedMessage(final FlowRemovedMessage notification) {
81         LOG.debug("NOOP: Flow-removed message during handshake phase not supported: {}", notification);
82     }
83
84     @Override
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();
95                 }
96
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();
102                 } else {
103                     LOG.debug("already out of handshake phase but still received hello message from device {}",
104                             connectionContext.getConnectionAdapter().getRemoteAddress());
105                 }
106             }
107         } else {
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());
112         }
113     }
114
115     @Override
116     public void onMultipartReplyMessage(final MultipartReplyMessage notification) {
117         LOG.debug("NOOP: Multipart-reply message during handshake phase not supported: {}", notification);
118     }
119
120     @Override
121     public void onPacketInMessage(final PacketInMessage notification) {
122         LOG.debug("NOOP: Packet-in message during handshake phase not supported: {}", notification);
123     }
124
125     @Override
126     public void onPortStatusMessage(final PortStatusMessage notification) {
127         connectionContext.handlePortStatusMessage(notification);
128     }
129
130     /**
131      * Check state of the connection context.
132      *
133      * @param expectedState - the expected state
134      */
135     protected boolean checkState(final ConnectionContext.CONNECTION_STATE expectedState) {
136         boolean verdict = true;
137         if (!Objects.equals(connectionContext.getConnectionState(), expectedState)) {
138             verdict = false;
139             LOG.info("Expected state: {}, actual state: {}", expectedState,
140                     connectionContext.getConnectionState());
141         }
142
143         return verdict;
144     }
145 }