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