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 | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
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 | import java.util.List; |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 19 | import java.util.Objects; |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 20 | |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.core.ApplicationId; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 23 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 24 | import static com.google.common.base.MoreObjects.toStringHelper; |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 25 | import static com.google.common.base.Preconditions.checkNotNull; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import static org.onosproject.net.intent.IntentOperation.Type.REPLACE; |
| 27 | import static org.onosproject.net.intent.IntentOperation.Type.SUBMIT; |
| 28 | import static org.onosproject.net.intent.IntentOperation.Type.UPDATE; |
| 29 | import static org.onosproject.net.intent.IntentOperation.Type.WITHDRAW; |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 30 | |
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 | * Batch of intent submit/withdraw/replace operations. |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 33 | */ |
Brian O'Connor | ea4d7d1 | 2015-01-28 16:37:46 -0800 | [diff] [blame] | 34 | @Deprecated |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 35 | public final class IntentOperations { |
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 | private final List<IntentOperation> operations; |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 38 | private final ApplicationId appId; |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 39 | |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Creates a batch of intent operations using the supplied list. |
| 42 | * |
| 43 | * @param operations list of intent operations |
| 44 | */ |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 45 | private IntentOperations(List<IntentOperation> operations, ApplicationId appId) { |
Sho SHIMIZU | e63e51e | 2014-12-03 10:54:38 -0800 | [diff] [blame] | 46 | checkNotNull(operations); |
| 47 | checkNotNull(appId); |
Sho SHIMIZU | 351e2f6 | 2014-12-03 14:20:06 -0800 | [diff] [blame] | 48 | // TODO: consider check whether operations are not empty because empty batch is meaningless |
| 49 | // but it affects the existing code to add this checking |
Sho SHIMIZU | e63e51e | 2014-12-03 10:54:38 -0800 | [diff] [blame] | 50 | |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 51 | this.operations = operations; |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 52 | this.appId = appId; |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /** |
| 56 | * List of operations that need to be executed as a unit. |
| 57 | * |
| 58 | * @return list of intent operations |
| 59 | */ |
| 60 | public List<IntentOperation> operations() { |
| 61 | return operations; |
| 62 | } |
| 63 | |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 64 | public ApplicationId appId() { |
| 65 | return appId; |
| 66 | } |
| 67 | |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Returns a builder for intent operation batches. |
| 70 | * |
| 71 | * @return intent operations builder |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 72 | * @param applicationId application id |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 73 | */ |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 74 | public static Builder builder(ApplicationId applicationId) { |
| 75 | return new Builder(applicationId); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 78 | @Override |
| 79 | public int hashCode() { |
| 80 | return Objects.hash(operations); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean equals(Object obj) { |
| 85 | if (this == obj) { |
| 86 | return true; |
| 87 | } |
| 88 | if (obj == null || getClass() != obj.getClass()) { |
| 89 | return false; |
| 90 | } |
| 91 | final IntentOperations other = (IntentOperations) obj; |
| 92 | return Objects.equals(this.operations, other.operations); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public String toString() { |
| 97 | return toStringHelper(this) |
| 98 | .add("operations", operations) |
| 99 | .toString(); |
| 100 | } |
| 101 | |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 102 | /** |
| 103 | * Builder for batches of intent operations. |
| 104 | */ |
| 105 | public static final class Builder { |
| 106 | |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 107 | private final ImmutableList.Builder<IntentOperation> builder = ImmutableList.builder(); |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 108 | private final ApplicationId appId; |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 109 | |
| 110 | // Public construction is forbidden. |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 111 | private Builder(ApplicationId appId) { |
| 112 | this.appId = appId; |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Adds an intent submit operation. |
| 117 | * |
| 118 | * @param intent intent to be submitted |
| 119 | * @return self |
| 120 | */ |
| 121 | public Builder addSubmitOperation(Intent intent) { |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 122 | checkNotNull(intent, "Intent cannot be null"); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 123 | builder.add(new IntentOperation(SUBMIT, intent.id(), intent)); |
| 124 | return this; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Adds an intent submit operation. |
| 129 | * |
| 130 | * @param oldIntentId intent to be replaced |
| 131 | * @param newIntent replacement intent |
| 132 | * @return self |
| 133 | */ |
| 134 | public Builder addReplaceOperation(IntentId oldIntentId, Intent newIntent) { |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 135 | checkNotNull(oldIntentId, "Intent ID cannot be null"); |
| 136 | checkNotNull(newIntent, "Intent cannot be null"); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 137 | builder.add(new IntentOperation(REPLACE, oldIntentId, newIntent)); |
| 138 | return this; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Adds an intent submit operation. |
| 143 | * |
| 144 | * @param intentId identifier of the intent to be withdrawn |
| 145 | * @return self |
| 146 | */ |
| 147 | public Builder addWithdrawOperation(IntentId intentId) { |
Thomas Vachuska | 83e090e | 2014-10-22 14:25:35 -0700 | [diff] [blame] | 148 | checkNotNull(intentId, "Intent ID cannot be null"); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 149 | builder.add(new IntentOperation(WITHDRAW, intentId, null)); |
| 150 | return this; |
| 151 | } |
| 152 | |
| 153 | /** |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 154 | * Adds an intent update operation. |
| 155 | * |
| 156 | * @param intentId identifier of the intent to be updated |
| 157 | * @return self |
| 158 | */ |
| 159 | public Builder addUpdateOperation(IntentId intentId) { |
| 160 | checkNotNull(intentId, "Intent ID cannot be null"); |
| 161 | builder.add(new IntentOperation(UPDATE, intentId, null)); |
| 162 | return this; |
| 163 | } |
| 164 | |
| 165 | /** |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 166 | * Builds a batch of intent operations. |
| 167 | * |
| 168 | * @return immutable batch of intent operations |
| 169 | */ |
| 170 | public IntentOperations build() { |
Brian O'Connor | 72a034c | 2014-11-26 18:24:23 -0800 | [diff] [blame] | 171 | return new IntentOperations(builder.build(), appId); |
Thomas Vachuska | 1fb982f | 2014-10-22 14:09:17 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | } |
Brian O'Connor | b876bf1 | 2014-10-02 14:59:37 -0700 | [diff] [blame] | 175 | } |