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 java.util.Formatter;
11 import java.util.List;
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;
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);
29 private ReconcileService reconciliationService = null;
31 public void setReconciliationService(final ReconcileService reconciliationService) {
32 this.reconciliationService = reconciliationService;
35 @Argument(name = "nodeId", description = "The NODE Id", multiValued = true)
38 @Option(name = "-all", description = "Reconcile all operative NODEs")
39 boolean reconcileAllNodes;
41 @SuppressWarnings("checkstyle:RegexpSinglelineJava")
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);
50 final var rpcResult = rpcOutput.get();
51 if (rpcResult.isSuccessful()) {
52 System.out.println("Reconciliation triggered for the node(s)");
53 printInProgressNodes(rpcResult.getResult());
55 System.out.println(rpcResult.getErrors().stream().findFirst().orElseThrow().getMessage());
57 } catch (ExecutionException e) {
58 LOG.error("Error occurred while invoking reconcile RPC for node {}", nodes, e);
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);
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();