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 | */ |
| 77 | public String getLog() { |
| 78 | return log; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Returns the hit chains. |
| 83 | * |
| 84 | * @return the pipeline hit chains |
| 85 | */ |
| 86 | public List<PipelineTraceableHitChain> getHitChains() { |
| 87 | return hitChains; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns the result of the computation. |
| 92 | * |
| 93 | * @return the pipeline traceable result |
| 94 | */ |
| 95 | public PipelineTraceableResult getResult() { |
| 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 | /** |
| 109 | * Builder of pipeline traceable entities. |
| 110 | */ |
| 111 | public static final class Builder { |
| 112 | |
| 113 | private StringBuilder log = new StringBuilder(); |
| 114 | private List<PipelineTraceableHitChain> hitChains = Lists.newArrayList(); |
| 115 | private PipelineTraceableResult result = PipelineTraceableResult.SUCCESS; |
| 116 | |
| 117 | /** |
| 118 | * Appends a message to the log. |
| 119 | * |
| 120 | * @param message the log message to be appended |
| 121 | * @return this builder |
| 122 | */ |
| 123 | public Builder appendToLog(String message) { |
| 124 | if (log.length() != 0) { |
| 125 | log.append("\n"); |
| 126 | } |
| 127 | log.append(message); |
| 128 | return this; |
| 129 | } |
| 130 | |
| 131 | private Builder setResult(PipelineTraceableResult result) { |
| 132 | // Do not override original failure |
| 133 | if (this.result == PipelineTraceableResult.SUCCESS) { |
| 134 | this.result = result; |
| 135 | } |
| 136 | return this; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Sets no flows in the result. |
| 141 | * |
| 142 | * @return this builder |
| 143 | */ |
| 144 | public Builder noFlows() { |
| 145 | return setResult(PipelineTraceableResult.NO_FLOWS); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Sets no groups in the result. |
| 150 | * |
| 151 | * @return this builder |
| 152 | */ |
| 153 | public Builder noGroups() { |
| 154 | return setResult(PipelineTraceableResult.NO_GROUPS); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Sets no flows in the result. |
| 159 | * |
| 160 | * @return this builder |
| 161 | */ |
| 162 | public Builder noMembers() { |
| 163 | return setResult(PipelineTraceableResult.NO_GROUP_MEMBERS); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Sets dropped in the result. |
| 168 | * |
| 169 | * @return this builder |
| 170 | */ |
| 171 | public Builder dropped() { |
| 172 | return setResult(PipelineTraceableResult.DROPPED); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Stores the provided hit chain. |
| 177 | * |
| 178 | * @param hitChain the provided hit chain |
| 179 | * @return this builder |
| 180 | */ |
| 181 | public Builder addHitChain(PipelineTraceableHitChain hitChain) { |
| 182 | if (!hitChains.contains(hitChain)) { |
| 183 | hitChains.add(hitChain); |
| 184 | } |
| 185 | return this; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Builds a new pipeline traceable output. |
| 190 | * |
| 191 | * @return a pipeline traceable object |
| 192 | */ |
| 193 | public PipelineTraceableOutput build() { |
| 194 | return new PipelineTraceableOutput(log.toString(), hitChains, result); |
| 195 | } |
| 196 | |
| 197 | } |
| 198 | |
| 199 | } |