Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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 | |
| 17 | package org.opencord.dhcpl2relay.cli; |
| 18 | |
| 19 | |
| 20 | import org.apache.karaf.shell.api.action.Argument; |
| 21 | import org.apache.karaf.shell.api.action.Command; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Option; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 24 | import org.onosproject.cli.AbstractShellCommand; |
| 25 | import org.opencord.dhcpl2relay.DhcpL2RelayEvent; |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 26 | import org.opencord.dhcpl2relay.impl.DhcpL2RelayCounterNames; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 27 | import org.opencord.dhcpl2relay.impl.DhcpL2RelayCountersIdentifier; |
| 28 | import org.opencord.dhcpl2relay.impl.DhcpL2RelayCountersStore; |
| 29 | |
| 30 | import java.util.Collections; |
| 31 | import java.util.Map; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Display/Reset the DHCP L2 relay application statistics. |
| 35 | */ |
| 36 | @Service |
| 37 | @Command(scope = "onos", name = "dhcpl2relay-stats", |
| 38 | description = "Display or Reset the DHCP L2 relay application statistics") |
| 39 | public class DhcpL2RelayStatsCommand extends AbstractShellCommand { |
| 40 | private static final String CONFIRM_PHRASE = "please"; |
| 41 | |
| 42 | @Option(name = "-r", aliases = "--reset", description = "Reset the counter[s]\n" + |
| 43 | "(WARNING!!!: In case no counter name is explicitly specified, all DHCP L2 Relay counters will be reset).", |
| 44 | required = false, multiValued = false) |
| 45 | private boolean reset = false; |
| 46 | |
| 47 | @Option(name = "-s", aliases = "--subscriberId", description = "Subscriber Id\n", |
| 48 | required = false, multiValued = false) |
| 49 | private String subscriberId = null; |
| 50 | |
| 51 | @Option(name = "-p", aliases = "--please", description = "Confirmation phrase", |
| 52 | required = false, multiValued = false) |
| 53 | String please = null; |
| 54 | |
| 55 | @Argument(index = 0, name = "counter", |
| 56 | description = "The counter to display (or reset). In case not specified, all counters\nwill be " + |
| 57 | "displayed (or reset in case the -r option is specified).", |
| 58 | required = false, multiValued = false) |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 59 | DhcpL2RelayCounterNames counter = null; |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 60 | |
| 61 | @Override |
| 62 | protected void doExecute() { |
| 63 | DhcpL2RelayCountersStore dhcpCounters = AbstractShellCommand.get( |
| 64 | DhcpL2RelayCountersStore.class); |
| 65 | |
| 66 | if ((subscriberId == null) || (subscriberId.equals("global"))) { |
| 67 | // All subscriber Ids |
| 68 | subscriberId = DhcpL2RelayEvent.GLOBAL_COUNTER; |
| 69 | } |
| 70 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 71 | if (reset) { |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 72 | if (please == null || !please.equals(CONFIRM_PHRASE)) { |
| 73 | print("WARNING: Be aware that you are going to reset the counters. " + |
| 74 | "Enter confirmation phrase to continue."); |
| 75 | return; |
| 76 | } |
| 77 | if (counter == null) { |
| 78 | // Reset all global counters |
| 79 | dhcpCounters.resetCounters(subscriberId); |
| 80 | } else { |
| 81 | // Reset the specified counter |
| 82 | dhcpCounters.setCounter(subscriberId, counter, (long) 0); |
| 83 | } |
| 84 | } else { |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 85 | Map<DhcpL2RelayCountersIdentifier, Long> countersMap = dhcpCounters.getCounters().counters(); |
| 86 | if (countersMap.size() > 0) { |
| 87 | if (counter == null) { |
| 88 | String jsonString = ""; |
| 89 | if (outputJson()) { |
| 90 | jsonString = String.format("{\"%s\":{", dhcpCounters.NAME); |
| 91 | } else { |
| 92 | print("%s [%s] :", dhcpCounters.NAME, subscriberId); |
| 93 | } |
| 94 | DhcpL2RelayCounterNames[] counters = DhcpL2RelayCounterNames.values(); |
| 95 | for (int i = 0; i < counters.length; i++) { |
| 96 | DhcpL2RelayCounterNames counterType = counters[i]; |
| 97 | Long value = countersMap.get(new DhcpL2RelayCountersIdentifier(subscriberId, counterType)); |
| 98 | if (value == null) { |
| 99 | value = 0L; |
| 100 | } |
| 101 | if (outputJson()) { |
| 102 | jsonString += String.format("\"%s\":%d", counterType, value); |
| 103 | if (i < counters.length - 1) { |
| 104 | jsonString += ","; |
| 105 | } |
| 106 | } else { |
| 107 | printCounter(counterType, value); |
| 108 | } |
| 109 | } |
| 110 | if (outputJson()) { |
| 111 | jsonString += "}}"; |
| 112 | print("%s", jsonString); |
| 113 | } |
| 114 | } else { |
| 115 | // Show only the specified counter |
| 116 | Long value = countersMap.get(new DhcpL2RelayCountersIdentifier(subscriberId, counter)); |
| 117 | if (value == null) { |
| 118 | value = 0L; |
| 119 | } |
| 120 | if (outputJson()) { |
| 121 | print("{\"%s\":%d}", counter, value); |
| 122 | } else { |
| 123 | printCounter(counter, value); |
| 124 | } |
| 125 | } |
| 126 | } else { |
| 127 | print("No DHCP L2 Relay Counters were created yet for counter class [%s]", |
| 128 | DhcpL2RelayEvent.GLOBAL_COUNTER); |
| 129 | } |
| 130 | } |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 131 | } |
| 132 | |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 133 | private void printCounter(DhcpL2RelayCounterNames counterNames, long value) { |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 134 | // print in non-JSON format |
Jonathan Hart | 77ca315 | 2020-02-21 14:31:21 -0800 | [diff] [blame^] | 135 | print(" %s %s %-4d", counterNames, |
| 136 | String.join("", Collections.nCopies(50 - counterNames.toString().length(), ".")), value); |
Marcos Aurelio Carrero | eaf02b8 | 2019-11-25 13:34:25 -0300 | [diff] [blame] | 137 | } |
| 138 | } |