blob: 0335dff452f89217851c0c223f62353b43474ad9 [file] [log] [blame]
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -03001/*
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
17package org.opencord.dhcpl2relay.cli;
18
19
20import org.apache.karaf.shell.api.action.Argument;
21import org.apache.karaf.shell.api.action.Command;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030022import org.apache.karaf.shell.api.action.Option;
Jonathan Hart77ca3152020-02-21 14:31:21 -080023import org.apache.karaf.shell.api.action.lifecycle.Service;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030024import org.onosproject.cli.AbstractShellCommand;
25import org.opencord.dhcpl2relay.DhcpL2RelayEvent;
Jonathan Hart77ca3152020-02-21 14:31:21 -080026import org.opencord.dhcpl2relay.impl.DhcpL2RelayCounterNames;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030027import org.opencord.dhcpl2relay.impl.DhcpL2RelayCountersIdentifier;
28import org.opencord.dhcpl2relay.impl.DhcpL2RelayCountersStore;
29
30import java.util.Collections;
31import java.util.Map;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030032
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")
39public 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 Hart77ca3152020-02-21 14:31:21 -080059 DhcpL2RelayCounterNames counter = null;
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030060
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 Hart77ca3152020-02-21 14:31:21 -080071 if (reset) {
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -030072 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 Hart77ca3152020-02-21 14:31:21 -080085 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 Carreroeaf02b82019-11-25 13:34:25 -0300131 }
132
Jonathan Hart77ca3152020-02-21 14:31:21 -0800133 private void printCounter(DhcpL2RelayCounterNames counterNames, long value) {
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300134 // print in non-JSON format
Jonathan Hart77ca3152020-02-21 14:31:21 -0800135 print(" %s %s %-4d", counterNames,
136 String.join("", Collections.nCopies(50 - counterNames.toString().length(), ".")), value);
Marcos Aurelio Carreroeaf02b82019-11-25 13:34:25 -0300137 }
138}