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;
@Override
public Map<String, Map<String, List<String>>> showDevice(final String deviceIp, final String devicePort) {
final Map<String, Map<String, List<String>>> device = new HashMap<>();
- List<Node> 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<String, List<String>> 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<String, List<String>> 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;
}
@Override
public Map<String, Map<String, List<String>>> showDevice(final String deviceId) {
final Map<String, Map<String, List<String>>> device = new HashMap<>();
- final List<Node> 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<String, List<String>> 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<String, List<String>> 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;
}
*/
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;
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<Node> getNetconfNodeFromIp(final String deviceIp, final DataBroker db) {
- final Topology topology = read(LogicalDatastoreType.OPERATIONAL, NetconfIidFactory.NETCONF_TOPOLOGY_IID, db);
- List<Node> 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<Node> 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);
}
/**
* @return :<code>true</code> if not empty, else, <code>false</code>
*/
private static Collection<Node> netconfNodes(final Topology topology) {
- return topology == null ? ImmutableList.of() : topology.nonnullNode().values();
+ return topology == null ? List.of() : topology.nonnullNode().values();
}
/**
* @param db :An instance of the {@link DataBroker}
* @return :data read from path
*/
- public static <D extends org.opendaylight.yangtools.yang.binding.DataObject> D read(
- final LogicalDatastoreType store, final InstanceIdentifier<D> path, final DataBroker db) {
+ public static <D extends DataObject> D read(final LogicalDatastoreType store, final InstanceIdentifier<D> path,
+ final DataBroker db) {
final ListenableFuture<Optional<D>> future;
try (ReadTransaction transaction = db.newReadOnlyTransaction()) {
future = transaction.read(store, path);
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;
}
}