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