9a91a4fe6b1589aded8a2c9e6455a8170967415d
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / util / ShellUtil.java
1 /*
2  * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowplugin.applications.southboundcli.util;
10
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;
31
32 public final class ShellUtil {
33     private static final Logger LOG = LoggerFactory.getLogger(ShellUtil.class);
34
35     public static final String NODE_PREFIX = "openflow:";
36
37     private ShellUtil() {
38     }
39
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);
44             return null;
45         } else {
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);
50                 return null;
51             } else {
52                 for (String port : nodeInfo.getPorts()) {
53                     ports.add(port);
54                 }
55                 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
56             }
57         }
58     }
59
60     public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
61         OFNode ofNode = null;
62         InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
63                 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
64
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();
69                 String name;
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();
75                 } else {
76                     LOG.error("Error while converting OFNode:{} to FlowCapableNode", node.getId());
77                     return null;
78                 }
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());
84                         return null;
85                     } else {
86                         String portName = flowCapableNodeConnector.getName();
87                         portList.add(portName);
88                     }
89                 }
90                 ofNode = new OFNode(nodeId, name, portList);
91             } else {
92                 LOG.error("OFNode with nodeId {} not present Inventory DS", nodeId);
93                 return null;
94             }
95         } catch (ExecutionException | InterruptedException e) {
96             LOG.error("Error reading node {} from Inventory DS", nodeId, e);
97         }
98         return ofNode;
99     }
100
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);
109             return List.of();
110         }
111     }
112 }