blob: 1eb1643385e8ac9fb7aa1c0c97b9bcb85913e6e3 [file] [log] [blame]
Madan Jampanibff6d8f2015-03-31 16:53:47 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Madan Jampanibff6d8f2015-03-31 16:53:47 -07003 *
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070022import org.onosproject.cli.AbstractShellCommand;
Madan Jampanicadd70b2016-02-08 13:45:43 -080023import org.onosproject.store.primitives.TransactionId;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070024import org.onosproject.store.service.StorageAdminService;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070025
26import com.fasterxml.jackson.databind.JsonNode;
27import com.fasterxml.jackson.databind.ObjectMapper;
28import com.fasterxml.jackson.databind.node.ArrayNode;
Madan Jampanibff6d8f2015-03-31 16:53:47 -070029
30/**
Madan Jampanicadd70b2016-02-08 13:45:43 -080031 * CLI to view in-progress database transactions in the system.
Madan Jampanibff6d8f2015-03-31 16:53:47 -070032 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070033@Service
Madan Jampanibff6d8f2015-03-31 16:53:47 -070034@Command(scope = "onos", name = "transactions",
Madan Jampanicadd70b2016-02-08 13:45:43 -080035 description = "Utility for listing pending/inprogress transactions")
Madan Jampanibff6d8f2015-03-31 16:53:47 -070036public class TransactionsCommand extends AbstractShellCommand {
37
Madan Jampanibff6d8f2015-03-31 16:53:47 -070038 /**
39 * Converts collection of transactions into a JSON object.
40 *
Madan Jampanicadd70b2016-02-08 13:45:43 -080041 * @param transactionIds transaction identifiers
Madan Jampanibff6d8f2015-03-31 16:53:47 -070042 */
Madan Jampanicadd70b2016-02-08 13:45:43 -080043 private JsonNode json(Collection<TransactionId> transactionIds) {
Madan Jampanibff6d8f2015-03-31 16:53:47 -070044 ObjectMapper mapper = new ObjectMapper();
45 ArrayNode txns = mapper.createArrayNode();
Madan Jampanicadd70b2016-02-08 13:45:43 -080046 transactionIds.forEach(id -> txns.add(id.toString()));
Madan Jampanibff6d8f2015-03-31 16:53:47 -070047 return txns;
48 }
49
50 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070051 protected void doExecute() {
Madan Jampanibff6d8f2015-03-31 16:53:47 -070052 StorageAdminService storageAdminService = get(StorageAdminService.class);
Madan Jampanicadd70b2016-02-08 13:45:43 -080053 Collection<TransactionId> transactionIds = storageAdminService.getPendingTransactions();
Madan Jampanibff6d8f2015-03-31 16:53:47 -070054 if (outputJson()) {
Madan Jampanicadd70b2016-02-08 13:45:43 -080055 print("%s", json(transactionIds));
Madan Jampanibff6d8f2015-03-31 16:53:47 -070056 } else {
Madan Jampanicadd70b2016-02-08 13:45:43 -080057 transactionIds.forEach(id -> print("%s", id.toString()));
Madan Jampanibff6d8f2015-03-31 16:53:47 -070058 }
59 }
60}