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; |
| 21 | import org.onlab.packet.Ip4Address; |
| 22 | import org.onlab.packet.Ip4Prefix; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onlab.packet.MacAddress; |
| 25 | import org.onlab.packet.TpPort; |
| 26 | import org.onosproject.inbandtelemetry.api.IntIntent; |
| 27 | import org.onosproject.inbandtelemetry.api.IntIntentId; |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 28 | import org.onosproject.net.behaviour.inbandtelemetry.IntMetadataType; |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 29 | import org.onosproject.inbandtelemetry.api.IntService; |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 30 | import org.onosproject.net.behaviour.inbandtelemetry.IntDeviceConfig; |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.TrafficSelector; |
| 33 | import org.onosproject.ui.RequestHandler; |
| 34 | import org.onosproject.ui.UiMessageHandler; |
| 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
| 38 | import java.util.Collection; |
| 39 | |
Davide Scano | b5ade98 | 2020-06-03 21:47:13 +0200 | [diff] [blame^] | 40 | import static org.onosproject.inbandtelemetry.api.IntIntent.IntHeaderType.HOP_BY_HOP; |
| 41 | |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 42 | public 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 Scano | b5ade98 | 2020-06-03 21:47:13 +0200 | [diff] [blame^] | 59 | //new intConfigDelRequestHandler() |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 60 | ); |
| 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 Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 73 | IntDeviceConfig.Builder builder = IntDeviceConfig.builder(); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 74 | |
| 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 Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 158 | builder.withMetadataType(IntMetadataType.SWITCH_ID); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 159 | break; |
| 160 | case "PORT_ID": |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 161 | builder.withMetadataType(IntMetadataType.L1_PORT_ID); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 162 | break; |
| 163 | case "HOP_LATENCY": |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 164 | builder.withMetadataType(IntMetadataType.HOP_LATENCY); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 165 | break; |
| 166 | case "QUEUE_OCCUPANCY": |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 167 | builder.withMetadataType(IntMetadataType.QUEUE_OCCUPANCY); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 168 | break; |
| 169 | case "INGRESS_TIMESTAMP": |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 170 | builder.withMetadataType(IntMetadataType.INGRESS_TIMESTAMP); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 171 | break; |
| 172 | case "EGRESS_TIMESTAMP": |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 173 | builder.withMetadataType(IntMetadataType.EGRESS_TIMESTAMP); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 174 | break; |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 175 | case "EGRESS_TX_UTIL": |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 176 | builder.withMetadataType(IntMetadataType.EGRESS_TX_UTIL); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 177 | break; |
| 178 | default: |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | builder.withSelector(sBuilder.build()) |
Davide Scano | b5ade98 | 2020-06-03 21:47:13 +0200 | [diff] [blame^] | 186 | .withHeaderType(HOP_BY_HOP) |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 187 | .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 | } |