blob: a3d0f35de22af75a837fbc0fe0d08d34778776b2 [file] [log] [blame]
Madan Jampaniaac58882016-08-31 14:24:31 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampaniaac58882016-08-31 14:24:31 -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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
Madan Jampaniaac58882016-08-31 14:24:31 -070021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.store.service.AtomicCounter;
23import org.onosproject.store.service.StorageService;
24
25import com.fasterxml.jackson.databind.ObjectMapper;
26import com.fasterxml.jackson.databind.node.ObjectNode;
27
28/**
29 * Command to display the current value of a atomic counter.
30 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070031@Service
Madan Jampaniaac58882016-08-31 14:24:31 -070032@Command(scope = "onos", name = "counter",
33 description = "Displays the current value of a atomic counter")
34public class CounterCommand extends AbstractShellCommand {
35
36 @Argument(index = 0, name = "counterName", description = "Counter Name",
37 required = true, multiValued = false)
38 String name = null;
39
40 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041 protected void doExecute() {
Madan Jampaniaac58882016-08-31 14:24:31 -070042 StorageService storageService = get(StorageService.class);
43 AtomicCounter counter = storageService.getAtomicCounter(name);
44
45 if (outputJson()) {
46 ObjectMapper mapper = new ObjectMapper();
47 ObjectNode counterJsonNode = mapper.createObjectNode();
48 counterJsonNode.put("value", counter.get());
49 print("%s", counterJsonNode);
50 } else {
51 print("%d", counter.get());
52 }
53 }
54}