Added device transaction log cli
[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 org.apache.karaf.shell.commands.Command;
11 import org.apache.karaf.shell.commands.Option;
12 import org.apache.karaf.shell.console.OsgiCommandSupport;
13 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
14 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionElement;
15 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionHistory;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
18 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Comparator;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.stream.Collectors;
27
28 @Command(scope = "hwvtep", name = "txlog", description = "prints hwvtep tx log")
29 public class TransactionHistoryCmd extends OsgiCommandSupport {
30
31     @Option(name = "-nodeid", description = "Node Id",
32             required = false, multiValued = false)
33     String nodeid;
34
35     private HwvtepSouthboundProvider hwvtepProvider;
36     private DataBroker dataBroker;
37
38     public void setDataBroker(DataBroker dataBroker) {
39         this.dataBroker = dataBroker;
40     }
41
42     public void setHwvtepProvider(HwvtepSouthboundProvider hwvtepProvider) {
43         this.hwvtepProvider = hwvtepProvider;
44     }
45
46     @Override
47     protected Object doExecute() throws Exception {
48         Map<InstanceIdentifier<Node>, TransactionHistory> controllerTxLogs
49                 = hwvtepProvider.getHwvtepConnectionManager().getControllerTxHistory();
50         Map<InstanceIdentifier<Node>, TransactionHistory> deviceUpdateLogs
51                 = hwvtepProvider.getHwvtepConnectionManager().getDeviceUpdateHistory();
52         if (nodeid != null) {
53             InstanceIdentifier<Node> iid = HwvtepSouthboundMapper.createInstanceIdentifier(new NodeId(nodeid));
54             printLogs(controllerTxLogs, deviceUpdateLogs, iid);
55         } else {
56             Map<InstanceIdentifier<Node>, TransactionHistory> txlogs
57                     = controllerTxLogs.isEmpty() ? deviceUpdateLogs : controllerTxLogs;
58             txlogs.keySet().forEach( (iid) -> {
59                 printLogs(controllerTxLogs, deviceUpdateLogs, iid);
60             });
61             session.getConsole().println("Device tx logs size "+deviceUpdateLogs.keySet().size());
62         }
63         return null;
64     }
65
66     private void printLogs(Map<InstanceIdentifier<Node>, TransactionHistory> controllerTxLogs,
67                            Map<InstanceIdentifier<Node>, TransactionHistory> deviceUpdateLogs,
68                            InstanceIdentifier<Node> iid) {
69         session.getConsole().println("Printing for iid "+ iid);
70         List<HwvtepTransactionLogElement> controllerTxLog = controllerTxLogs.get(iid).getElements()
71                 .stream().map(ele -> new HwvtepTransactionLogElement(ele, false)).collect(Collectors.toList());
72         List<HwvtepTransactionLogElement> deviceUpdateLog = deviceUpdateLogs.get(iid).getElements()
73                 .stream().map(ele -> new HwvtepTransactionLogElement(ele, false)).collect(Collectors.toList());
74         //deviceUpdateLog.forEach( (log) -> log.setDeviceLog(true));
75         List<HwvtepTransactionLogElement> allLogs = mergeLogsByDate(controllerTxLog, deviceUpdateLog);
76         session.getConsole().println("======================================");
77         session.getConsole().println("======================================");
78         session.getConsole().print("printing logs for node ");
79         session.getConsole().println(iid);
80         printLogs(allLogs);
81     }
82
83     private void sortLogsByDate(ArrayList<TransactionElement> logs) {
84         Collections.sort(logs, new Comparator<TransactionElement>() {
85             @Override
86             public int compare(TransactionElement o1, TransactionElement o2) {
87                 return (int) (o1.getDate() - o2.getDate());
88             }
89         });
90     }
91
92     private List<HwvtepTransactionLogElement> mergeLogsByDate(
93             List<HwvtepTransactionLogElement> logs1,
94             List<HwvtepTransactionLogElement> logs2) {
95
96         ArrayList<HwvtepTransactionLogElement> result = new ArrayList();
97         int firstIdx = 0;
98         int secondIdx = 0;
99         int firstSize = logs1.size();
100         int secondSize = logs2.size();
101         while ( firstIdx < firstSize && secondIdx < secondSize) {
102             if (logs1.get(firstIdx).getDate() < logs2.get(secondIdx).getDate()) {
103                 result.add(logs1.get(firstIdx));
104                 firstIdx++;
105             } else {
106                 result.add(logs2.get(secondIdx));
107                 secondIdx++;
108             }
109         }
110         while (firstIdx < firstSize) {
111             result.add(logs1.get(firstIdx));
112             firstIdx++;
113         }
114         while (secondIdx < secondSize) {
115             result.add(logs2.get(secondIdx));
116             secondIdx++;
117         }
118         return result;
119     }
120
121     private void printLogs(List<HwvtepTransactionLogElement> logs) {
122         logs.forEach( (log) -> {
123             session.getConsole().print(new Date(log.getDate()));
124             session.getConsole().print(" ");
125             session.getConsole().print(log.getTransactionType());
126             session.getConsole().print(" ");
127             session.getConsole().println(log.getData());
128         });
129     }
130 }