blob: 3f62650b986fd9f8201e22256913c5a92c31aa30 [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 */
Andrea Campanella4005c062019-01-17 16:05:03 +010033@Path("")
Sudhir Kumar Maurya2cc77982018-12-14 06:41:33 -050034public class PacketStatsWebResource extends AbstractWebResource {
35 private final ObjectMapper mapper = new ObjectMapper();
36 private static final String METRIC_NAME = null;
37 MetricFilter filter = METRIC_NAME != null ? (name, metric) -> name.equals(METRIC_NAME) : MetricFilter.ALL;
38
39 @GET
40 @Path("counters")
41 @Produces(MediaType.APPLICATION_JSON)
42 public Response packetStatsCounters() {
43 ObjectNode node = getPacketStatsCountersJson();
44 return Response.status(200).entity(node).build();
45 }
46
47 private ObjectNode getPacketStatsCountersJson() {
48 MetricsService service = get(MetricsService.class);
49 ObjectNode node = mapper.createObjectNode();
50 ObjectNode pktCounterNode = mapper.createObjectNode();
51 Map<String, Counter> counters = service.getCounters(filter);
52
53 Counter arpCounter = counters.get("packetStatisticsComponent.arpFeature.arpPC");
54 Counter lldpCounter = counters.get("packetStatisticsComponent.lldpFeature.lldpPC");
55 Counter nsCounter = counters.get("packetStatisticsComponent.nbrSolicitFeature.nbrSolicitPC");
56 Counter naCounter = counters.get("packetStatisticsComponent.nbrAdvertFeature.nbrAdvertPC");
57
58 pktCounterNode.put("arpCounter", arpCounter.getCount());
59 pktCounterNode.put("lldpCounter", lldpCounter.getCount());
60 pktCounterNode.put("nsCounter", nsCounter.getCount());
61 pktCounterNode.put("naCounter", naCounter.getCount());
62
63 node.put("packet_stats_counters", pktCounterNode);
64 return node;
65
66 }
67}