blob: 363cbfef77cfb7a88c90f11f0110593f147a625d [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
tom32085cf2014-10-16 00:04:33 -070018import com.fasterxml.jackson.databind.ObjectMapper;
tom9b4030d2014-10-06 10:39:03 -070019import org.apache.karaf.shell.commands.Command;
Jonathan Hart5fda3812016-04-13 15:04:59 -070020import org.onlab.packet.IpAddress;
21import org.onosproject.cluster.ClusterService;
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;
Jonathan Hart5fda3812016-04-13 15:04:59 -070024import org.onosproject.core.Version;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import 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;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.topology.TopologyService;
tom9b4030d2014-10-06 10:39:03 -070031
Jonathan Hart5fda3812016-04-13 15:04:59 -070032import java.util.Set;
33
tom9b4030d2014-10-06 10:39:03 -070034/**
35 * Provides summary of ONOS model.
36 */
37@Command(scope = "onos", name = "summary",
38 description = "Provides summary of ONOS model")
39public class SummaryCommand extends AbstractShellCommand {
40
Ray Milkey356f28a2014-12-08 11:59:49 -080041 /**
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 Hart5fda3812016-04-13 15:04:59 -070047 private long activeNodes(Set<ControllerNode> nodes) {
48 ClusterService clusterService = get(ClusterService.class);
Ray Milkey356f28a2014-12-08 11:59:49 -080049
Jonathan Hart5fda3812016-04-13 15:04:59 -070050 return nodes.stream()
51 .map(node -> clusterService.getState(node.id()))
52 .filter(nodeState -> nodeState.isActive())
53 .count();
Ray Milkey356f28a2014-12-08 11:59:49 -080054 }
55
tom9b4030d2014-10-06 10:39:03 -070056 @Override
57 protected void execute() {
Jonathan Hart5fda3812016-04-13 15:04:59 -070058 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
tom32085cf2014-10-16 00:04:33 -070068 if (outputJson()) {
69 print("%s", new ObjectMapper().createObjectNode()
Jonathan Hart5fda3812016-04-13 15:04:59 -070070 .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));
tom32085cf2014-10-16 00:04:33 -070079 } else {
Jonathan Hart5fda3812016-04-13 15:04:59 -070080 print("node=%s, version=%s", nodeIp, version);
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080081 print("nodes=%d, devices=%d, links=%d, hosts=%d, SCC(s)=%s, flows=%d, intents=%d",
Jonathan Hart5fda3812016-04-13 15:04:59 -070082 numNodes, numDevices, numLinks, numHosts, numScc, numFlows, numIntents);
tom32085cf2014-10-16 00:04:33 -070083 }
tom9b4030d2014-10-06 10:39:03 -070084 }
85
86}