Modernize test-common's use of NotificationService
[openflowplugin.git] / test-common / src / main / java / org / opendaylight / openflowplugin / testcommon / DropTestRpcSender.java
1 /*
2  * Copyright (c) 2013 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.testcommon;
9
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import org.opendaylight.mdsal.binding.api.NotificationService;
15 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
27 import org.opendaylight.yangtools.concepts.Registration;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.common.RpcResult;
30 import org.opendaylight.yangtools.yang.common.Uint64;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
36  * device using {@link SalFlowService} strategy.
37  */
38 public class DropTestRpcSender extends AbstractDropTest {
39     private static final Logger LOG = LoggerFactory.getLogger(DropTestRpcSender.class);
40
41     private SalFlowService flowService;
42
43     public void setFlowService(final SalFlowService flowService) {
44         this.flowService = flowService;
45     }
46
47     private static final ThreadLocal<AddFlowInputBuilder> BUILDER = ThreadLocal.withInitial(() -> {
48         final AddFlowInputBuilder fb = new AddFlowInputBuilder();
49
50         fb.setPriority(PRIORITY);
51         fb.setBufferId(BUFFER_ID);
52
53         final FlowCookie cookie = new FlowCookie(Uint64.TEN);
54         fb.setCookie(cookie);
55         fb.setCookieMask(cookie);
56         fb.setTableId(TABLE_ID);
57         fb.setHardTimeout(HARD_TIMEOUT);
58         fb.setIdleTimeout(IDLE_TIMEOUT);
59         fb.setFlags(new FlowModFlags(false, false, false, false, false));
60
61         return fb;
62     });
63
64     private NotificationService notificationService;
65
66     private Registration notificationRegistration;
67
68     /**
69      * Start listening on packetIn.
70      */
71     @SuppressWarnings("checkstyle:IllegalCatch")
72     public void start() {
73         final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
74                 STARTUP_LOOP_MAX_RETRIES);
75         try {
76             notificationRegistration = looper.loopUntilNoException(
77                 () -> notificationService.registerListener(PacketReceived.class, this));
78         } catch (Exception e) {
79             LOG.warn("DropTest sender notification listener registration fail!");
80             LOG.debug("DropTest sender notification listener registration fail! ..", e);
81             throw new IllegalStateException("DropTest startup fail! Try again later.", e);
82         }
83     }
84
85     @Override
86     protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
87             final Instructions instructions) {
88         final AddFlowInputBuilder fb = BUILDER.get();
89
90         // Finally build our flow
91         fb.setMatch(match);
92         fb.setInstructions(instructions);
93
94         // Construct the flow instance id
95
96         fb.setNode(new NodeRef(node));
97
98         // Add flow
99         final AddFlowInput flow = fb.build();
100         if (LOG.isDebugEnabled()) {
101             LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
102         }
103         ListenableFuture<RpcResult<AddFlowOutput>> result = flowService.addFlow(flow);
104         Futures.addCallback(result, new FutureCallback<RpcResult<AddFlowOutput>>() {
105             @Override
106             public void onSuccess(final RpcResult<AddFlowOutput> result) {
107                 countFutureSuccess();
108             }
109
110             @Override
111             public void onFailure(final Throwable throwable) {
112                 countFutureError();
113             }
114         }, MoreExecutors.directExecutor());
115     }
116
117     public void setNotificationService(final NotificationService notificationService) {
118         this.notificationService = notificationService;
119     }
120
121     @Override
122     @SuppressWarnings("checkstyle:IllegalCatch")
123     public void close() {
124         super.close();
125         try {
126             LOG.debug("DropTestProvider stopped.");
127             if (notificationRegistration != null) {
128                 notificationRegistration.close();
129             }
130         } catch (RuntimeException e) {
131             LOG.warn("unregistration of notification listener failed: {}", e.getMessage());
132             LOG.debug("unregistration of notification listener failed.. ", e);
133         }
134     }
135 }