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