Update MRI projects for Aluminium
[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.Collections;
14 import java.util.List;
15 import java.util.Map;
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;
34
35 public final class ShellUtil {
36     private static final Logger LOG = LoggerFactory.getLogger(ShellUtil.class);
37
38     public static final String NODE_PREFIX = "openflow:";
39
40     private ShellUtil() {
41     }
42
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());
47             dpnList.add(dpn);
48             LOG.trace("Added OFNode: {} to the list", dpn.getNodeId());
49         }
50         Collections.sort(dpnList);
51         return dpnList;
52     }
53
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);
58             return null;
59         } else {
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);
64                 return null;
65             } else {
66                 for (String port : nodeInfo.getPorts()) {
67                     ports.add(port);
68                 }
69                 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
70             }
71         }
72     }
73
74     public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
75         OFNode ofNode = null;
76         InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
77                 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
78
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();
83                 String name;
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();
89                 } else {
90                     LOG.error("Error while converting OFNode:{} to FlowCapableNode", node.getId());
91                     return null;
92                 }
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());
98                         return null;
99                     } else {
100                         String portName = flowCapableNodeConnector.getName();
101                         portList.add(portName);
102                     }
103                 }
104                 ofNode = new OFNode(nodeId, name, portList);
105             } else {
106                 LOG.error("OFNode with nodeId {} not present Inventory DS", nodeId);
107                 return null;
108             }
109         } catch (ExecutionException | InterruptedException e) {
110             LOG.error("Error reading node {} from Inventory DS", nodeId, e);
111         }
112         return ofNode;
113     }
114
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();
124             }
125         } catch (ExecutionException | InterruptedException | NullPointerException e) {
126             LOG.error("Error reading reconciliation counter from datastore", e);
127         }
128         return output;
129     }
130 }