blob: e56035d7d567224c196b77323fa0e812fc0dda94 [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 Campanellae4084402017-12-15 15:27:31 +010020import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.DeviceId;
22import org.onosproject.net.flow.FlowEntry;
23import org.onosproject.net.flow.TrafficSelector;
24
25import java.util.ArrayList;
26import java.util.HashMap;
27import java.util.List;
28import java.util.Map;
29
Simon Hunt026a2872017-11-13 17:09:43 -080030/**
31 * Encapsulates the result of tracing a packet (traffic selector) through
32 * the current topology.
33 */
34public class StaticPacketTrace {
35
Andrea Campanellae4084402017-12-15 15:27:31 +010036 private final TrafficSelector inPacket;
37 private final ConnectPoint in;
38 List<List<ConnectPoint>> completePaths;
39 private Map<DeviceId, List<GroupsInDevice>> outputsForDevice;
40 private Map<DeviceId, List<FlowEntry>> flowsForDevice;
41 private StringBuilder resultMessage;
42
43 /**
44 * Builds the trace with a given packet and a connect point.
45 *
46 * @param packet the packet to trace
47 * @param in the initial connect point
48 */
49 public StaticPacketTrace(TrafficSelector packet, ConnectPoint in) {
50 this.inPacket = packet;
51 this.in = in;
52 completePaths = new ArrayList<>();
53 outputsForDevice = new HashMap<>();
54 flowsForDevice = new HashMap<>();
55 resultMessage = new StringBuilder();
56 }
57
58 /**
59 * Return the initial packet.
60 *
61 * @return the initial packet in the form of a selector.
62 */
63 public TrafficSelector getInitialPacket() {
64 return inPacket;
65 }
66
67 /**
68 * Returns the first connect point the packet came in through.
69 *
70 * @return the connect point
71 */
72 public ConnectPoint getInitialConnectPoint() {
73 return in;
74 }
75
76 /**
77 * Add a result message for the Trace.
78 *
79 * @param resultMessage the message
80 */
81 public void addResultMessage(String resultMessage) {
82 if (this.resultMessage.length() != 0) {
83 this.resultMessage.append("\n");
84 }
85 this.resultMessage.append(resultMessage);
86 }
87
88 /**
89 * Return the result message.
90 *
91 * @return the message
92 */
93 public String resultMessage() {
94 return resultMessage.toString();
95 }
96
97 /**
98 * Adds the groups for a given device.
99 *
100 * @param deviceId the device
101 * @param outputPath the groups in device objects
102 */
103 public void addGroupOutputPath(DeviceId deviceId, GroupsInDevice outputPath) {
104 if (!outputsForDevice.containsKey(deviceId)) {
105 outputsForDevice.put(deviceId, new ArrayList<>());
106 }
107 outputsForDevice.get(deviceId).add(outputPath);
108 }
109
110 /**
111 * Returns all the possible group-based outputs for a given device.
112 *
113 * @param deviceId the device
114 * @return the list of Groups for this device.
115 */
116 public List<GroupsInDevice> getGroupOuputs(DeviceId deviceId) {
117 return outputsForDevice.get(deviceId);
118 }
119
120 /**
121 * Adds a complete possible path.
122 *
123 * @param completePath the path
124 */
125 public void addCompletePath(List<ConnectPoint> completePath) {
126 completePaths.add(completePath);
127 }
128
129 /**
130 * Return all the possible path the packet can take through the network.
131 *
132 * @return a list of paths
133 */
134 public List<List<ConnectPoint>> getCompletePaths() {
135 return completePaths;
136 }
137
138 /**
139 * Add the flows traversed by the packet in a given device.
140 *
141 * @param deviceId the device considered
142 * @param flows the flows
143 */
144 public void addFlowsForDevice(DeviceId deviceId, List<FlowEntry> flows) {
145 flowsForDevice.put(deviceId, flows);
146 }
147
148 /**
149 * Returns the flows matched by this trace's packet for a given device.
150 *
151 * @param deviceId the device
152 * @return the flows matched
153 */
154 public List<FlowEntry> getFlowsForDevice(DeviceId deviceId) {
Andrea Campanella20c17052018-01-30 16:19:35 +0100155 return flowsForDevice.getOrDefault(deviceId, ImmutableList.of());
Andrea Campanellae4084402017-12-15 15:27:31 +0100156 }
157
158 @Override
159 public String toString() {
160 return "StaticPacketTrace{" +
161 "inPacket=" + inPacket +
162 ", in=" + in +
163 ", completePaths=" + completePaths +
164 ", outputsForDevice=" + outputsForDevice +
165 ", flowsForDevice=" + flowsForDevice +
166 ", resultMessage=" + resultMessage +
167 '}';
168 }
Simon Hunt026a2872017-11-13 17:09:43 -0800169}