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 | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 16 | package org.onlab.onos.net.intent; |
| 17 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame^] | 18 | import static com.google.common.base.Preconditions.checkArgument; |
| 19 | import static com.google.common.base.Preconditions.checkNotNull; |
| 20 | |
| 21 | import org.onlab.onos.net.intent.IntentStore.BatchWrite.Operation; |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 22 | import org.onlab.onos.store.Store; |
| 23 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame^] | 24 | import com.google.common.base.MoreObjects; |
| 25 | import com.google.common.collect.ImmutableList; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.Collections; |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 29 | import java.util.List; |
| 30 | |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Manages inventory of end-station intents; not intended for direct use. |
| 33 | */ |
| 34 | public interface IntentStore extends Store<IntentEvent, IntentStoreDelegate> { |
| 35 | |
| 36 | /** |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 37 | * Submits a new intent into the store. If the returned event is not |
| 38 | * null, the manager is expected to dispatch the event and then to kick |
| 39 | * off intent compilation process. Otherwise, another node has been elected |
| 40 | * to perform the compilation process and the node will learn about |
| 41 | * the submittal and results of the intent compilation via the delegate |
| 42 | * mechanism. |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 43 | * |
tom | 85258ee | 2014-10-07 00:10:02 -0700 | [diff] [blame] | 44 | * @param intent intent to be submitted |
| 45 | * @return event indicating the intent was submitted or null if no |
| 46 | * change resulted, e.g. duplicate intent |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 47 | */ |
| 48 | IntentEvent createIntent(Intent intent); |
| 49 | |
| 50 | /** |
| 51 | * Removes the specified intent from the inventory. |
| 52 | * |
| 53 | * @param intentId intent identification |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 54 | */ |
Thomas Vachuska | e4b6bb2 | 2014-11-25 17:09:43 -0800 | [diff] [blame] | 55 | void removeIntent(IntentId intentId); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * Returns the number of intents in the store. |
Toshio Koide | d32809b | 2014-10-09 10:58:54 -0700 | [diff] [blame] | 59 | * |
| 60 | * @return the number of intents in the store |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 61 | */ |
| 62 | long getIntentCount(); |
| 63 | |
| 64 | /** |
| 65 | * Returns a collection of all intents in the store. |
| 66 | * |
| 67 | * @return iterable collection of all intents |
| 68 | */ |
| 69 | Iterable<Intent> getIntents(); |
| 70 | |
| 71 | /** |
Toshio Koide | d32809b | 2014-10-09 10:58:54 -0700 | [diff] [blame] | 72 | * Returns the intent with the specified identifier. |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 73 | * |
| 74 | * @param intentId intent identification |
| 75 | * @return intent or null if not found |
| 76 | */ |
| 77 | Intent getIntent(IntentId intentId); |
| 78 | |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 79 | /** |
| 80 | * Returns the state of the specified intent. |
| 81 | * |
| 82 | * @param intentId intent identification |
| 83 | * @return current intent state |
| 84 | */ |
| 85 | IntentState getIntentState(IntentId intentId); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 86 | |
| 87 | /** |
| 88 | * Sets the state of the specified intent to the new state. |
| 89 | * |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 90 | * @param intent intent whose state is to be changed |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 91 | * @param newState new state |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 92 | * @return state transition event |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 93 | */ |
| 94 | IntentEvent setState(Intent intent, IntentState newState); |
| 95 | |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 96 | /** |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 97 | * Sets the installable intents which resulted from compilation of the |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 98 | * specified original intent. |
| 99 | * |
| 100 | * @param intentId original intent identifier |
| 101 | * @param installableIntents compiled installable intents |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 102 | */ |
Yuta HIGUCHI | 10a31c3 | 2014-10-28 14:42:06 -0700 | [diff] [blame] | 103 | void setInstallableIntents(IntentId intentId, List<Intent> installableIntents); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 104 | |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 105 | /** |
| 106 | * Returns the list of the installable events associated with the specified |
| 107 | * original intent. |
| 108 | * |
| 109 | * @param intentId original intent identifier |
| 110 | * @return compiled installable intents |
| 111 | */ |
Thomas Vachuska | c96058a | 2014-10-20 23:00:16 -0700 | [diff] [blame] | 112 | List<Intent> getInstallableIntents(IntentId intentId); |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 113 | |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Removes any installable intents which resulted from compilation of the |
| 116 | * specified original intent. |
| 117 | * |
| 118 | * @param intentId original intent identifier |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 119 | */ |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 120 | void removeInstalledIntents(IntentId intentId); |
tom | a1d16b6 | 2014-10-02 23:45:11 -0700 | [diff] [blame] | 121 | |
Yuta HIGUCHI | a94c6e8 | 2014-11-28 18:49:54 -0800 | [diff] [blame^] | 122 | |
| 123 | /** |
| 124 | * Returns a new empty batch write operation buider. |
| 125 | * |
| 126 | * @return BatchWrite |
| 127 | */ |
| 128 | default BatchWrite newBatchWrite() { |
| 129 | return new BatchWrite(); |
| 130 | } |
| 131 | |
| 132 | // default implementation simply executes them sequentially. |
| 133 | // Store implementation should override and implement actual batch write. |
| 134 | /** |
| 135 | * Execute writes in a batch. |
| 136 | * |
| 137 | * @param batch BatchWrite to execute |
| 138 | * @return failed operations |
| 139 | */ |
| 140 | default List<Operation> batchWrite(BatchWrite batch) { |
| 141 | List<Operation> failed = new ArrayList<>(); |
| 142 | for (Operation op : batch.operations) { |
| 143 | switch (op.type) { |
| 144 | case CREATE_INTENT: |
| 145 | checkArgument(op.args.size() == 1, |
| 146 | "CREATE_INTENT takes 1 argument. %s", op); |
| 147 | Intent intent = (Intent) op.args.get(0); |
| 148 | if (createIntent(intent) == null) { |
| 149 | failed.add(op); |
| 150 | } |
| 151 | break; |
| 152 | |
| 153 | case REMOVE_INTENT: |
| 154 | checkArgument(op.args.size() == 1, |
| 155 | "REMOVE_INTENT takes 1 argument. %s", op); |
| 156 | IntentId intentId = (IntentId) op.args.get(0); |
| 157 | removeIntent(intentId); |
| 158 | break; |
| 159 | |
| 160 | case REMOVE_INSTALLED: |
| 161 | checkArgument(op.args.size() == 1, |
| 162 | "REMOVE_INSTALLED takes 1 argument. %s", op); |
| 163 | intentId = (IntentId) op.args.get(0); |
| 164 | removeInstalledIntents(intentId); |
| 165 | break; |
| 166 | |
| 167 | case SET_INSTALLABLE: |
| 168 | checkArgument(op.args.size() == 2, |
| 169 | "SET_INSTALLABLE takes 2 arguments. %s", op); |
| 170 | intentId = (IntentId) op.args.get(0); |
| 171 | @SuppressWarnings("unchecked") |
| 172 | List<Intent> installableIntents = (List<Intent>) op.args.get(1); |
| 173 | setInstallableIntents(intentId, installableIntents); |
| 174 | break; |
| 175 | |
| 176 | case SET_STATE: |
| 177 | checkArgument(op.args.size() == 2, |
| 178 | "SET_STATE takes 2 arguments. %s", op); |
| 179 | intent = (Intent) op.args.get(0); |
| 180 | IntentState newState = (IntentState) op.args.get(1); |
| 181 | setState(intent, newState); |
| 182 | break; |
| 183 | |
| 184 | default: |
| 185 | break; |
| 186 | } |
| 187 | } |
| 188 | return failed; |
| 189 | } |
| 190 | |
| 191 | public static class BatchWrite { |
| 192 | |
| 193 | public enum OpType { |
| 194 | CREATE_INTENT, |
| 195 | REMOVE_INTENT, |
| 196 | SET_STATE, |
| 197 | SET_INSTALLABLE, |
| 198 | REMOVE_INSTALLED |
| 199 | } |
| 200 | |
| 201 | List<Operation> operations = new ArrayList<>(); |
| 202 | |
| 203 | public List<Operation> operations() { |
| 204 | return Collections.unmodifiableList(operations); |
| 205 | } |
| 206 | |
| 207 | public boolean isEmpty() { |
| 208 | return operations.isEmpty(); |
| 209 | } |
| 210 | |
| 211 | public BatchWrite createIntent(Intent intent) { |
| 212 | operations.add(Operation.of(OpType.CREATE_INTENT, |
| 213 | ImmutableList.of(intent))); |
| 214 | return this; |
| 215 | } |
| 216 | |
| 217 | public BatchWrite removeIntent(IntentId intentId) { |
| 218 | operations.add(Operation.of(OpType.REMOVE_INTENT, |
| 219 | ImmutableList.of(intentId))); |
| 220 | return this; |
| 221 | } |
| 222 | |
| 223 | public BatchWrite setState(Intent intent, IntentState newState) { |
| 224 | operations.add(Operation.of(OpType.SET_STATE, |
| 225 | ImmutableList.of(intent, newState))); |
| 226 | return this; |
| 227 | } |
| 228 | |
| 229 | public BatchWrite setInstallableIntents(IntentId intentId, List<Intent> installableIntents) { |
| 230 | operations.add(Operation.of(OpType.SET_INSTALLABLE, |
| 231 | ImmutableList.of(intentId, installableIntents))); |
| 232 | return this; |
| 233 | } |
| 234 | |
| 235 | public BatchWrite removeInstalledIntents(IntentId intentId) { |
| 236 | operations.add(Operation.of(OpType.REMOVE_INSTALLED, |
| 237 | ImmutableList.of(intentId))); |
| 238 | return this; |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public String toString() { |
| 243 | return MoreObjects.toStringHelper(getClass()) |
| 244 | .add("operations", operations) |
| 245 | .toString(); |
| 246 | } |
| 247 | |
| 248 | public static class Operation { |
| 249 | final OpType type; |
| 250 | final ImmutableList<Object> args; |
| 251 | |
| 252 | public static Operation of(OpType type, List<Object> args) { |
| 253 | return new Operation(type, args); |
| 254 | } |
| 255 | |
| 256 | public Operation(OpType type, List<Object> args) { |
| 257 | this.type = checkNotNull(type); |
| 258 | this.args = ImmutableList.copyOf(args); |
| 259 | } |
| 260 | |
| 261 | public OpType type() { |
| 262 | return type; |
| 263 | } |
| 264 | |
| 265 | public ImmutableList<Object> args() { |
| 266 | return args; |
| 267 | } |
| 268 | |
| 269 | @SuppressWarnings("unchecked") |
| 270 | public <T> T arg(int i) { |
| 271 | return (T) args.get(i); |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public String toString() { |
| 276 | return MoreObjects.toStringHelper(getClass()) |
| 277 | .add("type", type) |
| 278 | .add("args", args) |
| 279 | .toString(); |
| 280 | } |
| 281 | } |
| 282 | } |
Brian O'Connor | 66630c8 | 2014-10-02 21:08:19 -0700 | [diff] [blame] | 283 | } |