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.ctl.client; |
| 18 | |
| 19 | import com.google.common.util.concurrent.Futures; |
| 20 | import com.google.protobuf.TextFormat; |
Carmelo Cascone | 75a9a89 | 2019-04-22 12:12:23 -0700 | [diff] [blame] | 21 | import io.grpc.Status; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 22 | import io.grpc.stub.StreamObserver; |
| 23 | import org.onosproject.net.pi.model.PiPipeconf; |
| 24 | import org.onosproject.net.pi.runtime.PiEntity; |
| 25 | import org.onosproject.net.pi.runtime.PiHandle; |
| 26 | import org.onosproject.p4runtime.api.P4RuntimeWriteClient; |
| 27 | import org.onosproject.p4runtime.ctl.codec.CodecException; |
| 28 | import org.slf4j.Logger; |
| 29 | import p4.v1.P4RuntimeOuterClass; |
| 30 | |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 31 | import java.util.Collection; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 32 | import java.util.concurrent.CompletableFuture; |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 33 | import java.util.concurrent.atomic.AtomicBoolean; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 34 | |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 36 | import static com.google.common.base.Preconditions.checkState; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 37 | import static java.lang.String.format; |
| 38 | import static java.util.concurrent.CompletableFuture.completedFuture; |
Carmelo Cascone | 75a9a89 | 2019-04-22 12:12:23 -0700 | [diff] [blame] | 39 | import static org.onosproject.p4runtime.api.P4RuntimeWriteClient.EntityUpdateStatus.PENDING; |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 40 | import static org.onosproject.p4runtime.ctl.client.P4RuntimeClientImpl.SHORT_TIMEOUT_SECONDS; |
| 41 | import static org.onosproject.p4runtime.ctl.codec.Codecs.CODECS; |
| 42 | import static org.slf4j.LoggerFactory.getLogger; |
| 43 | |
| 44 | /** |
| 45 | * Handles the creation of P4Runtime WriteRequest and execution of the Write RPC |
| 46 | * on the server. |
| 47 | */ |
| 48 | final class WriteRequestImpl implements P4RuntimeWriteClient.WriteRequest { |
| 49 | |
| 50 | private static final Logger log = getLogger(WriteRequestImpl.class); |
| 51 | |
| 52 | private static final P4RuntimeOuterClass.WriteResponse P4RT_DEFAULT_WRITE_RESPONSE_MSG = |
| 53 | P4RuntimeOuterClass.WriteResponse.getDefaultInstance(); |
| 54 | |
| 55 | private final P4RuntimeClientImpl client; |
| 56 | private final PiPipeconf pipeconf; |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 57 | private final AtomicBoolean submitted = new AtomicBoolean(false); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 58 | // The P4Runtime WriteRequest protobuf message we need to populate. |
| 59 | private final P4RuntimeOuterClass.WriteRequest.Builder requestMsg; |
| 60 | // WriteResponse instance builder. We populate entity responses as we add new |
| 61 | // entities to this request. The status of each entity response will be |
| 62 | // set once we receive a response from the device. |
| 63 | private final WriteResponseImpl.Builder responseBuilder; |
| 64 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 65 | WriteRequestImpl(P4RuntimeClientImpl client, long p4DeviceId, PiPipeconf pipeconf) { |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 66 | this.client = checkNotNull(client); |
| 67 | this.pipeconf = checkNotNull(pipeconf); |
| 68 | this.requestMsg = P4RuntimeOuterClass.WriteRequest.newBuilder() |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 69 | .setDeviceId(p4DeviceId); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 70 | this.responseBuilder = WriteResponseImpl.builder(client.deviceId()); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public P4RuntimeWriteClient.WriteRequest withAtomicity( |
| 75 | P4RuntimeWriteClient.Atomicity atomicity) { |
| 76 | checkNotNull(atomicity); |
| 77 | switch (atomicity) { |
| 78 | case CONTINUE_ON_ERROR: |
| 79 | requestMsg.setAtomicity( |
| 80 | P4RuntimeOuterClass.WriteRequest.Atomicity.CONTINUE_ON_ERROR); |
| 81 | break; |
| 82 | case ROLLBACK_ON_ERROR: |
| 83 | case DATAPLANE_ATOMIC: |
| 84 | // Supporting this while allowing codec exceptions to be |
| 85 | // reported as write responses can be tricky. Assuming write on |
| 86 | // device succeed but we have a codec exception and |
| 87 | // atomicity is rollback on error. |
| 88 | default: |
| 89 | throw new UnsupportedOperationException(format( |
| 90 | "Atomicity mode %s not supported", atomicity)); |
| 91 | } |
| 92 | return this; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public P4RuntimeWriteClient.WriteRequest insert(PiEntity entity) { |
| 97 | return entity(entity, P4RuntimeWriteClient.UpdateType.INSERT); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public P4RuntimeWriteClient.WriteRequest insert( |
| 102 | Iterable<? extends PiEntity> entities) { |
| 103 | return entities(entities, P4RuntimeWriteClient.UpdateType.INSERT); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public P4RuntimeWriteClient.WriteRequest modify(PiEntity entity) { |
| 108 | return entity(entity, P4RuntimeWriteClient.UpdateType.MODIFY); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public P4RuntimeWriteClient.WriteRequest modify( |
| 113 | Iterable<? extends PiEntity> entities) { |
| 114 | return entities(entities, P4RuntimeWriteClient.UpdateType.MODIFY); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public P4RuntimeWriteClient.WriteRequest delete( |
| 119 | Iterable<? extends PiHandle> handles) { |
| 120 | checkNotNull(handles); |
| 121 | handles.forEach(this::delete); |
| 122 | return this; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public P4RuntimeWriteClient.WriteRequest entities( |
| 127 | Iterable<? extends PiEntity> entities, |
| 128 | P4RuntimeWriteClient.UpdateType updateType) { |
| 129 | checkNotNull(entities); |
| 130 | entities.forEach(e -> this.entity(e, updateType)); |
| 131 | return this; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public P4RuntimeWriteClient.WriteRequest entity( |
| 136 | PiEntity entity, P4RuntimeWriteClient.UpdateType updateType) { |
| 137 | checkNotNull(entity); |
| 138 | checkNotNull(updateType); |
| 139 | appendToRequestMsg(updateType, entity, entity.handle(client.deviceId())); |
| 140 | return this; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public P4RuntimeWriteClient.WriteRequest delete(PiHandle handle) { |
| 145 | checkNotNull(handle); |
| 146 | appendToRequestMsg(P4RuntimeWriteClient.UpdateType.DELETE, null, handle); |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public P4RuntimeWriteClient.WriteResponse submitSync() { |
| 152 | return Futures.getUnchecked(submit()); |
| 153 | } |
| 154 | |
| 155 | @Override |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 156 | public Collection<P4RuntimeWriteClient.EntityUpdateRequest> pendingUpdates() { |
| 157 | return responseBuilder.pendingUpdates(); |
| 158 | } |
| 159 | |
| 160 | @Override |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 161 | public CompletableFuture<P4RuntimeWriteClient.WriteResponse> submit() { |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 162 | checkState(!submitted.getAndSet(true), |
| 163 | "Request has already been submitted, cannot submit again"); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 164 | final P4RuntimeOuterClass.WriteRequest writeRequest = requestMsg |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 165 | .setElectionId(client.lastUsedElectionId( |
| 166 | requestMsg.getDeviceId())) |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 167 | .build(); |
| 168 | log.debug("Sending write request to {} with {} updates...", |
| 169 | client.deviceId(), writeRequest.getUpdatesCount()); |
| 170 | if (writeRequest.getUpdatesCount() == 0) { |
| 171 | // No need to ask the server. |
| 172 | return completedFuture(WriteResponseImpl.EMPTY); |
| 173 | } |
| 174 | final CompletableFuture<P4RuntimeWriteClient.WriteResponse> future = |
| 175 | new CompletableFuture<>(); |
| 176 | final StreamObserver<P4RuntimeOuterClass.WriteResponse> observer = |
| 177 | new StreamObserver<P4RuntimeOuterClass.WriteResponse>() { |
| 178 | @Override |
| 179 | public void onNext(P4RuntimeOuterClass.WriteResponse value) { |
| 180 | if (!P4RT_DEFAULT_WRITE_RESPONSE_MSG.equals(value)) { |
| 181 | log.warn("Received invalid WriteResponse message from {}: {}", |
| 182 | client.deviceId(), TextFormat.shortDebugString(value)); |
| 183 | // Leave all entity responses in pending state. |
| 184 | future.complete(responseBuilder.buildAsIs()); |
| 185 | } else { |
| 186 | log.debug("Received write response from {}...", |
| 187 | client.deviceId()); |
| 188 | // All good, all entities written successfully. |
| 189 | future.complete(responseBuilder.setSuccessAllAndBuild()); |
| 190 | } |
| 191 | } |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 192 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 193 | @Override |
| 194 | public void onError(Throwable t) { |
Carmelo Cascone | 75a9a89 | 2019-04-22 12:12:23 -0700 | [diff] [blame] | 195 | final WriteResponseImpl response = responseBuilder |
| 196 | .setErrorsAndBuild(t); |
| 197 | if (Status.fromThrowable(t).getCode() != Status.Code.UNKNOWN |
| 198 | || !response.status(PENDING).isEmpty()) { |
| 199 | // If UNKNOWN and no entities are in PENDING state, |
| 200 | // it means we have processed the response error |
| 201 | // details and a log message will be produced for |
| 202 | // each failed entity. No need to log the top level |
Carmelo Cascone | 2254dcd | 2019-08-20 17:05:27 -0700 | [diff] [blame] | 203 | // SRE. Otherwise, log a generic WRITE error. |
Carmelo Cascone | 75a9a89 | 2019-04-22 12:12:23 -0700 | [diff] [blame] | 204 | client.handleRpcError(t, "WRITE"); |
| 205 | } |
| 206 | future.complete(response); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 207 | } |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 208 | |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 209 | @Override |
| 210 | public void onCompleted() { |
| 211 | // Nothing to do, unary call. |
| 212 | } |
| 213 | }; |
| 214 | client.execRpc(s -> s.write(writeRequest, observer), SHORT_TIMEOUT_SECONDS); |
| 215 | return future; |
| 216 | } |
| 217 | |
| 218 | private void appendToRequestMsg(P4RuntimeWriteClient.UpdateType updateType, |
| 219 | PiEntity piEntity, PiHandle handle) { |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 220 | checkState(!submitted.get(), |
| 221 | "Request has already been submitted, cannot add more entities"); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 222 | final P4RuntimeOuterClass.Update.Type p4UpdateType; |
| 223 | final P4RuntimeOuterClass.Entity entityMsg; |
| 224 | try { |
| 225 | if (updateType.equals(P4RuntimeWriteClient.UpdateType.DELETE)) { |
| 226 | p4UpdateType = P4RuntimeOuterClass.Update.Type.DELETE; |
| 227 | entityMsg = CODECS.handle().encode(handle, null, pipeconf); |
| 228 | } else { |
| 229 | p4UpdateType = updateType == P4RuntimeWriteClient.UpdateType.INSERT |
| 230 | ? P4RuntimeOuterClass.Update.Type.INSERT |
| 231 | : P4RuntimeOuterClass.Update.Type.MODIFY; |
| 232 | entityMsg = CODECS.entity().encode(piEntity, null, pipeconf); |
| 233 | } |
| 234 | final P4RuntimeOuterClass.Update updateMsg = P4RuntimeOuterClass.Update |
| 235 | .newBuilder() |
| 236 | .setEntity(entityMsg) |
| 237 | .setType(p4UpdateType) |
| 238 | .build(); |
| 239 | requestMsg.addUpdates(updateMsg); |
| 240 | responseBuilder.addPendingResponse(handle, piEntity, updateType); |
pierventre | 219dd8f | 2021-04-21 12:57:47 +0200 | [diff] [blame^] | 241 | if (log.isTraceEnabled()) { |
| 242 | log.trace("Adding {} update to write request for {}: {}", updateType, handle.deviceId(), |
| 243 | piEntity == null ? handle : piEntity); |
| 244 | } |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 245 | } catch (CodecException e) { |
| 246 | responseBuilder.addFailedResponse( |
| 247 | handle, piEntity, updateType, e.getMessage(), |
Carmelo Cascone | 6146946 | 2019-03-05 23:59:11 -0800 | [diff] [blame] | 248 | P4RuntimeWriteClient.EntityUpdateStatus.CODEC_ERROR); |
Carmelo Cascone | 4c289b7 | 2019-01-22 15:30:45 -0800 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |