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