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