blob: 354ccbc3994d303b3347c57e4095fb812070e461 [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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
alshabib58fe6dc2015-08-19 17:16:13 -070022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.meter.Meter;
Jordi Ortizcfc7a612017-02-27 15:04:24 +010025import org.onosproject.net.meter.MeterId;
alshabib58fe6dc2015-08-19 17:16:13 -070026import org.onosproject.net.meter.MeterService;
27
28import java.util.Collection;
29
Jordi Ortizcfc7a612017-02-27 15:04:24 +010030import static com.google.common.base.Strings.isNullOrEmpty;
31
alshabib58fe6dc2015-08-19 17:16:13 -070032/**
sangyun-han483731c2016-06-06 12:03:16 +090033 * Lists all meters.
alshabib58fe6dc2015-08-19 17:16:13 -070034 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070035@Service
alshabib58fe6dc2015-08-19 17:16:13 -070036@Command(scope = "onos", name = "meters",
37 description = "Shows meters")
sangyun-han483731c2016-06-06 12:03:16 +090038public class MetersListCommand extends AbstractShellCommand {
alshabib58fe6dc2015-08-19 17:16:13 -070039
40 @Argument(index = 0, name = "uri", description = "Device ID",
41 required = false, multiValued = false)
42 String uri = null;
43
Jordi Ortizcfc7a612017-02-27 15:04:24 +010044 @Argument(index = 1, name = "meter", description = "Meter ID",
45 required = false, multiValued = false)
46 String meterstr = null;
47
48 MeterId meterId = null;
alshabib58fe6dc2015-08-19 17:16:13 -070049
50 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070051 protected void doExecute() {
Jordi Ortizcfc7a612017-02-27 15:04:24 +010052
53 if (!isNullOrEmpty(meterstr)) {
54 meterId = MeterId.meterId(Long.parseLong(meterstr));
55 }
56
alshabib58fe6dc2015-08-19 17:16:13 -070057 MeterService service = get(MeterService.class);
58
Jordi Ortizcfc7a612017-02-27 15:04:24 +010059 if (meterId != null && uri != null) {
60 Meter m = service.getMeter(DeviceId.deviceId(uri), meterId);
61 if (m != null) {
62 print("%s", m);
63 } else {
64 error("Meter %s not found for device %s", meterId, uri);
65 }
alshabib58fe6dc2015-08-19 17:16:13 -070066 } else {
Jordi Ortizcfc7a612017-02-27 15:04:24 +010067 Collection<Meter> meters = service.getAllMeters();
68 if (uri == null) {
69 printMeters(meters);
70 } else {
71 printMeters(Collections2.filter(meters,
72 m -> m.deviceId().equals(DeviceId.deviceId(uri))));
73 }
alshabib58fe6dc2015-08-19 17:16:13 -070074 }
75 }
76
77 private void printMeters(Collection<Meter> meters) {
78 meters.forEach(m -> print(" %s", m));
79 }
80}