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