WIP: Bump upstreams
[openflowplugin.git] / test-common / src / main / java / org / opendaylight / openflowplugin / testcommon / DropTestRpcSender.java
index 841d1c88744fc1f917346e699ff4ae8ede287371..8906f8bf38a89a18b230fd68b404aac519e4f480 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  */
 package org.opendaylight.openflowplugin.testcommon;
 
-import java.math.BigInteger;
-
-import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
+import org.opendaylight.mdsal.binding.api.NotificationService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.binding.NotificationListener;
+import org.opendaylight.yangtools.yang.common.RpcResult;
+import org.opendaylight.yangtools.yang.common.Uint64;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to 
- * device using {@link SalFlowService} strategy
+ * Provides cbench responder behavior: upon packetIn arrival addFlow action is sent out to
+ * device using {@link SalFlowService} strategy.
  */
 public class DropTestRpcSender extends AbstractDropTest {
-    private final static Logger LOG = LoggerFactory.getLogger(DropTestRpcSender.class);
+    private static final Logger LOG = LoggerFactory.getLogger(DropTestRpcSender.class);
+    private static final ThreadLocal<AddFlowInputBuilder> BUILDER = ThreadLocal.withInitial(() -> {
+        final var cookie = new FlowCookie(Uint64.TEN);
+        return new AddFlowInputBuilder()
+            .setPriority(PRIORITY)
+            .setBufferId(BUFFER_ID)
+            .setCookie(cookie)
+            .setCookieMask(cookie)
+            .setTableId(TABLE_ID)
+            .setHardTimeout(HARD_TIMEOUT)
+            .setIdleTimeout(IDLE_TIMEOUT)
+            .setFlags(new FlowModFlags(false, false, false, false, false));
+    });
 
-    private SalFlowService flowService;
-    
-    /**
-     * @param flowService the flowService to set
-     */
-    public void setFlowService(SalFlowService flowService) {
+    private NotificationService notificationService = null;
+    private Registration notificationRegistration = null;
+    private SalFlowService flowService = null;
+
+    public void setFlowService(final SalFlowService flowService) {
         this.flowService = flowService;
     }
 
-    private static final ThreadLocal<AddFlowInputBuilder> BUILDER = new ThreadLocal<AddFlowInputBuilder>() {
-        @Override
-        protected AddFlowInputBuilder initialValue() {
-            final AddFlowInputBuilder fb = new AddFlowInputBuilder();
-
-            fb.setPriority(4);
-            fb.setBufferId(0L);
-
-            final FlowCookie cookie = new FlowCookie(BigInteger.TEN);
-            fb.setCookie(cookie);
-            fb.setCookieMask(cookie);
-            fb.setTableId((short) 0);
-            fb.setHardTimeout(300);
-            fb.setIdleTimeout(240);
-            fb.setFlags(new FlowModFlags(false, false, false, false, false));
-
-            return fb;
-        }
-    };
-
-    private NotificationProviderService notificationService;
-
-    private ListenerRegistration<NotificationListener> notificationRegistration;
-    
     /**
-     * start listening on packetIn
+     * Start listening on packetIn.
      */
     public void start() {
-        notificationRegistration = notificationService.registerNotificationListener(this);
+        notificationRegistration = notificationService.registerListener(PacketReceived.class, this);
     }
 
     @Override
-    protected void processPacket(final NodeKey node, final Match match, final Instructions instructions) {
+    protected void processPacket(final InstanceIdentifier<Node> node, final Match match,
+            final Instructions instructions) {
         final AddFlowInputBuilder fb = BUILDER.get();
 
         // Finally build our flow
         fb.setMatch(match);
         fb.setInstructions(instructions);
-        //fb.setId(new FlowId(Long.toString(fb.hashCode)));
 
         // Construct the flow instance id
-        final InstanceIdentifier<Node> flowInstanceId = InstanceIdentifier
-                .builder(Nodes.class) // File under nodes
-                .child(Node.class, node).build(); // A particular node identified by nodeKey
-        fb.setNode(new NodeRef(flowInstanceId));
+
+        fb.setNode(new NodeRef(node));
 
         // Add flow
-        AddFlowInput flow = fb.build();
-        if (LOG.isDebugEnabled()) {
-            LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
-        }
-        flowService.addFlow(flow);
+        final AddFlowInput flow = fb.build();
+        LOG.debug("onPacketReceived - About to write flow (via SalFlowService) {}", flow);
+        ListenableFuture<RpcResult<AddFlowOutput>> result = flowService.addFlow(flow);
+        Futures.addCallback(result, new FutureCallback<RpcResult<AddFlowOutput>>() {
+            @Override
+            public void onSuccess(final RpcResult<AddFlowOutput> result) {
+                countFutureSuccess();
+            }
+
+            @Override
+            public void onFailure(final Throwable throwable) {
+                countFutureError();
+            }
+        }, MoreExecutors.directExecutor());
     }
 
-    /**
-     * @param notificationService
-     */
-    public void setNotificationService(NotificationProviderService notificationService) {
+    public void setNotificationService(final NotificationService notificationService) {
         this.notificationService = notificationService;
     }
-    
+
     @Override
     public void close() {
-        try {
-            LOG.debug("DropTestProvider stopped.");
-            if (notificationRegistration != null) {
-                notificationRegistration.close();
-            }
-        } catch (Exception e) {
-            LOG.error("unregistration of notification listener failed", e);
+        super.close();
+        LOG.debug("DropTestProvider stopped.");
+        if (notificationRegistration != null) {
+            notificationRegistration.close();
         }
     }
 }