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