7d4adbeae13fc0ebe158dc3bb5e535c60350af04
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / cli / Reconciliation.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 package org.opendaylight.openflowplugin.applications.southboundcli.cli;
9
10 import java.util.Formatter;
11 import java.util.List;
12 import java.util.Set;
13 import java.util.concurrent.ExecutionException;
14 import java.util.stream.Collectors;
15 import org.apache.karaf.shell.commands.Argument;
16 import org.apache.karaf.shell.commands.Command;
17 import org.apache.karaf.shell.commands.Option;
18 import org.apache.karaf.shell.console.OsgiCommandSupport;
19 import org.opendaylight.openflowplugin.applications.southboundcli.ReconcileService;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconcileOutput;
21 import org.opendaylight.yangtools.yang.common.Uint64;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 @Command(scope = "openflow", name = "reconcile", description = "Launch reconciliation for openflow nodes")
26 public class Reconciliation extends OsgiCommandSupport {
27     private static final Logger LOG = LoggerFactory.getLogger(Reconciliation.class);
28
29     private ReconcileService reconciliationService = null;
30
31     public void setReconciliationService(final ReconcileService reconciliationService) {
32         this.reconciliationService = reconciliationService;
33     }
34
35     @Argument(name = "nodeId", description = "The NODE Id", multiValued = true)
36     List<Long> nodeIds;
37
38     @Option(name = "-all", description = "Reconcile all operative NODEs")
39     boolean reconcileAllNodes;
40
41     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
42     @Override
43     protected Object doExecute() throws Exception {
44         final var nodes = nodeIds == null ? Set.<Uint64>of()
45             : nodeIds.stream().map(Uint64::valueOf).collect(Collectors.toSet());
46         final var rpcOutput = reconcileAllNodes ? reconciliationService.reconcileAll()
47             : reconciliationService.reconcile(nodes);
48         LOG.debug("Triggering reconciliation for nodes {}", nodes);
49         try {
50             final var rpcResult = rpcOutput.get();
51             if (rpcResult.isSuccessful()) {
52                 System.out.println("Reconciliation triggered for the node(s)");
53                 printInProgressNodes(rpcResult.getResult());
54             } else {
55                 System.out.println(rpcResult.getErrors().stream().findFirst().orElseThrow().getMessage());
56             }
57         } catch (ExecutionException e) {
58             LOG.error("Error occurred while invoking reconcile RPC for node {}", nodes, e);
59         }
60         return null;
61     }
62
63     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
64     private static void printInProgressNodes(final ReconcileOutput reconcileOutput) {
65         final var inprogressNodes = reconcileOutput.getInprogressNodes();
66         if (inprogressNodes.size() > 0) {
67             final var stringBuilder = new StringBuilder();
68             try (var formatter = new Formatter(stringBuilder)) {
69                 System.out.println(getReconcileHeaderOutput());
70                 System.out.println("----------------------------------------------------");
71                 for (Uint64 node : inprogressNodes) {
72                     System.out.println(formatter.format("%-15s %n",node).toString());
73                     stringBuilder.setLength(0);
74                 }
75             }
76         }
77     }
78
79     private static String getReconcileHeaderOutput() {
80         try (var formatter = new Formatter()) {
81             return formatter.format("%-15s %n", "Reconciliation already InProgress for below node(s)").toString();
82         }
83     }
84 }