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