Migrate OSGI compendium reference
[controller.git] / opendaylight / md-sal / mdsal-trace / cli / src / main / java / org / opendaylight / controller / md / sal / trace / cli / PrintOpenTransactionsCommand.java
1 /*
2  * Copyright (c) 2017 Red Hat, Inc. 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.controller.md.sal.trace.cli;
9
10 import java.util.List;
11 import org.apache.karaf.shell.api.action.Action;
12 import org.apache.karaf.shell.api.action.Argument;
13 import org.apache.karaf.shell.api.action.Command;
14 import org.apache.karaf.shell.api.action.lifecycle.Reference;
15 import org.apache.karaf.shell.api.action.lifecycle.Service;
16 import org.opendaylight.controller.md.sal.trace.api.TracingDOMDataBroker;
17
18 /**
19  * Karaf CLI command to dump all open transactions.
20  *
21  * @author Michael Vorburger.ch
22  */
23 @Service
24 @Command(scope = "trace", name = "transactions",
25     description = "Show all (still) open transactions; including stack trace of creator, "
26     + "if transaction-debug-context-enabled is true in mdsaltrace_config.xml")
27 public class PrintOpenTransactionsCommand implements Action {
28
29     @Argument(index = 0, name = "minOpenTransactions", required = false, multiValued = false,
30             description = "Minimum open number of transactions (leaks with fewer are suppressed)")
31     Integer minOpenTransactions = 1;
32
33     @Reference
34     private List<TracingDOMDataBroker> tracingDOMDataBrokers;
35
36     // NB: Do NOT have a non-default constructor for injection of @Reference
37     // Karaf needs a default constructor to create the command - and it works as is.
38
39     @Override
40     @SuppressWarnings("checkstyle:RegexpSingleLineJava")
41     public Object execute() {
42         boolean hasFound = false;
43         for (TracingDOMDataBroker tracingDOMDataBroker : tracingDOMDataBrokers) {
44             hasFound |= tracingDOMDataBroker.printOpenTransactions(System.out, minOpenTransactions);
45         }
46         if (hasFound) {
47             System.out.println(
48                     "Actually did find real leaks with more than " + minOpenTransactions + " open transactions");
49         } else {
50             System.out.println(
51                     "Did not find any real leaks with more than " + minOpenTransactions + " open transactions");
52         }
53         return hasFound;
54     }
55
56 }