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.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;
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);
40 private SalFlowService flowService;
42 public void setFlowService(final SalFlowService flowService) {
43 this.flowService = flowService;
46 private static final ThreadLocal<AddFlowInputBuilder> BUILDER = ThreadLocal.withInitial(() -> {
47 final AddFlowInputBuilder fb = new AddFlowInputBuilder();
49 fb.setPriority(PRIORITY);
50 fb.setBufferId(BUFFER_ID);
52 final FlowCookie cookie = new FlowCookie(Uint64.TEN);
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));
63 private NotificationService notificationService;
65 private ListenerRegistration<DropTestRpcSender> notificationRegistration;
68 * Start listening on packetIn.
70 @SuppressWarnings("checkstyle:IllegalCatch")
72 final SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK,
73 STARTUP_LOOP_MAX_RETRIES);
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);
85 protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
86 final Instructions instructions) {
87 final AddFlowInputBuilder fb = BUILDER.get();
89 // Finally build our flow
91 fb.setInstructions(instructions);
93 // Construct the flow instance id
95 fb.setNode(new NodeRef(node));
98 final AddFlowInput flow = fb.build();
99 if (LOG.isDebugEnabled()) {
100 LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
102 ListenableFuture<RpcResult<AddFlowOutput>> result = flowService.addFlow(flow);
103 Futures.addCallback(result, new FutureCallback<RpcResult<AddFlowOutput>>() {
105 public void onSuccess(final RpcResult<AddFlowOutput> result) {
106 countFutureSuccess();
110 public void onFailure(final Throwable throwable) {
113 }, MoreExecutors.directExecutor());
116 public void setNotificationService(final NotificationService notificationService) {
117 this.notificationService = notificationService;
121 @SuppressWarnings("checkstyle:IllegalCatch")
122 public void close() {
125 LOG.debug("DropTestProvider stopped.");
126 if (notificationRegistration != null) {
127 notificationRegistration.close();
129 } catch (RuntimeException e) {
130 LOG.warn("unregistration of notification listener failed: {}", e.getMessage());
131 LOG.debug("unregistration of notification listener failed.. ", e);