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.concurrent.Future;
15 import java.util.stream.Collectors;
16 import org.apache.karaf.shell.commands.Argument;
17 import org.apache.karaf.shell.commands.Command;
18 import org.apache.karaf.shell.commands.Option;
19 import org.apache.karaf.shell.console.OsgiCommandSupport;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconcileInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconcileInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconcileOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconciliationService;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.common.Uint64;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 @Command(scope = "openflow", name = "reconcile", description = "Launch reconciliation for openflow nodes")
30 public class Reconciliation extends OsgiCommandSupport {
32 private static final Logger LOG = LoggerFactory.getLogger(Reconciliation.class);
33 private ReconciliationService reconciliationService;
35 public void setReconciliationService(ReconciliationService reconciliationService) {
36 this.reconciliationService = reconciliationService;
39 @Argument(name = "nodeId", description = "The NODE Id", multiValued = true)
42 @Option(name = "-all", description = "Reconcile all operative NODEs")
43 boolean reconcileAllNodes;
45 @SuppressWarnings("checkstyle:RegexpSinglelineJava")
47 protected Object doExecute() throws Exception {
48 Set<Uint64> nodes = nodeIds == null
50 : nodeIds.stream().distinct().map(Uint64::valueOf).collect(Collectors.toUnmodifiableSet());
51 LOG.debug("Triggering reconciliation for nodes {}", nodes);
52 ReconcileInput rpcInput = new ReconcileInputBuilder().setNodes(nodes)
53 .setReconcileAllNodes(reconcileAllNodes).build();
54 Future<RpcResult<ReconcileOutput>> rpcOutput = reconciliationService.reconcile(rpcInput);
56 RpcResult<ReconcileOutput> rpcResult = rpcOutput.get();
57 if (rpcResult.isSuccessful()) {
58 System.out.println("Reconciliation triggered for the node(s)");
59 printInProgressNodes(rpcResult.getResult());
61 System.out.println(rpcResult.getErrors().stream().findFirst().get().getMessage());
63 } catch (ExecutionException e) {
64 LOG.error("Error occurred while invoking reconcile RPC for node {}", nodes, e);
69 @SuppressWarnings("checkstyle:RegexpSinglelineJava")
70 private static void printInProgressNodes(ReconcileOutput reconcileOutput) {
71 Set<Uint64> inprogressNodes = reconcileOutput.getInprogressNodes();
72 if (inprogressNodes.size() > 0) {
73 StringBuilder stringBuilder = new StringBuilder();
74 final Formatter formatter = new Formatter(stringBuilder);
75 System.out.println(getReconcileHeaderOutput());
76 System.out.println("----------------------------------------------------");
77 for (Uint64 node : inprogressNodes) {
78 System.out.println(formatter.format("%-15s %n",node).toString());
79 stringBuilder.setLength(0);
84 private static String getReconcileHeaderOutput() {
85 final Formatter formatter = new Formatter();
86 String header = formatter.format("%-15s %n", "Reconciliation already InProgress for below node(s)").toString();