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