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