2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.applications.southboundcli.util;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.List;
16 import java.util.Optional;
17 import java.util.concurrent.ExecutionException;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.mdsal.binding.api.ReadTransaction;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.openflowplugin.applications.southboundcli.NodeListener;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconciliationCounter;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.reconciliation.counter.ReconcileCounter;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public final class ShellUtil {
36 private static final Logger LOG = LoggerFactory.getLogger(ShellUtil.class);
38 public static final String NODE_PREFIX = "openflow:";
43 public static List<OFNode> getAllNodes(final NodeListener nodeListener) {
44 List<OFNode> dpnList = new ArrayList<>();
45 for (Map.Entry<Long, String> entry : nodeListener.getDpnIdToNameCache().entrySet()) {
46 OFNode dpn = new OFNode(entry.getKey(), entry.getValue());
48 LOG.trace("Added OFNode: {} to the list", dpn.getNodeId());
50 Collections.sort(dpnList);
54 public static OFNode getNode(final long nodeId, final DataBroker broker) {
55 OFNode nodeInfo = getNodeInfo(nodeId, broker);
56 if (nodeInfo == null) {
57 LOG.info("No ports exist for this node with nodeId {}", nodeId);
60 List<String> ports = new ArrayList<>();
61 // OFNode State is not provided by plugin, hence using null
62 if (nodeInfo.getPorts() == null) {
63 LOG.info("No ports exist for this node with nodeId {}", nodeId);
66 for (String port : nodeInfo.getPorts()) {
69 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
74 public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
76 InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
77 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
79 try (ReadTransaction tx = broker.newReadOnlyTransaction()) {
80 Optional<Node> result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get();
81 if (result.isPresent()) {
82 Node node = result.get();
84 Collection<NodeConnector> nodeConnectors = node.nonnullNodeConnector().values();
85 List<String> portList = new ArrayList<>();
86 FlowCapableNode flowCapableNode = node.<FlowCapableNode>augmentation(FlowCapableNode.class);
87 if (flowCapableNode != null) {
88 name = node.<FlowCapableNode>augmentation(FlowCapableNode.class).getDescription();
90 LOG.error("Error while converting OFNode:{} to FlowCapableNode", node.getId());
93 for (NodeConnector nodeConnector : nodeConnectors) {
94 FlowCapableNodeConnector flowCapableNodeConnector =
95 nodeConnector.augmentation(FlowCapableNodeConnector.class);
96 if (flowCapableNodeConnector == null) {
97 LOG.error("Error for OFNode:{} while reading nodeConnectors", node.getId());
100 String portName = flowCapableNodeConnector.getName();
101 portList.add(portName);
104 ofNode = new OFNode(nodeId, name, portList);
106 LOG.error("OFNode with nodeId {} not present Inventory DS", nodeId);
109 } catch (ExecutionException | InterruptedException e) {
110 LOG.error("Error reading node {} from Inventory DS", nodeId, e);
115 public static Collection<ReconcileCounter> getReconcileCount(final DataBroker dataBroker) {
116 InstanceIdentifier<ReconciliationCounter> instanceIdentifier = InstanceIdentifier
117 .builder(ReconciliationCounter.class).build();
118 Collection<ReconcileCounter> output = Collections.emptyList();
119 try (ReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
120 Optional<ReconciliationCounter> result =
121 tx.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier).get();
122 if (result.isPresent()) {
123 output = result.get().nonnullReconcileCounter().values();
125 } catch (ExecutionException | InterruptedException | NullPointerException e) {
126 LOG.error("Error reading reconciliation counter from datastore", e);