blob: ca205797e451610c474dd8b3249d9e0bc1d96b2f [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska7d693f52014-10-21 19:17:57 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli;
tom9b4030d2014-10-06 10:39:03 -070017
Ray Milkey356f28a2014-12-08 11:59:49 -080018import java.util.Set;
19
tom32085cf2014-10-16 00:04:33 -070020import com.fasterxml.jackson.databind.ObjectMapper;
tom9b4030d2014-10-06 10:39:03 -070021import org.apache.karaf.shell.commands.Command;
Ray Milkey356f28a2014-12-08 11:59:49 -080022import org.onosproject.cluster.ControllerNode;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.core.CoreService;
24import org.onosproject.cluster.ClusterService;
25import org.onosproject.net.device.DeviceService;
26import org.onosproject.net.flow.FlowRuleService;
27import org.onosproject.net.host.HostService;
28import org.onosproject.net.intent.IntentService;
29import org.onosproject.net.link.LinkService;
30import org.onosproject.net.topology.Topology;
31import org.onosproject.net.topology.TopologyService;
tom9b4030d2014-10-06 10:39:03 -070032
33/**
34 * Provides summary of ONOS model.
35 */
36@Command(scope = "onos", name = "summary",
37 description = "Provides summary of ONOS model")
38public class SummaryCommand extends AbstractShellCommand {
39
Ray Milkey356f28a2014-12-08 11:59:49 -080040 /**
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());
Thomas Vachuska7a8de842016-03-07 20:56:35 -080052 if (nodeState.isActive()) {
Ray Milkey356f28a2014-12-08 11:59:49 -080053 nodeCount++;
54 }
55 }
56 return nodeCount;
57 }
58
tom9b4030d2014-10-06 10:39:03 -070059 @Override
60 protected void execute() {
61 TopologyService topologyService = get(TopologyService.class);
tom800709a2014-10-06 20:52:14 -070062 Topology topology = topologyService.currentTopology();
tom32085cf2014-10-16 00:04:33 -070063 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 Vachuska47635c62014-11-22 01:21:36 -080068 .put("devices", topology.deviceCount())
69 .put("links", topology.linkCount())
tom32085cf2014-10-16 00:04:33 -070070 .put("hosts", get(HostService.class).getHostCount())
Yuta HIGUCHI39cb61c2014-12-03 01:26:04 -080071 .put("SCC(s)", topology.clusterCount())
tom32085cf2014-10-16 00:04:33 -070072 .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 Vachuskac31d9f12015-01-22 12:33:27 -080078 print("nodes=%d, devices=%d, links=%d, hosts=%d, SCC(s)=%s, flows=%d, intents=%d",
Ray Milkey356f28a2014-12-08 11:59:49 -080079 activeNodes(get(ClusterService.class).getNodes()),
tom32085cf2014-10-16 00:04:33 -070080 get(DeviceService.class).getDeviceCount(),
81 get(LinkService.class).getLinkCount(),
82 get(HostService.class).getHostCount(),
83 topologyService.getClusters(topology).size(),
tom32085cf2014-10-16 00:04:33 -070084 get(FlowRuleService.class).getFlowRuleCount(),
85 get(IntentService.class).getIntentCount());
86 }
tom9b4030d2014-10-06 10:39:03 -070087 }
88
89}