blob: aefd84e3173eebcfef93256e7ee9d4e84b06d103 [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;
24import org.slf4j.Logger;
25
26import javax.ws.rs.GET;
27import javax.ws.rs.Path;
28import javax.ws.rs.Produces;
29import javax.ws.rs.core.MediaType;
30import javax.ws.rs.core.Response;
31
32import java.util.List;
33import static org.slf4j.LoggerFactory.getLogger;
34import static org.onosproject.net.packet.PacketProcessor.ADVISOR_MAX;
35import static org.onosproject.net.packet.PacketProcessor.DIRECTOR_MAX;
36
37/**
38 * Manage inventory of packet processors.
39 */
40
41@Path("packet/processors")
42public class PacketProcessorsWebResource extends AbstractWebResource {
43
44 private final Logger log = getLogger(getClass());
45 PacketService service = get(PacketService.class);
46 private final ObjectNode root = mapper().createObjectNode();
47 private final ArrayNode pktProcNode = root.putArray("packet-processors");
48
49 /**
50 * Gets packet processors. Returns array of all packet processors.
51
52 * @onos.rsModel PacketProcessorsGet
53 * @return 200 OK with array of all packet processors.
54 */
55 @GET
56 @Produces(MediaType.APPLICATION_JSON)
57 public Response getPacketProcessors() {
58 List<PacketProcessorEntry> processors = service.getProcessors();
59 ObjectMapper mapper = new ObjectMapper();
60 for (PacketProcessorEntry p : processors) {
61 pktProcNode.add(mapper.createObjectNode()
62 .put("priority", priorityFormat(p.priority()))
63 .put("class", p.processor().getClass().getName())
64 .put("packets", p.invocations())
65 .put("avgNanos", p.averageNanos()));
66 }
67
68 return ok(root).build();
69 }
70
71 private String priorityFormat(int priority) {
72 if (priority > DIRECTOR_MAX) {
73 return "observer(" + (priority - DIRECTOR_MAX - 1) + ")";
74 } else if (priority > ADVISOR_MAX) {
75 return "director(" + (priority - ADVISOR_MAX - 1) + ")";
76 }
77 return "advisor(" + (priority - 1) + ")";
78 }
79}