alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
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; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Argument; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.meter.Meter; |
Jordi Ortiz | cfc7a61 | 2017-02-27 15:04:24 +0100 | [diff] [blame] | 26 | import org.onosproject.net.meter.MeterId; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 27 | import org.onosproject.net.meter.MeterService; |
| 28 | |
| 29 | import java.util.Collection; |
| 30 | |
Jordi Ortiz | cfc7a61 | 2017-02-27 15:04:24 +0100 | [diff] [blame] | 31 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 32 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 33 | /** |
sangyun-han | 483731c | 2016-06-06 12:03:16 +0900 | [diff] [blame] | 34 | * Lists all meters. |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 35 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 36 | @Service |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 37 | @Command(scope = "onos", name = "meters", |
| 38 | description = "Shows meters") |
sangyun-han | 483731c | 2016-06-06 12:03:16 +0900 | [diff] [blame] | 39 | public class MetersListCommand extends AbstractShellCommand { |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 40 | |
| 41 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 42 | required = false, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 43 | @Completion(DeviceIdCompleter.class) |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 44 | String uri = null; |
| 45 | |
Jordi Ortiz | cfc7a61 | 2017-02-27 15:04:24 +0100 | [diff] [blame] | 46 | @Argument(index = 1, name = "meter", description = "Meter ID", |
| 47 | required = false, multiValued = false) |
| 48 | String meterstr = null; |
| 49 | |
| 50 | MeterId meterId = null; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 51 | |
| 52 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 53 | protected void doExecute() { |
Jordi Ortiz | cfc7a61 | 2017-02-27 15:04:24 +0100 | [diff] [blame] | 54 | |
| 55 | if (!isNullOrEmpty(meterstr)) { |
| 56 | meterId = MeterId.meterId(Long.parseLong(meterstr)); |
| 57 | } |
| 58 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 59 | MeterService service = get(MeterService.class); |
| 60 | |
Jordi Ortiz | cfc7a61 | 2017-02-27 15:04:24 +0100 | [diff] [blame] | 61 | if (meterId != null && uri != null) { |
| 62 | Meter m = service.getMeter(DeviceId.deviceId(uri), meterId); |
| 63 | if (m != null) { |
| 64 | print("%s", m); |
| 65 | } else { |
| 66 | error("Meter %s not found for device %s", meterId, uri); |
| 67 | } |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 68 | } else { |
Jordi Ortiz | cfc7a61 | 2017-02-27 15:04:24 +0100 | [diff] [blame] | 69 | Collection<Meter> meters = service.getAllMeters(); |
| 70 | if (uri == null) { |
| 71 | printMeters(meters); |
| 72 | } else { |
| 73 | printMeters(Collections2.filter(meters, |
| 74 | m -> m.deviceId().equals(DeviceId.deviceId(uri)))); |
| 75 | } |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | private void printMeters(Collection<Meter> meters) { |
| 80 | meters.forEach(m -> print(" %s", m)); |
| 81 | } |
| 82 | } |