pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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.net; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 20 | |
| 21 | import java.util.List; |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | /** |
| 25 | * Class to represent the pipeline hit chain and the result of the pipeline processing. |
| 26 | */ |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 27 | public final class PipelineTraceableHitChain { |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 28 | |
| 29 | private ConnectPoint outputPort; |
| 30 | private List<DataPlaneEntity> hitChain; |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 31 | private PipelineTraceablePacket egressPacket; |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 32 | // By default packets are dropped |
| 33 | private boolean dropped = true; |
| 34 | |
| 35 | private PipelineTraceableHitChain() { |
| 36 | hitChain = Lists.newArrayList(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Creates a new PipelineTraceableHitChain. |
| 41 | * |
| 42 | * @param output the output connect point |
| 43 | * @param hits the hits in the pipeline (flows, groups and other abstractions) |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 44 | * @param packet the traceable packet representing the final packet |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 45 | */ |
| 46 | public PipelineTraceableHitChain(ConnectPoint output, List<DataPlaneEntity> hits, |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 47 | PipelineTraceablePacket packet) { |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 48 | this.outputPort = output; |
| 49 | this.hitChain = hits; |
| 50 | this.egressPacket = packet; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Creates an empty pipeline hit chain. |
| 55 | * |
| 56 | * @return an empty pipeline hit chain |
| 57 | */ |
| 58 | public static PipelineTraceableHitChain emptyHitChain() { |
| 59 | return new PipelineTraceableHitChain(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns the output connect point. |
| 64 | * |
| 65 | * @return the connect point |
| 66 | */ |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 67 | public ConnectPoint outputPort() { |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 68 | return outputPort; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Sets the output port. |
| 73 | * |
| 74 | * @param outputPort the output port |
| 75 | */ |
| 76 | public void setOutputPort(ConnectPoint outputPort) { |
| 77 | this.outputPort = outputPort; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns the hit chain. |
| 82 | * |
| 83 | * @return flows and groups that matched. |
| 84 | */ |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 85 | public List<DataPlaneEntity> hitChain() { |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 86 | return hitChain; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Adds the provided dataplane entity to the end of the chain. |
| 91 | * |
| 92 | * @param dataPlaneEntity the dataplane entity |
| 93 | */ |
| 94 | public void addDataPlaneEntity(DataPlaneEntity dataPlaneEntity) { |
| 95 | if (!hitChain.contains(dataPlaneEntity)) { |
| 96 | hitChain.add(dataPlaneEntity); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Removes the provided dataplane entity from the chain. |
| 102 | * |
| 103 | * @param dataPlaneEntity the dataplane entity |
| 104 | */ |
| 105 | public void removeDataPlaneEntity(DataPlaneEntity dataPlaneEntity) { |
| 106 | if (hitChain.isEmpty()) { |
| 107 | return; |
| 108 | } |
| 109 | hitChain.remove(dataPlaneEntity); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns the egress packet after traversing the pipeline. |
| 114 | * |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 115 | * @return the traceable packet representing the packet infos |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 116 | */ |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 117 | public PipelineTraceablePacket egressPacket() { |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 118 | return egressPacket; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Sets the egress packet. |
| 123 | * |
| 124 | * @param egressPacket the egress packet |
| 125 | */ |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 126 | public void setEgressPacket(PipelineTraceablePacket egressPacket) { |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 127 | this.egressPacket = egressPacket; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Return whether or not the packet has been dropped by the pipeline. |
| 132 | * |
| 133 | * @return true if the packet has been dropped. False, otherwise. |
| 134 | */ |
| 135 | public boolean isDropped() { |
| 136 | return dropped; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Set the dropped flag. |
| 141 | */ |
| 142 | public void dropped() { |
| 143 | this.dropped = true; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Unset the dropped flag. |
| 148 | */ |
| 149 | public void pass() { |
| 150 | this.dropped = false; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public int hashCode() { |
| 155 | return Objects.hash(outputPort, hitChain, egressPacket, dropped); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public boolean equals(Object obj) { |
| 160 | if (this == obj) { |
| 161 | return true; |
| 162 | } |
| 163 | if (obj instanceof PipelineTraceableHitChain) { |
| 164 | PipelineTraceableHitChain that = (PipelineTraceableHitChain) obj; |
| 165 | return Objects.equals(this.outputPort, that.outputPort) && |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 166 | Objects.equals(this.hitChain, that.hitChain) && |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 167 | Objects.equals(this.egressPacket, that.egressPacket) && |
| 168 | Objects.equals(this.dropped, that.dropped); |
| 169 | } |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public String toString() { |
| 175 | return "PipelineTraceableHitChain{" + |
| 176 | "outputPort=" + outputPort + |
| 177 | ", hitChain=" + hitChain + |
| 178 | ", egressPacket=" + egressPacket + |
| 179 | ", dropped=" + dropped + |
| 180 | '}'; |
| 181 | } |
| 182 | } |