rsahot036 | 848a7c6 | 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; |
| 24 | import org.slf4j.Logger; |
| 25 | |
| 26 | import javax.ws.rs.GET; |
| 27 | import javax.ws.rs.Path; |
| 28 | import javax.ws.rs.Produces; |
| 29 | import javax.ws.rs.core.MediaType; |
| 30 | import javax.ws.rs.core.Response; |
| 31 | |
| 32 | import java.util.List; |
| 33 | import static org.slf4j.LoggerFactory.getLogger; |
| 34 | import static org.onosproject.net.packet.PacketProcessor.ADVISOR_MAX; |
| 35 | import static org.onosproject.net.packet.PacketProcessor.DIRECTOR_MAX; |
| 36 | |
| 37 | /** |
| 38 | * Manage inventory of packet processors. |
| 39 | */ |
| 40 | |
| 41 | @Path("packet/processors") |
| 42 | public 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 | } |