blob: d6cd51d7f69c25a02b58dbae00d22708195122a6 [file] [log] [blame]
Madan Jampaniaac58882016-08-31 14:24:31 -07001/*
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 */
16package org.onosproject.cli.net;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.store.service.AtomicCounter;
22import org.onosproject.store.service.StorageService;
23
24import com.fasterxml.jackson.databind.ObjectMapper;
25import 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")
32public 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}