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