Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
| 17 | package org.onosproject.t3.api; |
| 18 | |
Andrea Campanella | 20c1705 | 2018-01-30 16:19:35 +0100 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 20 | import org.apache.commons.lang3.tuple.Pair; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.DeviceId; |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 23 | import org.onosproject.net.Host; |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 24 | import org.onosproject.net.PipelineTraceableHitChain; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 25 | import org.onosproject.net.flow.FlowEntry; |
| 26 | import org.onosproject.net.flow.TrafficSelector; |
| 27 | |
| 28 | import java.util.ArrayList; |
pierventre | 4fd3b46 | 2020-10-15 17:31:44 +0200 | [diff] [blame^] | 29 | import java.util.Collection; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 30 | import java.util.HashMap; |
| 31 | import java.util.List; |
| 32 | import java.util.Map; |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 33 | import java.util.Optional; |
pierventre | 4fd3b46 | 2020-10-15 17:31:44 +0200 | [diff] [blame^] | 34 | import java.util.stream.Collectors; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 35 | |
Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 36 | /** |
| 37 | * Encapsulates the result of tracing a packet (traffic selector) through |
| 38 | * the current topology. |
| 39 | */ |
| 40 | public class StaticPacketTrace { |
| 41 | |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 42 | private final TrafficSelector ingressPacket; |
| 43 | private final ConnectPoint ingressPoint; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 44 | List<List<ConnectPoint>> completePaths; |
| 45 | private Map<DeviceId, List<GroupsInDevice>> outputsForDevice; |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 46 | private Map<DeviceId, List<PipelineTraceableHitChain>> hitChainsForDevice; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 47 | private Map<DeviceId, List<FlowEntry>> flowsForDevice; |
| 48 | private StringBuilder resultMessage; |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 49 | private Pair<Host, Host> hosts; |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 50 | private List<Boolean> success = new ArrayList<>(); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * Builds the trace with a given packet and a connect point. |
| 54 | * |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 55 | * @param inPacket the packet to trace |
| 56 | * @param inPoint the initial connect point |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 57 | */ |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 58 | public StaticPacketTrace(TrafficSelector inPacket, ConnectPoint inPoint) { |
| 59 | this.ingressPacket = inPacket; |
| 60 | this.ingressPoint = inPoint; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 61 | completePaths = new ArrayList<>(); |
| 62 | outputsForDevice = new HashMap<>(); |
| 63 | flowsForDevice = new HashMap<>(); |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 64 | hitChainsForDevice = new HashMap<>(); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 65 | resultMessage = new StringBuilder(); |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 66 | hosts = null; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Builds the trace with a given packet and a connect point. |
| 71 | * |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 72 | * @param inPacket the packet to trace |
| 73 | * @param inPoint the initial connect point |
| 74 | * @param hosts pair of source and destination hosts |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 75 | */ |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 76 | public StaticPacketTrace(TrafficSelector inPacket, ConnectPoint inPoint, Pair<Host, Host> hosts) { |
| 77 | this.ingressPacket = inPacket; |
| 78 | this.ingressPoint = inPoint; |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 79 | completePaths = new ArrayList<>(); |
| 80 | outputsForDevice = new HashMap<>(); |
| 81 | flowsForDevice = new HashMap<>(); |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 82 | hitChainsForDevice = new HashMap<>(); |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 83 | resultMessage = new StringBuilder(); |
| 84 | this.hosts = hosts; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Return the initial packet. |
| 89 | * |
| 90 | * @return the initial packet in the form of a selector. |
| 91 | */ |
| 92 | public TrafficSelector getInitialPacket() { |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 93 | return ingressPacket; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Returns the first connect point the packet came in through. |
| 98 | * |
| 99 | * @return the connect point |
| 100 | */ |
| 101 | public ConnectPoint getInitialConnectPoint() { |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 102 | return ingressPoint; |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Add a result message for the Trace. |
| 107 | * |
| 108 | * @param resultMessage the message |
| 109 | */ |
| 110 | public void addResultMessage(String resultMessage) { |
| 111 | if (this.resultMessage.length() != 0) { |
| 112 | this.resultMessage.append("\n"); |
| 113 | } |
| 114 | this.resultMessage.append(resultMessage); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Return the result message. |
| 119 | * |
| 120 | * @return the message |
| 121 | */ |
| 122 | public String resultMessage() { |
| 123 | return resultMessage.toString(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Adds the groups for a given device. |
| 128 | * |
| 129 | * @param deviceId the device |
| 130 | * @param outputPath the groups in device objects |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 131 | * @deprecated in t3-4.0 |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 132 | */ |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 133 | @Deprecated |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 134 | public void addGroupOutputPath(DeviceId deviceId, GroupsInDevice outputPath) { |
| 135 | if (!outputsForDevice.containsKey(deviceId)) { |
| 136 | outputsForDevice.put(deviceId, new ArrayList<>()); |
| 137 | } |
| 138 | outputsForDevice.get(deviceId).add(outputPath); |
| 139 | } |
| 140 | |
| 141 | /** |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 142 | * Adds the pipeline hit chain for a given device. |
| 143 | * |
| 144 | * @param deviceId the device |
| 145 | * @param hitChain the hit chain |
| 146 | */ |
| 147 | public void addHitChain(DeviceId deviceId, PipelineTraceableHitChain hitChain) { |
| 148 | hitChainsForDevice.compute(deviceId, (k, v) -> { |
| 149 | if (v == null) { |
| 150 | v = new ArrayList<>(); |
| 151 | } |
| 152 | if (!v.contains(hitChain)) { |
| 153 | v.add(hitChain); |
| 154 | } |
| 155 | return v; |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | /** |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 160 | * Returns all the possible group-based outputs for a given device. |
| 161 | * |
| 162 | * @param deviceId the device |
| 163 | * @return the list of Groups for this device. |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 164 | * @deprecated in t3-4.0 |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 165 | */ |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 166 | @Deprecated |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 167 | public List<GroupsInDevice> getGroupOuputs(DeviceId deviceId) { |
Andrea Campanella | 7fa8f0a | 2018-03-09 15:30:22 -0800 | [diff] [blame] | 168 | return outputsForDevice.get(deviceId) == null ? null : ImmutableList.copyOf(outputsForDevice.get(deviceId)); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 172 | * Returns all the possible pipeline hit chains for a given device. |
| 173 | * |
| 174 | * @param deviceId the device |
| 175 | * @return the list of hit chains |
| 176 | */ |
| 177 | public List<PipelineTraceableHitChain> getHitChains(DeviceId deviceId) { |
| 178 | List<PipelineTraceableHitChain> hitChains = hitChainsForDevice.get(deviceId); |
| 179 | return hitChains == null ? null : ImmutableList.copyOf(hitChains); |
pierventre | 4fd3b46 | 2020-10-15 17:31:44 +0200 | [diff] [blame^] | 180 | } |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 181 | |
pierventre | 4fd3b46 | 2020-10-15 17:31:44 +0200 | [diff] [blame^] | 182 | /** |
| 183 | * Return all the dropped hit chains. |
| 184 | * |
| 185 | * @return the dropped hit chains |
| 186 | */ |
| 187 | public List<PipelineTraceableHitChain> getDroppedHitChains() { |
| 188 | return hitChainsForDevice.values().stream() |
| 189 | .flatMap(Collection::stream) |
| 190 | .filter(PipelineTraceableHitChain::isDropped) |
| 191 | .collect(Collectors.toList()); |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /** |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 195 | * Adds a complete possible path. |
| 196 | * |
| 197 | * @param completePath the path |
| 198 | */ |
| 199 | public void addCompletePath(List<ConnectPoint> completePath) { |
| 200 | completePaths.add(completePath); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Return all the possible path the packet can take through the network. |
| 205 | * |
| 206 | * @return a list of paths |
| 207 | */ |
| 208 | public List<List<ConnectPoint>> getCompletePaths() { |
| 209 | return completePaths; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Add the flows traversed by the packet in a given device. |
| 214 | * |
| 215 | * @param deviceId the device considered |
| 216 | * @param flows the flows |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 217 | * @deprecated in t3-4.0 |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 218 | */ |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 219 | @Deprecated |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 220 | public void addFlowsForDevice(DeviceId deviceId, List<FlowEntry> flows) { |
| 221 | flowsForDevice.put(deviceId, flows); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Returns the flows matched by this trace's packet for a given device. |
| 226 | * |
| 227 | * @param deviceId the device |
| 228 | * @return the flows matched |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 229 | * @deprecated in t3-4.0 |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 230 | */ |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 231 | @Deprecated |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 232 | public List<FlowEntry> getFlowsForDevice(DeviceId deviceId) { |
Andrea Campanella | 20c1705 | 2018-01-30 16:19:35 +0100 | [diff] [blame] | 233 | return flowsForDevice.getOrDefault(deviceId, ImmutableList.of()); |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 236 | /** |
| 237 | * Return, if present, the two hosts at the endpoints of this trace. |
| 238 | * |
| 239 | * @return pair of source and destination hosts |
| 240 | */ |
| 241 | public Optional<Pair<Host, Host>> getEndpointHosts() { |
| 242 | return Optional.ofNullable(hosts); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Sets the two hosts at the endpoints of this trace. |
| 247 | * |
| 248 | * @param endpointHosts pair of source and destination hosts |
| 249 | */ |
| 250 | public void addEndpointHosts(Pair<Host, Host> endpointHosts) { |
| 251 | hosts = endpointHosts; |
| 252 | } |
| 253 | |
Andrea Campanella | 0cc6acd | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 254 | /** |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 255 | * Return if all the possible paths of this trace are successful. |
Andrea Campanella | 7fa8f0a | 2018-03-09 15:30:22 -0800 | [diff] [blame] | 256 | * |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 257 | * @return true if all paths are successful |
Andrea Campanella | 0cc6acd | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 258 | */ |
| 259 | public boolean isSuccess() { |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 260 | return !success.contains(false); |
Andrea Campanella | 0cc6acd | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | /** |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 264 | * Sets if a path from this trace is successful. |
Andrea Campanella | 7fa8f0a | 2018-03-09 15:30:22 -0800 | [diff] [blame] | 265 | * |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 266 | * @param success true if a path of trace is successful. |
Andrea Campanella | 0cc6acd | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 267 | */ |
| 268 | public void setSuccess(boolean success) { |
Andrea Campanella | 5af2d2b | 2018-03-12 19:25:44 -0700 | [diff] [blame] | 269 | this.success.add(success); |
Andrea Campanella | 0cc6acd | 2018-02-28 16:43:16 +0100 | [diff] [blame] | 270 | } |
| 271 | |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 272 | @Override |
| 273 | public String toString() { |
| 274 | return "StaticPacketTrace{" + |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 275 | "ingressPacket=" + ingressPacket + |
| 276 | ", ingressPoint=" + ingressPoint + |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 277 | ", completePaths=" + completePaths + |
| 278 | ", outputsForDevice=" + outputsForDevice + |
| 279 | ", flowsForDevice=" + flowsForDevice + |
pierventre | fe57fda | 2020-08-04 22:52:02 +0200 | [diff] [blame] | 280 | ", hitChains=" + hitChainsForDevice + |
Andrea Campanella | e408440 | 2017-12-15 15:27:31 +0100 | [diff] [blame] | 281 | ", resultMessage=" + resultMessage + |
| 282 | '}'; |
| 283 | } |
Simon Hunt | 026a287 | 2017-11-13 17:09:43 -0800 | [diff] [blame] | 284 | } |