Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent; |
| 17 | |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | |
| 20 | import java.util.List; |
| 21 | |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 22 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 23 | import static org.onlab.onos.net.intent.IntentOperation.Type.REPLACE; |
| 24 | import static org.onlab.onos.net.intent.IntentOperation.Type.SUBMIT; |
| 25 | import static org.onlab.onos.net.intent.IntentOperation.Type.WITHDRAW; |
| 26 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 27 | /** |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 28 | * Batch of intent submit/withdraw/replace operations. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 29 | */ |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 30 | public final class IntentOperations { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 31 | |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 32 | private final List<IntentOperation> operations; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 33 | |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Creates a batch of intent operations using the supplied list. |
| 36 | * |
| 37 | * @param operations list of intent operations |
| 38 | */ |
| 39 | private IntentOperations(List<IntentOperation> operations) { |
| 40 | this.operations = operations; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * List of operations that need to be executed as a unit. |
| 45 | * |
| 46 | * @return list of intent operations |
| 47 | */ |
| 48 | public List<IntentOperation> operations() { |
| 49 | return operations; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Returns a builder for intent operation batches. |
| 54 | * |
| 55 | * @return intent operations builder |
| 56 | */ |
| 57 | public static Builder builder() { |
| 58 | return new Builder(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Builder for batches of intent operations. |
| 63 | */ |
| 64 | public static final class Builder { |
| 65 | |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 66 | private final ImmutableList.Builder<IntentOperation> builder = ImmutableList.builder(); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 67 | |
| 68 | // Public construction is forbidden. |
| 69 | private Builder() { |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Adds an intent submit operation. |
| 74 | * |
| 75 | * @param intent intent to be submitted |
| 76 | * @return self |
| 77 | */ |
| 78 | public Builder addSubmitOperation(Intent intent) { |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 79 | checkNotNull(intent, "Intent cannot be null"); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 80 | builder.add(new IntentOperation(SUBMIT, intent.id(), intent)); |
| 81 | return this; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Adds an intent submit operation. |
| 86 | * |
| 87 | * @param oldIntentId intent to be replaced |
| 88 | * @param newIntent replacement intent |
| 89 | * @return self |
| 90 | */ |
| 91 | public Builder addReplaceOperation(IntentId oldIntentId, Intent newIntent) { |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 92 | checkNotNull(oldIntentId, "Intent ID cannot be null"); |
| 93 | checkNotNull(newIntent, "Intent cannot be null"); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 94 | builder.add(new IntentOperation(REPLACE, oldIntentId, newIntent)); |
| 95 | return this; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Adds an intent submit operation. |
| 100 | * |
| 101 | * @param intentId identifier of the intent to be withdrawn |
| 102 | * @return self |
| 103 | */ |
| 104 | public Builder addWithdrawOperation(IntentId intentId) { |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 105 | checkNotNull(intentId, "Intent ID cannot be null"); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 106 | builder.add(new IntentOperation(WITHDRAW, intentId, null)); |
| 107 | return this; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Builds a batch of intent operations. |
| 112 | * |
| 113 | * @return immutable batch of intent operations |
| 114 | */ |
| 115 | public IntentOperations build() { |
| 116 | return new IntentOperations(builder.build()); |
| 117 | } |
| 118 | |
| 119 | } |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 120 | } |