Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | * |
| 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 |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.p4runtime.api; |
| 18 | |
| 19 | import org.onosproject.net.pi.model.PiActionProfileId; |
| 20 | import org.onosproject.net.pi.model.PiCounterId; |
| 21 | import org.onosproject.net.pi.model.PiMeterId; |
| 22 | import org.onosproject.net.pi.model.PiPipeconf; |
| 23 | import org.onosproject.net.pi.model.PiTableId; |
| 24 | import org.onosproject.net.pi.runtime.PiEntity; |
| 25 | import org.onosproject.net.pi.runtime.PiHandle; |
| 26 | |
| 27 | import java.util.Collection; |
| 28 | import java.util.concurrent.CompletableFuture; |
| 29 | |
| 30 | /** |
| 31 | * P4Runtime client interface for the Read RPC that allows reading multiple |
| 32 | * entities with one request. |
| 33 | */ |
| 34 | public interface P4RuntimeReadClient { |
| 35 | |
| 36 | /** |
| 37 | * Returns a new {@link ReadRequest} instance that can bed used to build a |
| 38 | * batched read request, for the given pipeconf. |
| 39 | * |
| 40 | * @param pipeconf pipeconf |
| 41 | * @return new read request |
| 42 | */ |
| 43 | ReadRequest read(PiPipeconf pipeconf); |
| 44 | |
| 45 | /** |
| 46 | * Abstraction of a P4Runtime read request that follows the builder pattern. |
| 47 | * Multiple entities can be added to the same request before submitting it. |
| 48 | */ |
| 49 | interface ReadRequest { |
| 50 | |
| 51 | /** |
| 52 | * Requests to read one entity identified by the given handle. |
| 53 | * |
| 54 | * @param handle handle |
| 55 | * @return this |
| 56 | */ |
| 57 | ReadRequest handle(PiHandle handle); |
| 58 | |
| 59 | /** |
| 60 | * Requests to read multiple entities identified by the given handles. |
| 61 | * |
| 62 | * @param handles iterable of handles |
| 63 | * @return this |
| 64 | */ |
| 65 | ReadRequest handles(Iterable<? extends PiHandle> handles); |
| 66 | |
| 67 | /** |
| 68 | * Requests to read all table entries from the given table ID. |
| 69 | * |
| 70 | * @param tableId table ID |
| 71 | * @return this |
| 72 | */ |
| 73 | ReadRequest tableEntries(PiTableId tableId); |
| 74 | |
| 75 | /** |
| 76 | * Requests to read all table entries from the given table IDs. |
| 77 | * |
| 78 | * @param tableIds table IDs |
| 79 | * @return this |
| 80 | */ |
| 81 | ReadRequest tableEntries(Iterable<PiTableId> tableIds); |
| 82 | |
| 83 | /** |
| 84 | * Requests to read the default table entry from the given table. |
| 85 | * |
| 86 | * @param tableId table ID |
| 87 | * @return this |
| 88 | */ |
| 89 | ReadRequest defaultTableEntry(PiTableId tableId); |
| 90 | |
| 91 | /** |
| 92 | * Requests to read the default table entry from the given tables. |
| 93 | * |
| 94 | * @param tableIds table IDs |
| 95 | * @return this |
| 96 | */ |
| 97 | ReadRequest defaultTableEntry(Iterable<PiTableId> tableIds); |
| 98 | |
| 99 | /** |
| 100 | * Requests to read all action profile groups from the given action |
| 101 | * profile. |
| 102 | * |
| 103 | * @param actionProfileId action profile ID |
| 104 | * @return this |
| 105 | */ |
| 106 | ReadRequest actionProfileGroups(PiActionProfileId actionProfileId); |
| 107 | |
| 108 | /** |
| 109 | * Requests to read all action profile groups from the given action |
| 110 | * profiles. |
| 111 | * |
| 112 | * @param actionProfileIds action profile IDs |
| 113 | * @return this |
| 114 | */ |
| 115 | ReadRequest actionProfileGroups(Iterable<PiActionProfileId> actionProfileIds); |
| 116 | |
| 117 | /** |
| 118 | * Requests to read all action profile members from the given action |
| 119 | * profile. |
| 120 | * |
| 121 | * @param actionProfileId action profile ID |
| 122 | * @return this |
| 123 | */ |
| 124 | ReadRequest actionProfileMembers(PiActionProfileId actionProfileId); |
| 125 | |
| 126 | /** |
| 127 | * Requests to read all action profile members from the given action |
| 128 | * profiles. |
| 129 | * |
| 130 | * @param actionProfileIds action profile IDs |
| 131 | * @return this |
| 132 | */ |
| 133 | ReadRequest actionProfileMembers(Iterable<PiActionProfileId> actionProfileIds); |
| 134 | |
| 135 | /** |
| 136 | * Requests to read all counter cells from the given counter. |
| 137 | * |
| 138 | * @param counterId counter ID |
| 139 | * @return this |
| 140 | */ |
| 141 | ReadRequest counterCells(PiCounterId counterId); |
| 142 | |
| 143 | /** |
| 144 | * Requests to read all counter cells from the given counters. |
| 145 | * |
| 146 | * @param counterIds counter IDs |
| 147 | * @return this |
| 148 | */ |
| 149 | ReadRequest counterCells(Iterable<PiCounterId> counterIds); |
| 150 | |
| 151 | /** |
| 152 | * Requests to read all direct counter cells from the given table. |
| 153 | * |
| 154 | * @param tableId table ID |
| 155 | * @return this |
| 156 | */ |
| 157 | ReadRequest directCounterCells(PiTableId tableId); |
| 158 | |
| 159 | /** |
| 160 | * Requests to read all direct counter cells from the given tables. |
| 161 | * |
| 162 | * @param tableIds table IDs |
| 163 | * @return this |
| 164 | */ |
| 165 | ReadRequest directCounterCells(Iterable<PiTableId> tableIds); |
| 166 | |
| 167 | /** |
| 168 | * Requests to read all meter cell configs from the given meter ID. |
| 169 | * |
| 170 | * @param meterId meter ID |
| 171 | * @return this |
| 172 | */ |
| 173 | ReadRequest meterCells(PiMeterId meterId); |
| 174 | |
| 175 | /** |
| 176 | * Requests to read all meter cell configs from the given meter IDs. |
| 177 | * |
| 178 | * @param meterIds meter IDs |
| 179 | * @return this |
| 180 | */ |
| 181 | ReadRequest meterCells(Iterable<PiMeterId> meterIds); |
| 182 | |
| 183 | /** |
| 184 | * Requests to read all direct meter cell configs from the given table. |
| 185 | * |
| 186 | * @param tableId table ID |
| 187 | * @return this |
| 188 | */ |
| 189 | ReadRequest directMeterCells(PiTableId tableId); |
| 190 | |
| 191 | /** |
| 192 | * Requests to read all direct meter cell configs from the given |
| 193 | * tables. |
| 194 | * |
| 195 | * @param tableIds table IDs |
| 196 | * @return this |
| 197 | */ |
| 198 | ReadRequest directMeterCells(Iterable<PiTableId> tableIds); |
| 199 | |
| 200 | /** |
| 201 | * Submits the read request and returns a read response wrapped in a |
| 202 | * completable future. The future is completed once all entities have |
| 203 | * been received by the P4Runtime client. |
| 204 | * |
| 205 | * @return completable future of a read response |
| 206 | */ |
| 207 | CompletableFuture<ReadResponse> submit(); |
| 208 | |
| 209 | /** |
| 210 | * Similar to {@link #submit()}, but blocks until the operation is |
| 211 | * completed, after which, it returns a read response. |
| 212 | * |
| 213 | * @return read response |
| 214 | */ |
| 215 | ReadResponse submitSync(); |
| 216 | |
| 217 | //TODO: implement per-entity asynchronous reads. This would allow a user |
| 218 | // of this client to process read entities as they arrive, instead of |
| 219 | // waiting for the client to receive them all. Java 9 Reactive Streams |
| 220 | // seems a good way of doing it. |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Response to a P4Runtime read request. |
| 225 | */ |
| 226 | interface ReadResponse { |
| 227 | |
| 228 | /** |
| 229 | * Returns true if the request was successful with no errors, otherwise |
| 230 | * returns false. In case of errors, further details can be obtained |
| 231 | * with {@link #explanation()} and {@link #throwable()}. |
| 232 | * |
| 233 | * @return true if the request was successful with no errors, false |
| 234 | * otherwise |
| 235 | */ |
| 236 | boolean isSuccess(); |
| 237 | |
| 238 | /** |
| 239 | * Returns a collection of all PI entities returned by the server. |
| 240 | * |
| 241 | * @return collection of all PI entities returned by the server |
| 242 | */ |
| 243 | Collection<PiEntity> all(); |
| 244 | |
| 245 | /** |
| 246 | * Returns a collection of all PI entities of a given class returned by |
| 247 | * the server. |
| 248 | * |
| 249 | * @param clazz PI entity class |
| 250 | * @param <E> PI entity class |
| 251 | * @return collection of all PI entities returned by the server for the |
| 252 | * given PI entity class |
| 253 | */ |
| 254 | <E extends PiEntity> Collection<E> all(Class<E> clazz); |
| 255 | |
| 256 | /** |
| 257 | * If the read request was not successful, this method returns a message |
| 258 | * explaining the error occurred. Returns an empty string if such |
| 259 | * message is not available, or {@code null} if no errors occurred. |
| 260 | * |
| 261 | * @return error explanation or empty string or null |
| 262 | */ |
| 263 | String explanation(); |
| 264 | |
| 265 | /** |
| 266 | * If the read request was not successful, this method returns the |
| 267 | * internal throwable instance associated with the error (e.g. a {@link |
| 268 | * io.grpc.StatusRuntimeException} instance). Returns null if such |
| 269 | * throwable instance is not available or if no errors occurred. |
| 270 | * |
| 271 | * @return throwable instance |
| 272 | */ |
| 273 | Throwable throwable(); |
| 274 | } |
| 275 | } |