Switch to MD-SAL APIs
[openflowplugin.git] / applications / bulk-o-matic / src / main / java / org / opendaylight / openflowplugin / applications / bulk / o / matic / FlowReader.java
index bae183d53ec0b883920ed04f1ee30e8925ad125a..edf3dded44e69df8f75934bc85b7e353adbbcd4a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 Ericsson Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2016, 2017 Ericsson Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -7,11 +7,13 @@
  */
 package org.opendaylight.openflowplugin.applications.bulk.o.matic;
 
-import com.google.common.base.Optional;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.ReadTransaction;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
@@ -26,10 +28,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.concurrent.atomic.AtomicLong;
-
-public class FlowReader implements Runnable, FlowCounterMBean {
+public final class FlowReader implements Runnable, FlowCounterMBean {
     private static final Logger LOG = LoggerFactory.getLogger(FlowReader.class);
     private final DataBroker dataBroker;
     private final Integer dpnCount;
@@ -38,17 +37,11 @@ public class FlowReader implements Runnable, FlowCounterMBean {
     private final short startTableId;
     private final short endTableId;
     private final boolean isConfigDs;
-    private AtomicLong flowCount = new AtomicLong(0);
-    private AtomicInteger readOpStatus = new AtomicInteger(FlowCounter.OperationStatus.INIT.status());
-    private final String UNITS = "ns";
+    private final AtomicLong flowCount = new AtomicLong();
+    private final AtomicInteger readOpStatus = new AtomicInteger(FlowCounter.OperationStatus.INIT.status());
 
-    private FlowReader(final DataBroker dataBroker,
-                      final Integer dpnCount,
-                      final int flowsPerDpn,
-                      final boolean verbose,
-                      final boolean isConfigDs,
-                      final short startTableId,
-                      final short endTableId) {
+    private FlowReader(final DataBroker dataBroker, final Integer dpnCount, final int flowsPerDpn,
+            final boolean verbose, final boolean isConfigDs, final short startTableId, final short endTableId) {
         this.dataBroker = dataBroker;
         this.dpnCount = dpnCount;
         this.verbose = verbose;
@@ -59,41 +52,39 @@ public class FlowReader implements Runnable, FlowCounterMBean {
     }
 
     public static FlowReader getNewInstance(final DataBroker dataBroker,
-                                      final Integer dpnCount,
-                                      final int flowsPerDpn,
-                                      final boolean verbose,
-                                      final boolean isConfigDs,
-                                      final short startTableId,
-                                      final short endTableId) {
-        return new FlowReader(dataBroker, dpnCount, flowsPerDpn, verbose,
-                isConfigDs, startTableId, endTableId);
+            final Integer dpnCount, final int flowsPerDpn,
+            final boolean verbose, final boolean isConfigDs,
+            final short startTableId, final short endTableId) {
+        return new FlowReader(dataBroker, dpnCount,
+                flowsPerDpn, verbose,
+                isConfigDs, startTableId,
+                endTableId);
     }
 
     @Override
     public void run() {
-        readFlowsX(dpnCount, flowsPerDpn, verbose);
+        readFlowsX();
     }
 
-    private void readFlowsX(Integer dpnCount, Integer flowsPerDPN, boolean verbose) {
+    private void readFlowsX() {
         readOpStatus.set(FlowCounter.OperationStatus.IN_PROGRESS.status());
         for (int i = 1; i <= dpnCount; i++) {
             String dpId = BulkOMaticUtils.DEVICE_TYPE_PREFIX + i;
-            for (int j = 0; j < flowsPerDPN; j++) {
-                short tableRollover = (short)(endTableId - startTableId + 1);
-                short tableId = (short) (((j) % tableRollover) + startTableId);
+            for (int j = 0; j < flowsPerDpn; j++) {
+                short tableRollover = (short) (endTableId - startTableId + 1);
+                short tableId = (short) (j % tableRollover + startTableId);
 
                 Integer sourceIp = j + 1;
 
                 String flowId = "Flow-" + dpId + "." + tableId + "." + sourceIp;
                 InstanceIdentifier<Flow> flowIid = getFlowInstanceIdentifier(dpId, tableId, flowId);
 
-                ReadOnlyTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction();
-                try {
+                try (ReadTransaction readOnlyTransaction = dataBroker.newReadOnlyTransaction()) {
                     Optional<Flow> flowOptional;
-                    if(isConfigDs) {
-                        flowOptional = readOnlyTransaction.read(LogicalDatastoreType.CONFIGURATION, flowIid).checkedGet();
+                    if (isConfigDs) {
+                        flowOptional = readOnlyTransaction.read(LogicalDatastoreType.CONFIGURATION, flowIid).get();
                     } else {
-                        flowOptional = readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, flowIid).checkedGet();
+                        flowOptional = readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, flowIid).get();
                     }
 
                     if (flowOptional.isPresent()) {
@@ -106,24 +97,22 @@ public class FlowReader implements Runnable, FlowCounterMBean {
                             LOG.info("Flow: {} not found", flowIid);
                         }
                     }
-                } catch (ReadFailedException e) {
+                } catch (InterruptedException | ExecutionException e) {
                     readOpStatus.set(FlowCounter.OperationStatus.FAILURE.status());
-                    LOG.error(e.getMessage(), e);
+                    LOG.error("Error {}", e);
                 }
             }
         }
-        if(readOpStatus.get() != FlowCounter.OperationStatus.FAILURE.status()) {
+        if (readOpStatus.get() != FlowCounter.OperationStatus.FAILURE.status()) {
             readOpStatus.set(FlowCounter.OperationStatus.SUCCESS.status());
         }
         LOG.info("Total Flows read: {}", flowCount);
     }
 
-    private InstanceIdentifier<Flow> getFlowInstanceIdentifier(String dpId, Short tableId, String flowId){
+    private InstanceIdentifier<Flow> getFlowInstanceIdentifier(String dpId, Short tableId, String flowId) {
         return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(dpId)))
-                .augmentation(FlowCapableNode.class)
-                .child(Table.class, new TableKey(tableId))
-                .child(Flow.class,
-                        new FlowKey(new FlowId(flowId)));
+                .augmentation(FlowCapableNode.class).child(Table.class, new TableKey(tableId))
+                .child(Flow.class, new FlowKey(new FlowId(flowId)));
     }
 
     @Override
@@ -135,19 +124,4 @@ public class FlowReader implements Runnable, FlowCounterMBean {
     public int getReadOpStatus() {
         return readOpStatus.get();
     }
-
-    @Override
-    public int getWriteOpStatus() {
-        return BulkOMaticUtils.DEFUALT_STATUS;
-    }
-
-    @Override
-    public long getTaskCompletionTime() {
-        return BulkOMaticUtils.DEFAULT_COMPLETION_TIME;
-    }
-
-    @Override
-    public String getUnits() {
-        return UNITS;
-    }
-}
\ No newline at end of file
+}