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 | |
| 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"; |
| 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 Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 71 | IntDeviceConfig.Builder builder = IntDeviceConfig.builder(); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 72 | |
| 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 Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 156 | builder.withMetadataType(IntMetadataType.SWITCH_ID); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 157 | break; |
| 158 | case "PORT_ID": |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 159 | builder.withMetadataType(IntMetadataType.L1_PORT_ID); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 160 | break; |
| 161 | case "HOP_LATENCY": |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 162 | builder.withMetadataType(IntMetadataType.HOP_LATENCY); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 163 | break; |
| 164 | case "QUEUE_OCCUPANCY": |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 165 | builder.withMetadataType(IntMetadataType.QUEUE_OCCUPANCY); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 166 | break; |
| 167 | case "INGRESS_TIMESTAMP": |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 168 | builder.withMetadataType(IntMetadataType.INGRESS_TIMESTAMP); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 169 | break; |
| 170 | case "EGRESS_TIMESTAMP": |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 171 | builder.withMetadataType(IntMetadataType.EGRESS_TIMESTAMP); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 172 | break; |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 173 | case "EGRESS_TX_UTIL": |
Carmelo Cascone | defc74e | 2020-07-17 15:27:02 -0700 | [diff] [blame] | 174 | builder.withMetadataType(IntMetadataType.EGRESS_TX_UTIL); |
Jonghwan Hyun | 13a430d | 2018-07-22 17:02:51 +0900 | [diff] [blame] | 175 | 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 | } |