blob: 8dc70d83dbb81338fba604230183bfb9aa4dfc18 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connor66630c82014-10-02 21:08:19 -070016package org.onlab.onos.net.intent;
17
Yuta HIGUCHIa94c6e82014-11-28 18:49:54 -080018import static com.google.common.base.Preconditions.checkArgument;
19import static com.google.common.base.Preconditions.checkNotNull;
20
21import org.onlab.onos.net.intent.IntentStore.BatchWrite.Operation;
Brian O'Connor66630c82014-10-02 21:08:19 -070022import org.onlab.onos.store.Store;
23
Yuta HIGUCHIa94c6e82014-11-28 18:49:54 -080024import com.google.common.base.MoreObjects;
25import com.google.common.collect.ImmutableList;
26
27import java.util.ArrayList;
28import java.util.Collections;
toma1d16b62014-10-02 23:45:11 -070029import java.util.List;
30
Brian O'Connor66630c82014-10-02 21:08:19 -070031/**
32 * Manages inventory of end-station intents; not intended for direct use.
33 */
34public interface IntentStore extends Store<IntentEvent, IntentStoreDelegate> {
35
36 /**
tom85258ee2014-10-07 00:10:02 -070037 * 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'Connor66630c82014-10-02 21:08:19 -070043 *
tom85258ee2014-10-07 00:10:02 -070044 * @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'Connor66630c82014-10-02 21:08:19 -070047 */
48 IntentEvent createIntent(Intent intent);
49
50 /**
51 * Removes the specified intent from the inventory.
52 *
53 * @param intentId intent identification
Brian O'Connor66630c82014-10-02 21:08:19 -070054 */
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -080055 void removeIntent(IntentId intentId);
Brian O'Connor66630c82014-10-02 21:08:19 -070056
57 /**
58 * Returns the number of intents in the store.
Toshio Koided32809b2014-10-09 10:58:54 -070059 *
60 * @return the number of intents in the store
Brian O'Connor66630c82014-10-02 21:08:19 -070061 */
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 Koided32809b2014-10-09 10:58:54 -070072 * Returns the intent with the specified identifier.
Brian O'Connor66630c82014-10-02 21:08:19 -070073 *
74 * @param intentId intent identification
75 * @return intent or null if not found
76 */
77 Intent getIntent(IntentId intentId);
78
toma1d16b62014-10-02 23:45:11 -070079 /**
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'Connor66630c82014-10-02 21:08:19 -070086
87 /**
88 * Sets the state of the specified intent to the new state.
89 *
toma1d16b62014-10-02 23:45:11 -070090 * @param intent intent whose state is to be changed
Brian O'Connor66630c82014-10-02 21:08:19 -070091 * @param newState new state
toma1d16b62014-10-02 23:45:11 -070092 * @return state transition event
Brian O'Connor66630c82014-10-02 21:08:19 -070093 */
94 IntentEvent setState(Intent intent, IntentState newState);
95
toma1d16b62014-10-02 23:45:11 -070096 /**
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -070097 * Sets the installable intents which resulted from compilation of the
toma1d16b62014-10-02 23:45:11 -070098 * specified original intent.
99 *
100 * @param intentId original intent identifier
101 * @param installableIntents compiled installable intents
toma1d16b62014-10-02 23:45:11 -0700102 */
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700103 void setInstallableIntents(IntentId intentId, List<Intent> installableIntents);
Brian O'Connor66630c82014-10-02 21:08:19 -0700104
toma1d16b62014-10-02 23:45:11 -0700105 /**
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 Vachuskac96058a2014-10-20 23:00:16 -0700112 List<Intent> getInstallableIntents(IntentId intentId);
Brian O'Connor66630c82014-10-02 21:08:19 -0700113
toma1d16b62014-10-02 23:45:11 -0700114 /**
115 * Removes any installable intents which resulted from compilation of the
116 * specified original intent.
117 *
118 * @param intentId original intent identifier
toma1d16b62014-10-02 23:45:11 -0700119 */
Brian O'Connor66630c82014-10-02 21:08:19 -0700120 void removeInstalledIntents(IntentId intentId);
toma1d16b62014-10-02 23:45:11 -0700121
Yuta HIGUCHIa94c6e82014-11-28 18:49:54 -0800122
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'Connor66630c82014-10-02 21:08:19 -0700283}