blob: 498cc056be8015329e067e8ed4412a3cdb884619 [file] [log] [blame]
Brian O'Connor72cb19a2015-01-16 16:14:41 -08001/*
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 */
16package org.onosproject.net.flow;
17
18import com.google.common.base.MoreObjects;
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.ImmutableSet;
21import com.google.common.collect.Lists;
22
23import java.util.List;
24import java.util.Set;
25
26import 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 */
32public 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 /**
107 * Appends a flow rule modify to the current stage.
108 *
109 * @param flowRule flow rule
110 * @return this
111 */
112 public Builder modify(FlowRule flowRule) {
113 currentStage.add(new FlowRuleOperation(flowRule, MODIFY));
114 return this;
115 }
116
117 /**
118 * Appends a flow rule remove to the current stage.
119 *
120 * @param flowRule flow rule
121 * @return this
122 */
123 // FIXME this is confusing, consider renaming
124 public Builder remove(FlowRule flowRule) {
125 currentStage.add(new FlowRuleOperation(flowRule, REMOVE));
126 return this;
127 }
128
129 /**
130 * Closes the current stage.
131 */
132 private void closeStage() {
133 ImmutableSet<FlowRuleOperation> stage = currentStage.build();
134 if (!stage.isEmpty()) {
135 listBuilder.add(stage);
136 }
137 }
138
139 /**
140 * Closes the current stage and starts a new one.
141 *
142 * @return this
143 */
144 public Builder newStage() {
145 closeStage();
146 currentStage = ImmutableSet.builder();
147 return this;
148 }
149
150 /**
151 * Builds the immutable flow rule operations.
152 *
153 * @return flow rule operations
154 */
155 public FlowRuleOperations build() {
156 return build(null);
157 }
158
159 /**
160 * Builds the immutable flow rule operations.
161 *
162 * @param cb the callback to call when this operation completes
163 * @return flow rule operations
164 */
165 public FlowRuleOperations build(FlowRuleOperationsContext cb) {
166 closeStage();
167 return new FlowRuleOperations(listBuilder.build(), cb);
168 }
169 }
170}