rsahot036 | 4df80f1 | 2018-10-15 17:46:14 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.rest.resources; |
| 17 | |
| 18 | import org.onosproject.rest.AbstractWebResource; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.onosproject.net.packet.PacketProcessorEntry; |
| 23 | import org.onosproject.net.packet.PacketService; |
rsahot036 | 4df80f1 | 2018-10-15 17:46:14 -0400 | [diff] [blame] | 24 | |
| 25 | import javax.ws.rs.GET; |
| 26 | import javax.ws.rs.Path; |
| 27 | import javax.ws.rs.Produces; |
| 28 | import javax.ws.rs.core.MediaType; |
| 29 | import javax.ws.rs.core.Response; |
| 30 | |
| 31 | import java.util.List; |
rsahot036 | 4df80f1 | 2018-10-15 17:46:14 -0400 | [diff] [blame] | 32 | import static org.onosproject.net.packet.PacketProcessor.ADVISOR_MAX; |
| 33 | import static org.onosproject.net.packet.PacketProcessor.DIRECTOR_MAX; |
| 34 | |
| 35 | /** |
| 36 | * Manage inventory of packet processors. |
| 37 | */ |
| 38 | |
| 39 | @Path("packet/processors") |
| 40 | public class PacketProcessorsWebResource extends AbstractWebResource { |
| 41 | |
rsahot036 | 4df80f1 | 2018-10-15 17:46:14 -0400 | [diff] [blame] | 42 | /** |
| 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 Chaundkar | 1a098eb | 2019-01-15 00:05:57 +0000 | [diff] [blame] | 51 | PacketService service = get(PacketService.class); |
| 52 | ObjectNode root = mapper().createObjectNode(); |
| 53 | ArrayNode pktProcNode = root.putArray("packet-processors"); |
rsahot036 | 4df80f1 | 2018-10-15 17:46:14 -0400 | [diff] [blame] | 54 | 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 | } |