blob: 4fe3590815b03e4133192bb22a661afaa027bef8 [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;
Ray Milkey0068fd02018-10-11 15:45:39 -070021import org.apache.karaf.shell.api.action.Completion;
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;
Jordi Ortizcfc7a612017-02-27 15:04:24 +010026import org.onosproject.net.meter.MeterId;
alshabib58fe6dc2015-08-19 17:16:13 -070027import org.onosproject.net.meter.MeterService;
28
29import java.util.Collection;
30
Jordi Ortizcfc7a612017-02-27 15:04:24 +010031import static com.google.common.base.Strings.isNullOrEmpty;
32
alshabib58fe6dc2015-08-19 17:16:13 -070033/**
sangyun-han483731c2016-06-06 12:03:16 +090034 * Lists all meters.
alshabib58fe6dc2015-08-19 17:16:13 -070035 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070036@Service
alshabib58fe6dc2015-08-19 17:16:13 -070037@Command(scope = "onos", name = "meters",
38 description = "Shows meters")
sangyun-han483731c2016-06-06 12:03:16 +090039public class MetersListCommand extends AbstractShellCommand {
alshabib58fe6dc2015-08-19 17:16:13 -070040
41 @Argument(index = 0, name = "uri", description = "Device ID",
42 required = false, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070043 @Completion(DeviceIdCompleter.class)
alshabib58fe6dc2015-08-19 17:16:13 -070044 String uri = null;
45
Jordi Ortizcfc7a612017-02-27 15:04:24 +010046 @Argument(index = 1, name = "meter", description = "Meter ID",
47 required = false, multiValued = false)
48 String meterstr = null;
49
50 MeterId meterId = null;
alshabib58fe6dc2015-08-19 17:16:13 -070051
52 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070053 protected void doExecute() {
Jordi Ortizcfc7a612017-02-27 15:04:24 +010054
55 if (!isNullOrEmpty(meterstr)) {
56 meterId = MeterId.meterId(Long.parseLong(meterstr));
57 }
58
alshabib58fe6dc2015-08-19 17:16:13 -070059 MeterService service = get(MeterService.class);
60
Jordi Ortizcfc7a612017-02-27 15:04:24 +010061 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 }
alshabib58fe6dc2015-08-19 17:16:13 -070068 } else {
Jordi Ortizcfc7a612017-02-27 15:04:24 +010069 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 }
alshabib58fe6dc2015-08-19 17:16:13 -070076 }
77 }
78
79 private void printMeters(Collection<Meter> meters) {
80 meters.forEach(m -> print(" %s", m));
81 }
82}