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 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Completion; |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 21 | import org.apache.karaf.shell.api.action.Option; |
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; |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 26 | import org.onosproject.net.meter.MeterCellId; |
Jordi Ortiz | cfc7a61 | 2017-02-27 15:04:24 +0100 | [diff] [blame] | 27 | import org.onosproject.net.meter.MeterId; |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 28 | import org.onosproject.net.meter.MeterScope; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 29 | import org.onosproject.net.meter.MeterService; |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 30 | import org.onosproject.net.pi.model.PiMeterId; |
| 31 | import org.onosproject.net.pi.runtime.PiMeterCellId; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 32 | |
| 33 | import java.util.Collection; |
| 34 | |
Jordi Ortiz | cfc7a61 | 2017-02-27 15:04:24 +0100 | [diff] [blame] | 35 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 36 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 37 | /** |
sangyun-han | 483731c | 2016-06-06 12:03:16 +0900 | [diff] [blame] | 38 | * Lists all meters. |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 39 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 40 | @Service |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 41 | @Command(scope = "onos", name = "meters", |
| 42 | description = "Shows meters") |
sangyun-han | 483731c | 2016-06-06 12:03:16 +0900 | [diff] [blame] | 43 | public class MetersListCommand extends AbstractShellCommand { |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 44 | |
| 45 | @Argument(index = 0, name = "uri", description = "Device ID", |
| 46 | required = false, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 47 | @Completion(DeviceIdCompleter.class) |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 48 | String uri = null; |
| 49 | |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 50 | @Argument(index = 1, name = "index", description = "Index", |
Jordi Ortiz | cfc7a61 | 2017-02-27 15:04:24 +0100 | [diff] [blame] | 51 | required = false, multiValued = false) |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 52 | private String indexString = null; |
Jordi Ortiz | cfc7a61 | 2017-02-27 15:04:24 +0100 | [diff] [blame] | 53 | |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 54 | @Option(name = "-sc", aliases = "--scope", description = "Scope", |
| 55 | required = false, multiValued = false) |
| 56 | private String scopeString = null; |
| 57 | |
| 58 | MeterScope meterScope; |
| 59 | Long index; |
| 60 | MeterCellId meterCellId; |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 61 | |
| 62 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 63 | protected void doExecute() { |
Jordi Ortiz | cfc7a61 | 2017-02-27 15:04:24 +0100 | [diff] [blame] | 64 | |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 65 | if (!isNullOrEmpty(scopeString)) { |
| 66 | meterScope = MeterScope.of(scopeString); |
| 67 | } |
| 68 | |
| 69 | if (!isNullOrEmpty(indexString)) { |
| 70 | index = Long.parseLong(indexString); |
| 71 | if (meterScope == null) { |
| 72 | meterScope = MeterScope.globalScope(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | if (index != null) { |
| 77 | if (meterScope != null && meterScope.equals(MeterScope.globalScope())) { |
| 78 | meterCellId = MeterId.meterId(index); |
| 79 | } else if (meterScope != null) { |
| 80 | meterCellId = PiMeterCellId.ofIndirect(PiMeterId.of(meterScope.id()), index); |
| 81 | } |
Jordi Ortiz | cfc7a61 | 2017-02-27 15:04:24 +0100 | [diff] [blame] | 82 | } |
| 83 | |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 84 | MeterService service = get(MeterService.class); |
| 85 | |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 86 | Collection<Meter> meters; |
| 87 | if (isNullOrEmpty(uri)) { |
| 88 | meters = service.getAllMeters(); |
| 89 | printMeters(meters); |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 90 | } else { |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 91 | if (meterCellId != null) { |
| 92 | Meter m = service.getMeter(DeviceId.deviceId(uri), meterCellId); |
| 93 | if (m != null) { |
| 94 | print("%s", m); |
| 95 | } else { |
| 96 | error("Meter %s not found for device %s", meterCellId, uri); |
| 97 | } |
| 98 | } else if (meterScope != null) { |
| 99 | meters = service.getMeters(DeviceId.deviceId(uri), meterScope); |
Jordi Ortiz | cfc7a61 | 2017-02-27 15:04:24 +0100 | [diff] [blame] | 100 | printMeters(meters); |
| 101 | } else { |
pierventre | c0914ec | 2021-08-27 15:25:02 +0200 | [diff] [blame^] | 102 | meters = service.getMeters(DeviceId.deviceId(uri)); |
| 103 | printMeters(meters); |
Jordi Ortiz | cfc7a61 | 2017-02-27 15:04:24 +0100 | [diff] [blame] | 104 | } |
alshabib | 58fe6dc | 2015-08-19 17:16:13 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | private void printMeters(Collection<Meter> meters) { |
| 109 | meters.forEach(m -> print(" %s", m)); |
| 110 | } |
| 111 | } |