blob: 75da2be48ddb9552879e181963b67673b4efd2df [file] [log] [blame]
Madan Jampanibff6d8f2015-03-31 16:53:47 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.cli.net;
17
18import java.util.Collection;
19
20import org.apache.karaf.shell.commands.Command;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070021import org.onosproject.cli.AbstractShellCommand;
Madan Jampanicadd70b2016-02-08 13:45:43 -080022import org.onosproject.store.primitives.TransactionId;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070023import org.onosproject.store.service.StorageAdminService;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070024
25import com.fasterxml.jackson.databind.JsonNode;
26import com.fasterxml.jackson.databind.ObjectMapper;
27import com.fasterxml.jackson.databind.node.ArrayNode;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070028
29/**
Madan Jampanicadd70b2016-02-08 13:45:43 -080030 * CLI to view in-progress database transactions in the system.
Madan Jampanibff6d8f2015-03-31 16:53:47 -070031 */
32@Command(scope = "onos", name = "transactions",
Madan Jampanicadd70b2016-02-08 13:45:43 -080033 description = "Utility for listing pending/inprogress transactions")
Madan Jampanibff6d8f2015-03-31 16:53:47 -070034public class TransactionsCommand extends AbstractShellCommand {
35
Madan Jampanibff6d8f2015-03-31 16:53:47 -070036 /**
37 * Converts collection of transactions into a JSON object.
38 *
Madan Jampanicadd70b2016-02-08 13:45:43 -080039 * @param transactionIds transaction identifiers
Madan Jampanibff6d8f2015-03-31 16:53:47 -070040 */
Madan Jampanicadd70b2016-02-08 13:45:43 -080041 private JsonNode json(Collection<TransactionId> transactionIds) {
Madan Jampanibff6d8f2015-03-31 16:53:47 -070042 ObjectMapper mapper = new ObjectMapper();
43 ArrayNode txns = mapper.createArrayNode();
Madan Jampanicadd70b2016-02-08 13:45:43 -080044 transactionIds.forEach(id -> txns.add(id.toString()));
Madan Jampanibff6d8f2015-03-31 16:53:47 -070045 return txns;
46 }
47
48 @Override
49 protected void execute() {
50 StorageAdminService storageAdminService = get(StorageAdminService.class);
Madan Jampanicadd70b2016-02-08 13:45:43 -080051 Collection<TransactionId> transactionIds = storageAdminService.getPendingTransactions();
Madan Jampanibff6d8f2015-03-31 16:53:47 -070052 if (outputJson()) {
Madan Jampanicadd70b2016-02-08 13:45:43 -080053 print("%s", json(transactionIds));
Madan Jampanibff6d8f2015-03-31 16:53:47 -070054 } else {
Madan Jampanicadd70b2016-02-08 13:45:43 -080055 transactionIds.forEach(id -> print("%s", id.toString()));
Madan Jampanibff6d8f2015-03-31 16:53:47 -070056 }
57 }
58}