Bump upstreams
[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.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
26 import org.opendaylight.yangtools.concepts.Registration;
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     private static final ThreadLocal<AddFlowInputBuilder> BUILDER = ThreadLocal.withInitial(() -> {
40         final var cookie = new FlowCookie(Uint64.TEN);
41         return new AddFlowInputBuilder()
42             .setPriority(PRIORITY)
43             .setBufferId(BUFFER_ID)
44             .setCookie(cookie)
45             .setCookieMask(cookie)
46             .setTableId(TABLE_ID)
47             .setHardTimeout(HARD_TIMEOUT)
48             .setIdleTimeout(IDLE_TIMEOUT)
49             .setFlags(new FlowModFlags(false, false, false, false, false));
50     });
51
52     private NotificationService notificationService = null;
53     private Registration notificationRegistration = null;
54     private SalFlowService flowService = null;
55
56     public void setFlowService(final SalFlowService flowService) {
57         this.flowService = flowService;
58     }
59
60     /**
61      * Start listening on packetIn.
62      */
63     public void start() {
64         notificationRegistration = notificationService.registerListener(PacketReceived.class, this);
65     }
66
67     @Override
68     protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
69             final Instructions instructions) {
70         final AddFlowInputBuilder fb = BUILDER.get();
71
72         // Finally build our flow
73         fb.setMatch(match);
74         fb.setInstructions(instructions);
75
76         // Construct the flow instance id
77
78         fb.setNode(new NodeRef(node));
79
80         // Add flow
81         final AddFlowInput flow = fb.build();
82         LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
83         ListenableFuture<RpcResult<AddFlowOutput>> result = flowService.addFlow(flow);
84         Futures.addCallback(result, new FutureCallback<RpcResult<AddFlowOutput>>() {
85             @Override
86             public void onSuccess(final RpcResult<AddFlowOutput> result) {
87                 countFutureSuccess();
88             }
89
90             @Override
91             public void onFailure(final Throwable throwable) {
92                 countFutureError();
93             }
94         }, MoreExecutors.directExecutor());
95     }
96
97     public void setNotificationService(final NotificationService notificationService) {
98         this.notificationService = notificationService;
99     }
100
101     @Override
102     public void close() {
103         super.close();
104         LOG.debug("DropTestProvider stopped.");
105         if (notificationRegistration != null) {
106             notificationRegistration.close();
107         }
108     }
109 }