Close ReadTransactions
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / util / ShellUtil.java
index 69f46ed7b71a07ce70a3f13f0ff4601407094f95..086f4a361371007f1232ab490990e6ec4264e9a8 100644 (file)
@@ -24,8 +24,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
 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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.admin.reconciliation.service.rev180227.ReconciliationCounter;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.admin.reconciliation.service.rev180227.reconciliation.counter.ReconcileCounter;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconciliationCounter;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.reconciliation.counter.ReconcileCounter;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -41,9 +41,8 @@ public final class ShellUtil {
     @Nonnull
     public static List<OFNode> getAllNodes(final DataBroker broker) {
         List<Node> nodes = null;
-        ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
         InstanceIdentifier<Nodes> path = InstanceIdentifier.builder(Nodes.class).build();
-        try {
+        try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
             Optional<Nodes> result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get();
             if (result.isPresent()) {
                 nodes = result.get().getNode();
@@ -95,11 +94,10 @@ public final class ShellUtil {
 
     public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
         OFNode ofNode = null;
-        ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
         InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
                 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
 
-        try {
+        try (ReadOnlyTransaction tx = broker.newReadOnlyTransaction()) {
             Optional<Node> result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get();
             if (result.isPresent()) {
                 Node node = result.get();
@@ -110,7 +108,7 @@ public final class ShellUtil {
                 if (flowCapableNode != null) {
                     name = node.<FlowCapableNode>augmentation(FlowCapableNode.class).getDescription();
                 } else {
-                    LOG.error("Error while converting OFNode:{} to FlowCapableNode: {}", node.getId());
+                    LOG.error("Error while converting OFNode:{} to FlowCapableNode", node.getId());
                     return null;
                 }
                 nodeConnectors = node.getNodeConnector();
@@ -118,7 +116,7 @@ public final class ShellUtil {
                     FlowCapableNodeConnector flowCapableNodeConnector =
                             nodeConnector.augmentation(FlowCapableNodeConnector.class);
                     if (flowCapableNodeConnector == null) {
-                        LOG.error("Error for OFNode:{} while reading nodeConnectors {}", node.getId());
+                        LOG.error("Error for OFNode:{} while reading nodeConnectors", node.getId());
                         return null;
                     } else {
                         String portName = flowCapableNodeConnector.getName();
@@ -127,21 +125,20 @@ public final class ShellUtil {
                 }
                 ofNode = new OFNode(nodeId, name, portList);
             } else {
-                LOG.error("OFNode with nodeId {} not present Inventory DS: {}", nodeId);
+                LOG.error("OFNode with nodeId {} not present Inventory DS", nodeId);
                 return null;
             }
         } catch (ExecutionException | InterruptedException e) {
-            LOG.error("Error reading node {} from Inventory DS: {}", nodeId, e);
+            LOG.error("Error reading node {} from Inventory DS", nodeId, e);
         }
         return ofNode;
     }
 
     public static List<ReconcileCounter> getReconcileCount(final DataBroker dataBroker) {
-        ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction();
         InstanceIdentifier<ReconciliationCounter> instanceIdentifier = InstanceIdentifier
                 .builder(ReconciliationCounter.class).build();
         List<ReconcileCounter> output = Collections.emptyList();
-        try {
+        try (ReadOnlyTransaction tx = dataBroker.newReadOnlyTransaction()) {
             Optional<ReconciliationCounter> result =
                     tx.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier).get();
             if (result.isPresent()) {