2 * Copyright (c) 2017 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.ovsdb.hwvtepsouthbound;
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;
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;
26 import java.util.stream.Collectors;
28 @Command(scope = "hwvtep", name = "txlog", description = "prints hwvtep tx log")
29 public class TransactionHistoryCmd extends OsgiCommandSupport {
31 @Option(name = "-nodeid", description = "Node Id",
32 required = false, multiValued = false)
35 private HwvtepSouthboundProvider hwvtepProvider;
36 private DataBroker dataBroker;
38 public void setDataBroker(DataBroker dataBroker) {
39 this.dataBroker = dataBroker;
42 public void setHwvtepProvider(HwvtepSouthboundProvider hwvtepProvider) {
43 this.hwvtepProvider = hwvtepProvider;
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();
53 InstanceIdentifier<Node> iid = HwvtepSouthboundMapper.createInstanceIdentifier(new NodeId(nodeid));
54 printLogs(controllerTxLogs, deviceUpdateLogs, iid);
56 Map<InstanceIdentifier<Node>, TransactionHistory> txlogs
57 = controllerTxLogs.isEmpty() ? deviceUpdateLogs : controllerTxLogs;
58 txlogs.keySet().forEach( (iid) -> {
59 printLogs(controllerTxLogs, deviceUpdateLogs, iid);
61 session.getConsole().println("Device tx logs size "+deviceUpdateLogs.keySet().size());
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);
83 private void sortLogsByDate(ArrayList<TransactionElement> logs) {
84 Collections.sort(logs, new Comparator<TransactionElement>() {
86 public int compare(TransactionElement o1, TransactionElement o2) {
87 return (int) (o1.getDate() - o2.getDate());
92 private List<HwvtepTransactionLogElement> mergeLogsByDate(
93 List<HwvtepTransactionLogElement> logs1,
94 List<HwvtepTransactionLogElement> logs2) {
96 ArrayList<HwvtepTransactionLogElement> result = new ArrayList();
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));
106 result.add(logs2.get(secondIdx));
110 while (firstIdx < firstSize) {
111 result.add(logs1.get(firstIdx));
114 while (secondIdx < secondSize) {
115 result.add(logs2.get(secondIdx));
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());