alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 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 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import com.google.common.collect.Collections2; |
| 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.net.DeviceId; |
| 23 | import org.onosproject.net.meter.Meter; |
| 24 | import org.onosproject.net.meter.MeterService; |
| 25 | |
| 26 | import java.util.Collection; |
| 27 | |
| 28 | /** |
sangyun-han | 483731c | 2016-06-06 12:03:16 +0900 | [diff] [blame] | 29 | * Lists all meters. |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 30 | */ |
| 31 | @Command(scope = "onos", name = "meters", |
| 32 | description = "Shows meters") |
sangyun-han | 483731c | 2016-06-06 12:03:16 +0900 | [diff] [blame] | 33 | public class MetersListCommand extends AbstractShellCommand { |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 34 | |
| 35 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 36 | required = false, multiValued = false) |
| 37 | String uri = null; |
| 38 | |
| 39 | |
| 40 | @Override |
| 41 | protected void execute() { |
| 42 | MeterService service = get(MeterService.class); |
| 43 | |
| 44 | Collection<Meter> meters = service.getAllMeters(); |
| 45 | if (uri != null) { |
| 46 | DeviceId deviceId = DeviceId.deviceId(uri); |
| 47 | Collection<Meter> devMeters = Collections2.filter(meters, |
| 48 | m -> m.deviceId().equals(deviceId)); |
| 49 | printMeters(devMeters); |
| 50 | } else { |
| 51 | printMeters(meters); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | private void printMeters(Collection<Meter> meters) { |
| 56 | meters.forEach(m -> print(" %s", m)); |
| 57 | } |
| 58 | } |