Madan Jampani | bff6d8f | 2015-03-31 16:53:47 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.apache.karaf.shell.commands.Option; |
| 22 | import org.onlab.util.Tools; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.store.service.StorageAdminService; |
| 25 | import org.onosproject.store.service.Transaction; |
| 26 | |
| 27 | import com.fasterxml.jackson.databind.JsonNode; |
| 28 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 29 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 30 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 31 | |
| 32 | /** |
| 33 | * CLI to work with database transactions in the system. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "transactions", |
| 36 | description = "Utility for viewing and redriving database transactions") |
| 37 | public class TransactionsCommand extends AbstractShellCommand { |
| 38 | |
| 39 | @Option(name = "-r", aliases = "--redrive", |
| 40 | description = "Redrive stuck transactions while removing those that are done", |
| 41 | required = false, multiValued = false) |
| 42 | private boolean redrive = false; |
| 43 | |
| 44 | private static final String FMT = "%-20s %-15s %-10s"; |
| 45 | |
| 46 | /** |
| 47 | * Displays transactions as text. |
| 48 | * |
| 49 | * @param transactions transactions |
| 50 | */ |
| 51 | private void displayTransactions(Collection<Transaction> transactions) { |
| 52 | print("---------------------------------------------"); |
| 53 | print(FMT, "Id", "State", "Updated"); |
| 54 | print("---------------------------------------------"); |
| 55 | transactions.forEach(txn -> print(FMT, txn.id(), txn.state(), Tools.timeAgo(txn.lastUpdated()))); |
| 56 | if (transactions.size() > 0) { |
| 57 | print("---------------------------------------------"); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Converts collection of transactions into a JSON object. |
| 63 | * |
| 64 | * @param transactions transactions |
| 65 | */ |
| 66 | private JsonNode json(Collection<Transaction> transactions) { |
| 67 | ObjectMapper mapper = new ObjectMapper(); |
| 68 | ArrayNode txns = mapper.createArrayNode(); |
| 69 | |
| 70 | // Create a JSON node for each transaction |
| 71 | transactions.stream().forEach(txn -> { |
| 72 | ObjectNode txnNode = mapper.createObjectNode(); |
| 73 | txnNode.put("id", txn.id()) |
| 74 | .put("state", txn.state().toString()) |
| 75 | .put("lastUpdated", txn.lastUpdated()); |
| 76 | txns.add(txnNode); |
| 77 | }); |
| 78 | |
| 79 | return txns; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected void execute() { |
| 84 | StorageAdminService storageAdminService = get(StorageAdminService.class); |
| 85 | |
| 86 | if (redrive) { |
| 87 | storageAdminService.redriveTransactions(); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | Collection<Transaction> transactions = storageAdminService.getTransactions(); |
| 92 | if (outputJson()) { |
| 93 | print("%s", json(transactions)); |
| 94 | } else { |
| 95 | displayTransactions(transactions); |
| 96 | } |
| 97 | } |
| 98 | } |