blob: 6c87521efb574b1947483f22d21a79e1e9f50758 [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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
kdarapu97843dc2018-05-10 12:46:32 +053022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.cluster.NodeId;
nitinanand920fcb42018-05-18 17:30:36 +053024import org.onosproject.nodemetrics.NodeMemoryUsage;
kdarapu97843dc2018-05-10 12:46:32 +053025import org.onosproject.nodemetrics.NodeMetricsService;
26
27import java.util.Collection;
28import java.util.Objects;
29
30/**
31 * Lists memory usage across nodes.
32 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070033@Service
kdarapu97843dc2018-05-10 12:46:32 +053034@Command(scope = "onos", name = "node-memory",
35 description = "Lists all node memory utilization")
nitinanand920fcb42018-05-18 17:30:36 +053036public class ShowNodeMemoryUsageCommand extends AbstractShellCommand {
kdarapu97843dc2018-05-10 12:46:32 +053037
38 @Argument(index = 0, name = "nodeId", description = "Node identity",
39 required = false, multiValued = false)
40 String nodeId = null;
41 private NodeMetricsService nodeService = AbstractShellCommand
42 .get(NodeMetricsService.class);
43
44 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070045 protected void doExecute() {
kdarapu97843dc2018-05-10 12:46:32 +053046 if (nodeId != null) {
nitinanand920fcb42018-05-18 17:30:36 +053047 NodeMemoryUsage memory = nodeService.memory(NodeId.nodeId(nodeId));
kdarapu97843dc2018-05-10 12:46:32 +053048 if (Objects.nonNull(memory)) {
49 print("Memory usage : %s", memory.toString());
50 } else {
51 print("Node %s doesn't exists");
52 }
53 } else {
nitinanand920fcb42018-05-18 17:30:36 +053054 Collection<NodeMemoryUsage> memory = nodeService.memory().values();
55 printMemoryUsage(memory);
kdarapu97843dc2018-05-10 12:46:32 +053056 }
57 }
58
nitinanand920fcb42018-05-18 17:30:36 +053059 private void printMemoryUsage(Collection<NodeMemoryUsage> memoryList) {
kdarapu97843dc2018-05-10 12:46:32 +053060 memoryList.forEach(memory -> print("%s", memory));
61 }
62}