2 * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.applications.southboundcli.cli;
10 import static org.opendaylight.openflowplugin.applications.southboundcli.util.ShellUtil.LINE_SEPARATOR;
12 import java.util.Formatter;
13 import java.util.List;
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;
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);
37 ReconcileService reconciliationService = null;
39 @Argument(name = "nodeId", description = "The NODE Id", multiValued = true)
42 @Option(name = "-all", description = "Reconcile all operative NODEs")
43 boolean reconcileAllNodes;
46 public Object execute() throws Exception {
47 if (reconciliationService == null) {
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);
58 final var rpcResult = rpcOutput.get();
59 if (rpcResult.isSuccessful()) {
60 session.getConsole().println("Reconciliation triggered for the node(s)");
61 printInProgressNodes(rpcResult.getResult());
63 session.getConsole().println(rpcResult.getErrors().stream().findFirst().orElseThrow().getMessage());
65 } catch (ExecutionException e) {
66 LOG.error("Error occurred while invoking reconcile RPC for node {}", nodes, e);
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);
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();