Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.intent; |
| 2 | |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame^] | 3 | import com.google.common.collect.ImmutableList; |
| 4 | |
| 5 | import java.util.List; |
| 6 | |
| 7 | import static org.onlab.onos.net.intent.IntentOperation.Type.REPLACE; |
| 8 | import static org.onlab.onos.net.intent.IntentOperation.Type.SUBMIT; |
| 9 | import static org.onlab.onos.net.intent.IntentOperation.Type.WITHDRAW; |
| 10 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 11 | /** |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame^] | 12 | * Batch of intent submit/withdraw/replace operations. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 13 | */ |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame^] | 14 | public final class IntentOperations { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 15 | |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame^] | 16 | private final List<IntentOperation> operations; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame^] | 18 | /** |
| 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'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 100 | } |