Bump MRI upstreams
[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.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;
28
29 @Command(scope = "openflow", name = "reconcile", description = "Launch reconciliation for openflow nodes")
30 public class Reconciliation extends OsgiCommandSupport {
31
32     private static final Logger LOG = LoggerFactory.getLogger(Reconciliation.class);
33     private ReconciliationService reconciliationService;
34
35     public void setReconciliationService(ReconciliationService reconciliationService) {
36         this.reconciliationService = reconciliationService;
37     }
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     @SuppressWarnings("checkstyle:RegexpSinglelineJava")
46     @Override
47     protected Object doExecute() throws Exception {
48         Set<Uint64> nodes = nodeIds == null
49                 ? Set.of()
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);
55         try {
56             RpcResult<ReconcileOutput> rpcResult = rpcOutput.get();
57             if (rpcResult.isSuccessful()) {
58                 System.out.println("Reconciliation triggered for the node(s)");
59                 printInProgressNodes(rpcResult.getResult());
60             } else {
61                 System.out.println(rpcResult.getErrors().stream().findFirst().get().getMessage());
62             }
63         } catch (ExecutionException e) {
64             LOG.error("Error occurred while invoking reconcile RPC for node {}", nodes, e);
65         }
66         return null;
67     }
68
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);
80             }
81         }
82     }
83
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();
87         formatter.close();
88         return header;
89     }
90 }