blob: 3ff0cb58fbb005b69d3f766b52bab67a11a70e0d [file] [log] [blame]
Madan Jampanib5d72d52015-04-03 16:53:50 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Madan Jampanib5d72d52015-04-03 16:53:50 -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 java.util.Map;
19
20import org.apache.karaf.shell.commands.Command;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.store.service.StorageAdminService;
23
Madan Jampanib5d72d52015-04-03 16:53:50 -070024import com.fasterxml.jackson.databind.ObjectMapper;
Madan Jampanib5d72d52015-04-03 16:53:50 -070025import com.fasterxml.jackson.databind.node.ObjectNode;
26
27/**
28 * Command to list the various counters in the system.
29 */
30@Command(scope = "onos", name = "counters",
31 description = "Lists information about atomic counters in the system")
32public class CountersListCommand extends AbstractShellCommand {
33
Madan Jampani3e033bd2015-04-08 13:03:49 -070034 private static final String FMT = "name=%s value=%d";
Madan Jampanib5d72d52015-04-03 16:53:50 -070035
Madan Jampanib5d72d52015-04-03 16:53:50 -070036 @Override
37 protected void execute() {
38 StorageAdminService storageAdminService = get(StorageAdminService.class);
Madan Jampani832686d2016-04-04 21:57:26 -070039 Map<String, Long> counters = storageAdminService.getCounters();
Madan Jampanib5d72d52015-04-03 16:53:50 -070040 if (outputJson()) {
Madan Jampani832686d2016-04-04 21:57:26 -070041 ObjectMapper mapper = new ObjectMapper();
42 ObjectNode jsonCounters = mapper.createObjectNode();
43 counters.forEach((k, v) -> jsonCounters.put(k, v));
44 print("%s", jsonCounters);
Madan Jampanib5d72d52015-04-03 16:53:50 -070045 } else {
Madan Jampani832686d2016-04-04 21:57:26 -070046 counters.keySet().stream().sorted().forEach(name -> print(FMT, name, counters.get(name)));
Madan Jampanib5d72d52015-04-03 16:53:50 -070047 }
48 }
49}