X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=applications%2Fsouthbound-cli%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fapplications%2Fsouthboundcli%2Futil%2FShellUtil.java;h=d0be9bc924e5c37231412b2bdea27911426683b1;hb=HEAD;hp=0da9d0d4541e49055f29fe779d15913db23369ae;hpb=b7e8e4ab19857d92fbf22795406174cf453128e9;p=openflowplugin.git diff --git a/applications/southbound-cli/src/main/java/org/opendaylight/openflowplugin/applications/southboundcli/util/ShellUtil.java b/applications/southbound-cli/src/main/java/org/opendaylight/openflowplugin/applications/southboundcli/util/ShellUtil.java index 0da9d0d454..d0be9bc924 100644 --- a/applications/southbound-cli/src/main/java/org/opendaylight/openflowplugin/applications/southboundcli/util/ShellUtil.java +++ b/applications/southbound-cli/src/main/java/org/opendaylight/openflowplugin/applications/southboundcli/util/ShellUtil.java @@ -8,17 +8,14 @@ package org.opendaylight.openflowplugin.applications.southboundcli.util; -import com.google.common.base.Optional; -import com.google.common.util.concurrent.CheckedFuture; import java.util.ArrayList; -import java.util.Collections; +import java.util.Collection; import java.util.List; +import java.util.Optional; import java.util.concurrent.ExecutionException; -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 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress; +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.FlowCapableNodeConnector; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; @@ -26,6 +23,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.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; @@ -33,49 +32,12 @@ import org.slf4j.LoggerFactory; public final class ShellUtil { private static final Logger LOG = LoggerFactory.getLogger(ShellUtil.class); + public static final String LINE_SEPARATOR = "-".repeat(100); public static final String NODE_PREFIX = "openflow:"; private ShellUtil() { } - public static List getAllNodes(final DataBroker broker) { - List nodes = null; - ReadOnlyTransaction tx = broker.newReadOnlyTransaction(); - InstanceIdentifier path = InstanceIdentifier.builder(Nodes.class).build(); - try { - CheckedFuture, ReadFailedException> checkedFuture = - tx.read(LogicalDatastoreType.OPERATIONAL, path); - Optional result = checkedFuture.get(); - if (result.isPresent()) { - nodes = result.get().getNode(); - } - } catch (ExecutionException | InterruptedException | NullPointerException e) { - LOG.error("Error reading nodes from Inventory DS", e); - } - if (nodes != null) { - List nodeList = new ArrayList<>(); - for (Node node : nodes) { - String[] nodeId = node.getId().getValue().split(":"); - String name = null; - FlowCapableNode flowCapableNode = node.getAugmentation(FlowCapableNode.class); - if (flowCapableNode != null) { - name = node.getAugmentation(FlowCapableNode.class).getDescription(); - } else { - LOG.error("Error while converting OFNode: {} to FlowCapableNode", node.getId()); - return null; - } - OFNode ofNode = new OFNode(Long.parseLong(nodeId[1]), name); - if (ofNode != null) { - LOG.trace("Added OFNode: {} to the list", ofNode.getNodeId()); - nodeList.add(ofNode); - } - } - Collections.sort(nodeList); - return nodeList; - } - return Collections.emptyList(); - } - public static OFNode getNode(final long nodeId, final DataBroker broker) { OFNode nodeInfo = getNodeInfo(nodeId, broker); if (nodeInfo == null) { @@ -98,47 +60,54 @@ public final class ShellUtil { public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) { OFNode ofNode = null; - ReadOnlyTransaction tx = broker.newReadOnlyTransaction(); InstanceIdentifier path = InstanceIdentifier.builder(Nodes.class) .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build(); - Optional result; - try { - CheckedFuture, ReadFailedException> checkedFuture = - tx.read(LogicalDatastoreType.OPERATIONAL, path); - result = checkedFuture.get(); + + try (ReadTransaction tx = broker.newReadOnlyTransaction()) { + Optional result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get(); if (result.isPresent()) { - Node node = result.get(); - String name = null; - List nodeConnectors = null; + Node node = result.orElseThrow(); + String name; + Collection nodeConnectors = node.nonnullNodeConnector().values(); List portList = new ArrayList<>(); - FlowCapableNode flowCapableNode = node.getAugmentation(FlowCapableNode.class); + FlowCapableNode flowCapableNode = node.augmentation(FlowCapableNode.class); if (flowCapableNode != null) { - name = node.getAugmentation(FlowCapableNode.class).getDescription(); + name = node.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(); for (NodeConnector nodeConnector : nodeConnectors) { FlowCapableNodeConnector flowCapableNodeConnector = - nodeConnector.getAugmentation(FlowCapableNodeConnector.class); + 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 { - MacAddress hardwareAddress = flowCapableNodeConnector.getHardwareAddress(); String portName = flowCapableNodeConnector.getName(); portList.add(portName); } } 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 Collection getReconcileCount(final DataBroker dataBroker) { + InstanceIdentifier instanceIdentifier = InstanceIdentifier + .builder(ReconciliationCounter.class).build(); + try (ReadTransaction tx = dataBroker.newReadOnlyTransaction()) { + final var result = tx.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier).get(); + return result.map(counter -> counter.nonnullReconcileCounter().values()).orElse(List.of()); + } catch (ExecutionException | InterruptedException e) { + LOG.error("Error reading reconciliation counter from datastore", e); + return List.of(); + } + } }