blob: 72dca0dfa30c95f441db3ec01aae28ea37c5a520 [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
18import com.google.common.collect.Collections2;
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.meter.Meter;
Jordi Ortizcfc7a612017-02-27 15:04:24 +010024import org.onosproject.net.meter.MeterId;
alshabib58fe6dc2015-08-19 17:16:13 -070025import org.onosproject.net.meter.MeterService;
26
27import java.util.Collection;
28
Jordi Ortizcfc7a612017-02-27 15:04:24 +010029import static com.google.common.base.Strings.isNullOrEmpty;
30
alshabib58fe6dc2015-08-19 17:16:13 -070031/**
sangyun-han483731c2016-06-06 12:03:16 +090032 * Lists all meters.
alshabib58fe6dc2015-08-19 17:16:13 -070033 */
34@Command(scope = "onos", name = "meters",
35 description = "Shows meters")
sangyun-han483731c2016-06-06 12:03:16 +090036public class MetersListCommand extends AbstractShellCommand {
alshabib58fe6dc2015-08-19 17:16:13 -070037
38 @Argument(index = 0, name = "uri", description = "Device ID",
39 required = false, multiValued = false)
40 String uri = null;
41
Jordi Ortizcfc7a612017-02-27 15:04:24 +010042 @Argument(index = 1, name = "meter", description = "Meter ID",
43 required = false, multiValued = false)
44 String meterstr = null;
45
46 MeterId meterId = null;
alshabib58fe6dc2015-08-19 17:16:13 -070047
48 @Override
49 protected void execute() {
Jordi Ortizcfc7a612017-02-27 15:04:24 +010050
51 if (!isNullOrEmpty(meterstr)) {
52 meterId = MeterId.meterId(Long.parseLong(meterstr));
53 }
54
alshabib58fe6dc2015-08-19 17:16:13 -070055 MeterService service = get(MeterService.class);
56
Jordi Ortizcfc7a612017-02-27 15:04:24 +010057 if (meterId != null && uri != null) {
58 Meter m = service.getMeter(DeviceId.deviceId(uri), meterId);
59 if (m != null) {
60 print("%s", m);
61 } else {
62 error("Meter %s not found for device %s", meterId, uri);
63 }
alshabib58fe6dc2015-08-19 17:16:13 -070064 } else {
Jordi Ortizcfc7a612017-02-27 15:04:24 +010065 Collection<Meter> meters = service.getAllMeters();
66 if (uri == null) {
67 printMeters(meters);
68 } else {
69 printMeters(Collections2.filter(meters,
70 m -> m.deviceId().equals(DeviceId.deviceId(uri))));
71 }
alshabib58fe6dc2015-08-19 17:16:13 -070072 }
73 }
74
75 private void printMeters(Collection<Meter> meters) {
76 meters.forEach(m -> print(" %s", m));
77 }
78}