Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.ui.impl; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.onosproject.net.packet.PacketProcessorEntry; |
| 22 | import org.onosproject.net.packet.PacketService; |
| 23 | import org.onosproject.ui.RequestHandler; |
| 24 | import org.onosproject.ui.UiMessageHandler; |
| 25 | import org.onosproject.ui.table.TableModel; |
| 26 | import org.onosproject.ui.table.TableRequestHandler; |
| 27 | import org.onosproject.ui.table.cell.NumberFormatter; |
| 28 | |
| 29 | import java.util.Collection; |
| 30 | |
| 31 | import static org.onosproject.net.packet.PacketProcessor.ADVISOR_MAX; |
| 32 | import static org.onosproject.net.packet.PacketProcessor.DIRECTOR_MAX; |
| 33 | |
| 34 | /** |
| 35 | * Message handler for packet processor view related messages. |
| 36 | */ |
| 37 | public class ProcessorViewMessageHandler extends UiMessageHandler { |
| 38 | |
| 39 | private static final String PROCESSOR_DATA_REQ = "processorDataRequest"; |
| 40 | private static final String PROCESSOR_DATA_RESP = "processorDataResponse"; |
| 41 | private static final String PROCESSORS = "processors"; |
| 42 | |
| 43 | private static final String ID = "id"; |
| 44 | private static final String TYPE = "type"; |
| 45 | private static final String PRIORITY = "priority"; |
| 46 | private static final String PROCESSOR = "processor"; |
| 47 | private static final String PACKETS = "packets"; |
| 48 | private static final String AVG_MS = "avgMillis"; |
| 49 | |
| 50 | private static final long NANOS_IN_MS = 1_000_000; |
| 51 | |
| 52 | private static final String[] COL_IDS = { |
| 53 | ID, TYPE, PRIORITY, PROCESSOR, PACKETS, AVG_MS |
| 54 | }; |
| 55 | |
| 56 | @Override |
| 57 | protected Collection<RequestHandler> createRequestHandlers() { |
| 58 | return ImmutableSet.of(new ProcessorDataRequest()); |
| 59 | } |
| 60 | |
| 61 | // handler for link table requests |
| 62 | private final class ProcessorDataRequest extends TableRequestHandler { |
| 63 | private ProcessorDataRequest() { |
| 64 | super(PROCESSOR_DATA_REQ, PROCESSOR_DATA_RESP, PROCESSORS); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | protected String[] getColumnIds() { |
| 69 | return COL_IDS; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | protected String defaultColumnId() { |
| 74 | return ID; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected TableModel createTableModel() { |
| 79 | TableModel tm = super.createTableModel(); |
| 80 | tm.setFormatter(AVG_MS, new NumberFormatter()); |
| 81 | return tm; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 86 | PacketService ps = get(PacketService.class); |
| 87 | ps.getProcessors().forEach(entry -> populateRow(tm.addRow(), entry)); |
| 88 | } |
| 89 | |
| 90 | private void populateRow(TableModel.Row row, PacketProcessorEntry entry) { |
| 91 | row.cell(ID, entry.priority()) |
| 92 | .cell(TYPE, processorType(entry.priority())) |
| 93 | .cell(PRIORITY, processorPriority(entry.priority())) |
| 94 | .cell(PROCESSOR, entry.processor().getClass().getName()) |
| 95 | .cell(PACKETS, entry.invocations()) |
| 96 | .cell(AVG_MS, entry.averageNanos() / NANOS_IN_MS); |
| 97 | } |
| 98 | |
| 99 | private String processorType(int p) { |
| 100 | return p > DIRECTOR_MAX ? "observer" : |
| 101 | p > ADVISOR_MAX ? "director" : "observer"; |
| 102 | } |
| 103 | |
| 104 | private int processorPriority(int p) { |
| 105 | return p > DIRECTOR_MAX ? (p - DIRECTOR_MAX - 1) : |
| 106 | p > ADVISOR_MAX ? (p - ADVISOR_MAX - 1) : (p - 1); |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | } |