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