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 NODE_PREFIX = "openflow:";
40 public static OFNode getNode(final long nodeId, final DataBroker broker) {
41 OFNode nodeInfo = getNodeInfo(nodeId, broker);
42 if (nodeInfo == null) {
43 LOG.info("No ports exist for this node with nodeId {}", nodeId);
46 List<String> ports = new ArrayList<>();
47 // OFNode State is not provided by plugin, hence using null
48 if (nodeInfo.getPorts() == null) {
49 LOG.info("No ports exist for this node with nodeId {}", nodeId);
52 for (String port : nodeInfo.getPorts()) {
55 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
60 public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
62 InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
63 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
65 try (ReadTransaction tx = broker.newReadOnlyTransaction()) {
66 Optional<Node> result = tx.read(LogicalDatastoreType.OPERATIONAL, path).get();
67 if (result.isPresent()) {
68 Node node = result.orElseThrow();
70 Collection<NodeConnector> nodeConnectors = node.nonnullNodeConnector().values();
71 List<String> portList = new ArrayList<>();
72 FlowCapableNode flowCapableNode = node.<FlowCapableNode>augmentation(FlowCapableNode.class);
73 if (flowCapableNode != null) {
74 name = node.<FlowCapableNode>augmentation(FlowCapableNode.class).getDescription();
76 LOG.error("Error while converting OFNode:{} to FlowCapableNode", node.getId());
79 for (NodeConnector nodeConnector : nodeConnectors) {
80 FlowCapableNodeConnector flowCapableNodeConnector =
81 nodeConnector.augmentation(FlowCapableNodeConnector.class);
82 if (flowCapableNodeConnector == null) {
83 LOG.error("Error for OFNode:{} while reading nodeConnectors", node.getId());
86 String portName = flowCapableNodeConnector.getName();
87 portList.add(portName);
90 ofNode = new OFNode(nodeId, name, portList);
92 LOG.error("OFNode with nodeId {} not present Inventory DS", nodeId);
95 } catch (ExecutionException | InterruptedException e) {
96 LOG.error("Error reading node {} from Inventory DS", nodeId, e);
101 public static Collection<ReconcileCounter> getReconcileCount(final DataBroker dataBroker) {
102 InstanceIdentifier<ReconciliationCounter> instanceIdentifier = InstanceIdentifier
103 .builder(ReconciliationCounter.class).build();
104 try (ReadTransaction tx = dataBroker.newReadOnlyTransaction()) {
105 final var result = tx.read(LogicalDatastoreType.OPERATIONAL, instanceIdentifier).get();
106 return result.map(counter -> counter.nonnullReconcileCounter().values()).orElse(List.of());
107 } catch (ExecutionException | InterruptedException e) {
108 LOG.error("Error reading reconciliation counter from datastore", e);