Ditch use of SystemNotificationsListener
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / listener / OpenflowProtocolListenerFullImpl.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.device.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 org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.AlienMessageListener;
15 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceReplyProcessor;
16 import org.opendaylight.openflowplugin.api.openflow.device.listener.OpenflowMessageListenerFacade;
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.OfHeader;
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 OpenflowProtocolListenerFullImpl implements AlienMessageListener, OpenflowMessageListenerFacade {
33
34     private static final Logger LOG = LoggerFactory.getLogger(OpenflowProtocolListenerFullImpl.class);
35
36     private final ConnectionAdapter connectionAdapter;
37     private final DeviceReplyProcessor deviceReplyProcessor;
38
39     /**
40      * Constructor.
41      *
42      * @param connectionAdapter - connection adapter
43      * @param deviceReplyProcessor - device replay processor
44      */
45     public OpenflowProtocolListenerFullImpl(final ConnectionAdapter connectionAdapter,
46                                             final DeviceReplyProcessor deviceReplyProcessor) {
47         this.connectionAdapter = connectionAdapter;
48         this.deviceReplyProcessor = deviceReplyProcessor;
49     }
50
51     @Override
52     public void onEchoRequestMessage(final EchoRequestMessage echoRequestMessage) {
53         final var xid = echoRequestMessage.getXid();
54         LOG.debug("echo request received: {}", xid);
55         Futures.addCallback(connectionAdapter.echoReply(
56             new EchoReplyInputBuilder()
57                 .setVersion(echoRequestMessage.getVersion())
58                 .setXid(xid)
59                 .setData(echoRequestMessage.getData())
60                 .build()),
61             new FutureCallback<>() {
62                 @Override
63                 public void onSuccess(final RpcResult<EchoReplyOutput> result) {
64                     LOG.debug("echo reply sent: {}", xid);
65                 }
66
67                 @Override
68                 public void onFailure(final Throwable cause) {
69                     LOG.debug("echo reply failed: {}", xid, cause);
70                 }
71             }, MoreExecutors.directExecutor());
72     }
73
74     @Override
75     public void onErrorMessage(final ErrorMessage notification) {
76         deviceReplyProcessor.processReply(notification);
77     }
78
79     @Override
80     public void onExperimenterMessage(final ExperimenterMessage notification) {
81         LOG.trace("Received experiementer message: {}", notification.getClass());
82         deviceReplyProcessor.processExperimenterMessage(notification);
83     }
84
85     @Override
86     public void onFlowRemovedMessage(final FlowRemovedMessage notification) {
87         deviceReplyProcessor.processFlowRemovedMessage(notification);
88     }
89
90     @Override
91     public void onHelloMessage(final HelloMessage hello) {
92         LOG.warn("hello message received outside handshake phase -> dropping connection {}",
93                 connectionAdapter.getRemoteAddress());
94         connectionAdapter.disconnect();
95     }
96
97     @Override
98     public void onMultipartReplyMessage(final MultipartReplyMessage notification) {
99         if (LOG.isTraceEnabled()) {
100             LOG.trace("Multipart Reply with XID: {}", notification.getXid());
101         }
102         // multiMsgCollector.addMultipartMsg(notification);
103     }
104
105     @Override
106     public void onPacketInMessage(final PacketInMessage notification) {
107         deviceReplyProcessor.processPacketInMessage(notification);
108     }
109
110     @Override
111     public void onPortStatusMessage(final PortStatusMessage notification) {
112         deviceReplyProcessor.processPortStatusMessage(notification);
113     }
114
115     @Override
116     public boolean onAlienMessage(final OfHeader message) {
117         return deviceReplyProcessor.processAlienMessage(message);
118     }
119
120 }