Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 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 | package org.onosproject.cpman.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 20 | import org.onlab.util.Tools; |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 22 | import org.onosproject.cluster.NodeId; |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 23 | import org.onosproject.cpman.ControlLoadSnapshot; |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 24 | import org.onosproject.cpman.ControlMetricType; |
| 25 | import org.onosproject.cpman.ControlPlaneMonitorService; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | |
| 28 | import java.util.Optional; |
| 29 | import java.util.Set; |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 30 | import java.util.concurrent.CompletableFuture; |
| 31 | import java.util.concurrent.TimeUnit; |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 32 | |
| 33 | import static org.onosproject.cpman.ControlResource.CONTROL_MESSAGE_METRICS; |
| 34 | import static org.onosproject.cpman.ControlResource.CPU_METRICS; |
| 35 | import static org.onosproject.cpman.ControlResource.DISK_METRICS; |
| 36 | import static org.onosproject.cpman.ControlResource.MEMORY_METRICS; |
| 37 | import 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") |
| 44 | public 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 Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 50 | 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 Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 57 | description = "Resource type (cpu|memory|disk|network|control_message)", |
| 58 | required = true, multiValued = false) |
| 59 | String type = null; |
| 60 | |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 61 | @Argument(index = 2, name = "name", description = "Resource name (or Device Id)", |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 62 | required = false, multiValued = false) |
| 63 | String name = null; |
| 64 | |
| 65 | @Override |
| 66 | protected void execute() { |
| 67 | ControlPlaneMonitorService service = get(ControlPlaneMonitorService.class); |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 68 | NodeId nodeId = NodeId.nodeId(node); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 69 | 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 Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 84 | printMetricsStats(service, nodeId, CONTROL_MESSAGE_METRICS, |
| 85 | DeviceId.deviceId(name)); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 86 | } |
| 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 Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 100 | Set<ControlMetricType> typeSet, String resName) { |
| 101 | printMetricsStats(service, nodeId, typeSet, resName, null); |
Jian Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 102 | } |
| 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 Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 110 | 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 Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Jian Li | 67e1e15 | 2016-04-18 17:52:58 -0700 | [diff] [blame^] | 138 | 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 Li | 85060ac | 2016-02-04 09:58:56 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |