blob: 149cba7ebeac8af19ac59c7aa59ee8ab245ead3a [file] [log] [blame]
Simon Hunt026a2872017-11-13 17:09:43 -08001/*
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
17package org.onosproject.t3.api;
18
Andrea Campanella20c17052018-01-30 16:19:35 +010019import com.google.common.collect.ImmutableList;
Andrea Campanella6a614fa2018-02-21 14:28:20 +010020import org.apache.commons.lang3.tuple.Pair;
Andrea Campanellae4084402017-12-15 15:27:31 +010021import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.DeviceId;
Andrea Campanella6a614fa2018-02-21 14:28:20 +010023import org.onosproject.net.Host;
Andrea Campanellae4084402017-12-15 15:27:31 +010024import org.onosproject.net.flow.FlowEntry;
25import org.onosproject.net.flow.TrafficSelector;
26
27import java.util.ArrayList;
28import java.util.HashMap;
29import java.util.List;
30import java.util.Map;
Andrea Campanella6a614fa2018-02-21 14:28:20 +010031import java.util.Optional;
Andrea Campanellae4084402017-12-15 15:27:31 +010032
Simon Hunt026a2872017-11-13 17:09:43 -080033/**
34 * Encapsulates the result of tracing a packet (traffic selector) through
35 * the current topology.
36 */
37public class StaticPacketTrace {
38
Andrea Campanellae4084402017-12-15 15:27:31 +010039 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 Campanella6a614fa2018-02-21 14:28:20 +010045 private Pair<Host, Host> hosts;
Andrea Campanella0cc6acd2018-02-28 16:43:16 +010046 private boolean success = false;
Andrea Campanellae4084402017-12-15 15:27:31 +010047
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 Campanella6a614fa2018-02-21 14:28:20 +010061 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 Campanellae4084402017-12-15 15:27:31 +010079 }
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 Campanella20c17052018-01-30 16:19:35 +0100178 return flowsForDevice.getOrDefault(deviceId, ImmutableList.of());
Andrea Campanellae4084402017-12-15 15:27:31 +0100179 }
180
Andrea Campanella6a614fa2018-02-21 14:28:20 +0100181 /**
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 Campanella0cc6acd2018-02-28 16:43:16 +0100199 /**
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 Campanellae4084402017-12-15 15:27:31 +0100216 @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 Hunt026a2872017-11-13 17:09:43 -0800227}