blob: 02ff37df727787e79e3faa5ea000f16ca83fdd97 [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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.lifecycle.Service;
Jonathan Hart5fda3812016-04-13 15:04:59 -070021import org.onlab.packet.IpAddress;
Yuta HIGUCHIde4b7e22017-09-13 10:42:30 -070022import org.onosproject.cluster.ClusterMetadataService;
Jonathan Hart5fda3812016-04-13 15:04:59 -070023import org.onosproject.cluster.ClusterService;
Ray Milkey356f28a2014-12-08 11:59:49 -080024import org.onosproject.cluster.ControllerNode;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.core.CoreService;
Jonathan Hart5fda3812016-04-13 15:04:59 -070026import org.onosproject.core.Version;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.device.DeviceService;
28import org.onosproject.net.flow.FlowRuleService;
29import org.onosproject.net.host.HostService;
30import org.onosproject.net.intent.IntentService;
31import org.onosproject.net.link.LinkService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.topology.TopologyService;
tom9b4030d2014-10-06 10:39:03 -070033
Jonathan Hart5fda3812016-04-13 15:04:59 -070034import java.util.Set;
35
tom9b4030d2014-10-06 10:39:03 -070036/**
37 * Provides summary of ONOS model.
38 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070039@Service
tom9b4030d2014-10-06 10:39:03 -070040@Command(scope = "onos", name = "summary",
41 description = "Provides summary of ONOS model")
42public class SummaryCommand extends AbstractShellCommand {
43
Ray Milkey356f28a2014-12-08 11:59:49 -080044 /**
45 * Count the active ONOS controller nodes.
46 *
47 * @param nodes set of all of the controller nodes in the cluster
48 * @return count of active nodes
49 */
Jonathan Hart5fda3812016-04-13 15:04:59 -070050 private long activeNodes(Set<ControllerNode> nodes) {
51 ClusterService clusterService = get(ClusterService.class);
Ray Milkey356f28a2014-12-08 11:59:49 -080052
Jonathan Hart5fda3812016-04-13 15:04:59 -070053 return nodes.stream()
54 .map(node -> clusterService.getState(node.id()))
55 .filter(nodeState -> nodeState.isActive())
56 .count();
Ray Milkey356f28a2014-12-08 11:59:49 -080057 }
58
tom9b4030d2014-10-06 10:39:03 -070059 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070060 protected void doExecute() {
Jonathan Hart5fda3812016-04-13 15:04:59 -070061 IpAddress nodeIp = get(ClusterService.class).getLocalNode().ip();
62 Version version = get(CoreService.class).version();
63 long numNodes = activeNodes(get(ClusterService.class).getNodes());
64 int numDevices = get(DeviceService.class).getDeviceCount();
65 int numLinks = get(LinkService.class).getLinkCount();
66 int numHosts = get(HostService.class).getHostCount();
67 int numScc = get(TopologyService.class).currentTopology().clusterCount();
68 int numFlows = get(FlowRuleService.class).getFlowRuleCount();
69 long numIntents = get(IntentService.class).getIntentCount();
Yuta HIGUCHIde4b7e22017-09-13 10:42:30 -070070 String clusterId = get(ClusterMetadataService.class).getClusterMetadata().getName();
Jonathan Hart5fda3812016-04-13 15:04:59 -070071
tom32085cf2014-10-16 00:04:33 -070072 if (outputJson()) {
73 print("%s", new ObjectMapper().createObjectNode()
Jonathan Hart5fda3812016-04-13 15:04:59 -070074 .put("node", nodeIp.toString())
75 .put("version", version.toString())
Yuta HIGUCHIde4b7e22017-09-13 10:42:30 -070076 .put("clusterId", clusterId)
Jonathan Hart5fda3812016-04-13 15:04:59 -070077 .put("nodes", numNodes)
78 .put("devices", numDevices)
79 .put("links", numLinks)
80 .put("hosts", numHosts)
81 .put("SCC(s)", numScc)
82 .put("flows", numFlows)
83 .put("intents", numIntents));
tom32085cf2014-10-16 00:04:33 -070084 } else {
Yuta HIGUCHIde4b7e22017-09-13 10:42:30 -070085 print("node=%s, version=%s clusterId=%s", nodeIp, version, clusterId);
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080086 print("nodes=%d, devices=%d, links=%d, hosts=%d, SCC(s)=%s, flows=%d, intents=%d",
Jonathan Hart5fda3812016-04-13 15:04:59 -070087 numNodes, numDevices, numLinks, numHosts, numScc, numFlows, numIntents);
tom32085cf2014-10-16 00:04:33 -070088 }
tom9b4030d2014-10-06 10:39:03 -070089 }
90
91}