blob: a6bf1909a02e753a3dfb07dffd66a833db5b9925 [file] [log] [blame]
Sudhir Kumar Maurya2cc77982018-12-14 06:41:33 -05001/*
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*/
16package org.onosproject.packetstats.rest;
17import javax.ws.rs.Path;
18import javax.ws.rs.GET;
19import javax.ws.rs.Produces;
20import javax.ws.rs.core.MediaType;
21import javax.ws.rs.core.Response;
22import org.onosproject.rest.AbstractWebResource;
23import com.codahale.metrics.MetricFilter;
24import com.codahale.metrics.Counter;
25import com.fasterxml.jackson.databind.ObjectMapper;
26import com.fasterxml.jackson.databind.node.ObjectNode;
27import org.onlab.metrics.MetricsService;
28import java.util.Map;
29
30/**
31 * Packet Stats REST API.
32 */
33public class PacketStatsWebResource extends AbstractWebResource {
34 private final ObjectMapper mapper = new ObjectMapper();
35 private static final String METRIC_NAME = null;
36 MetricFilter filter = METRIC_NAME != null ? (name, metric) -> name.equals(METRIC_NAME) : MetricFilter.ALL;
37
38 @GET
39 @Path("counters")
40 @Produces(MediaType.APPLICATION_JSON)
41 public Response packetStatsCounters() {
42 ObjectNode node = getPacketStatsCountersJson();
43 return Response.status(200).entity(node).build();
44 }
45
46 private ObjectNode getPacketStatsCountersJson() {
47 MetricsService service = get(MetricsService.class);
48 ObjectNode node = mapper.createObjectNode();
49 ObjectNode pktCounterNode = mapper.createObjectNode();
50 Map<String, Counter> counters = service.getCounters(filter);
51
52 Counter arpCounter = counters.get("packetStatisticsComponent.arpFeature.arpPC");
53 Counter lldpCounter = counters.get("packetStatisticsComponent.lldpFeature.lldpPC");
54 Counter nsCounter = counters.get("packetStatisticsComponent.nbrSolicitFeature.nbrSolicitPC");
55 Counter naCounter = counters.get("packetStatisticsComponent.nbrAdvertFeature.nbrAdvertPC");
56
57 pktCounterNode.put("arpCounter", arpCounter.getCount());
58 pktCounterNode.put("lldpCounter", lldpCounter.getCount());
59 pktCounterNode.put("nsCounter", nsCounter.getCount());
60 pktCounterNode.put("naCounter", naCounter.getCount());
61
62 node.put("packet_stats_counters", pktCounterNode);
63 return node;
64
65 }
66}