blob: 226c428d6a3d9f7bf853620421581b2846a8da93 [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
40public 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";
44 private static final String INT_CONFIG_ADD_REQUEST = "intConfigAddRequest";
45// private static final String INT_CONFIG_DEL_REQUEST = "intConfigDelRequest";
46
47 private final Logger log = LoggerFactory.getLogger(getClass());
48
49 private IntService intService;
50
51 @Override
52 protected Collection<RequestHandler> createRequestHandlers() {
53 return ImmutableSet.of(
54 new IntIntentAddRequestHandler(),
55 new IntIntentDelRequestHandler(),
56 new IntConfigAddRequestHandler()
57// new intConfigDelRequestHandler()
58 );
59 }
60
61 private final class IntConfigAddRequestHandler extends RequestHandler {
62 private IntConfigAddRequestHandler() {
63 super(INT_CONFIG_ADD_REQUEST);
64 }
65
66 @Override
67 public void process(ObjectNode payload) {
68 log.info("intConfigAddRequest: {}", payload);
69
70 intService = get(IntService.class);
Carmelo Casconedefc74e2020-07-17 15:27:02 -070071 IntDeviceConfig.Builder builder = IntDeviceConfig.builder();
Jonghwan Hyun13a430d2018-07-22 17:02:51 +090072
73 if (payload.get("collectorIp") != null) {
74 builder.withCollectorIp(IpAddress.valueOf(payload.get("collectorIp").asText()));
75 } else {
76 builder.withCollectorIp(IpAddress.valueOf("127.0.0.1"));
77 }
78
79 if (payload.get("collectorPort") != null) {
80 builder.withCollectorPort(TpPort.tpPort(
81 payload.get("collectorPort").asInt()));
82 } else {
83 builder.withCollectorPort(TpPort.tpPort(1234));
84 }
85
86 builder.enabled(true)
87 .withSinkIp(IpAddress.valueOf("10.192.19.180"))
88 .withSinkMac(MacAddress.NONE)
89 .withCollectorNextHopMac(MacAddress.BROADCAST);
90
91 intService.setConfig(builder.build());
92 }
93 }
94
95 private final class IntIntentDelRequestHandler extends RequestHandler {
96 private IntIntentDelRequestHandler() {
97 super(INT_INTENT_DEL_REQUEST);
98 }
99
100 @Override
101 public void process(ObjectNode payload) {
102 log.info("intIntentDelRequest: {}", payload);
103
104 intService = get(IntService.class);
105
106 if (payload.get("intentId") != null) {
107 intService.removeIntIntent(IntIntentId.valueOf(payload.get("intentId").asLong()));
108 }
109 }
110 }
111
112 private final class IntIntentAddRequestHandler extends RequestHandler {
113 private IntIntentAddRequestHandler() {
114 super(INT_INTENT_ADD_REQUEST);
115 }
116
117 @Override
118 public void process(ObjectNode payload) {
119 log.info("intIntentAddRequest: {}", payload);
120
121 intService = get(IntService.class);
122
123 TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder();
124 IntIntent.Builder builder = IntIntent.builder();
125
126 if (payload.get("ip4SrcPrefix") != null) {
127 sBuilder.matchIPSrc(parseIp4Prefix(payload.get("ip4SrcPrefix").asText()));
128 }
129
130 if (payload.get("ip4DstPrefix") != null) {
131 sBuilder.matchIPDst(parseIp4Prefix(payload.get("ip4DstPrefix").asText()));
132 }
133
134 if (payload.get("l4SrcPort") != null) {
135 if (payload.get("protocol") != null && payload.get("protocol").asText().equalsIgnoreCase("TCP")) {
136 sBuilder.matchTcpSrc(TpPort.tpPort(payload.get("l4SrcPort").asInt()));
137 } else {
138 sBuilder.matchUdpSrc(TpPort.tpPort(payload.get("l4SrcPort").asInt()));
139 }
140 }
141
142 if (payload.get("l4DstPort") != null) {
143 if (payload.get("protocol") != null && payload.get("protocol").asText().equalsIgnoreCase("TCP")) {
144 sBuilder.matchTcpDst(TpPort.tpPort(payload.get("l4DstPort").asInt()));
145 } else {
146 sBuilder.matchUdpDst(TpPort.tpPort(payload.get("l4DstPort").asInt()));
147 }
148 }
149
150 if (payload.get("metadata") != null) {
151 JsonNode meta = payload.get("metadata");
152 if (meta.isArray()) {
153 for (final JsonNode json : meta) {
154 switch (json.asText()) {
155 case "SWITCH_ID":
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700156 builder.withMetadataType(IntMetadataType.SWITCH_ID);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900157 break;
158 case "PORT_ID":
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700159 builder.withMetadataType(IntMetadataType.L1_PORT_ID);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900160 break;
161 case "HOP_LATENCY":
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700162 builder.withMetadataType(IntMetadataType.HOP_LATENCY);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900163 break;
164 case "QUEUE_OCCUPANCY":
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700165 builder.withMetadataType(IntMetadataType.QUEUE_OCCUPANCY);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900166 break;
167 case "INGRESS_TIMESTAMP":
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700168 builder.withMetadataType(IntMetadataType.INGRESS_TIMESTAMP);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900169 break;
170 case "EGRESS_TIMESTAMP":
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700171 builder.withMetadataType(IntMetadataType.EGRESS_TIMESTAMP);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900172 break;
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900173 case "EGRESS_TX_UTIL":
Carmelo Casconedefc74e2020-07-17 15:27:02 -0700174 builder.withMetadataType(IntMetadataType.EGRESS_TX_UTIL);
Jonghwan Hyun13a430d2018-07-22 17:02:51 +0900175 break;
176 default:
177 break;
178 }
179 }
180 }
181 }
182
183 builder.withSelector(sBuilder.build())
184 .withHeaderType(IntIntent.IntHeaderType.HOP_BY_HOP)
185 .withReportType(IntIntent.IntReportType.TRACKED_FLOW)
186 .withTelemetryMode(IntIntent.TelemetryMode.INBAND_TELEMETRY);
187 intService.installIntIntent(builder.build());
188 }
189
190 private Ip4Prefix parseIp4Prefix(String prefixString) {
191 if (prefixString == null) {
192 return null;
193 }
194 String[] splitString = prefixString.split("/");
195 Ip4Address ip4Address = Ip4Address.valueOf(splitString[0]);
196 int mask = splitString.length > 1 ? Integer.parseInt(splitString[1]) : 32;
197 return Ip4Prefix.valueOf(ip4Address, mask);
198 }
199 }
200}