blob: b696b1d0ee1d094df3ee0e8c3d7c1d6bad29407c [file] [log] [blame]
Jonghwan Hyun13a430d2018-07-22 17:02:51 +09001/*
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 */
16package org.onosproject.inbandtelemetry.app.ui;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import com.google.common.collect.ImmutableSet;
20import org.onosproject.inbandtelemetry.api.IntIntent;
21import org.onosproject.inbandtelemetry.api.IntIntentId;
Carmelo Casconedefc74e2020-07-17 15:27:02 -070022import org.onosproject.net.behaviour.inbandtelemetry.IntMetadataType;
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090023import org.onosproject.inbandtelemetry.api.IntService;
24import org.onosproject.net.flow.criteria.Criterion;
25import org.onosproject.net.flow.criteria.IPCriterion;
26import org.onosproject.net.flow.criteria.TcpPortCriterion;
27import org.onosproject.net.flow.criteria.UdpPortCriterion;
28import org.onosproject.ui.RequestHandler;
29import org.onosproject.ui.UiMessageHandler;
30import org.onosproject.ui.table.TableModel;
31import org.onosproject.ui.table.TableRequestHandler;
32import org.slf4j.Logger;
33import org.slf4j.LoggerFactory;
34
35import java.util.Collection;
36import java.util.Map;
37import java.util.Set;
38
39/**
40 * Message handler for installed INT intents table in the application UI.
41 */
42public class IntAppTableMessageHandler extends UiMessageHandler {
43 private static final String INT_APP_INT_INTENT = "intAppIntIntent";
Davide Scanob5ade982020-06-03 21:47:13 +020044 private static final String INT_APP_INT_INTENT_PAYLOAD = "intAppIntIntents";
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090045 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";
Yi Tseng4027cac2020-10-13 19:15:12 -070059 private static final String TELEMETRY_MODE = "telemetryMode";
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090060
Yi Tseng4027cac2020-10-13 19:15:12 -070061 private static final String[] COLUMN_IDS = {
62 ID, SRC_ADDR, DST_ADDR, SRC_PORT, DST_PORT, PROTOCOL, METADATA, TELEMETRY_MODE};
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090063
64 private final Logger log = LoggerFactory.getLogger(getClass());
65
66 protected IntService intService;
67
68 @Override
69 protected Collection<RequestHandler> createRequestHandlers() {
70 return ImmutableSet.of(
71 new IntAppIntIntentRequestHandler(),
72 new IntAppDelIntIntentRequestHandler()
73 );
74 }
75
76 // handler for table requests
77 private final class IntAppIntIntentRequestHandler extends TableRequestHandler {
78
79 private IntAppIntIntentRequestHandler() {
Davide Scanob5ade982020-06-03 21:47:13 +020080 super(INT_APP_INT_INTENT_DATA_REQUEST, INT_APP_INT_INTENT_DATA_RESPONSE, INT_APP_INT_INTENT_PAYLOAD);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090081 }
82
83 @Override
84 protected String[] getColumnIds() {
85 return COLUMN_IDS;
86 }
87
88 @Override
89 protected String noRowsMessage(ObjectNode payload) {
90 return NO_ROWS_MESSAGE;
91 }
92
93 private Map<IntIntentId, IntIntent> getAllIntIntents() {
94 intService = get(IntService.class);
95 return intService.getIntIntents();
96 }
97
98 @Override
99 protected void populateTable(TableModel tm, ObjectNode payload) {
100 Map<IntIntentId, IntIntent> intentMap = getAllIntIntents();
101 intentMap.entrySet().forEach(entry ->
102 populateRow(tm.addRow(), entry.getKey(), entry.getValue()));
103 }
104
105 private void populateRow(TableModel.Row row, IntIntentId intentId, IntIntent intent) {
106 IPCriterion ip4Src = (IPCriterion) intent.selector().getCriterion(Criterion.Type.IPV4_SRC);
107 IPCriterion ip4Dst = (IPCriterion) intent.selector().getCriterion(Criterion.Type.IPV4_DST);
108 TcpPortCriterion tcpSrcPort = (TcpPortCriterion) intent.selector().getCriterion(Criterion.Type.TCP_SRC);
109 TcpPortCriterion tcpDstPort = (TcpPortCriterion) intent.selector().getCriterion(Criterion.Type.TCP_DST);
110 UdpPortCriterion udpSrcPort = (UdpPortCriterion) intent.selector().getCriterion(Criterion.Type.UDP_SRC);
111 UdpPortCriterion udpDstPort = (UdpPortCriterion) intent.selector().getCriterion(Criterion.Type.UDP_DST);
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700112 Set<IntMetadataType> metadataTypes = intent.metadataTypes();
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900113 row.cell(ID, intentId.toString())
114 .cell(SRC_ADDR, ip4Src == null ? "N/A" : ip4Src.ip().toString())
115 .cell(DST_ADDR, ip4Dst == null ? "N/A" : ip4Dst.ip().toString());
116 if (tcpSrcPort != null || tcpDstPort != null) {
117 row.cell(PROTOCOL, "TCP")
118 .cell(SRC_PORT, tcpSrcPort == null ? "N/A" : tcpSrcPort.tcpPort().toString())
119 .cell(DST_PORT, tcpDstPort == null ? "N/A" : tcpDstPort.tcpPort().toString());
120 } else if (udpSrcPort != null || udpDstPort != null) {
121 row.cell(PROTOCOL, "UDP")
122 .cell(SRC_PORT, udpSrcPort == null ? "N/A" : udpSrcPort.udpPort().toString())
123 .cell(DST_PORT, udpDstPort == null ? "N/A" : udpDstPort.udpPort().toString());
124 } else {
125 row.cell(PROTOCOL, "N/A")
126 .cell(SRC_PORT, "N/A")
127 .cell(DST_PORT, "N/A");
128 }
129 String metaStr = "";
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700130 for (IntMetadataType metadataType : metadataTypes) {
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900131 metaStr += metadataType.toString();
132 metaStr += ", ";
133 }
134 row.cell(METADATA, metaStr);
Yi Tseng4027cac2020-10-13 19:15:12 -0700135 row.cell(TELEMETRY_MODE, intent.telemetryMode());
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900136 }
137 }
138
139 private final class IntAppDelIntIntentRequestHandler extends RequestHandler {
140
141 private IntAppDelIntIntentRequestHandler() {
142 super(INT_APP_DEL_INT_INTENT_REQ);
143 }
144
145 @Override
146 public void process(ObjectNode payload) {
147 intService = get(IntService.class);
148 if (payload.get(ID) != null) {
149 intService.removeIntIntent(IntIntentId.valueOf(payload.get(ID).asLong()));
150 }
151 }
152 }
153}