16c9b953299fda788c919602183c938dc18a9aae
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / cli / ReconciliationCount.java
1 /*
2  * Copyright (c) 2018 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.cli;
10
11 import java.util.Collection;
12 import java.util.Formatter;
13 import org.apache.karaf.shell.commands.Command;
14 import org.apache.karaf.shell.console.OsgiCommandSupport;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.openflowplugin.applications.southboundcli.util.ShellUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.reconciliation.counter.ReconcileCounter;
18
19 @Command(scope = "openflow", name = "getReconciliationCount",
20         description = "Displays the number of reconciliation triggered for openflow nodes")
21 public class ReconciliationCount extends OsgiCommandSupport {
22
23     private DataBroker dataBroker;
24
25     public void setDataBroker(final DataBroker dataBroker) {
26         this.dataBroker = dataBroker;
27     }
28
29     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
30     @Override
31     protected Object doExecute() {
32         Collection<ReconcileCounter> result = ShellUtil.getReconcileCount(dataBroker);
33         if (result.isEmpty()) {
34             System.out.println("Reconciliation count not yet available for openflow nodes.");
35         } else {
36             StringBuilder stringBuilder = new StringBuilder();
37             final Formatter formatter = new Formatter(stringBuilder);
38             System.out.println(getReconcileCountHeaderOutput());
39             System.out.println("--------------------------------------------------------------------------"
40                     + "---------------------------");
41             for (ReconcileCounter reconcile : result) {
42                 System.out.println(formatter.format("%-15s %3s %-15s %9s %-20s %4s %-20s %n",
43                         reconcile.getNodeId(), "", reconcile.getSuccessCount(), "", reconcile.getFailureCount(), "",
44                         reconcile.getLastRequestTime().getValue()).toString());
45                 stringBuilder.setLength(0);
46             }
47             formatter.close();
48         }
49         return null;
50     }
51
52     private static String getReconcileCountHeaderOutput() {
53         final Formatter formatter = new Formatter();
54         String header = formatter.format("%-15s %3s %-15s %3s %-15s %3s %-15s %n", "NodeId", "",
55                 "ReconcileSuccessCount", "", "ReconcileFailureCount", "", "LastReconcileTime").toString();
56         formatter.close();
57         return header;
58     }
59 }