Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 19 | package org.onlab.onos.net.intent; |
| 20 | |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
| 22 | |
| 23 | import java.util.List; |
| 24 | |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkNotNull; |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 26 | import static org.onlab.onos.net.intent.IntentOperation.Type.REPLACE; |
| 27 | import static org.onlab.onos.net.intent.IntentOperation.Type.SUBMIT; |
| 28 | import static org.onlab.onos.net.intent.IntentOperation.Type.WITHDRAW; |
| 29 | |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 30 | /** |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 31 | * Batch of intent submit/withdraw/replace operations. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 32 | */ |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 33 | public final class IntentOperations { |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 34 | |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 35 | private final List<IntentOperation> operations; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 36 | |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Creates a batch of intent operations using the supplied list. |
| 39 | * |
| 40 | * @param operations list of intent operations |
| 41 | */ |
| 42 | private IntentOperations(List<IntentOperation> operations) { |
| 43 | this.operations = operations; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * List of operations that need to be executed as a unit. |
| 48 | * |
| 49 | * @return list of intent operations |
| 50 | */ |
| 51 | public List<IntentOperation> operations() { |
| 52 | return operations; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns a builder for intent operation batches. |
| 57 | * |
| 58 | * @return intent operations builder |
| 59 | */ |
| 60 | public static Builder builder() { |
| 61 | return new Builder(); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Builder for batches of intent operations. |
| 66 | */ |
| 67 | public static final class Builder { |
| 68 | |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 69 | private final ImmutableList.Builder<IntentOperation> builder = ImmutableList.builder(); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 70 | |
| 71 | // Public construction is forbidden. |
| 72 | private Builder() { |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Adds an intent submit operation. |
| 77 | * |
| 78 | * @param intent intent to be submitted |
| 79 | * @return self |
| 80 | */ |
| 81 | public Builder addSubmitOperation(Intent intent) { |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 82 | checkNotNull(intent, "Intent cannot be null"); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 83 | builder.add(new IntentOperation(SUBMIT, intent.id(), intent)); |
| 84 | return this; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Adds an intent submit operation. |
| 89 | * |
| 90 | * @param oldIntentId intent to be replaced |
| 91 | * @param newIntent replacement intent |
| 92 | * @return self |
| 93 | */ |
| 94 | public Builder addReplaceOperation(IntentId oldIntentId, Intent newIntent) { |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 95 | checkNotNull(oldIntentId, "Intent ID cannot be null"); |
| 96 | checkNotNull(newIntent, "Intent cannot be null"); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 97 | builder.add(new IntentOperation(REPLACE, oldIntentId, newIntent)); |
| 98 | return this; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Adds an intent submit operation. |
| 103 | * |
| 104 | * @param intentId identifier of the intent to be withdrawn |
| 105 | * @return self |
| 106 | */ |
| 107 | public Builder addWithdrawOperation(IntentId intentId) { |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 108 | checkNotNull(intentId, "Intent ID cannot be null"); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 109 | builder.add(new IntentOperation(WITHDRAW, intentId, null)); |
| 110 | return this; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Builds a batch of intent operations. |
| 115 | * |
| 116 | * @return immutable batch of intent operations |
| 117 | */ |
| 118 | public IntentOperations build() { |
| 119 | return new IntentOperations(builder.build()); |
| 120 | } |
| 121 | |
| 122 | } |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 123 | } |