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