blob: 02b1a7697ab0f2dd835b87c0c9245e82e48ab648 [file] [log] [blame]
Madan Jampanib5d72d52015-04-03 16:53:50 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.lifecycle.Service;
Madan Jampanib5d72d52015-04-03 16:53:50 -070022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.store.service.StorageAdminService;
24
Madan Jampanib5d72d52015-04-03 16:53:50 -070025import com.fasterxml.jackson.databind.ObjectMapper;
Madan Jampanib5d72d52015-04-03 16:53:50 -070026import com.fasterxml.jackson.databind.node.ObjectNode;
27
28/**
29 * Command to list the various counters in the system.
30 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031@Service
Madan Jampanib5d72d52015-04-03 16:53:50 -070032@Command(scope = "onos", name = "counters",
33 description = "Lists information about atomic counters in the system")
34public class CountersListCommand extends AbstractShellCommand {
35
Madan Jampani3e033bd2015-04-08 13:03:49 -070036 private static final String FMT = "name=%s value=%d";
Madan Jampanib5d72d52015-04-03 16:53:50 -070037
Madan Jampanib5d72d52015-04-03 16:53:50 -070038 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070039 protected void doExecute() {
Madan Jampanib5d72d52015-04-03 16:53:50 -070040 StorageAdminService storageAdminService = get(StorageAdminService.class);
Madan Jampani832686d2016-04-04 21:57:26 -070041 Map<String, Long> counters = storageAdminService.getCounters();
Madan Jampanib5d72d52015-04-03 16:53:50 -070042 if (outputJson()) {
Madan Jampani832686d2016-04-04 21:57:26 -070043 ObjectMapper mapper = new ObjectMapper();
44 ObjectNode jsonCounters = mapper.createObjectNode();
45 counters.forEach((k, v) -> jsonCounters.put(k, v));
46 print("%s", jsonCounters);
Madan Jampanib5d72d52015-04-03 16:53:50 -070047 } else {
Madan Jampani832686d2016-04-04 21:57:26 -070048 counters.keySet().stream().sorted().forEach(name -> print(FMT, name, counters.get(name)));
Madan Jampanib5d72d52015-04-03 16:53:50 -070049 }
50 }
51}