blob: ee7389ae1eac390a33579b8a804a8f154b23a045 [file] [log] [blame]
Brian O'Connorb876bf12014-10-02 14:59:37 -07001package org.onlab.onos.net.intent;
2
Thomas Vachuska1fb982f2014-10-22 14:09:17 -07003import com.google.common.collect.ImmutableList;
4
5import java.util.List;
6
7import static org.onlab.onos.net.intent.IntentOperation.Type.REPLACE;
8import static org.onlab.onos.net.intent.IntentOperation.Type.SUBMIT;
9import static org.onlab.onos.net.intent.IntentOperation.Type.WITHDRAW;
10
Brian O'Connorb876bf12014-10-02 14:59:37 -070011/**
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070012 * Batch of intent submit/withdraw/replace operations.
Brian O'Connorb876bf12014-10-02 14:59:37 -070013 */
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070014public final class IntentOperations {
Brian O'Connorb876bf12014-10-02 14:59:37 -070015
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070016 private final List<IntentOperation> operations;
Brian O'Connorb876bf12014-10-02 14:59:37 -070017
Thomas Vachuska1fb982f2014-10-22 14:09:17 -070018 /**
19 * Creates a batch of intent operations using the supplied list.
20 *
21 * @param operations list of intent operations
22 */
23 private IntentOperations(List<IntentOperation> operations) {
24 this.operations = operations;
25 }
26
27 /**
28 * List of operations that need to be executed as a unit.
29 *
30 * @return list of intent operations
31 */
32 public List<IntentOperation> operations() {
33 return operations;
34 }
35
36 /**
37 * Returns a builder for intent operation batches.
38 *
39 * @return intent operations builder
40 */
41 public static Builder builder() {
42 return new Builder();
43 }
44
45 /**
46 * Builder for batches of intent operations.
47 */
48 public static final class Builder {
49
50 ImmutableList.Builder<IntentOperation> builder = ImmutableList.builder();
51
52 // Public construction is forbidden.
53 private Builder() {
54 }
55
56 /**
57 * Adds an intent submit operation.
58 *
59 * @param intent intent to be submitted
60 * @return self
61 */
62 public Builder addSubmitOperation(Intent intent) {
63 builder.add(new IntentOperation(SUBMIT, intent.id(), intent));
64 return this;
65 }
66
67 /**
68 * Adds an intent submit operation.
69 *
70 * @param oldIntentId intent to be replaced
71 * @param newIntent replacement intent
72 * @return self
73 */
74 public Builder addReplaceOperation(IntentId oldIntentId, Intent newIntent) {
75 builder.add(new IntentOperation(REPLACE, oldIntentId, newIntent));
76 return this;
77 }
78
79 /**
80 * Adds an intent submit operation.
81 *
82 * @param intentId identifier of the intent to be withdrawn
83 * @return self
84 */
85 public Builder addWithdrawOperation(IntentId intentId) {
86 builder.add(new IntentOperation(WITHDRAW, intentId, null));
87 return this;
88 }
89
90 /**
91 * Builds a batch of intent operations.
92 *
93 * @return immutable batch of intent operations
94 */
95 public IntentOperations build() {
96 return new IntentOperations(builder.build());
97 }
98
99 }
Brian O'Connorb876bf12014-10-02 14:59:37 -0700100}