blob: e449079f202e6416d3f9117c37139a30345bc00a [file] [log] [blame]
rsahot0364df80f12018-10-15 17:46:14 -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 org.onosproject.rest.AbstractWebResource;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.onosproject.net.packet.PacketProcessorEntry;
23import org.onosproject.net.packet.PacketService;
rsahot0364df80f12018-10-15 17:46:14 -040024
25import javax.ws.rs.GET;
26import javax.ws.rs.Path;
27import javax.ws.rs.Produces;
28import javax.ws.rs.core.MediaType;
29import javax.ws.rs.core.Response;
30
31import java.util.List;
rsahot0364df80f12018-10-15 17:46:14 -040032import static org.onosproject.net.packet.PacketProcessor.ADVISOR_MAX;
33import static org.onosproject.net.packet.PacketProcessor.DIRECTOR_MAX;
34
35/**
36 * Manage inventory of packet processors.
37 */
38
39@Path("packet/processors")
40public class PacketProcessorsWebResource extends AbstractWebResource {
41
rsahot0364df80f12018-10-15 17:46:14 -040042 /**
43 * Gets packet processors. Returns array of all packet processors.
44
45 * @onos.rsModel PacketProcessorsGet
46 * @return 200 OK with array of all packet processors.
47 */
48 @GET
49 @Produces(MediaType.APPLICATION_JSON)
50 public Response getPacketProcessors() {
Harshada Chaundkar1a098eb2019-01-15 00:05:57 +000051 PacketService service = get(PacketService.class);
52 ObjectNode root = mapper().createObjectNode();
53 ArrayNode pktProcNode = root.putArray("packet-processors");
rsahot0364df80f12018-10-15 17:46:14 -040054 List<PacketProcessorEntry> processors = service.getProcessors();
55 ObjectMapper mapper = new ObjectMapper();
56 for (PacketProcessorEntry p : processors) {
57 pktProcNode.add(mapper.createObjectNode()
58 .put("priority", priorityFormat(p.priority()))
59 .put("class", p.processor().getClass().getName())
60 .put("packets", p.invocations())
61 .put("avgNanos", p.averageNanos()));
62 }
63
64 return ok(root).build();
65 }
66
67 private String priorityFormat(int priority) {
68 if (priority > DIRECTOR_MAX) {
69 return "observer(" + (priority - DIRECTOR_MAX - 1) + ")";
70 } else if (priority > ADVISOR_MAX) {
71 return "director(" + (priority - ADVISOR_MAX - 1) + ")";
72 }
73 return "advisor(" + (priority - 1) + ")";
74 }
75}