Merge "Simplify lambdas"
[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.JdkFutureAdapters;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.math.BigInteger;
16 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
17 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yangtools.concepts.ListenerRegistration;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
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(BigInteger.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 ListenerRegistration<DropTestRpcSender> 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.registerNotificationListener(DropTestRpcSender.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 =
104                 JdkFutureAdapters.listenInPoolThread(flowService.addFlow(flow));
105         Futures.addCallback(result, new FutureCallback<RpcResult<AddFlowOutput>>() {
106             @Override
107             public void onSuccess(final RpcResult<AddFlowOutput> result) {
108                 countFutureSuccess();
109             }
110
111             @Override
112             public void onFailure(final Throwable throwable) {
113                 countFutureError();
114             }
115         }, MoreExecutors.directExecutor());
116     }
117
118     public void setNotificationService(final NotificationService notificationService) {
119         this.notificationService = notificationService;
120     }
121
122     @Override
123     @SuppressWarnings("checkstyle:IllegalCatch")
124     public void close() {
125         super.close();
126         try {
127             LOG.debug("DropTestProvider stopped.");
128             if (notificationRegistration != null) {
129                 notificationRegistration.close();
130             }
131         } catch (RuntimeException e) {
132             LOG.warn("unregistration of notification listener failed: {}", e.getMessage());
133             LOG.debug("unregistration of notification listener failed.. ", e);
134         }
135     }
136 }