ConnectionManager proposal
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / connection / listener / OpenflowProtocolListenerImpl.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 org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
11 import org.opendaylight.openflowplugin.api.openflow.connection.HandshakeContext;
12 import org.opendaylight.openflowplugin.openflow.md.core.HandshakeStepWrapper;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.common.base.Objects;
26
27 /**
28  * 
29  */
30 public class OpenflowProtocolListenerImpl implements OpenflowProtocolListener {
31     
32     private static final Logger LOG = LoggerFactory.getLogger(OpenflowProtocolListenerImpl.class);
33
34     private ConnectionContext connectionContext;
35     private HandshakeContext handshakeContext;
36
37     /**
38      * @param connectionContext
39      * @param handshakeContext 
40      */
41     public OpenflowProtocolListenerImpl(ConnectionContext connectionContext,
42             HandshakeContext handshakeContext) {
43         this.connectionContext = connectionContext;
44         this.handshakeContext = handshakeContext;
45     }
46
47     @Override
48     public void onEchoRequestMessage(EchoRequestMessage notification) {
49         // TODO Auto-generated method stub
50
51     }
52
53     @Override
54     public void onErrorMessage(ErrorMessage notification) {
55         // TODO Auto-generated method stub
56
57     }
58
59     @Override
60     public void onExperimenterMessage(ExperimenterMessage notification) {
61         // TODO Auto-generated method stub
62
63     }
64
65     @Override
66     public void onFlowRemovedMessage(FlowRemovedMessage notification) {
67         // TODO Auto-generated method stub
68
69     }
70
71     @Override
72     public void onHelloMessage(HelloMessage hello) {
73         LOG.debug("processing HELLO.xid: {}", hello.getXid());
74         if (connectionContext.getConnectionState() == null) {
75             connectionContext.setConnectionState(ConnectionContext.CONNECTION_STATE.HANDSHAKING);
76         }
77         
78         if (checkState(ConnectionContext.CONNECTION_STATE.HANDSHAKING)) {
79             HandshakeStepWrapper handshakeStepWrapper = new HandshakeStepWrapper(
80                     hello, handshakeContext.getHandshakeManager(), connectionContext.getConnectionAdapter());
81             handshakeContext.getHandshakePool().submit(handshakeStepWrapper);
82         } else {
83             //TODO: consider disconnecting of bad behaving device
84         }
85         
86     }
87
88     @Override
89     public void onMultipartReplyMessage(MultipartReplyMessage notification) {
90         // TODO Auto-generated method stub
91
92     }
93
94     @Override
95     public void onPacketInMessage(PacketInMessage notification) {
96         // TODO Auto-generated method stub
97
98     }
99
100     @Override
101     public void onPortStatusMessage(PortStatusMessage notification) {
102         // TODO Auto-generated method stub
103
104     }
105     
106     /**
107      * @param expectedState
108      */
109     protected boolean checkState(ConnectionContext.CONNECTION_STATE expectedState) {
110         boolean verdict = true;
111         if (! Objects.equal(connectionContext.getConnectionState(), expectedState)) {
112             verdict = false;
113             LOG.info("Expected state: {}, actual state: {}", expectedState, 
114                     connectionContext.getConnectionState());
115         }
116         
117         return verdict;
118     }
119
120 }