Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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 | |
Ray Milkey | 356f28a | 2014-12-08 11:59:49 -0800 | [diff] [blame] | 18 | import java.util.Set; |
| 19 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Command; |
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; |
| 24 | import org.onosproject.cluster.ClusterService; |
| 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; |
| 30 | import org.onosproject.net.topology.Topology; |
| 31 | import org.onosproject.net.topology.TopologyService; |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Provides summary of ONOS model. |
| 35 | */ |
| 36 | @Command(scope = "onos", name = "summary", |
| 37 | description = "Provides summary of ONOS model") |
| 38 | public class SummaryCommand extends AbstractShellCommand { |
| 39 | |
Ray Milkey | 356f28a | 2014-12-08 11:59:49 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Count the active ONOS controller nodes. |
| 42 | * |
| 43 | * @param nodes set of all of the controller nodes in the cluster |
| 44 | * @return count of active nodes |
| 45 | */ |
| 46 | private int activeNodes(Set<ControllerNode> nodes) { |
| 47 | int nodeCount = 0; |
| 48 | |
| 49 | for (final ControllerNode node : nodes) { |
| 50 | final ControllerNode.State nodeState = |
| 51 | get(ClusterService.class).getState(node.id()); |
| 52 | if (nodeState == ControllerNode.State.ACTIVE) { |
| 53 | nodeCount++; |
| 54 | } |
| 55 | } |
| 56 | return nodeCount; |
| 57 | } |
| 58 | |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 59 | @Override |
| 60 | protected void execute() { |
| 61 | TopologyService topologyService = get(TopologyService.class); |
tom | 800709a | 2014-10-06 20:52:14 -0700 | [diff] [blame] | 62 | Topology topology = topologyService.currentTopology(); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 63 | if (outputJson()) { |
| 64 | print("%s", new ObjectMapper().createObjectNode() |
| 65 | .put("node", get(ClusterService.class).getLocalNode().ip().toString()) |
| 66 | .put("version", get(CoreService.class).version().toString()) |
| 67 | .put("nodes", get(ClusterService.class).getNodes().size()) |
Thomas Vachuska | 47635c6 | 2014-11-22 01:21:36 -0800 | [diff] [blame] | 68 | .put("devices", topology.deviceCount()) |
| 69 | .put("links", topology.linkCount()) |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 70 | .put("hosts", get(HostService.class).getHostCount()) |
Yuta HIGUCHI | 39cb61c | 2014-12-03 01:26:04 -0800 | [diff] [blame] | 71 | .put("SCC(s)", topology.clusterCount()) |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 72 | .put("flows", get(FlowRuleService.class).getFlowRuleCount()) |
| 73 | .put("intents", get(IntentService.class).getIntentCount())); |
| 74 | } else { |
| 75 | print("node=%s, version=%s", |
| 76 | get(ClusterService.class).getLocalNode().ip(), |
| 77 | get(CoreService.class).version().toString()); |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame] | 78 | print("nodes=%d, devices=%d, links=%d, hosts=%d, SCC(s)=%s, flows=%d, intents=%d", |
Ray Milkey | 356f28a | 2014-12-08 11:59:49 -0800 | [diff] [blame] | 79 | activeNodes(get(ClusterService.class).getNodes()), |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 80 | get(DeviceService.class).getDeviceCount(), |
| 81 | get(LinkService.class).getLinkCount(), |
| 82 | get(HostService.class).getHostCount(), |
| 83 | topologyService.getClusters(topology).size(), |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 84 | get(FlowRuleService.class).getFlowRuleCount(), |
| 85 | get(IntentService.class).getIntentCount()); |
| 86 | } |
tom | 9b4030d | 2014-10-06 10:39:03 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | } |