blob: 51a2fc360a8ef0ad43b066627859731220298c82 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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;
Yuta HIGUCHIde4b7e22017-09-13 10:42:30 -070021import org.onosproject.cluster.ClusterMetadataService;
Jonathan Hart5fda3812016-04-13 15:04:59 -070022import org.onosproject.cluster.ClusterService;
Ray Milkey356f28a2014-12-08 11:59:49 -080023import org.onosproject.cluster.ControllerNode;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.core.CoreService;
Jonathan Hart5fda3812016-04-13 15:04:59 -070025import org.onosproject.core.Version;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.device.DeviceService;
27import org.onosproject.net.flow.FlowRuleService;
28import org.onosproject.net.host.HostService;
29import org.onosproject.net.intent.IntentService;
30import org.onosproject.net.link.LinkService;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.topology.TopologyService;
tom9b4030d2014-10-06 10:39:03 -070032
Jonathan Hart5fda3812016-04-13 15:04:59 -070033import java.util.Set;
34
tom9b4030d2014-10-06 10:39:03 -070035/**
36 * Provides summary of ONOS model.
37 */
38@Command(scope = "onos", name = "summary",
39 description = "Provides summary of ONOS model")
40public class SummaryCommand extends AbstractShellCommand {
41
Ray Milkey356f28a2014-12-08 11:59:49 -080042 /**
43 * Count the active ONOS controller nodes.
44 *
45 * @param nodes set of all of the controller nodes in the cluster
46 * @return count of active nodes
47 */
Jonathan Hart5fda3812016-04-13 15:04:59 -070048 private long activeNodes(Set<ControllerNode> nodes) {
49 ClusterService clusterService = get(ClusterService.class);
Ray Milkey356f28a2014-12-08 11:59:49 -080050
Jonathan Hart5fda3812016-04-13 15:04:59 -070051 return nodes.stream()
52 .map(node -> clusterService.getState(node.id()))
53 .filter(nodeState -> nodeState.isActive())
54 .count();
Ray Milkey356f28a2014-12-08 11:59:49 -080055 }
56
tom9b4030d2014-10-06 10:39:03 -070057 @Override
58 protected void execute() {
Jonathan Hart5fda3812016-04-13 15:04:59 -070059 IpAddress nodeIp = get(ClusterService.class).getLocalNode().ip();
60 Version version = get(CoreService.class).version();
61 long numNodes = activeNodes(get(ClusterService.class).getNodes());
62 int numDevices = get(DeviceService.class).getDeviceCount();
63 int numLinks = get(LinkService.class).getLinkCount();
64 int numHosts = get(HostService.class).getHostCount();
65 int numScc = get(TopologyService.class).currentTopology().clusterCount();
66 int numFlows = get(FlowRuleService.class).getFlowRuleCount();
67 long numIntents = get(IntentService.class).getIntentCount();
Yuta HIGUCHIde4b7e22017-09-13 10:42:30 -070068 String clusterId = get(ClusterMetadataService.class).getClusterMetadata().getName();
Jonathan Hart5fda3812016-04-13 15:04:59 -070069
tom32085cf2014-10-16 00:04:33 -070070 if (outputJson()) {
71 print("%s", new ObjectMapper().createObjectNode()
Jonathan Hart5fda3812016-04-13 15:04:59 -070072 .put("node", nodeIp.toString())
73 .put("version", version.toString())
Yuta HIGUCHIde4b7e22017-09-13 10:42:30 -070074 .put("clusterId", clusterId)
Jonathan Hart5fda3812016-04-13 15:04:59 -070075 .put("nodes", numNodes)
76 .put("devices", numDevices)
77 .put("links", numLinks)
78 .put("hosts", numHosts)
79 .put("SCC(s)", numScc)
80 .put("flows", numFlows)
81 .put("intents", numIntents));
tom32085cf2014-10-16 00:04:33 -070082 } else {
Yuta HIGUCHIde4b7e22017-09-13 10:42:30 -070083 print("node=%s, version=%s clusterId=%s", nodeIp, version, clusterId);
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080084 print("nodes=%d, devices=%d, links=%d, hosts=%d, SCC(s)=%s, flows=%d, intents=%d",
Jonathan Hart5fda3812016-04-13 15:04:59 -070085 numNodes, numDevices, numLinks, numHosts, numScc, numFlows, numIntents);
tom32085cf2014-10-16 00:04:33 -070086 }
tom9b4030d2014-10-06 10:39:03 -070087 }
88
89}