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