blob: 56a7f5f04756338345d90909439dad6c1e17c0f3 [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.JsonNode;
19import com.google.common.collect.ImmutableSet;
20import com.fasterxml.jackson.databind.node.ObjectNode;
Yi Tseng930b0cd2020-10-04 22:33:22 -070021import org.onlab.packet.IPv4;
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090022import org.onlab.packet.Ip4Address;
23import org.onlab.packet.Ip4Prefix;
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090024import org.onlab.packet.TpPort;
25import org.onosproject.inbandtelemetry.api.IntIntent;
26import org.onosproject.inbandtelemetry.api.IntIntentId;
Carmelo Casconedefc74e2020-07-17 15:27:02 -070027import org.onosproject.net.behaviour.inbandtelemetry.IntMetadataType;
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090028import org.onosproject.inbandtelemetry.api.IntService;
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090029import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.ui.RequestHandler;
32import org.onosproject.ui.UiMessageHandler;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
35
36import java.util.Collection;
37
Davide Scanob5ade982020-06-03 21:47:13 +020038import static org.onosproject.inbandtelemetry.api.IntIntent.IntHeaderType.HOP_BY_HOP;
39
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090040public class IntAppUiMessageHandler extends UiMessageHandler {
41
42 private static final String INT_INTENT_ADD_REQUEST = "intIntentAddRequest";
43 private static final String INT_INTENT_DEL_REQUEST = "intIntentDelRequest";
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090044
45 private final Logger log = LoggerFactory.getLogger(getClass());
46
47 private IntService intService;
48
49 @Override
50 protected Collection<RequestHandler> createRequestHandlers() {
51 return ImmutableSet.of(
52 new IntIntentAddRequestHandler(),
Yi Tseng0f1ffd12020-09-18 11:10:47 -070053 new IntIntentDelRequestHandler()
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090054 );
55 }
56
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090057 private final class IntIntentDelRequestHandler extends RequestHandler {
58 private IntIntentDelRequestHandler() {
59 super(INT_INTENT_DEL_REQUEST);
60 }
61
62 @Override
63 public void process(ObjectNode payload) {
Yi Tseng930b0cd2020-10-04 22:33:22 -070064 log.debug("intIntentDelRequest: {}", payload);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090065 intService = get(IntService.class);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090066 if (payload.get("intentId") != null) {
67 intService.removeIntIntent(IntIntentId.valueOf(payload.get("intentId").asLong()));
68 }
69 }
70 }
71
72 private final class IntIntentAddRequestHandler extends RequestHandler {
73 private IntIntentAddRequestHandler() {
74 super(INT_INTENT_ADD_REQUEST);
75 }
76
77 @Override
78 public void process(ObjectNode payload) {
Yi Tseng930b0cd2020-10-04 22:33:22 -070079 log.debug("intIntentAddRequest: {}", payload);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090080
81 intService = get(IntService.class);
82
83 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
84 IntIntent.Builder builder = IntIntent.builder();
85
Yi Tseng930b0cd2020-10-04 22:33:22 -070086 JsonNode jsonNodeVal = payload.get("ip4SrcPrefix");
87 if (jsonNodeVal != null && !jsonNodeVal.asText().isEmpty()) {
88 sBuilder.matchIPSrc(parseIp4Prefix(jsonNodeVal.asText()));
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090089 }
90
Yi Tseng930b0cd2020-10-04 22:33:22 -070091 jsonNodeVal = payload.get("ip4DstPrefix");
92 if (jsonNodeVal != null && !jsonNodeVal.asText().isEmpty()) {
93 sBuilder.matchIPDst(parseIp4Prefix(jsonNodeVal.asText()));
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090094 }
95
Yi Tseng930b0cd2020-10-04 22:33:22 -070096 jsonNodeVal = payload.get("protocol");
97 byte ipProtocol = 0;
98 if (jsonNodeVal != null) {
99 if (jsonNodeVal.asText().equalsIgnoreCase("TCP")) {
100 ipProtocol = IPv4.PROTOCOL_TCP;
101 } else if (jsonNodeVal.asText().equalsIgnoreCase("UDP")) {
102 ipProtocol = IPv4.PROTOCOL_UDP;
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900103 }
104 }
105
Yi Tseng930b0cd2020-10-04 22:33:22 -0700106 jsonNodeVal = payload.get("l4SrcPort");
107 if (jsonNodeVal != null) {
108 int portNo = jsonNodeVal.asInt(0);
109 if (portNo != 0 && ipProtocol == IPv4.PROTOCOL_TCP) {
110 sBuilder.matchTcpSrc(TpPort.tpPort(portNo));
111 } else if (portNo != 0 && ipProtocol == IPv4.PROTOCOL_UDP) {
112 sBuilder.matchUdpSrc(TpPort.tpPort(portNo));
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900113 }
114 }
115
Yi Tseng930b0cd2020-10-04 22:33:22 -0700116 jsonNodeVal = payload.get("l4DstPort");
117 if (jsonNodeVal != null) {
118 int portNo = jsonNodeVal.asInt(0);
119 if (portNo != 0 && ipProtocol == IPv4.PROTOCOL_TCP) {
120 sBuilder.matchTcpDst(TpPort.tpPort(portNo));
121 } else if (portNo != 0 && ipProtocol == IPv4.PROTOCOL_UDP) {
122 sBuilder.matchUdpDst(TpPort.tpPort(portNo));
123 }
124 }
125
126 jsonNodeVal = payload.get("metadata");
127 if (jsonNodeVal != null && jsonNodeVal.isArray()) {
128 for (final JsonNode json : jsonNodeVal) {
129 switch (json.asText()) {
130 case "SWITCH_ID":
131 builder.withMetadataType(IntMetadataType.SWITCH_ID);
132 break;
133 case "PORT_ID":
134 builder.withMetadataType(IntMetadataType.L1_PORT_ID);
135 break;
136 case "HOP_LATENCY":
137 builder.withMetadataType(IntMetadataType.HOP_LATENCY);
138 break;
139 case "QUEUE_OCCUPANCY":
140 builder.withMetadataType(IntMetadataType.QUEUE_OCCUPANCY);
141 break;
142 case "INGRESS_TIMESTAMP":
143 builder.withMetadataType(IntMetadataType.INGRESS_TIMESTAMP);
144 break;
145 case "EGRESS_TIMESTAMP":
146 builder.withMetadataType(IntMetadataType.EGRESS_TIMESTAMP);
147 break;
148 case "EGRESS_TX_UTIL":
149 builder.withMetadataType(IntMetadataType.EGRESS_TX_UTIL);
150 break;
151 default:
152 break;
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900153 }
154 }
155 }
156
Yi Tseng4027cac2020-10-13 19:15:12 -0700157 jsonNodeVal = payload.get("telemetryMode");
158 if (jsonNodeVal != null) {
159 if (jsonNodeVal.asText()
160 .equalsIgnoreCase(IntIntent.TelemetryMode.POSTCARD.toString())) {
161 builder.withTelemetryMode(IntIntent.TelemetryMode.POSTCARD);
162 } else if (jsonNodeVal.asText()
163 .equalsIgnoreCase(IntIntent.TelemetryMode.INBAND_TELEMETRY.toString())) {
164 builder.withTelemetryMode(IntIntent.TelemetryMode.INBAND_TELEMETRY);
165 } else {
166 log.warn("Unsupport telemetry mode {}", jsonNodeVal.asText());
167 return;
168 }
169 }
170
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900171 builder.withSelector(sBuilder.build())
Davide Scanob5ade982020-06-03 21:47:13 +0200172 .withHeaderType(HOP_BY_HOP)
Yi Tseng4027cac2020-10-13 19:15:12 -0700173 .withReportType(IntIntent.IntReportType.TRACKED_FLOW);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900174 intService.installIntIntent(builder.build());
175 }
176
177 private Ip4Prefix parseIp4Prefix(String prefixString) {
178 if (prefixString == null) {
179 return null;
180 }
181 String[] splitString = prefixString.split("/");
182 Ip4Address ip4Address = Ip4Address.valueOf(splitString[0]);
183 int mask = splitString.length > 1 ? Integer.parseInt(splitString[1]) : 32;
184 return Ip4Prefix.valueOf(ip4Address, mask);
185 }
186 }
187}