blob: 442a2473f2b1c1941b6606dafbecca5c8ab6dbad [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.checkNotNull;
19
20import org.onlab.onos.net.intent.IntentStore.BatchWrite.Operation;
Brian O'Connor66630c82014-10-02 21:08:19 -070021import org.onlab.onos.store.Store;
22
Yuta HIGUCHIa94c6e82014-11-28 18:49:54 -080023import com.google.common.base.MoreObjects;
24import com.google.common.collect.ImmutableList;
25
26import java.util.ArrayList;
27import java.util.Collections;
toma1d16b62014-10-02 23:45:11 -070028import java.util.List;
29
Brian O'Connor66630c82014-10-02 21:08:19 -070030/**
31 * Manages inventory of end-station intents; not intended for direct use.
32 */
33public interface IntentStore extends Store<IntentEvent, IntentStoreDelegate> {
34
35 /**
tom85258ee2014-10-07 00:10:02 -070036 * 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'Connor66630c82014-10-02 21:08:19 -070042 *
tom85258ee2014-10-07 00:10:02 -070043 * @param intent intent to be submitted
Brian O'Connor66630c82014-10-02 21:08:19 -070044 */
alshabiba9819bf2014-11-30 18:15:52 -080045 @Deprecated
46 void createIntent(Intent intent);
Brian O'Connor66630c82014-10-02 21:08:19 -070047
48 /**
49 * Removes the specified intent from the inventory.
50 *
51 * @param intentId intent identification
Brian O'Connor66630c82014-10-02 21:08:19 -070052 */
alshabiba9819bf2014-11-30 18:15:52 -080053 @Deprecated
Thomas Vachuskae4b6bb22014-11-25 17:09:43 -080054 void removeIntent(IntentId intentId);
Brian O'Connor66630c82014-10-02 21:08:19 -070055
56 /**
57 * Returns the number of intents in the store.
Toshio Koided32809b2014-10-09 10:58:54 -070058 *
59 * @return the number of intents in the store
Brian O'Connor66630c82014-10-02 21:08:19 -070060 */
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 Koided32809b2014-10-09 10:58:54 -070071 * Returns the intent with the specified identifier.
Brian O'Connor66630c82014-10-02 21:08:19 -070072 *
73 * @param intentId intent identification
74 * @return intent or null if not found
75 */
76 Intent getIntent(IntentId intentId);
77
toma1d16b62014-10-02 23:45:11 -070078 /**
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'Connor66630c82014-10-02 21:08:19 -070085
86 /**
87 * Sets the state of the specified intent to the new state.
88 *
toma1d16b62014-10-02 23:45:11 -070089 * @param intent intent whose state is to be changed
Brian O'Connor66630c82014-10-02 21:08:19 -070090 * @param newState new state
91 */
alshabiba9819bf2014-11-30 18:15:52 -080092 void setState(Intent intent, IntentState newState);
Brian O'Connor66630c82014-10-02 21:08:19 -070093
toma1d16b62014-10-02 23:45:11 -070094 /**
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -070095 * Sets the installable intents which resulted from compilation of the
toma1d16b62014-10-02 23:45:11 -070096 * specified original intent.
97 *
98 * @param intentId original intent identifier
99 * @param installableIntents compiled installable intents
toma1d16b62014-10-02 23:45:11 -0700100 */
Yuta HIGUCHI10a31c32014-10-28 14:42:06 -0700101 void setInstallableIntents(IntentId intentId, List<Intent> installableIntents);
Brian O'Connor66630c82014-10-02 21:08:19 -0700102
toma1d16b62014-10-02 23:45:11 -0700103 /**
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 Vachuskac96058a2014-10-20 23:00:16 -0700110 List<Intent> getInstallableIntents(IntentId intentId);
Brian O'Connor66630c82014-10-02 21:08:19 -0700111
toma1d16b62014-10-02 23:45:11 -0700112 /**
113 * Removes any installable intents which resulted from compilation of the
114 * specified original intent.
115 *
116 * @param intentId original intent identifier
toma1d16b62014-10-02 23:45:11 -0700117 */
Brian O'Connor66630c82014-10-02 21:08:19 -0700118 void removeInstalledIntents(IntentId intentId);
toma1d16b62014-10-02 23:45:11 -0700119
Yuta HIGUCHIa94c6e82014-11-28 18:49:54 -0800120
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 HIGUCHIa94c6e82014-11-28 18:49:54 -0800130 /**
131 * Execute writes in a batch.
132 *
133 * @param batch BatchWrite to execute
134 * @return failed operations
135 */
alshabiba9819bf2014-11-30 18:15:52 -0800136 List<Operation> batchWrite(BatchWrite batch);
Yuta HIGUCHIa94c6e82014-11-28 18:49:54 -0800137
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'Connor66630c82014-10-02 21:08:19 -0700230}