Kalhee Kim | d94ceea | 2017-11-29 19:03:02 +0000 | [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.onosproject.dhcprelay.cli; |
| 18 | |
| 19 | import org.apache.karaf.shell.commands.Argument; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.dhcprelay.api.DhcpRelayService; |
| 23 | import org.onosproject.dhcprelay.store.DhcpRelayCounters; |
| 24 | import org.onosproject.dhcprelay.store.DhcpRelayCountersStore; |
| 25 | |
| 26 | |
| 27 | import java.util.Map; |
| 28 | import java.util.Optional; |
| 29 | |
| 30 | /** |
| 31 | * Prints Dhcp FPM Routes information. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "dhcp-relay-agg-counters", |
| 34 | description = "DHCP Relay Aggregate Counters cli.") |
| 35 | public class DhcpRelayAggCountersCommand extends AbstractShellCommand { |
| 36 | @Argument(index = 0, name = "reset", |
| 37 | description = "reset counters or not", |
| 38 | required = false, multiValued = false) |
| 39 | String reset = null; |
| 40 | |
| 41 | private static final String HEADER = "DHCP Relay Aggregate Counters :"; |
| 42 | private static final String GCOUNT = "global"; |
| 43 | private static final DhcpRelayService DHCP_RELAY_SERVICE = get(DhcpRelayService.class); |
| 44 | |
| 45 | @Override |
| 46 | protected void execute() { |
| 47 | boolean toResetFlag; |
| 48 | |
| 49 | if (reset != null) { |
| 50 | if (reset.equals("reset") || reset.equals("[reset]")) { |
| 51 | toResetFlag = true; |
| 52 | } else { |
| 53 | print("Last parameter is [reset]"); |
| 54 | return; |
| 55 | } |
| 56 | } else { |
| 57 | toResetFlag = false; |
| 58 | } |
| 59 | |
| 60 | print(HEADER); |
| 61 | |
| 62 | DhcpRelayCountersStore counterStore = AbstractShellCommand.get(DhcpRelayCountersStore.class); |
| 63 | |
| 64 | Optional<DhcpRelayCounters> perClassCounters = counterStore.getCounters(GCOUNT); |
| 65 | |
| 66 | if (perClassCounters.isPresent()) { |
| 67 | Map<String, Integer> counters = perClassCounters.get().getCounters(); |
| 68 | if (counters.size() > 0) { |
| 69 | counters.forEach((name, value) -> { |
| 70 | print("%-30s ............................ %-4d packets", name, value); |
| 71 | }); |
| 72 | } else { |
| 73 | print("No counter for {}", GCOUNT); |
| 74 | } |
| 75 | |
| 76 | if (toResetFlag) { |
| 77 | counterStore.resetCounters(GCOUNT); |
| 78 | |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |