blob: 006c21c4213a07bd21412ff249d3f62fba8b15b9 [file] [log] [blame]
Thomas Vachuska7ef26712018-04-25 18:03:54 -04001/*
2 * Copyright 2015-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 */
16package org.onosproject.rest.resources;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.onlab.packet.IpAddress;
20import org.onosproject.cluster.ClusterMetadataService;
21import org.onosproject.cluster.ClusterService;
22import org.onosproject.cluster.ControllerNode;
23import org.onosproject.core.CoreService;
24import org.onosproject.core.Version;
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.TopologyService;
31import org.onosproject.rest.AbstractWebResource;
32
33import javax.ws.rs.GET;
34import javax.ws.rs.Path;
35import javax.ws.rs.Produces;
36import javax.ws.rs.core.MediaType;
37import javax.ws.rs.core.Response;
38import java.lang.management.ManagementFactory;
39import java.lang.management.MemoryUsage;
40import java.lang.management.ThreadMXBean;
41import java.util.Set;
42
43/**
44 * Provides high-level system information, version, basic sumaries, memory usage, etc.
45 */
46@Path("system")
47public class SystemInfoWebResource extends AbstractWebResource {
48
49 private long activeNodes(Set<ControllerNode> nodes) {
50 ClusterService clusterService = get(ClusterService.class);
51 return nodes.stream()
52 .map(node -> clusterService.getState(node.id()))
53 .filter(ControllerNode.State::isActive)
54 .count();
55 }
56
57 /**
58 * Get high-level system information, version, basic sumaries, memory usage, etc.
59 *
60 * @return 200 OK with a system summary
61 */
62 @GET
63 @Produces(MediaType.APPLICATION_JSON)
64 public Response getSystemInfo() {
65 IpAddress nodeIp = get(ClusterService.class).getLocalNode().ip();
66 Version version = get(CoreService.class).version();
67 long numNodes = activeNodes(get(ClusterService.class).getNodes());
68 int numDevices = get(DeviceService.class).getDeviceCount();
69 int numLinks = get(LinkService.class).getLinkCount();
70 int numHosts = get(HostService.class).getHostCount();
71 int numScc = get(TopologyService.class).currentTopology().clusterCount();
72 int numFlows = get(FlowRuleService.class).getFlowRuleCount();
73 long numIntents = get(IntentService.class).getIntentCount();
74 String clusterId = get(ClusterMetadataService.class).getClusterMetadata().getName();
75
76 ObjectNode root = mapper().createObjectNode()
77 .put("node", nodeIp.toString())
78 .put("version", version.toString())
79 .put("clusterId", clusterId)
80 .put("nodes", numNodes)
81 .put("devices", numDevices)
82 .put("links", numLinks)
83 .put("hosts", numHosts)
84 .put("sccs", numScc)
85 .put("flows", numFlows)
86 .put("intents", numIntents);
87
88 MemoryUsage mem = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage();
89 newObject(root, "mem")
90 .put("current", mem.getUsed())
91 .put("max", mem.getMax())
92 .put("committed", mem.getCommitted());
93
94 ThreadMXBean threads = ManagementFactory.getThreadMXBean();
95 newObject(root, "threads")
96 .put("live", threads.getThreadCount())
97 .put("daemon", threads.getDaemonThreadCount())
98 .put("peak", threads.getPeakThreadCount());
99
100 return ok(root).build();
101 }
102
103}