Eliminate blueprint for southbound-cli commands
[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 LINE_SEPARATOR = "-".repeat(100);
36     public static final String NODE_PREFIX = "openflow:";
37
38     private ShellUtil() {
39     }
40
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);
45             return null;
46         } else {
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);
51                 return null;
52             } else {
53                 for (String port : nodeInfo.getPorts()) {
54                     ports.add(port);
55                 }
56                 return new OFNode(nodeId, nodeInfo.getNodeName(), ports);
57             }
58         }
59     }
60
61     public static OFNode getNodeInfo(final Long nodeId, final DataBroker broker) {
62         OFNode ofNode = null;
63         InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class)
64                 .child(Node.class, new NodeKey(new NodeId(NODE_PREFIX + nodeId))).build();
65
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();
70                 String name;
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();
76                 } else {
77                     LOG.error("Error while converting OFNode:{} to FlowCapableNode", node.getId());
78                     return null;
79                 }
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());
85                         return null;
86                     } else {
87                         String portName = flowCapableNodeConnector.getName();
88                         portList.add(portName);
89                     }
90                 }
91                 ofNode = new OFNode(nodeId, name, portList);
92             } else {
93                 LOG.error("OFNode with nodeId {} not present Inventory DS", nodeId);
94                 return null;
95             }
96         } catch (ExecutionException | InterruptedException e) {
97             LOG.error("Error reading node {} from Inventory DS", nodeId, e);
98         }
99         return ofNode;
100     }
101
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);
110             return List.of();
111         }
112     }
113 }