Make methods static
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / TransactionHistoryCmd.java
1 /*
2  * Copyright (c) 2017 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.ovsdb.hwvtepsouthbound;
9
10 import java.util.ArrayList;
11 import java.util.Date;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.stream.Collectors;
15 import org.apache.commons.lang3.tuple.ImmutablePair;
16 import org.apache.commons.lang3.tuple.Pair;
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.ovsdb.utils.mdsal.utils.TransactionHistory;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24
25 @Command(scope = "hwvtep", name = "txlog", description = "prints hwvtep tx log")
26 public class TransactionHistoryCmd extends OsgiCommandSupport {
27
28     @Option(name = "-nodeid", description = "Node Id",
29             required = false, multiValued = false)
30     String nodeid;
31     private static final String SEPERATOR = "#######################################################";
32
33     private final HwvtepSouthboundProvider hwvtepProvider;
34
35     public TransactionHistoryCmd(HwvtepSouthboundProvider hwvtepProvider) {
36         this.hwvtepProvider = hwvtepProvider;
37     }
38
39     @Override
40     protected Object doExecute() throws Exception {
41         Map<InstanceIdentifier<Node>, TransactionHistory> controllerTxLogs
42                 = hwvtepProvider.getHwvtepConnectionManager().getControllerTxHistory();
43         Map<InstanceIdentifier<Node>, TransactionHistory> deviceUpdateLogs
44                 = hwvtepProvider.getHwvtepConnectionManager().getDeviceUpdateHistory();
45         if (nodeid != null) {
46             InstanceIdentifier<Node> iid = HwvtepSouthboundMapper.createInstanceIdentifier(new NodeId(nodeid));
47             printLogs(controllerTxLogs, deviceUpdateLogs, iid);
48         } else {
49             Map<InstanceIdentifier<Node>, TransactionHistory> txlogs
50                     = controllerTxLogs.isEmpty() ? deviceUpdateLogs : controllerTxLogs;
51             txlogs.keySet().forEach(iid -> {
52                 printLogs(controllerTxLogs, deviceUpdateLogs, iid);
53             });
54             session.getConsole().println("Device tx logs size " + deviceUpdateLogs.keySet().size());
55         }
56         return null;
57     }
58
59     private void printLogs(Map<InstanceIdentifier<Node>, TransactionHistory> controllerTxLogs,
60                            Map<InstanceIdentifier<Node>, TransactionHistory> deviceUpdateLogs,
61                            InstanceIdentifier<Node> iid) {
62         session.getConsole().println(SEPERATOR + " START " + SEPERATOR);
63         List<HwvtepTransactionLogElement> controllerTxLog = controllerTxLogs.get(iid).getElements()
64                 .stream().map(ele -> new HwvtepTransactionLogElement(ele, false)).collect(Collectors.toList());
65         List<HwvtepTransactionLogElement> deviceUpdateLog = deviceUpdateLogs.get(iid).getElements()
66                 .stream().map(ele -> new HwvtepTransactionLogElement(ele, true)).collect(Collectors.toList());
67         List<Pair<HwvtepTransactionLogElement, Boolean>> allLogs = mergeLogsByDate(controllerTxLog, deviceUpdateLog);
68         session.getConsole().print("Printing for Node :  ");
69         session.getConsole().println(iid.firstKeyOf(Node.class).getNodeId().getValue());
70         printLogs(allLogs);
71         session.getConsole().println(SEPERATOR + " END " + SEPERATOR);
72         session.getConsole().println();
73     }
74
75     private void printLogs(List<Pair<HwvtepTransactionLogElement, Boolean>> logs) {
76         logs.forEach((pair) -> {
77             HwvtepTransactionLogElement log = pair.getLeft();
78             session.getConsole().print(new Date(log.getDate()));
79             session.getConsole().print(" ");
80             session.getConsole().print(pair.getRight() ? "CONTROLLER" : "DEVICE");
81             session.getConsole().print(" ");
82             session.getConsole().print(log.getTransactionType());
83             session.getConsole().print(" ");
84             session.getConsole().println(log.getData());
85         });
86     }
87
88     private static List<Pair<HwvtepTransactionLogElement, Boolean>> mergeLogsByDate(
89             List<HwvtepTransactionLogElement> logs1,
90             List<HwvtepTransactionLogElement> logs2) {
91
92         ArrayList<Pair<HwvtepTransactionLogElement, Boolean>> result = new ArrayList();
93         int firstIdx = 0;
94         int secondIdx = 0;
95         int firstSize = logs1.size();
96         int secondSize = logs2.size();
97         while (firstIdx < firstSize && secondIdx < secondSize) {
98             if (logs1.get(firstIdx).getDate() < logs2.get(secondIdx).getDate()) {
99                 result.add(ImmutablePair.of(logs1.get(firstIdx++), Boolean.TRUE));
100             } else {
101                 result.add(ImmutablePair.of(logs2.get(secondIdx++), Boolean.FALSE));
102             }
103         }
104         while (firstIdx < firstSize) {
105             result.add(ImmutablePair.of(logs1.get(firstIdx++), Boolean.TRUE));
106         }
107         while (secondIdx < secondSize) {
108             result.add(ImmutablePair.of(logs2.get(secondIdx++), Boolean.FALSE));
109         }
110         return result;
111     }
112 }