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