Madan Jampani
Committed by Gerrit Code Review

CLI command to display the value of a atomic counter

Change-Id: Ib37ea4d4949dc40a43ddea1b071692783751a101
1 +/*
2 + * Copyright 2016-present Open Networking Laboratory
3 + *
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 + */
16 +package org.onosproject.cli.net;
17 +
18 +import org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.onosproject.cli.AbstractShellCommand;
21 +import org.onosproject.store.service.AtomicCounter;
22 +import org.onosproject.store.service.StorageService;
23 +
24 +import com.fasterxml.jackson.databind.ObjectMapper;
25 +import com.fasterxml.jackson.databind.node.ObjectNode;
26 +
27 +/**
28 + * Command to display the current value of a atomic counter.
29 + */
30 +@Command(scope = "onos", name = "counter",
31 + description = "Displays the current value of a atomic counter")
32 +public class CounterCommand extends AbstractShellCommand {
33 +
34 + @Argument(index = 0, name = "counterName", description = "Counter Name",
35 + required = true, multiValued = false)
36 + String name = null;
37 +
38 + @Override
39 + protected void execute() {
40 + StorageService storageService = get(StorageService.class);
41 + AtomicCounter counter = storageService.getAtomicCounter(name);
42 +
43 + if (outputJson()) {
44 + ObjectMapper mapper = new ObjectMapper();
45 + ObjectNode counterJsonNode = mapper.createObjectNode();
46 + counterJsonNode.put("value", counter.get());
47 + print("%s", counterJsonNode);
48 + } else {
49 + print("%d", counter.get());
50 + }
51 + }
52 +}
...@@ -396,6 +396,9 @@ ...@@ -396,6 +396,9 @@
396 <action class="org.onosproject.cli.net.TransactionsCommand"/> 396 <action class="org.onosproject.cli.net.TransactionsCommand"/>
397 </command> 397 </command>
398 <command> 398 <command>
399 + <action class="org.onosproject.cli.net.CounterCommand"/>
400 + </command>
401 + <command>
399 <action class="org.onosproject.cli.net.ClusterDevicesCommand"/> 402 <action class="org.onosproject.cli.net.ClusterDevicesCommand"/>
400 <completers> 403 <completers>
401 <ref component-id="clusterIdCompleter"/> 404 <ref component-id="clusterIdCompleter"/>
......