Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli; |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 17 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.api.action.Command; |
| 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Jonathan Hart | 5fda381 | 2016-04-13 15:04:59 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
Yuta HIGUCHI | de4b7e2 | 2017-09-13 10:42:30 -0700 | [diff] [blame] | 22 | import org.onosproject.cluster.ClusterMetadataService; |
Jonathan Hart | 5fda381 | 2016-04-13 15:04:59 -0700 | [diff] [blame] | 23 | import org.onosproject.cluster.ClusterService; |
Ray Milkey | 356f28a | 2014-12-08 11:59:49 -0800 | [diff] [blame] | 24 | import org.onosproject.cluster.ControllerNode; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.core.CoreService; |
Jonathan Hart | 5fda381 | 2016-04-13 15:04:59 -0700 | [diff] [blame] | 26 | import org.onosproject.core.Version; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.device.DeviceService; |
| 28 | import org.onosproject.net.flow.FlowRuleService; |
| 29 | import org.onosproject.net.host.HostService; |
| 30 | import org.onosproject.net.intent.IntentService; |
| 31 | import org.onosproject.net.link.LinkService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.topology.TopologyService; |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 33 | |
Jonathan Hart | 5fda381 | 2016-04-13 15:04:59 -0700 | [diff] [blame] | 34 | import java.util.Set; |
| 35 | |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Provides summary of ONOS model. |
| 38 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 39 | @Service |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 40 | @Command(scope = "onos", name = "summary", |
| 41 | description = "Provides summary of ONOS model") |
| 42 | public class SummaryCommand extends AbstractShellCommand { |
| 43 | |
Ray Milkey | 356f28a | 2014-12-08 11:59:49 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Count the active ONOS controller nodes. |
| 46 | * |
| 47 | * @param nodes set of all of the controller nodes in the cluster |
| 48 | * @return count of active nodes |
| 49 | */ |
Jonathan Hart | 5fda381 | 2016-04-13 15:04:59 -0700 | [diff] [blame] | 50 | private long activeNodes(Set<ControllerNode> nodes) { |
| 51 | ClusterService clusterService = get(ClusterService.class); |
Ray Milkey | 356f28a | 2014-12-08 11:59:49 -0800 | [diff] [blame] | 52 | |
Jonathan Hart | 5fda381 | 2016-04-13 15:04:59 -0700 | [diff] [blame] | 53 | return nodes.stream() |
| 54 | .map(node -> clusterService.getState(node.id())) |
| 55 | .filter(nodeState -> nodeState.isActive()) |
| 56 | .count(); |
Ray Milkey | 356f28a | 2014-12-08 11:59:49 -0800 | [diff] [blame] | 57 | } |
| 58 | |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 59 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 60 | protected void doExecute() { |
Jonathan Hart | 5fda381 | 2016-04-13 15:04:59 -0700 | [diff] [blame] | 61 | IpAddress nodeIp = get(ClusterService.class).getLocalNode().ip(); |
| 62 | Version version = get(CoreService.class).version(); |
| 63 | long numNodes = activeNodes(get(ClusterService.class).getNodes()); |
| 64 | int numDevices = get(DeviceService.class).getDeviceCount(); |
| 65 | int numLinks = get(LinkService.class).getLinkCount(); |
| 66 | int numHosts = get(HostService.class).getHostCount(); |
| 67 | int numScc = get(TopologyService.class).currentTopology().clusterCount(); |
| 68 | int numFlows = get(FlowRuleService.class).getFlowRuleCount(); |
| 69 | long numIntents = get(IntentService.class).getIntentCount(); |
Yuta HIGUCHI | de4b7e2 | 2017-09-13 10:42:30 -0700 | [diff] [blame] | 70 | String clusterId = get(ClusterMetadataService.class).getClusterMetadata().getName(); |
Jonathan Hart | 5fda381 | 2016-04-13 15:04:59 -0700 | [diff] [blame] | 71 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 72 | if (outputJson()) { |
| 73 | print("%s", new ObjectMapper().createObjectNode() |
Jonathan Hart | 5fda381 | 2016-04-13 15:04:59 -0700 | [diff] [blame] | 74 | .put("node", nodeIp.toString()) |
| 75 | .put("version", version.toString()) |
Yuta HIGUCHI | de4b7e2 | 2017-09-13 10:42:30 -0700 | [diff] [blame] | 76 | .put("clusterId", clusterId) |
Jonathan Hart | 5fda381 | 2016-04-13 15:04:59 -0700 | [diff] [blame] | 77 | .put("nodes", numNodes) |
| 78 | .put("devices", numDevices) |
| 79 | .put("links", numLinks) |
| 80 | .put("hosts", numHosts) |
| 81 | .put("SCC(s)", numScc) |
| 82 | .put("flows", numFlows) |
| 83 | .put("intents", numIntents)); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 84 | } else { |
Yuta HIGUCHI | de4b7e2 | 2017-09-13 10:42:30 -0700 | [diff] [blame] | 85 | print("node=%s, version=%s clusterId=%s", nodeIp, version, clusterId); |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 86 | print("nodes=%d, devices=%d, links=%d, hosts=%d, SCC(s)=%s, flows=%d, intents=%d", |
Jonathan Hart | 5fda381 | 2016-04-13 15:04:59 -0700 | [diff] [blame] | 87 | numNodes, numDevices, numLinks, numHosts, numScc, numFlows, numIntents); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 88 | } |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | } |