From: Robert Varga Date: Fri, 16 Jul 2021 16:56:35 +0000 (+0200) Subject: Cleanup NetconfConsoleUtils X-Git-Tag: v2.0.1~4 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=5af0bc07af7251e4f1a029f44252a905a254f6d0;p=netconf.git Cleanup NetconfConsoleUtils We have a few strange methods here, clean them up to streamline. Change-Id: Icf0ca4661d4b62cf9e3855af99a6611d1b719869 Signed-off-by: Robert Varga --- diff --git a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/impl/NetconfCommandsImpl.java b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/impl/NetconfCommandsImpl.java index 9f284f2093..d51c803cdf 100644 --- a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/impl/NetconfCommandsImpl.java +++ b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/impl/NetconfCommandsImpl.java @@ -10,11 +10,8 @@ package org.opendaylight.netconf.console.impl; import static java.util.Objects.requireNonNull; import com.google.common.base.Strings; -import com.google.common.collect.ImmutableList; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.MoreExecutors; -import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -93,35 +90,30 @@ public class NetconfCommandsImpl implements NetconfCommands { @Override public Map>> showDevice(final String deviceIp, final String devicePort) { final Map>> device = new HashMap<>(); - List nodeList = new ArrayList<>(); + final Node node; if (devicePort != null) { - nodeList.add(NetconfConsoleUtils.getNetconfNodeFromIpAndPort(deviceIp, devicePort, dataBroker)); + node = NetconfConsoleUtils.getNetconfNodeFromIpAndPort(deviceIp, devicePort, dataBroker); } else { - nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceIp, dataBroker); + node = NetconfConsoleUtils.getNetconfNodeFromId(deviceIp, dataBroker); } - if (nodeList != null) { - for (final Node node : nodeList) { - if (node != null) { - final NetconfNode netconfNode = node.augmentation(NetconfNode.class); - final Map> attributes = new HashMap<>(); - attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue())); - attributes.put(NetconfConsoleConstants.NETCONF_IP, - ImmutableList.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue())); - attributes.put(NetconfConsoleConstants.NETCONF_PORT, - ImmutableList.of(netconfNode.getPort().getValue().toString())); - attributes.put(NetconfConsoleConstants.STATUS, - ImmutableList.of(netconfNode.getConnectionStatus().name())); - if (netconfNode.getConnectionStatus().equals( - NetconfNodeConnectionStatus.ConnectionStatus.Connected)) { - attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, - netconfNode.getAvailableCapabilities().getAvailableCapability().stream() - .map(AvailableCapability::getCapability).collect(Collectors.toList())); - } else { - attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Collections.singletonList("")); - } - device.put(node.getNodeId().getValue(), attributes); - } + if (node != null) { + final NetconfNode netconfNode = node.augmentation(NetconfNode.class); + final Map> attributes = new HashMap<>(); + attributes.put(NetconfConsoleConstants.NETCONF_ID, List.of(node.getNodeId().getValue())); + attributes.put(NetconfConsoleConstants.NETCONF_IP, + List.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue())); + attributes.put(NetconfConsoleConstants.NETCONF_PORT, List.of(netconfNode.getPort().getValue().toString())); + attributes.put(NetconfConsoleConstants.STATUS, List.of(netconfNode.getConnectionStatus().name())); + if (netconfNode.getConnectionStatus().equals( + NetconfNodeConnectionStatus.ConnectionStatus.Connected)) { + attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, netconfNode.getAvailableCapabilities() + .getAvailableCapability().stream() + .map(AvailableCapability::getCapability) + .collect(Collectors.toList())); + } else { + attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, List.of("")); } + device.put(node.getNodeId().getValue(), attributes); } return device; } @@ -129,27 +121,23 @@ public class NetconfCommandsImpl implements NetconfCommands { @Override public Map>> showDevice(final String deviceId) { final Map>> device = new HashMap<>(); - final List nodeList = NetconfConsoleUtils.getNetconfNodeFromId(deviceId, dataBroker); - if (nodeList != null && nodeList.size() > 0) { - for (final Node node : nodeList) { - final NetconfNode netconfNode = node.augmentation(NetconfNode.class); - final Map> attributes = new HashMap<>(); - attributes.put(NetconfConsoleConstants.NETCONF_ID, ImmutableList.of(node.getNodeId().getValue())); - attributes.put(NetconfConsoleConstants.NETCONF_IP, - ImmutableList.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue())); - attributes.put(NetconfConsoleConstants.NETCONF_PORT, - ImmutableList.of(netconfNode.getPort().getValue().toString())); - attributes.put(NetconfConsoleConstants.STATUS, - ImmutableList.of(netconfNode.getConnectionStatus().name())); - if (netconfNode.getConnectionStatus().equals(NetconfNodeConnectionStatus.ConnectionStatus.Connected)) { - attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, - netconfNode.getAvailableCapabilities().getAvailableCapability().stream() - .map(AvailableCapability::getCapability).collect(Collectors.toList())); - } else { - attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, Collections.singletonList("")); - } - device.put(node.getNodeId().getValue(), attributes); + final Node node = NetconfConsoleUtils.getNetconfNodeFromId(deviceId, dataBroker); + if (node != null) { + final NetconfNode netconfNode = node.augmentation(NetconfNode.class); + final Map> attributes = new HashMap<>(); + attributes.put(NetconfConsoleConstants.NETCONF_ID, List.of(node.getNodeId().getValue())); + attributes.put(NetconfConsoleConstants.NETCONF_IP, + List.of(netconfNode.getHost().getIpAddress().getIpv4Address().getValue())); + attributes.put(NetconfConsoleConstants.NETCONF_PORT, List.of(netconfNode.getPort().getValue().toString())); + attributes.put(NetconfConsoleConstants.STATUS, List.of(netconfNode.getConnectionStatus().name())); + if (netconfNode.getConnectionStatus().equals(NetconfNodeConnectionStatus.ConnectionStatus.Connected)) { + attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, netconfNode.getAvailableCapabilities() + .getAvailableCapability().stream() + .map(AvailableCapability::getCapability).collect(Collectors.toList())); + } else { + attributes.put(NetconfConsoleConstants.AVAILABLE_CAPABILITIES, List.of("")); } + device.put(node.getNodeId().getValue(), attributes); } return device; } diff --git a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/utils/NetconfConsoleUtils.java b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/utils/NetconfConsoleUtils.java index 691d793ea9..f21385ebc4 100644 --- a/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/utils/NetconfConsoleUtils.java +++ b/netconf/netconf-console/src/main/java/org/opendaylight/netconf/console/utils/NetconfConsoleUtils.java @@ -7,10 +7,7 @@ */ package org.opendaylight.netconf.console.utils; -import com.google.common.collect.ImmutableList; import com.google.common.util.concurrent.ListenableFuture; -import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Optional; @@ -21,49 +18,26 @@ import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; +import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public final class NetconfConsoleUtils { - private static final Logger LOG = LoggerFactory.getLogger(NetconfConsoleUtils.class); private NetconfConsoleUtils() { throw new IllegalStateException("Instantiating utility class."); } - /** - * Returns a list of NETCONF nodes for the IP. - * @param deviceIp :IP address of NETCONF device - * @param db :An instance of the {@link DataBroker} - * @return :list on NETCONF nodes - */ - public static List getNetconfNodeFromIp(final String deviceIp, final DataBroker db) { - final Topology topology = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.NETCONF_TOPOLOGY_IID, db); - List nodes = new ArrayList<>(); - for (Node node : netconfNodes(topology)) { - final NetconfNode netconfNode = node.augmentation(NetconfNode.class); - if (netconfNode != null - && netconfNode.getHost().getIpAddress().getIpv4Address().getValue().equals(deviceIp)) { - nodes.add(node); - } - } - return nodes.isEmpty() ? null : nodes; - } - /** * Returns the NETCONF node associated with the given nodeId. * @param nodeId :Id of the NETCONF device * @param db :An instance of the {@link DataBroker} - * @return :list on NETCONF nodes + * @return A node, or null if it is not present */ - public static List getNetconfNodeFromId(final String nodeId, final DataBroker db) { - final Node node = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.netconfNodeIid(nodeId), db); - if (node != null) { - return Arrays.asList(node); - } - return null; + public static Node getNetconfNodeFromId(final String nodeId, final DataBroker db) { + return read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.netconfNodeIid(nodeId), db); } /** @@ -92,7 +66,7 @@ public final class NetconfConsoleUtils { * @return :true if not empty, else, false */ private static Collection netconfNodes(final Topology topology) { - return topology == null ? ImmutableList.of() : topology.nonnullNode().values(); + return topology == null ? List.of() : topology.nonnullNode().values(); } /** @@ -102,8 +76,8 @@ public final class NetconfConsoleUtils { * @param db :An instance of the {@link DataBroker} * @return :data read from path */ - public static D read( - final LogicalDatastoreType store, final InstanceIdentifier path, final DataBroker db) { + public static D read(final LogicalDatastoreType store, final InstanceIdentifier path, + final DataBroker db) { final ListenableFuture> future; try (ReadTransaction transaction = db.newReadOnlyTransaction()) { future = transaction.read(store, path); @@ -121,7 +95,9 @@ public final class NetconfConsoleUtils { return optionalData.get(); } - LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], path); + if (LOG.isDebugEnabled()) { + LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], path); + } return null; } } diff --git a/netconf/netconf-console/src/test/java/org/opendaylight/netconf/console/impl/NetconfCommandsImplTest.java b/netconf/netconf-console/src/test/java/org/opendaylight/netconf/console/impl/NetconfCommandsImplTest.java index 8b338d539b..90a3924cf4 100644 --- a/netconf/netconf-console/src/test/java/org/opendaylight/netconf/console/impl/NetconfCommandsImplTest.java +++ b/netconf/netconf-console/src/test/java/org/opendaylight/netconf/console/impl/NetconfCommandsImplTest.java @@ -8,10 +8,8 @@ package org.opendaylight.netconf.console.impl; import static junit.framework.TestCase.assertFalse; -import static junit.framework.TestCase.assertNull; import static junit.framework.TestCase.assertTrue; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -202,16 +200,6 @@ public class NetconfCommandsImplTest { }); } - @Test - public void testNetconfNodeFromIp() throws TimeoutException, InterruptedException, ExecutionException { - final List nodesNotExist = NetconfConsoleUtils.getNetconfNodeFromIp(IP, dataBroker); - assertNull(nodesNotExist); - createTopology(LogicalDatastoreType.OPERATIONAL); - final List nodes = NetconfConsoleUtils.getNetconfNodeFromIp(IP, dataBroker); - assertNotNull(nodes); - assertEquals(1, nodes.size()); - } - private void createTopology(final LogicalDatastoreType dataStoreType) throws TimeoutException, InterruptedException, ExecutionException { final Node node = getNetconfNode(NODE_ID, IP, PORT, CONN_STATUS, CAP_PREFIX);