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; |
| 20 | |
| 21 | import java.util.List; |
| 22 | |
| 23 | /** |
| 24 | * Represents the output of the pipeline traceable processing. |
| 25 | */ |
| 26 | public final class PipelineTraceableOutput { |
| 27 | |
| 28 | /** |
| 29 | * Represents the result of the pipeline traceable processing. |
| 30 | */ |
| 31 | public enum PipelineTraceableResult { |
| 32 | /** |
| 33 | * Means packet went through the pipeline. |
| 34 | */ |
| 35 | SUCCESS, |
| 36 | /** |
| 37 | * Means packet stopped due to missing flows. |
| 38 | */ |
| 39 | NO_FLOWS, |
| 40 | /** |
| 41 | * Means packet stopped due to missing groups. |
| 42 | */ |
| 43 | NO_GROUPS, |
| 44 | /** |
| 45 | * Means packet stopped due to an empty group. |
| 46 | */ |
| 47 | NO_GROUP_MEMBERS, |
| 48 | /** |
| 49 | * Means packet is dropped by the pipeline. |
| 50 | */ |
| 51 | DROPPED |
| 52 | } |
| 53 | |
| 54 | private String log; |
| 55 | private List<PipelineTraceableHitChain> hitChains; |
| 56 | private PipelineTraceableResult result; |
| 57 | |
| 58 | /** |
| 59 | * Creates a new pipeline traceable output with the specified input. |
| 60 | * |
| 61 | * @param log the trace log |
| 62 | * @param hitChains the hit chains |
| 63 | * @param result the apply result |
| 64 | */ |
| 65 | private PipelineTraceableOutput(String log, List<PipelineTraceableHitChain> hitChains, |
| 66 | PipelineTraceableResult result) { |
| 67 | this.log = log; |
| 68 | this.hitChains = hitChains; |
| 69 | this.result = result; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the log message as string. |
| 74 | * |
| 75 | * @return the log message |
| 76 | */ |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 77 | public String log() { |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 78 | return log; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the hit chains. |
| 83 | * |
| 84 | * @return the pipeline hit chains |
| 85 | */ |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 86 | public List<PipelineTraceableHitChain> hitChains() { |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 87 | return hitChains; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns the result of the computation. |
| 92 | * |
| 93 | * @return the pipeline traceable result |
| 94 | */ |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 95 | public PipelineTraceableResult result() { |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 96 | return result; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns a new builder. |
| 101 | * |
| 102 | * @return an empty builder |
| 103 | */ |
| 104 | public static PipelineTraceableOutput.Builder builder() { |
| 105 | return new PipelineTraceableOutput.Builder(); |
| 106 | } |
| 107 | |
| 108 | /** |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 109 | * Returns a new builder initialized with the traceable output. |
| 110 | * |
| 111 | * @param pipelineTraceableOutput the output used for the initialization |
| 112 | * @return an initialized builder |
| 113 | */ |
| 114 | public static PipelineTraceableOutput.Builder builder(PipelineTraceableOutput pipelineTraceableOutput) { |
| 115 | return new PipelineTraceableOutput.Builder(pipelineTraceableOutput); |
| 116 | } |
| 117 | |
| 118 | /** |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 119 | * Builder of pipeline traceable entities. |
| 120 | */ |
| 121 | public static final class Builder { |
| 122 | |
| 123 | private StringBuilder log = new StringBuilder(); |
| 124 | private List<PipelineTraceableHitChain> hitChains = Lists.newArrayList(); |
| 125 | private PipelineTraceableResult result = PipelineTraceableResult.SUCCESS; |
| 126 | |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 127 | private Builder() { |
| 128 | } |
| 129 | |
| 130 | private Builder(PipelineTraceableOutput traceableOutput) { |
| 131 | appendToLog("\n" + traceableOutput.log()); |
| 132 | setResult(traceableOutput.result()); |
| 133 | traceableOutput.hitChains().forEach(this::addHitChain); |
| 134 | } |
| 135 | |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 136 | /** |
| 137 | * Appends a message to the log. |
| 138 | * |
| 139 | * @param message the log message to be appended |
| 140 | * @return this builder |
| 141 | */ |
| 142 | public Builder appendToLog(String message) { |
| 143 | if (log.length() != 0) { |
| 144 | log.append("\n"); |
| 145 | } |
| 146 | log.append(message); |
| 147 | return this; |
| 148 | } |
| 149 | |
pierventre | 5a936f5 | 2020-10-08 17:21:49 +0200 | [diff] [blame] | 150 | public Builder setResult(PipelineTraceableResult result) { |
pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 151 | // Do not override original failure |
| 152 | if (this.result == PipelineTraceableResult.SUCCESS) { |
| 153 | this.result = result; |
| 154 | } |
| 155 | return this; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Sets no flows in the result. |
| 160 | * |
| 161 | * @return this builder |
| 162 | */ |
| 163 | public Builder noFlows() { |
| 164 | return setResult(PipelineTraceableResult.NO_FLOWS); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Sets no groups in the result. |
| 169 | * |
| 170 | * @return this builder |
| 171 | */ |
| 172 | public Builder noGroups() { |
| 173 | return setResult(PipelineTraceableResult.NO_GROUPS); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Sets no flows in the result. |
| 178 | * |
| 179 | * @return this builder |
| 180 | */ |
| 181 | public Builder noMembers() { |
| 182 | return setResult(PipelineTraceableResult.NO_GROUP_MEMBERS); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Sets dropped in the result. |
| 187 | * |
| 188 | * @return this builder |
| 189 | */ |
| 190 | public Builder dropped() { |
| 191 | return setResult(PipelineTraceableResult.DROPPED); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Stores the provided hit chain. |
| 196 | * |
| 197 | * @param hitChain the provided hit chain |
| 198 | * @return this builder |
| 199 | */ |
| 200 | public Builder addHitChain(PipelineTraceableHitChain hitChain) { |
| 201 | if (!hitChains.contains(hitChain)) { |
| 202 | hitChains.add(hitChain); |
| 203 | } |
| 204 | return this; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Builds a new pipeline traceable output. |
| 209 | * |
| 210 | * @return a pipeline traceable object |
| 211 | */ |
| 212 | public PipelineTraceableOutput build() { |
| 213 | return new PipelineTraceableOutput(log.toString(), hitChains, result); |
| 214 | } |
| 215 | |
| 216 | } |
| 217 | |
| 218 | } |