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