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.List;
14 import java.util.Optional;
15 import java.util.concurrent.ExecutionException;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.ReadTransaction;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconciliationCounter;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.reconciliation.counter.ReconcileCounter;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public final class ShellUtil {
33 private static final Logger LOG = LoggerFactory.getLogger(ShellUtil.class);
35 public static final String LINE_SEPARATOR = "-".repeat(100);
36 public static final String NODE_PREFIX = "openflow:";
41 public static OFNode getNode(final long nodeId, final DataBroker broker) {
42 OFNode nodeInfo = getNodeInfo(nodeId, broker);
43 if (nodeInfo == null) {
44 LOG.info("No ports exist for this node with nodeId {}", nodeId);
47 List<String> ports = new ArrayList<>();
48 // OFNode State is not provided by plugin, hence using null
49 if (nodeInfo.getPorts() == null) {
50 LOG.info("No ports exist for this node with nodeId {}", nodeId);
53 for (String port : nodeInfo.getPorts()) {
56 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
61 public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
63 InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
64 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
66 try (ReadTransaction tx = broker.newReadOnlyTransaction()) {
67 Optional<Node> result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get();
68 if (result.isPresent()) {
69 Node node = result.orElseThrow();
71 Collection<NodeConnector> nodeConnectors = node.nonnullNodeConnector().values();
72 List<String> portList = new ArrayList<>();
73 FlowCapableNode flowCapableNode = node.<FlowCapableNode>augmentation(FlowCapableNode.class);
74 if (flowCapableNode != null) {
75 name = node.<FlowCapableNode>augmentation(FlowCapableNode.class).getDescription();
77 LOG.error("Error while converting OFNode:{} to FlowCapableNode", node.getId());
80 for (NodeConnector nodeConnector : nodeConnectors) {
81 FlowCapableNodeConnector flowCapableNodeConnector =
82 nodeConnector.augmentation(FlowCapableNodeConnector.class);
83 if (flowCapableNodeConnector == null) {
84 LOG.error("Error for OFNode:{} while reading nodeConnectors", node.getId());
87 String portName = flowCapableNodeConnector.getName();
88 portList.add(portName);
91 ofNode = new OFNode(nodeId, name, portList);
93 LOG.error("OFNode with nodeId {} not present Inventory DS", nodeId);
96 } catch (ExecutionException | InterruptedException e) {
97 LOG.error("Error reading node {} from Inventory DS", nodeId, e);
102 public static Collection<ReconcileCounter> getReconcileCount(final DataBroker dataBroker) {
103 InstanceIdentifier<ReconciliationCounter> instanceIdentifier = InstanceIdentifier
104 .builder(ReconciliationCounter.class).build();
105 try (ReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
106 final var result = tx.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier).get();
107 return result.map(counter -> counter.nonnullReconcileCounter().values()).orElse(List.of());
108 } catch (ExecutionException | InterruptedException e) {
109 LOG.error("Error reading reconciliation counter from datastore", e);