blob: c5e7fbf9833409d24e0eeb9e111d2fac2e6ac36e [file] [log] [blame]
alshabib58fe6dc2015-08-19 17:16:13 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
alshabib58fe6dc2015-08-19 17:16:13 -07003 *
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 */
16package org.onosproject.cli.net;
17
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070020import org.apache.karaf.shell.api.action.Completion;
pierventrec0914ec2021-08-27 15:25:02 +020021import org.apache.karaf.shell.api.action.Option;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
alshabib58fe6dc2015-08-19 17:16:13 -070023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.meter.Meter;
pierventrec0914ec2021-08-27 15:25:02 +020026import org.onosproject.net.meter.MeterCellId;
Jordi Ortizcfc7a612017-02-27 15:04:24 +010027import org.onosproject.net.meter.MeterId;
pierventrec0914ec2021-08-27 15:25:02 +020028import org.onosproject.net.meter.MeterScope;
alshabib58fe6dc2015-08-19 17:16:13 -070029import org.onosproject.net.meter.MeterService;
pierventrec0914ec2021-08-27 15:25:02 +020030import org.onosproject.net.pi.model.PiMeterId;
31import org.onosproject.net.pi.runtime.PiMeterCellId;
alshabib58fe6dc2015-08-19 17:16:13 -070032
33import java.util.Collection;
34
Jordi Ortizcfc7a612017-02-27 15:04:24 +010035import static com.google.common.base.Strings.isNullOrEmpty;
36
alshabib58fe6dc2015-08-19 17:16:13 -070037/**
sangyun-han483731c2016-06-06 12:03:16 +090038 * Lists all meters.
alshabib58fe6dc2015-08-19 17:16:13 -070039 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040@Service
alshabib58fe6dc2015-08-19 17:16:13 -070041@Command(scope = "onos", name = "meters",
42 description = "Shows meters")
sangyun-han483731c2016-06-06 12:03:16 +090043public class MetersListCommand extends AbstractShellCommand {
alshabib58fe6dc2015-08-19 17:16:13 -070044
45 @Argument(index = 0, name = "uri", description = "Device ID",
46 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070047 @Completion(DeviceIdCompleter.class)
alshabib58fe6dc2015-08-19 17:16:13 -070048 String uri = null;
49
pierventrec0914ec2021-08-27 15:25:02 +020050 @Argument(index = 1, name = "index", description = "Index",
Jordi Ortizcfc7a612017-02-27 15:04:24 +010051 required = false, multiValued = false)
pierventrec0914ec2021-08-27 15:25:02 +020052 private String indexString = null;
Jordi Ortizcfc7a612017-02-27 15:04:24 +010053
pierventrec0914ec2021-08-27 15:25:02 +020054 @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;
alshabib58fe6dc2015-08-19 17:16:13 -070061
62 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070063 protected void doExecute() {
Jordi Ortizcfc7a612017-02-27 15:04:24 +010064
pierventrec0914ec2021-08-27 15:25:02 +020065 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 Ortizcfc7a612017-02-27 15:04:24 +010082 }
83
alshabib58fe6dc2015-08-19 17:16:13 -070084 MeterService service = get(MeterService.class);
85
pierventrec0914ec2021-08-27 15:25:02 +020086 Collection<Meter> meters;
87 if (isNullOrEmpty(uri)) {
88 meters = service.getAllMeters();
89 printMeters(meters);
alshabib58fe6dc2015-08-19 17:16:13 -070090 } else {
pierventrec0914ec2021-08-27 15:25:02 +020091 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 Ortizcfc7a612017-02-27 15:04:24 +0100100 printMeters(meters);
101 } else {
pierventrec0914ec2021-08-27 15:25:02 +0200102 meters = service.getMeters(DeviceId.deviceId(uri));
103 printMeters(meters);
Jordi Ortizcfc7a612017-02-27 15:04:24 +0100104 }
alshabib58fe6dc2015-08-19 17:16:13 -0700105 }
106 }
107
108 private void printMeters(Collection<Meter> meters) {
109 meters.forEach(m -> print(" %s", m));
110 }
111}