Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | package org.onosproject.net.flow; |
| 17 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.Lists; |
| 22 | |
| 23 | import java.util.List; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | import static org.onosproject.net.flow.FlowRuleOperation.Type.*; |
| 27 | |
| 28 | /** |
| 29 | * A batch of flow rule operations that are broken into stages. |
| 30 | * TODO move this up to parent's package |
| 31 | */ |
| 32 | public class FlowRuleOperations { |
| 33 | |
| 34 | private final List<Set<FlowRuleOperation>> stages; |
| 35 | private final FlowRuleOperationsContext callback; // TODO consider Optional |
| 36 | |
| 37 | private FlowRuleOperations(List<Set<FlowRuleOperation>> stages, |
| 38 | FlowRuleOperationsContext cb) { |
| 39 | this.stages = stages; |
| 40 | this.callback = cb; |
| 41 | } |
| 42 | |
| 43 | // kryo-constructor |
| 44 | protected FlowRuleOperations() { |
| 45 | this.stages = Lists.newArrayList(); |
| 46 | this.callback = null; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the flow rule operations as sets of stages that should be |
| 51 | * executed sequentially. |
| 52 | * |
| 53 | * @return flow rule stages |
| 54 | */ |
| 55 | public List<Set<FlowRuleOperation>> stages() { |
| 56 | return stages; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns the callback for this batch of operations. |
| 61 | * |
| 62 | * @return callback |
| 63 | */ |
| 64 | public FlowRuleOperationsContext callback() { |
| 65 | return callback; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns a new builder. |
| 70 | * |
| 71 | * @return new builder |
| 72 | */ |
| 73 | public static Builder builder() { |
| 74 | return new Builder(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public String toString() { |
| 79 | return MoreObjects.toStringHelper(this) |
| 80 | .add("stages", stages) |
| 81 | .toString(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * A builder for constructing flow rule operations. |
| 86 | */ |
| 87 | public static final class Builder { |
| 88 | |
| 89 | private final ImmutableList.Builder<Set<FlowRuleOperation>> listBuilder = ImmutableList.builder(); |
| 90 | private ImmutableSet.Builder<FlowRuleOperation> currentStage = ImmutableSet.builder(); |
| 91 | |
| 92 | // prevent use of the default constructor outside of this file; use the above method |
| 93 | private Builder() {} |
| 94 | |
| 95 | /** |
| 96 | * Appends a flow rule add to the current stage. |
| 97 | * |
| 98 | * @param flowRule flow rule |
| 99 | * @return this |
| 100 | */ |
| 101 | public Builder add(FlowRule flowRule) { |
| 102 | currentStage.add(new FlowRuleOperation(flowRule, ADD)); |
| 103 | return this; |
| 104 | } |
| 105 | |
| 106 | /** |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 107 | * Appends an existing flow rule to the current stage. |
| 108 | * |
| 109 | * @param flowRuleOperation flow rule operation |
| 110 | * @return this |
| 111 | */ |
| 112 | public Builder operation(FlowRuleOperation flowRuleOperation) { |
| 113 | currentStage.add(flowRuleOperation); |
| 114 | return this; |
| 115 | } |
| 116 | |
| 117 | /** |
Brian O'Connor | 72cb19a | 2015-01-16 16:14:41 -0800 | [diff] [blame] | 118 | * Appends a flow rule modify to the current stage. |
| 119 | * |
| 120 | * @param flowRule flow rule |
| 121 | * @return this |
| 122 | */ |
| 123 | public Builder modify(FlowRule flowRule) { |
| 124 | currentStage.add(new FlowRuleOperation(flowRule, MODIFY)); |
| 125 | return this; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Appends a flow rule remove to the current stage. |
| 130 | * |
| 131 | * @param flowRule flow rule |
| 132 | * @return this |
| 133 | */ |
| 134 | // FIXME this is confusing, consider renaming |
| 135 | public Builder remove(FlowRule flowRule) { |
| 136 | currentStage.add(new FlowRuleOperation(flowRule, REMOVE)); |
| 137 | return this; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Closes the current stage. |
| 142 | */ |
| 143 | private void closeStage() { |
| 144 | ImmutableSet<FlowRuleOperation> stage = currentStage.build(); |
| 145 | if (!stage.isEmpty()) { |
| 146 | listBuilder.add(stage); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Closes the current stage and starts a new one. |
| 152 | * |
| 153 | * @return this |
| 154 | */ |
| 155 | public Builder newStage() { |
| 156 | closeStage(); |
| 157 | currentStage = ImmutableSet.builder(); |
| 158 | return this; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Builds the immutable flow rule operations. |
| 163 | * |
| 164 | * @return flow rule operations |
| 165 | */ |
| 166 | public FlowRuleOperations build() { |
| 167 | return build(null); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Builds the immutable flow rule operations. |
| 172 | * |
| 173 | * @param cb the callback to call when this operation completes |
| 174 | * @return flow rule operations |
| 175 | */ |
| 176 | public FlowRuleOperations build(FlowRuleOperationsContext cb) { |
| 177 | closeStage(); |
| 178 | return new FlowRuleOperations(listBuilder.build(), cb); |
| 179 | } |
| 180 | } |
| 181 | } |