Sudhir Kumar Maurya | 2cc7798 | 2018-12-14 06:41:33 -0500 | [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.packetstats.rest; |
| 17 | import javax.ws.rs.Path; |
| 18 | import javax.ws.rs.GET; |
| 19 | import javax.ws.rs.Produces; |
| 20 | import javax.ws.rs.core.MediaType; |
| 21 | import javax.ws.rs.core.Response; |
| 22 | import org.onosproject.rest.AbstractWebResource; |
| 23 | import com.codahale.metrics.MetricFilter; |
| 24 | import com.codahale.metrics.Counter; |
| 25 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 27 | import org.onlab.metrics.MetricsService; |
| 28 | import java.util.Map; |
| 29 | |
| 30 | /** |
| 31 | * Packet Stats REST API. |
| 32 | */ |
Andrea Campanella | 4005c06 | 2019-01-17 16:05:03 +0100 | [diff] [blame] | 33 | @Path("") |
Sudhir Kumar Maurya | 2cc7798 | 2018-12-14 06:41:33 -0500 | [diff] [blame] | 34 | public 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 | } |