blob: 3c6ec5bee6607d3c01dd212f3c4b67b6e7f4de5f [file] [log] [blame]
kdarapu97843dc2018-05-10 12:46:32 +05301/*
2 * Copyright 2017-present Open Networking Foundation
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
17package org.onosproject.nodemetrics.cli;
18
19import org.apache.karaf.shell.commands.Argument;
20import org.apache.karaf.shell.commands.Command;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.cluster.NodeId;
23import org.onosproject.nodemetrics.NodeMemory;
24import org.onosproject.nodemetrics.NodeMetricsService;
25
26import java.util.Collection;
27import java.util.Objects;
28
29/**
30 * Lists memory usage across nodes.
31 */
32@Command(scope = "onos", name = "node-memory",
33 description = "Lists all node memory utilization")
34public class ShowNodeMemoryCommand extends AbstractShellCommand {
35
36 @Argument(index = 0, name = "nodeId", description = "Node identity",
37 required = false, multiValued = false)
38 String nodeId = null;
39 private NodeMetricsService nodeService = AbstractShellCommand
40 .get(NodeMetricsService.class);
41
42 @Override
43 protected void execute() {
44 if (nodeId != null) {
45 NodeMemory memory = nodeService.memory(NodeId.nodeId(nodeId));
46 if (Objects.nonNull(memory)) {
47 print("Memory usage : %s", memory.toString());
48 } else {
49 print("Node %s doesn't exists");
50 }
51 } else {
52 Collection<NodeMemory> memory = nodeService.memory().values();
53 printMemory(memory);
54 }
55 }
56
57 private void printMemory(Collection<NodeMemory> memoryList) {
58 memoryList.forEach(memory -> print("%s", memory));
59 }
60}