blob: 34e8fb48331ff58cf150e9af4825e6980d37186c [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;
21import org.onlab.packet.Ip4Address;
22import org.onlab.packet.Ip4Prefix;
23import org.onlab.packet.IpAddress;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.TpPort;
26import org.onosproject.inbandtelemetry.api.IntIntent;
27import org.onosproject.inbandtelemetry.api.IntIntentId;
Carmelo Casconedefc74e2020-07-17 15:27:02 -070028import org.onosproject.net.behaviour.inbandtelemetry.IntMetadataType;
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090029import org.onosproject.inbandtelemetry.api.IntService;
Carmelo Casconedefc74e2020-07-17 15:27:02 -070030import org.onosproject.net.behaviour.inbandtelemetry.IntDeviceConfig;
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090031import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.ui.RequestHandler;
34import org.onosproject.ui.UiMessageHandler;
35import org.slf4j.Logger;
36import org.slf4j.LoggerFactory;
37
38import java.util.Collection;
39
Davide Scanob5ade982020-06-03 21:47:13 +020040import static org.onosproject.inbandtelemetry.api.IntIntent.IntHeaderType.HOP_BY_HOP;
41
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090042public class IntAppUiMessageHandler extends UiMessageHandler {
43
44 private static final String INT_INTENT_ADD_REQUEST = "intIntentAddRequest";
45 private static final String INT_INTENT_DEL_REQUEST = "intIntentDelRequest";
46 private static final String INT_CONFIG_ADD_REQUEST = "intConfigAddRequest";
47// private static final String INT_CONFIG_DEL_REQUEST = "intConfigDelRequest";
48
49 private final Logger log = LoggerFactory.getLogger(getClass());
50
51 private IntService intService;
52
53 @Override
54 protected Collection<RequestHandler> createRequestHandlers() {
55 return ImmutableSet.of(
56 new IntIntentAddRequestHandler(),
57 new IntIntentDelRequestHandler(),
58 new IntConfigAddRequestHandler()
Davide Scanob5ade982020-06-03 21:47:13 +020059 //new intConfigDelRequestHandler()
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090060 );
61 }
62
63 private final class IntConfigAddRequestHandler extends RequestHandler {
64 private IntConfigAddRequestHandler() {
65 super(INT_CONFIG_ADD_REQUEST);
66 }
67
68 @Override
69 public void process(ObjectNode payload) {
70 log.info("intConfigAddRequest: {}", payload);
71
72 intService = get(IntService.class);
Carmelo Casconedefc74e2020-07-17 15:27:02 -070073 IntDeviceConfig.Builder builder = IntDeviceConfig.builder();
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090074
75 if (payload.get("collectorIp") != null) {
76 builder.withCollectorIp(IpAddress.valueOf(payload.get("collectorIp").asText()));
77 } else {
78 builder.withCollectorIp(IpAddress.valueOf("127.0.0.1"));
79 }
80
81 if (payload.get("collectorPort") != null) {
82 builder.withCollectorPort(TpPort.tpPort(
83 payload.get("collectorPort").asInt()));
84 } else {
85 builder.withCollectorPort(TpPort.tpPort(1234));
86 }
87
88 builder.enabled(true)
89 .withSinkIp(IpAddress.valueOf("10.192.19.180"))
90 .withSinkMac(MacAddress.NONE)
91 .withCollectorNextHopMac(MacAddress.BROADCAST);
92
93 intService.setConfig(builder.build());
94 }
95 }
96
97 private final class IntIntentDelRequestHandler extends RequestHandler {
98 private IntIntentDelRequestHandler() {
99 super(INT_INTENT_DEL_REQUEST);
100 }
101
102 @Override
103 public void process(ObjectNode payload) {
104 log.info("intIntentDelRequest: {}", payload);
105
106 intService = get(IntService.class);
107
108 if (payload.get("intentId") != null) {
109 intService.removeIntIntent(IntIntentId.valueOf(payload.get("intentId").asLong()));
110 }
111 }
112 }
113
114 private final class IntIntentAddRequestHandler extends RequestHandler {
115 private IntIntentAddRequestHandler() {
116 super(INT_INTENT_ADD_REQUEST);
117 }
118
119 @Override
120 public void process(ObjectNode payload) {
121 log.info("intIntentAddRequest: {}", payload);
122
123 intService = get(IntService.class);
124
125 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
126 IntIntent.Builder builder = IntIntent.builder();
127
128 if (payload.get("ip4SrcPrefix") != null) {
129 sBuilder.matchIPSrc(parseIp4Prefix(payload.get("ip4SrcPrefix").asText()));
130 }
131
132 if (payload.get("ip4DstPrefix") != null) {
133 sBuilder.matchIPDst(parseIp4Prefix(payload.get("ip4DstPrefix").asText()));
134 }
135
136 if (payload.get("l4SrcPort") != null) {
137 if (payload.get("protocol") != null && payload.get("protocol").asText().equalsIgnoreCase("TCP")) {
138 sBuilder.matchTcpSrc(TpPort.tpPort(payload.get("l4SrcPort").asInt()));
139 } else {
140 sBuilder.matchUdpSrc(TpPort.tpPort(payload.get("l4SrcPort").asInt()));
141 }
142 }
143
144 if (payload.get("l4DstPort") != null) {
145 if (payload.get("protocol") != null && payload.get("protocol").asText().equalsIgnoreCase("TCP")) {
146 sBuilder.matchTcpDst(TpPort.tpPort(payload.get("l4DstPort").asInt()));
147 } else {
148 sBuilder.matchUdpDst(TpPort.tpPort(payload.get("l4DstPort").asInt()));
149 }
150 }
151
152 if (payload.get("metadata") != null) {
153 JsonNode meta = payload.get("metadata");
154 if (meta.isArray()) {
155 for (final JsonNode json : meta) {
156 switch (json.asText()) {
157 case "SWITCH_ID":
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700158 builder.withMetadataType(IntMetadataType.SWITCH_ID);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900159 break;
160 case "PORT_ID":
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700161 builder.withMetadataType(IntMetadataType.L1_PORT_ID);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900162 break;
163 case "HOP_LATENCY":
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700164 builder.withMetadataType(IntMetadataType.HOP_LATENCY);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900165 break;
166 case "QUEUE_OCCUPANCY":
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700167 builder.withMetadataType(IntMetadataType.QUEUE_OCCUPANCY);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900168 break;
169 case "INGRESS_TIMESTAMP":
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700170 builder.withMetadataType(IntMetadataType.INGRESS_TIMESTAMP);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900171 break;
172 case "EGRESS_TIMESTAMP":
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700173 builder.withMetadataType(IntMetadataType.EGRESS_TIMESTAMP);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900174 break;
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900175 case "EGRESS_TX_UTIL":
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700176 builder.withMetadataType(IntMetadataType.EGRESS_TX_UTIL);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900177 break;
178 default:
179 break;
180 }
181 }
182 }
183 }
184
185 builder.withSelector(sBuilder.build())
Davide Scanob5ade982020-06-03 21:47:13 +0200186 .withHeaderType(HOP_BY_HOP)
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900187 .withReportType(IntIntent.IntReportType.TRACKED_FLOW)
188 .withTelemetryMode(IntIntent.TelemetryMode.INBAND_TELEMETRY);
189 intService.installIntIntent(builder.build());
190 }
191
192 private Ip4Prefix parseIp4Prefix(String prefixString) {
193 if (prefixString == null) {
194 return null;
195 }
196 String[] splitString = prefixString.split("/");
197 Ip4Address ip4Address = Ip4Address.valueOf(splitString[0]);
198 int mask = splitString.length > 1 ? Integer.parseInt(splitString[1]) : 32;
199 return Ip4Prefix.valueOf(ip4Address, mask);
200 }
201 }
202}