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