2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.testcommon;
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;
34 * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
35 * device using {@link SalFlowService} strategy.
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)
45 .setCookieMask(cookie)
47 .setHardTimeout(HARD_TIMEOUT)
48 .setIdleTimeout(IDLE_TIMEOUT)
49 .setFlags(new FlowModFlags(false, false, false, false, false));
52 private NotificationService notificationService = null;
53 private Registration notificationRegistration = null;
54 private SalFlowService flowService = null;
56 public void setFlowService(final SalFlowService flowService) {
57 this.flowService = flowService;
61 * Start listening on packetIn.
64 notificationRegistration = notificationService.registerListener(PacketReceived.class, this);
68 protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
69 final Instructions instructions) {
70 final AddFlowInputBuilder fb = BUILDER.get();
72 // Finally build our flow
74 fb.setInstructions(instructions);
76 // Construct the flow instance id
78 fb.setNode(new NodeRef(node));
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>>() {
86 public void onSuccess(final RpcResult<AddFlowOutput> result) {
91 public void onFailure(final Throwable throwable) {
94 }, MoreExecutors.directExecutor());
97 public void setNotificationService(final NotificationService notificationService) {
98 this.notificationService = notificationService;
102 public void close() {
104 LOG.debug("DropTestProvider stopped.");
105 if (notificationRegistration != null) {
106 notificationRegistration.close();