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 static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import javax.annotation.PreDestroy;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.mdsal.binding.api.NotificationService;
19 import org.opendaylight.mdsal.binding.api.RpcService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlow;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
30 import org.opendaylight.yangtools.concepts.Registration;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.common.Uint64;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Deactivate;
37 import org.osgi.service.component.annotations.Reference;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to device using
43 * {@link AddFlow} strategy.
46 @Component(service = DropTestRpcSender.class, immediate = true)
47 public final class DropTestRpcSenderImpl extends AbstractDropTest implements DropTestRpcSender {
48 private static final Logger LOG = LoggerFactory.getLogger(DropTestRpcSenderImpl.class);
49 private static final ThreadLocal<AddFlowInputBuilder> BUILDER = ThreadLocal.withInitial(() -> {
50 final var cookie = new FlowCookie(Uint64.TEN);
51 return new AddFlowInputBuilder()
52 .setPriority(PRIORITY)
53 .setBufferId(BUFFER_ID)
55 .setCookieMask(cookie)
57 .setHardTimeout(HARD_TIMEOUT)
58 .setIdleTimeout(IDLE_TIMEOUT)
59 .setFlags(new FlowModFlags(false, false, false, false, false));
62 private final NotificationService notificationService;
63 private final AddFlow addFlow;
65 private Registration reg = null;
69 public DropTestRpcSenderImpl(@Reference final NotificationService notificationService,
70 @Reference final RpcService rpcService) {
71 this.notificationService = requireNonNull(notificationService);
72 addFlow = rpcService.getRpc(AddFlow.class);
81 LOG.debug("DropTestProvider terminated");
85 public synchronized boolean start() {
89 reg = notificationService.registerListener(PacketReceived.class, this);
90 LOG.debug("DropTestProvider started");
95 public synchronized boolean stop() {
101 LOG.debug("DropTestProvider stopped");
106 protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
107 final Instructions instructions) {
108 final AddFlowInputBuilder fb = BUILDER.get();
110 // Finally build our flow
112 fb.setInstructions(instructions);
114 // Construct the flow instance id
116 fb.setNode(new NodeRef(node));
119 final var flow = fb.build();
120 LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
121 Futures.addCallback(addFlow.invoke(flow), new FutureCallback<RpcResult<AddFlowOutput>>() {
123 public void onSuccess(final RpcResult<AddFlowOutput> result) {
124 countFutureSuccess();
128 public void onFailure(final Throwable throwable) {
131 }, MoreExecutors.directExecutor());