blob: adf65a95fa879aa16d3bdf0502d33836d8a1d0aa [file] [log] [blame]
Jian Li85060ac2016-02-04 09:58:56 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Li85060ac2016-02-04 09:58:56 -08003 *
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.cpman.cli;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
Jian Li67e1e152016-04-18 17:52:58 -070020import org.onlab.util.Tools;
Jian Li85060ac2016-02-04 09:58:56 -080021import org.onosproject.cli.AbstractShellCommand;
Jian Li85060ac2016-02-04 09:58:56 -080022import org.onosproject.cluster.NodeId;
Jian Li67e1e152016-04-18 17:52:58 -070023import org.onosproject.cpman.ControlLoadSnapshot;
Jian Li85060ac2016-02-04 09:58:56 -080024import org.onosproject.cpman.ControlMetricType;
25import org.onosproject.cpman.ControlPlaneMonitorService;
26import org.onosproject.net.DeviceId;
27
28import java.util.Optional;
29import java.util.Set;
Jian Li67e1e152016-04-18 17:52:58 -070030import java.util.concurrent.CompletableFuture;
31import java.util.concurrent.TimeUnit;
Jian Li85060ac2016-02-04 09:58:56 -080032
33import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS;
34import static org.onosproject.cpman.ControlResource.CPU_METRICS;
35import static org.onosproject.cpman.ControlResource.DISK_METRICS;
36import static org.onosproject.cpman.ControlResource.MEMORY_METRICS;
37import static org.onosproject.cpman.ControlResource.NETWORK_METRICS;
38
39/**
40 * Lists all stats information of control plane metrics.
41 */
42@Command(scope = "onos", name = "cpman-stats-list",
43 description = "Lists control metrics statistics")
44public class ControlMetricsStatsListCommand extends AbstractShellCommand {
45
46 private static final String FMT = "metricType=%s, latestValue=%d, " +
47 "averageValue=%d, latestTime=%s";
48 private static final String INVALID_TYPE = "Invalid control resource type.";
49
Jian Li67e1e152016-04-18 17:52:58 -070050 private static final long TIMEOUT_MILLIS = 3000;
51
52 @Argument(index = 0, name = "node", description = "ONOS node identifier",
53 required = true, multiValued = false)
54 String node = null;
55
56 @Argument(index = 1, name = "type",
Jian Li85060ac2016-02-04 09:58:56 -080057 description = "Resource type (cpu|memory|disk|network|control_message)",
58 required = true, multiValued = false)
59 String type = null;
60
Jian Li67e1e152016-04-18 17:52:58 -070061 @Argument(index = 2, name = "name", description = "Resource name (or Device Id)",
Jian Li85060ac2016-02-04 09:58:56 -080062 required = false, multiValued = false)
63 String name = null;
64
65 @Override
66 protected void execute() {
67 ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class);
Jian Li67e1e152016-04-18 17:52:58 -070068 NodeId nodeId = NodeId.nodeId(node);
Jian Li85060ac2016-02-04 09:58:56 -080069 switch (type) {
70 case "cpu":
71 printMetricsStats(service, nodeId, CPU_METRICS);
72 break;
73 case "memory":
74 printMetricsStats(service, nodeId, MEMORY_METRICS);
75 break;
76 case "disk":
77 printMetricsStats(service, nodeId, DISK_METRICS, name);
78 break;
79 case "network":
80 printMetricsStats(service, nodeId, NETWORK_METRICS, name);
81 break;
82 case "control_message":
83 if (name != null) {
Jian Li67e1e152016-04-18 17:52:58 -070084 printMetricsStats(service, nodeId, CONTROL_MESSAGE_METRICS,
85 DeviceId.deviceId(name));
Jian Li85060ac2016-02-04 09:58:56 -080086 }
87 break;
88 default:
89 print(INVALID_TYPE);
90 break;
91 }
92 }
93
94 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
95 Set<ControlMetricType> typeSet) {
96 printMetricsStats(service, nodeId, typeSet, null, null);
97 }
98
99 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
Jian Li67e1e152016-04-18 17:52:58 -0700100 Set<ControlMetricType> typeSet, String resName) {
101 printMetricsStats(service, nodeId, typeSet, resName, null);
Jian Li85060ac2016-02-04 09:58:56 -0800102 }
103
104 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
105 Set<ControlMetricType> typeSet, DeviceId did) {
106 printMetricsStats(service, nodeId, typeSet, null, did);
107 }
108
109 private void printMetricsStats(ControlPlaneMonitorService service, NodeId nodeId,
Jian Li67e1e152016-04-18 17:52:58 -0700110 Set<ControlMetricType> typeSet, String resName, DeviceId did) {
111 if (resName == null && did == null) {
112 typeSet.forEach(s -> {
113 CompletableFuture<ControlLoadSnapshot> cf =
114 service.getLoad(nodeId, s, Optional.empty());
115 ControlLoadSnapshot cmr =
116 Tools.futureGetOrElse(cf, TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, null);
117 printRemote(s, cmr);
118 });
119 } else if (resName == null && did != null) {
120 typeSet.forEach(s -> {
121 CompletableFuture<ControlLoadSnapshot> cf =
122 service.getLoad(nodeId, s, Optional.of(did));
123 ControlLoadSnapshot cmr =
124 Tools.futureGetOrElse(cf, TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, null);
125 printRemote(s, cmr);
126 });
127 } else if (resName != null && did == null) {
128 typeSet.forEach(s -> {
129 CompletableFuture<ControlLoadSnapshot> cf =
130 service.getLoad(nodeId, s, resName);
131 ControlLoadSnapshot cmr =
132 Tools.futureGetOrElse(cf, TIMEOUT_MILLIS, TimeUnit.MILLISECONDS, null);
133 printRemote(s, cmr);
134 });
Jian Li85060ac2016-02-04 09:58:56 -0800135 }
136 }
137
Jian Li67e1e152016-04-18 17:52:58 -0700138 private void printRemote(ControlMetricType cmType, ControlLoadSnapshot cls) {
139 if (cls != null) {
140 print(FMT, cmType.toString(), cls.latest(), cls.average(), cls.time());
141 } else {
142 print("Failed to retrieve metric value for type {}", cmType.toString());
Jian Li85060ac2016-02-04 09:58:56 -0800143 }
144 }
145}