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