Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [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.inbandtelemetry.app.ui; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.onosproject.inbandtelemetry.api.IntIntent; |
| 21 | import org.onosproject.inbandtelemetry.api.IntIntentId; |
| 22 | import org.onosproject.inbandtelemetry.api.IntService; |
| 23 | import org.onosproject.net.flow.criteria.Criterion; |
| 24 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 25 | import org.onosproject.net.flow.criteria.TcpPortCriterion; |
| 26 | import org.onosproject.net.flow.criteria.UdpPortCriterion; |
| 27 | import org.onosproject.ui.RequestHandler; |
| 28 | import org.onosproject.ui.UiMessageHandler; |
| 29 | import org.onosproject.ui.table.TableModel; |
| 30 | import org.onosproject.ui.table.TableRequestHandler; |
| 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| 34 | import java.util.Collection; |
| 35 | import java.util.Map; |
| 36 | import java.util.Set; |
| 37 | |
| 38 | /** |
| 39 | * Message handler for installed INT intents table in the application UI. |
| 40 | */ |
| 41 | public class IntAppTableMessageHandler extends UiMessageHandler { |
| 42 | private static final String INT_APP_INT_INTENT = "intAppIntIntent"; |
| 43 | private static final String INT_APP_INT_INTENT_DATA_REQUEST = INT_APP_INT_INTENT + "DataRequest"; |
| 44 | private static final String INT_APP_INT_INTENT_DATA_RESPONSE = INT_APP_INT_INTENT + "DataResponse"; |
| 45 | |
| 46 | private static final String INT_APP_DEL_INT_INTENT_REQ = "intAppDelIntIntentRequest"; |
| 47 | |
| 48 | private static final String NO_ROWS_MESSAGE = "No IntIntent found"; |
| 49 | |
| 50 | private static final String ID = "id"; |
| 51 | private static final String SRC_ADDR = "srcAddr"; |
| 52 | private static final String DST_ADDR = "dstAddr"; |
| 53 | private static final String SRC_PORT = "srcPort"; |
| 54 | private static final String DST_PORT = "dstPort"; |
| 55 | private static final String PROTOCOL = "protocol"; |
| 56 | private static final String METADATA = "metadata"; |
| 57 | |
| 58 | private static final String[] COLUMN_IDS = {ID, SRC_ADDR, DST_ADDR, SRC_PORT, DST_PORT, PROTOCOL, METADATA}; |
| 59 | |
| 60 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 61 | |
| 62 | protected IntService intService; |
| 63 | |
| 64 | @Override |
| 65 | protected Collection<RequestHandler> createRequestHandlers() { |
| 66 | return ImmutableSet.of( |
| 67 | new IntAppIntIntentRequestHandler(), |
| 68 | new IntAppDelIntIntentRequestHandler() |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | // handler for table requests |
| 73 | private final class IntAppIntIntentRequestHandler extends TableRequestHandler { |
| 74 | |
| 75 | private IntAppIntIntentRequestHandler() { |
| 76 | super(INT_APP_INT_INTENT_DATA_REQUEST, INT_APP_INT_INTENT_DATA_RESPONSE, INT_APP_INT_INTENT); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected String[] getColumnIds() { |
| 81 | return COLUMN_IDS; |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected String noRowsMessage(ObjectNode payload) { |
| 86 | return NO_ROWS_MESSAGE; |
| 87 | } |
| 88 | |
| 89 | private Map<IntIntentId, IntIntent> getAllIntIntents() { |
| 90 | intService = get(IntService.class); |
| 91 | return intService.getIntIntents(); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 96 | Map<IntIntentId, IntIntent> intentMap = getAllIntIntents(); |
| 97 | intentMap.entrySet().forEach(entry -> |
| 98 | populateRow(tm.addRow(), entry.getKey(), entry.getValue())); |
| 99 | } |
| 100 | |
| 101 | private void populateRow(TableModel.Row row, IntIntentId intentId, IntIntent intent) { |
| 102 | IPCriterion ip4Src = (IPCriterion) intent.selector().getCriterion(Criterion.Type.IPV4_SRC); |
| 103 | IPCriterion ip4Dst = (IPCriterion) intent.selector().getCriterion(Criterion.Type.IPV4_DST); |
| 104 | TcpPortCriterion tcpSrcPort = (TcpPortCriterion) intent.selector().getCriterion(Criterion.Type.TCP_SRC); |
| 105 | TcpPortCriterion tcpDstPort = (TcpPortCriterion) intent.selector().getCriterion(Criterion.Type.TCP_DST); |
| 106 | UdpPortCriterion udpSrcPort = (UdpPortCriterion) intent.selector().getCriterion(Criterion.Type.UDP_SRC); |
| 107 | UdpPortCriterion udpDstPort = (UdpPortCriterion) intent.selector().getCriterion(Criterion.Type.UDP_DST); |
| 108 | Set<IntIntent.IntMetadataType> metadataTypes = intent.metadataTypes(); |
| 109 | row.cell(ID, intentId.toString()) |
| 110 | .cell(SRC_ADDR, ip4Src == null ? "N/A" : ip4Src.ip().toString()) |
| 111 | .cell(DST_ADDR, ip4Dst == null ? "N/A" : ip4Dst.ip().toString()); |
| 112 | if (tcpSrcPort != null || tcpDstPort != null) { |
| 113 | row.cell(PROTOCOL, "TCP") |
| 114 | .cell(SRC_PORT, tcpSrcPort == null ? "N/A" : tcpSrcPort.tcpPort().toString()) |
| 115 | .cell(DST_PORT, tcpDstPort == null ? "N/A" : tcpDstPort.tcpPort().toString()); |
| 116 | } else if (udpSrcPort != null || udpDstPort != null) { |
| 117 | row.cell(PROTOCOL, "UDP") |
| 118 | .cell(SRC_PORT, udpSrcPort == null ? "N/A" : udpSrcPort.udpPort().toString()) |
| 119 | .cell(DST_PORT, udpDstPort == null ? "N/A" : udpDstPort.udpPort().toString()); |
| 120 | } else { |
| 121 | row.cell(PROTOCOL, "N/A") |
| 122 | .cell(SRC_PORT, "N/A") |
| 123 | .cell(DST_PORT, "N/A"); |
| 124 | } |
| 125 | String metaStr = ""; |
| 126 | for (IntIntent.IntMetadataType metadataType : metadataTypes) { |
| 127 | metaStr += metadataType.toString(); |
| 128 | metaStr += ", "; |
| 129 | } |
| 130 | row.cell(METADATA, metaStr); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | private final class IntAppDelIntIntentRequestHandler extends RequestHandler { |
| 135 | |
| 136 | private IntAppDelIntIntentRequestHandler() { |
| 137 | super(INT_APP_DEL_INT_INTENT_REQ); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void process(ObjectNode payload) { |
| 142 | intService = get(IntService.class); |
| 143 | if (payload.get(ID) != null) { |
| 144 | intService.removeIntIntent(IntIntentId.valueOf(payload.get(ID).asLong())); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |