Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.inbandtelemetry.app.ui; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Yi Tseng | 930b0cd | 2020-10-04 22:33:22 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IPv4; |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 22 | import org.onlab.packet.Ip4Address; |
| 23 | import org.onlab.packet.Ip4Prefix; |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 24 | import org.onlab.packet.TpPort; |
| 25 | import org.onosproject.inbandtelemetry.api.IntIntent; |
| 26 | import org.onosproject.inbandtelemetry.api.IntIntentId; |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 27 | import org.onosproject.net.behaviour.inbandtelemetry.IntMetadataType; |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 28 | import org.onosproject.inbandtelemetry.api.IntService; |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 29 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 30 | import org.onosproject.net.flow.TrafficSelector; |
| 31 | import org.onosproject.ui.RequestHandler; |
| 32 | import org.onosproject.ui.UiMessageHandler; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.util.Collection; |
| 37 | |
Davide Scano | b5ade98 | 2020-06-03 21:47:13 +0200 | [diff] [blame] | 38 | import static org.onosproject.inbandtelemetry.api.IntIntent.IntHeaderType.HOP_BY_HOP; |
| 39 | |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 40 | public 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 Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 44 | |
| 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 Tseng | 0f1ffd1 | 2020-09-18 11:10:47 -0700 | [diff] [blame] | 53 | new IntIntentDelRequestHandler() |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 54 | ); |
| 55 | } |
| 56 | |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 57 | 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 Tseng | 930b0cd | 2020-10-04 22:33:22 -0700 | [diff] [blame] | 64 | log.debug("intIntentDelRequest: {}", payload); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 65 | intService = get(IntService.class); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 66 | 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 Tseng | 930b0cd | 2020-10-04 22:33:22 -0700 | [diff] [blame] | 79 | log.debug("intIntentAddRequest: {}", payload); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 80 | |
| 81 | intService = get(IntService.class); |
| 82 | |
| 83 | TrafficSelector.Builder sBuilder = DefaultTrafficSelector.builder(); |
| 84 | IntIntent.Builder builder = IntIntent.builder(); |
| 85 | |
Yi Tseng | 930b0cd | 2020-10-04 22:33:22 -0700 | [diff] [blame] | 86 | JsonNode jsonNodeVal = payload.get("ip4SrcPrefix"); |
| 87 | if (jsonNodeVal != null && !jsonNodeVal.asText().isEmpty()) { |
| 88 | sBuilder.matchIPSrc(parseIp4Prefix(jsonNodeVal.asText())); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 89 | } |
| 90 | |
Yi Tseng | 930b0cd | 2020-10-04 22:33:22 -0700 | [diff] [blame] | 91 | jsonNodeVal = payload.get("ip4DstPrefix"); |
| 92 | if (jsonNodeVal != null && !jsonNodeVal.asText().isEmpty()) { |
| 93 | sBuilder.matchIPDst(parseIp4Prefix(jsonNodeVal.asText())); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 94 | } |
| 95 | |
Yi Tseng | 930b0cd | 2020-10-04 22:33:22 -0700 | [diff] [blame] | 96 | 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 Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Yi Tseng | 930b0cd | 2020-10-04 22:33:22 -0700 | [diff] [blame] | 106 | 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 Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Yi Tseng | 930b0cd | 2020-10-04 22:33:22 -0700 | [diff] [blame] | 116 | 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 Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
Yi Tseng | 4027cac | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 157 | 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 Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 171 | builder.withSelector(sBuilder.build()) |
Davide Scano | b5ade98 | 2020-06-03 21:47:13 +0200 | [diff] [blame] | 172 | .withHeaderType(HOP_BY_HOP) |
Yi Tseng | 4027cac | 2020-10-13 19:15:12 -0700 | [diff] [blame^] | 173 | .withReportType(IntIntent.IntReportType.TRACKED_FLOW); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 174 | 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 | } |