Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014-present Open Networking Foundation |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | package org.onosproject.cli2; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import org.apache.karaf.shell.api.action.Action; |
| 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onosproject.cluster.ClusterMetadataService; |
| 24 | import org.onosproject.cluster.ClusterService; |
| 25 | import org.onosproject.cluster.ControllerNode; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.core.Version; |
| 28 | import org.onosproject.net.device.DeviceService; |
| 29 | import org.onosproject.net.flow.FlowRuleService; |
| 30 | import org.onosproject.net.host.HostService; |
| 31 | import org.onosproject.net.intent.IntentService; |
| 32 | import org.onosproject.net.link.LinkService; |
| 33 | import org.onosproject.net.topology.TopologyService; |
| 34 | |
| 35 | import java.util.Set; |
| 36 | |
| 37 | /** |
| 38 | * Provides summary of ONOS model. |
| 39 | */ |
| 40 | @Command(scope = "onos", name = "summary", |
| 41 | description = "Provides summary of ONOS model") |
| 42 | @Service |
| 43 | public class SummaryCommand extends AbstractShellCommand { |
| 44 | |
| 45 | /** |
| 46 | * Count the active ONOS controller nodes. |
| 47 | * |
| 48 | * @param nodes set of all of the controller nodes in the cluster |
| 49 | * @return count of active nodes |
| 50 | */ |
| 51 | private long activeNodes(Set<ControllerNode> nodes) { |
| 52 | ClusterService clusterService = get(ClusterService.class); |
| 53 | |
| 54 | return nodes.stream() |
| 55 | .map(node -> clusterService.getState(node.id())) |
| 56 | .filter(nodeState -> nodeState.isActive()) |
| 57 | .count(); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected void doExecute() { |
| 62 | IpAddress nodeIp = get(ClusterService.class).getLocalNode().ip(); |
| 63 | Version version = get(CoreService.class).version(); |
| 64 | long numNodes = activeNodes(get(ClusterService.class).getNodes()); |
| 65 | int numDevices = get(DeviceService.class).getDeviceCount(); |
| 66 | int numLinks = get(LinkService.class).getLinkCount(); |
| 67 | int numHosts = get(HostService.class).getHostCount(); |
| 68 | int numScc = get(TopologyService.class).currentTopology().clusterCount(); |
| 69 | int numFlows = get(FlowRuleService.class).getFlowRuleCount(); |
| 70 | long numIntents = get(IntentService.class).getIntentCount(); |
| 71 | String clusterId = get(ClusterMetadataService.class).getClusterMetadata().getName(); |
| 72 | |
| 73 | if (outputJson()) { |
| 74 | print("%s", new ObjectMapper().createObjectNode() |
| 75 | .put("node", nodeIp.toString()) |
| 76 | .put("version", version.toString()) |
| 77 | .put("clusterId", clusterId) |
| 78 | .put("nodes", numNodes) |
| 79 | .put("devices", numDevices) |
| 80 | .put("links", numLinks) |
| 81 | .put("hosts", numHosts) |
| 82 | .put("SCC(s)", numScc) |
| 83 | .put("flows", numFlows) |
| 84 | .put("intents", numIntents)); |
| 85 | } else { |
| 86 | print("node=%s, version=%s clusterId=%s", nodeIp, version, clusterId); |
| 87 | print("nodes=%d, devices=%d, links=%d, hosts=%d, SCC(s)=%s, flows=%d, intents=%d", |
| 88 | numNodes, numDevices, numLinks, numHosts, numScc, numFlows, numIntents); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | } |