Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 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; |
| 18 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 19 | import com.google.common.collect.HashMultimap; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableMap; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 23 | import com.google.common.collect.Multimap; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 24 | import com.google.protobuf.ByteString; |
Yi Tseng | e67e141 | 2018-01-31 17:35:20 -0800 | [diff] [blame] | 25 | import com.google.protobuf.InvalidProtocolBufferException; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 26 | import io.grpc.Context; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 27 | import io.grpc.ManagedChannel; |
| 28 | import io.grpc.Status; |
| 29 | import io.grpc.StatusRuntimeException; |
| 30 | import io.grpc.stub.StreamObserver; |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 31 | import org.apache.commons.lang3.tuple.ImmutablePair; |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 32 | import org.onlab.osgi.DefaultServiceDirectory; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 33 | import org.onlab.util.Tools; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 34 | import org.onosproject.net.DeviceId; |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 35 | import org.onosproject.net.MastershipRole; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 36 | import org.onosproject.net.pi.model.PiActionProfileId; |
| 37 | import org.onosproject.net.pi.model.PiCounterId; |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 38 | import org.onosproject.net.pi.model.PiMeterId; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 39 | import org.onosproject.net.pi.model.PiPipeconf; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 40 | import org.onosproject.net.pi.model.PiTableId; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 41 | import org.onosproject.net.pi.runtime.PiActionGroup; |
| 42 | import org.onosproject.net.pi.runtime.PiActionGroupMember; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 43 | import org.onosproject.net.pi.runtime.PiCounterCellData; |
| 44 | import org.onosproject.net.pi.runtime.PiCounterCellId; |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 45 | import org.onosproject.net.pi.runtime.PiEntity; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 46 | import org.onosproject.net.pi.runtime.PiMeterCellConfig; |
| 47 | import org.onosproject.net.pi.runtime.PiMeterCellId; |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 48 | import org.onosproject.net.pi.runtime.PiPacketOperation; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 49 | import org.onosproject.net.pi.runtime.PiTableEntry; |
Yi Tseng | e67e141 | 2018-01-31 17:35:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.pi.service.PiPipeconfService; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 51 | import org.onosproject.p4runtime.api.P4RuntimeClient; |
| 52 | import org.onosproject.p4runtime.api.P4RuntimeEvent; |
| 53 | import org.slf4j.Logger; |
| 54 | import p4.P4RuntimeGrpc; |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 55 | import p4.P4RuntimeOuterClass; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 56 | import p4.P4RuntimeOuterClass.ActionProfileGroup; |
| 57 | import p4.P4RuntimeOuterClass.ActionProfileMember; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 58 | import p4.P4RuntimeOuterClass.Entity; |
| 59 | import p4.P4RuntimeOuterClass.ForwardingPipelineConfig; |
| 60 | import p4.P4RuntimeOuterClass.MasterArbitrationUpdate; |
| 61 | import p4.P4RuntimeOuterClass.PacketIn; |
| 62 | import p4.P4RuntimeOuterClass.ReadRequest; |
| 63 | import p4.P4RuntimeOuterClass.ReadResponse; |
| 64 | import p4.P4RuntimeOuterClass.SetForwardingPipelineConfigRequest; |
| 65 | import p4.P4RuntimeOuterClass.StreamMessageRequest; |
| 66 | import p4.P4RuntimeOuterClass.StreamMessageResponse; |
| 67 | import p4.P4RuntimeOuterClass.TableEntry; |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 68 | import p4.P4RuntimeOuterClass.Uint128; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 69 | import p4.P4RuntimeOuterClass.Update; |
| 70 | import p4.P4RuntimeOuterClass.WriteRequest; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 71 | import p4.config.P4InfoOuterClass.P4Info; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 72 | import p4.tmp.P4Config; |
| 73 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 74 | import java.nio.ByteBuffer; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 75 | import java.util.Collection; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 76 | import java.util.Collections; |
| 77 | import java.util.Iterator; |
| 78 | import java.util.List; |
| 79 | import java.util.Map; |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 80 | import java.util.Objects; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 81 | import java.util.Set; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 82 | import java.util.concurrent.CompletableFuture; |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 83 | import java.util.concurrent.ExecutionException; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 84 | import java.util.concurrent.Executor; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 85 | import java.util.concurrent.ExecutorService; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 86 | import java.util.concurrent.Executors; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 87 | import java.util.concurrent.TimeUnit; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 88 | import java.util.concurrent.locks.Lock; |
| 89 | import java.util.concurrent.locks.ReentrantLock; |
| 90 | import java.util.function.Supplier; |
| 91 | import java.util.stream.Collectors; |
| 92 | import java.util.stream.StreamSupport; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 93 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 94 | import static com.google.common.base.Preconditions.checkNotNull; |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 95 | import static java.lang.String.format; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 96 | import static org.onlab.util.Tools.groupedThreads; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 97 | import static org.slf4j.LoggerFactory.getLogger; |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 98 | import static p4.P4RuntimeOuterClass.Entity.EntityCase.ACTION_PROFILE_GROUP; |
| 99 | import static p4.P4RuntimeOuterClass.Entity.EntityCase.ACTION_PROFILE_MEMBER; |
| 100 | import static p4.P4RuntimeOuterClass.Entity.EntityCase.TABLE_ENTRY; |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 101 | import static p4.P4RuntimeOuterClass.PacketOut; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 102 | import static p4.P4RuntimeOuterClass.SetForwardingPipelineConfigRequest.Action.VERIFY_AND_COMMIT; |
| 103 | |
| 104 | /** |
| 105 | * Implementation of a P4Runtime client. |
| 106 | */ |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 107 | public final class P4RuntimeClientImpl implements P4RuntimeClient { |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 108 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 109 | private static final Map<WriteOperationType, Update.Type> UPDATE_TYPES = ImmutableMap.of( |
| 110 | WriteOperationType.UNSPECIFIED, Update.Type.UNSPECIFIED, |
| 111 | WriteOperationType.INSERT, Update.Type.INSERT, |
| 112 | WriteOperationType.MODIFY, Update.Type.MODIFY, |
| 113 | WriteOperationType.DELETE, Update.Type.DELETE |
| 114 | ); |
| 115 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 116 | private final Logger log = getLogger(getClass()); |
| 117 | |
| 118 | private final DeviceId deviceId; |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 119 | private final long p4DeviceId; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 120 | private final P4RuntimeControllerImpl controller; |
| 121 | private final P4RuntimeGrpc.P4RuntimeBlockingStub blockingStub; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 122 | private final Context.CancellableContext cancellableContext; |
| 123 | private final ExecutorService executorService; |
| 124 | private final Executor contextExecutor; |
| 125 | private final Lock writeLock = new ReentrantLock(); |
| 126 | private final StreamObserver<StreamMessageRequest> streamRequestObserver; |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 127 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 128 | private Map<Uint128, CompletableFuture<Boolean>> arbitrationUpdateMap = Maps.newConcurrentMap(); |
| 129 | protected Uint128 p4RuntimeElectionId; |
| 130 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 131 | /** |
| 132 | * Default constructor. |
| 133 | * |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 134 | * @param deviceId the ONOS device id |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 135 | * @param p4DeviceId the P4 device id |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 136 | * @param channel gRPC channel |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 137 | * @param controller runtime client controller |
| 138 | */ |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 139 | P4RuntimeClientImpl(DeviceId deviceId, long p4DeviceId, ManagedChannel channel, |
| 140 | P4RuntimeControllerImpl controller) { |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 141 | this.deviceId = deviceId; |
| 142 | this.p4DeviceId = p4DeviceId; |
| 143 | this.controller = controller; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 144 | this.cancellableContext = Context.current().withCancellation(); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 145 | this.executorService = Executors.newFixedThreadPool(15, groupedThreads( |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 146 | "onos/p4runtime-client-" + deviceId.toString(), |
| 147 | deviceId.toString() + "-%d")); |
| 148 | this.contextExecutor = this.cancellableContext.fixedContextExecutor(executorService); |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 149 | //TODO Investigate deadline or timeout in supplyInContext Method |
| 150 | this.blockingStub = P4RuntimeGrpc.newBlockingStub(channel); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 151 | P4RuntimeGrpc.P4RuntimeStub asyncStub = P4RuntimeGrpc.newStub(channel); |
| 152 | this.streamRequestObserver = asyncStub.streamChannel(new StreamChannelResponseObserver()); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Executes the given task (supplier) in the gRPC context executor of this client, such that if the context is |
| 157 | * cancelled (e.g. client shutdown) the RPC is automatically cancelled. |
| 158 | * <p> |
| 159 | * Important: Tasks submitted in parallel by different threads are forced executed sequentially. |
| 160 | * <p> |
| 161 | */ |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 162 | private <U> CompletableFuture<U> supplyInContext(Supplier<U> supplier, String opDescription) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 163 | return CompletableFuture.supplyAsync(() -> { |
| 164 | // TODO: explore a more relaxed locking strategy. |
| 165 | writeLock.lock(); |
| 166 | try { |
| 167 | return supplier.get(); |
Yi Tseng | e67e141 | 2018-01-31 17:35:20 -0800 | [diff] [blame] | 168 | } catch (StatusRuntimeException ex) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 169 | log.warn("Unable to execute {} on {}: {}", opDescription, deviceId, ex.toString()); |
Yi Tseng | e67e141 | 2018-01-31 17:35:20 -0800 | [diff] [blame] | 170 | throw ex; |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 171 | } catch (Throwable ex) { |
Yi Tseng | e67e141 | 2018-01-31 17:35:20 -0800 | [diff] [blame] | 172 | log.error("Exception in client of {}, executing {}", deviceId, opDescription, ex); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 173 | throw ex; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 174 | } finally { |
| 175 | writeLock.unlock(); |
| 176 | } |
| 177 | }, contextExecutor); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | public CompletableFuture<Boolean> initStreamChannel() { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 182 | return supplyInContext(this::doInitStreamChannel, "initStreamChannel"); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 183 | } |
| 184 | |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 185 | @Override |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 186 | public CompletableFuture<Boolean> setPipelineConfig(PiPipeconf pipeconf, ByteBuffer deviceData) { |
| 187 | return supplyInContext(() -> doSetPipelineConfig(pipeconf, deviceData), "setPipelineConfig"); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Override |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 191 | public CompletableFuture<Boolean> writeTableEntries(Collection<PiTableEntry> piTableEntries, |
| 192 | WriteOperationType opType, PiPipeconf pipeconf) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 193 | return supplyInContext(() -> doWriteTableEntries(piTableEntries, opType, pipeconf), |
| 194 | "writeTableEntries-" + opType.name()); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 198 | public CompletableFuture<Collection<PiTableEntry>> dumpTable(PiTableId piTableId, PiPipeconf pipeconf) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 199 | return supplyInContext(() -> doDumpTable(piTableId, pipeconf), "dumpTable-" + piTableId); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | @Override |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 203 | public CompletableFuture<Boolean> packetOut(PiPacketOperation packet, PiPipeconf pipeconf) { |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 204 | return supplyInContext(() -> doPacketOut(packet, pipeconf), "packetOut"); |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 207 | @Override |
| 208 | public CompletableFuture<Collection<PiCounterCellData>> readCounterCells(Set<PiCounterCellId> cellIds, |
| 209 | PiPipeconf pipeconf) { |
| 210 | return supplyInContext(() -> doReadCounterCells(cellIds, pipeconf), |
| 211 | "readCounterCells-" + cellIds.hashCode()); |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public CompletableFuture<Collection<PiCounterCellData>> readAllCounterCells(Set<PiCounterId> counterIds, |
| 216 | PiPipeconf pipeconf) { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 217 | return supplyInContext(() -> doReadAllCounterCells(counterIds, pipeconf), |
| 218 | "readAllCounterCells-" + counterIds.hashCode()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 219 | } |
| 220 | |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 221 | @Override |
| 222 | public CompletableFuture<Boolean> writeActionGroupMembers(PiActionGroup group, |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 223 | WriteOperationType opType, |
| 224 | PiPipeconf pipeconf) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 225 | return supplyInContext(() -> doWriteActionGroupMembers(group, opType, pipeconf), |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 226 | "writeActionGroupMembers-" + opType.name()); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public CompletableFuture<Boolean> writeActionGroup(PiActionGroup group, |
| 231 | WriteOperationType opType, |
| 232 | PiPipeconf pipeconf) { |
| 233 | return supplyInContext(() -> doWriteActionGroup(group, opType, pipeconf), |
| 234 | "writeActionGroup-" + opType.name()); |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public CompletableFuture<Collection<PiActionGroup>> dumpGroups(PiActionProfileId actionProfileId, |
| 239 | PiPipeconf pipeconf) { |
| 240 | return supplyInContext(() -> doDumpGroups(actionProfileId, pipeconf), |
| 241 | "dumpGroups-" + actionProfileId.id()); |
| 242 | } |
| 243 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 244 | @Override |
| 245 | public CompletableFuture<Boolean> sendMasterArbitrationUpdate() { |
| 246 | return supplyInContext(this::doArbitrationUpdate, "arbitrationUpdate"); |
| 247 | } |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 248 | public CompletableFuture<Boolean> writeMeterCells(Collection<PiMeterCellConfig> cellIds, PiPipeconf pipeconf) { |
| 249 | |
| 250 | return supplyInContext(() -> doWriteMeterCells(cellIds, pipeconf), |
| 251 | "writeMeterCells"); |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public CompletableFuture<Collection<PiMeterCellConfig>> readMeterCells(Set<PiMeterCellId> cellIds, |
| 256 | PiPipeconf pipeconf) { |
| 257 | return supplyInContext(() -> doReadMeterCells(cellIds, pipeconf), |
| 258 | "readMeterCells-" + cellIds.hashCode()); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public CompletableFuture<Collection<PiMeterCellConfig>> readAllMeterCells(Set<PiMeterId> meterIds, |
| 263 | PiPipeconf pipeconf) { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 264 | return supplyInContext(() -> doReadAllMeterCells(meterIds, pipeconf), |
| 265 | "readAllMeterCells-" + meterIds.hashCode()); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 266 | } |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 267 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 268 | /* Blocking method implementations below */ |
| 269 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 270 | private boolean doArbitrationUpdate() { |
| 271 | CompletableFuture<Boolean> result = new CompletableFuture<>(); |
| 272 | // TODO: currently we use 64-bit Long type for election id, should |
| 273 | // we use 128-bit ? |
| 274 | long nextElectId = controller.getNewMasterElectionId(); |
| 275 | Uint128 newElectionId = Uint128.newBuilder() |
| 276 | .setLow(nextElectId) |
| 277 | .build(); |
| 278 | MasterArbitrationUpdate arbitrationUpdate = MasterArbitrationUpdate.newBuilder() |
| 279 | .setDeviceId(p4DeviceId) |
| 280 | .setElectionId(newElectionId) |
| 281 | .build(); |
| 282 | StreamMessageRequest requestMsg = StreamMessageRequest.newBuilder() |
| 283 | .setArbitration(arbitrationUpdate) |
| 284 | .build(); |
| 285 | log.debug("Sending arbitration update to {} with election id {}...", |
| 286 | deviceId, newElectionId); |
| 287 | arbitrationUpdateMap.put(newElectionId, result); |
| 288 | try { |
| 289 | streamRequestObserver.onNext(requestMsg); |
| 290 | return result.get(); |
Yi Tseng | e67e141 | 2018-01-31 17:35:20 -0800 | [diff] [blame] | 291 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 292 | log.error("Unable to perform arbitration update on {}: {}", deviceId, e.getMessage()); |
Yi Tseng | e67e141 | 2018-01-31 17:35:20 -0800 | [diff] [blame] | 293 | arbitrationUpdateMap.remove(newElectionId); |
| 294 | return false; |
| 295 | } catch (InterruptedException | ExecutionException e) { |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 296 | log.warn("Arbitration update failed for {} due to {}", deviceId, e); |
| 297 | arbitrationUpdateMap.remove(newElectionId); |
| 298 | return false; |
| 299 | } |
| 300 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 301 | private boolean doInitStreamChannel() { |
| 302 | // To listen for packets and other events, we need to start the RPC. |
| 303 | // Here we do it by sending a master arbitration update. |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 304 | return doArbitrationUpdate(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 305 | } |
| 306 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 307 | private boolean doSetPipelineConfig(PiPipeconf pipeconf, ByteBuffer deviceData) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 308 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 309 | log.info("Setting pipeline config for {} to {}...", deviceId, pipeconf.id()); |
| 310 | |
| 311 | checkNotNull(deviceData, "deviceData cannot be null"); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 312 | |
| 313 | P4Info p4Info = PipeconfHelper.getP4Info(pipeconf); |
| 314 | if (p4Info == null) { |
| 315 | // Problem logged by PipeconfHelper. |
| 316 | return false; |
| 317 | } |
| 318 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 319 | P4Config.P4DeviceConfig p4DeviceConfigMsg = P4Config.P4DeviceConfig |
| 320 | .newBuilder() |
| 321 | .setExtras(P4Config.P4DeviceConfig.Extras.getDefaultInstance()) |
| 322 | .setReassign(true) |
| 323 | .setDeviceData(ByteString.copyFrom(deviceData)) |
| 324 | .build(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 325 | |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 326 | ForwardingPipelineConfig pipelineConfig = ForwardingPipelineConfig |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 327 | .newBuilder() |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 328 | .setP4Info(p4Info) |
| 329 | .setP4DeviceConfig(p4DeviceConfigMsg.toByteString()) |
| 330 | .build(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 331 | |
| 332 | SetForwardingPipelineConfigRequest request = SetForwardingPipelineConfigRequest |
| 333 | .newBuilder() |
Andrea Campanella | 8bcd586 | 2017-12-11 11:34:45 +0100 | [diff] [blame] | 334 | .setDeviceId(p4DeviceId) |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 335 | .setElectionId(p4RuntimeElectionId) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 336 | .setAction(VERIFY_AND_COMMIT) |
Andrea Campanella | 8bcd586 | 2017-12-11 11:34:45 +0100 | [diff] [blame] | 337 | .setConfig(pipelineConfig) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 338 | .build(); |
| 339 | |
| 340 | try { |
| 341 | this.blockingStub.setForwardingPipelineConfig(request); |
Carmelo Cascone | d61fdb3 | 2017-10-30 10:09:57 -0700 | [diff] [blame] | 342 | return true; |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 343 | } catch (StatusRuntimeException ex) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 344 | log.warn("Unable to set pipeline config on {}: {}", deviceId, ex.getMessage()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 345 | return false; |
| 346 | } |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | private boolean doWriteTableEntries(Collection<PiTableEntry> piTableEntries, WriteOperationType opType, |
| 350 | PiPipeconf pipeconf) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 351 | WriteRequest.Builder writeRequestBuilder = WriteRequest.newBuilder(); |
| 352 | |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 353 | if (piTableEntries.size() == 0) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 354 | return true; |
| 355 | } |
| 356 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 357 | Collection<Update> updateMsgs; |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 358 | try { |
| 359 | updateMsgs = TableEntryEncoder.encode(piTableEntries, pipeconf) |
| 360 | .stream() |
| 361 | .map(tableEntryMsg -> |
| 362 | Update.newBuilder() |
| 363 | .setEntity(Entity.newBuilder() |
| 364 | .setTableEntry(tableEntryMsg) |
| 365 | .build()) |
| 366 | .setType(UPDATE_TYPES.get(opType)) |
| 367 | .build()) |
| 368 | .collect(Collectors.toList()); |
| 369 | } catch (EncodeException e) { |
| 370 | log.error("Unable to encode table entries, aborting {} operation: {}", |
| 371 | opType.name(), e.getMessage()); |
| 372 | return false; |
| 373 | } |
| 374 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 375 | writeRequestBuilder |
| 376 | .setDeviceId(p4DeviceId) |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 377 | .setElectionId(p4RuntimeElectionId) |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 378 | .addAllUpdates(updateMsgs) |
| 379 | .build(); |
| 380 | |
| 381 | try { |
| 382 | blockingStub.write(writeRequestBuilder.build()); |
| 383 | return true; |
| 384 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 385 | logWriteErrors(piTableEntries, e, opType, "table entry"); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 386 | return false; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | private Collection<PiTableEntry> doDumpTable(PiTableId piTableId, PiPipeconf pipeconf) { |
| 391 | |
Carmelo Cascone | 9f00770 | 2017-08-24 13:30:51 +0200 | [diff] [blame] | 392 | log.debug("Dumping table {} from {} (pipeconf {})...", piTableId, deviceId, pipeconf.id()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 393 | |
| 394 | P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
| 395 | int tableId; |
| 396 | try { |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 397 | tableId = browser.tables().getByName(piTableId.id()).getPreamble().getId(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 398 | } catch (P4InfoBrowser.NotFoundException e) { |
| 399 | log.warn("Unable to dump table: {}", e.getMessage()); |
| 400 | return Collections.emptyList(); |
| 401 | } |
| 402 | |
| 403 | ReadRequest requestMsg = ReadRequest.newBuilder() |
| 404 | .setDeviceId(p4DeviceId) |
| 405 | .addEntities(Entity.newBuilder() |
| 406 | .setTableEntry(TableEntry.newBuilder() |
| 407 | .setTableId(tableId) |
| 408 | .build()) |
| 409 | .build()) |
| 410 | .build(); |
| 411 | |
| 412 | Iterator<ReadResponse> responses; |
| 413 | try { |
| 414 | responses = blockingStub.read(requestMsg); |
| 415 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 416 | log.warn("Unable to dump table {} from {}: {}", piTableId, deviceId, e.getMessage()); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 417 | return Collections.emptyList(); |
| 418 | } |
| 419 | |
| 420 | Iterable<ReadResponse> responseIterable = () -> responses; |
| 421 | List<TableEntry> tableEntryMsgs = StreamSupport |
| 422 | .stream(responseIterable.spliterator(), false) |
| 423 | .map(ReadResponse::getEntitiesList) |
| 424 | .flatMap(List::stream) |
| 425 | .filter(entity -> entity.getEntityCase() == TABLE_ENTRY) |
| 426 | .map(Entity::getTableEntry) |
| 427 | .collect(Collectors.toList()); |
| 428 | |
Carmelo Cascone | 9f00770 | 2017-08-24 13:30:51 +0200 | [diff] [blame] | 429 | log.debug("Retrieved {} entries from table {} on {}...", tableEntryMsgs.size(), piTableId, deviceId); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 430 | |
| 431 | return TableEntryEncoder.decode(tableEntryMsgs, pipeconf); |
| 432 | } |
| 433 | |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 434 | private boolean doPacketOut(PiPacketOperation packet, PiPipeconf pipeconf) { |
| 435 | try { |
| 436 | //encode the PiPacketOperation into a PacketOut |
| 437 | PacketOut packetOut = PacketIOCodec.encodePacketOut(packet, pipeconf); |
| 438 | |
| 439 | //Build the request |
| 440 | StreamMessageRequest packetOutRequest = StreamMessageRequest |
| 441 | .newBuilder().setPacket(packetOut).build(); |
| 442 | |
| 443 | //Send the request |
| 444 | streamRequestObserver.onNext(packetOutRequest); |
| 445 | |
| 446 | } catch (P4InfoBrowser.NotFoundException e) { |
| 447 | log.error("Cant find expected metadata in p4Info file. {}", e.getMessage()); |
| 448 | log.debug("Exception", e); |
| 449 | return false; |
| 450 | } |
| 451 | return true; |
| 452 | } |
| 453 | |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 454 | private void doPacketIn(PacketIn packetInMsg) { |
| 455 | |
| 456 | // Retrieve the pipeconf for this client's device. |
| 457 | PiPipeconfService pipeconfService = DefaultServiceDirectory.getService(PiPipeconfService.class); |
| 458 | if (pipeconfService == null) { |
| 459 | throw new IllegalStateException("PiPipeconfService is null. Can't handle packet in."); |
| 460 | } |
| 461 | final PiPipeconf pipeconf; |
| 462 | if (pipeconfService.ofDevice(deviceId).isPresent() && |
| 463 | pipeconfService.getPipeconf(pipeconfService.ofDevice(deviceId).get()).isPresent()) { |
| 464 | pipeconf = pipeconfService.getPipeconf(pipeconfService.ofDevice(deviceId).get()).get(); |
| 465 | } else { |
| 466 | log.warn("Unable to get pipeconf of {}. Can't handle packet in", deviceId); |
| 467 | return; |
| 468 | } |
| 469 | // Decode packet message and post event. |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 470 | PiPacketOperation packetOperation = PacketIOCodec.decodePacketIn(packetInMsg, pipeconf, deviceId); |
Carmelo Cascone | 2cad9ef | 2017-08-01 21:52:07 +0200 | [diff] [blame] | 471 | DefaultPacketIn packetInEventSubject = new DefaultPacketIn(deviceId, packetOperation); |
| 472 | P4RuntimeEvent event = new P4RuntimeEvent(P4RuntimeEvent.Type.PACKET_IN, packetInEventSubject); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 473 | log.debug("Received packet in: {}", event); |
| 474 | controller.postEvent(event); |
| 475 | } |
| 476 | |
| 477 | private void doArbitrationUpdateFromDevice(MasterArbitrationUpdate arbitrationMsg) { |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 478 | log.debug("Received arbitration update from {}: {}", deviceId, arbitrationMsg); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 479 | |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 480 | Uint128 electionId = arbitrationMsg.getElectionId(); |
| 481 | CompletableFuture<Boolean> mastershipFeature = arbitrationUpdateMap.remove(electionId); |
| 482 | |
| 483 | if (mastershipFeature == null) { |
| 484 | log.warn("Can't find completable future of election id {}", electionId); |
| 485 | return; |
| 486 | } |
| 487 | |
| 488 | this.p4RuntimeElectionId = electionId; |
| 489 | int statusCode = arbitrationMsg.getStatus().getCode(); |
| 490 | MastershipRole arbitrationRole; |
| 491 | // arbitration update success |
| 492 | |
| 493 | if (statusCode == Status.OK.getCode().value()) { |
| 494 | mastershipFeature.complete(true); |
| 495 | arbitrationRole = MastershipRole.MASTER; |
| 496 | } else { |
| 497 | mastershipFeature.complete(false); |
| 498 | arbitrationRole = MastershipRole.STANDBY; |
| 499 | } |
| 500 | |
| 501 | DefaultArbitration arbitrationEventSubject = new DefaultArbitration(arbitrationRole, electionId); |
| 502 | P4RuntimeEvent event = new P4RuntimeEvent(P4RuntimeEvent.Type.ARBITRATION, |
| 503 | arbitrationEventSubject); |
| 504 | controller.postEvent(event); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 505 | } |
| 506 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 507 | private Collection<PiCounterCellData> doReadAllCounterCells( |
| 508 | Collection<PiCounterId> counterIds, PiPipeconf pipeconf) { |
| 509 | return doReadCounterEntities( |
| 510 | CounterEntryCodec.readAllCellsEntities(counterIds, pipeconf), |
| 511 | pipeconf); |
| 512 | } |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 513 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 514 | private Collection<PiCounterCellData> doReadCounterCells( |
| 515 | Collection<PiCounterCellId> cellIds, PiPipeconf pipeconf) { |
| 516 | return doReadCounterEntities( |
| 517 | CounterEntryCodec.encodePiCounterCellIds(cellIds, pipeconf), |
| 518 | pipeconf); |
| 519 | } |
| 520 | |
| 521 | private Collection<PiCounterCellData> doReadCounterEntities( |
| 522 | Collection<Entity> counterEntities, PiPipeconf pipeconf) { |
| 523 | |
| 524 | if (counterEntities.size() == 0) { |
| 525 | return Collections.emptyList(); |
| 526 | } |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 527 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 528 | final ReadRequest request = ReadRequest.newBuilder() |
| 529 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 530 | .addAllEntities(counterEntities) |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 531 | .build(); |
| 532 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 533 | final Iterable<ReadResponse> responses; |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 534 | try { |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 535 | responses = () -> blockingStub.read(request); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 536 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 537 | log.warn("Unable to read counter cells from {}: {}", deviceId, e.getMessage()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 538 | return Collections.emptyList(); |
| 539 | } |
| 540 | |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 541 | List<Entity> entities = StreamSupport.stream(responses.spliterator(), false) |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 542 | .map(ReadResponse::getEntitiesList) |
| 543 | .flatMap(List::stream) |
Carmelo Cascone | 7f75be4 | 2017-09-07 14:37:02 +0200 | [diff] [blame] | 544 | .collect(Collectors.toList()); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 545 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 546 | return CounterEntryCodec.decodeCounterEntities(entities, pipeconf); |
Carmelo Cascone | b045ddc | 2017-09-01 01:26:35 +0200 | [diff] [blame] | 547 | } |
| 548 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 549 | private boolean doWriteActionGroupMembers(PiActionGroup group, WriteOperationType opType, PiPipeconf pipeconf) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 550 | final Collection<ActionProfileMember> actionProfileMembers = Lists.newArrayList(); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 551 | |
| 552 | for (PiActionGroupMember member : group.members()) { |
| 553 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 554 | actionProfileMembers.add(ActionProfileMemberEncoder.encode(group, member, pipeconf)); |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 555 | } catch (EncodeException | P4InfoBrowser.NotFoundException e) { |
| 556 | log.warn("Unable to encode group member, aborting {} operation: {} [{}]", |
| 557 | opType.name(), e.getMessage(), member.toString()); |
| 558 | return false; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 559 | } |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 562 | final Collection<Update> updateMsgs = actionProfileMembers.stream() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 563 | .map(actionProfileMember -> |
| 564 | Update.newBuilder() |
| 565 | .setEntity(Entity.newBuilder() |
| 566 | .setActionProfileMember(actionProfileMember) |
| 567 | .build()) |
| 568 | .setType(UPDATE_TYPES.get(opType)) |
| 569 | .build()) |
| 570 | .collect(Collectors.toList()); |
| 571 | |
| 572 | if (updateMsgs.size() == 0) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 573 | // Nothing to update. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 574 | return true; |
| 575 | } |
| 576 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 577 | WriteRequest writeRequestMsg = WriteRequest.newBuilder() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 578 | .setDeviceId(p4DeviceId) |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 579 | .setElectionId(p4RuntimeElectionId) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 580 | .addAllUpdates(updateMsgs) |
| 581 | .build(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 582 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 583 | blockingStub.write(writeRequestMsg); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 584 | return true; |
| 585 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 586 | logWriteErrors(group.members(), e, opType, "group member"); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 587 | return false; |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | private Collection<PiActionGroup> doDumpGroups(PiActionProfileId piActionProfileId, PiPipeconf pipeconf) { |
| 592 | log.debug("Dumping groups from action profile {} from {} (pipeconf {})...", |
| 593 | piActionProfileId.id(), deviceId, pipeconf.id()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 594 | |
| 595 | final P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 596 | if (browser == null) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 597 | log.warn("Unable to get a P4Info browser for pipeconf {}, aborting dump action profile", pipeconf); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 598 | return Collections.emptySet(); |
| 599 | } |
| 600 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 601 | final int actionProfileId; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 602 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 603 | actionProfileId = browser |
| 604 | .actionProfiles() |
Carmelo Cascone | cb0a49c | 2017-10-03 14:32:23 +0200 | [diff] [blame] | 605 | .getByName(piActionProfileId.id()) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 606 | .getPreamble() |
| 607 | .getId(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 608 | } catch (P4InfoBrowser.NotFoundException e) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 609 | log.warn("Unable to dump groups: {}", e.getMessage()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 610 | return Collections.emptySet(); |
| 611 | } |
| 612 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 613 | // Prepare read request to read all groups from the given action profile. |
| 614 | final ReadRequest groupRequestMsg = ReadRequest.newBuilder() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 615 | .setDeviceId(p4DeviceId) |
| 616 | .addEntities(Entity.newBuilder() |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 617 | .setActionProfileGroup( |
| 618 | ActionProfileGroup.newBuilder() |
| 619 | .setActionProfileId(actionProfileId) |
| 620 | .build()) |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 621 | .build()) |
| 622 | .build(); |
| 623 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 624 | // Read groups. |
| 625 | final Iterator<ReadResponse> groupResponses; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 626 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 627 | groupResponses = blockingStub.read(groupRequestMsg); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 628 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 629 | log.warn("Unable to dump action profile {} from {}: {}", piActionProfileId, deviceId, e.getMessage()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 630 | return Collections.emptySet(); |
| 631 | } |
| 632 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 633 | final List<ActionProfileGroup> groupMsgs = Tools.stream(() -> groupResponses) |
| 634 | .map(ReadResponse::getEntitiesList) |
| 635 | .flatMap(List::stream) |
| 636 | .filter(entity -> entity.getEntityCase() == ACTION_PROFILE_GROUP) |
| 637 | .map(Entity::getActionProfileGroup) |
| 638 | .collect(Collectors.toList()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 639 | |
| 640 | log.debug("Retrieved {} groups from action profile {} on {}...", |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 641 | groupMsgs.size(), piActionProfileId.id(), deviceId); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 642 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 643 | // Returned groups contain only a minimal description of their members. |
| 644 | // We need to issue a new request to get the full description of each member. |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 645 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 646 | // Keep a map of all member IDs for each group ID, will need it later. |
| 647 | final Multimap<Integer, Integer> groupIdToMemberIdsMap = HashMultimap.create(); |
| 648 | groupMsgs.forEach(g -> groupIdToMemberIdsMap.putAll( |
| 649 | g.getGroupId(), |
| 650 | g.getMembersList().stream() |
| 651 | .map(ActionProfileGroup.Member::getMemberId) |
| 652 | .collect(Collectors.toList()))); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 653 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 654 | // Prepare one big read request to read all members in one shot. |
| 655 | final Set<Entity> entityMsgs = groupMsgs.stream() |
| 656 | .flatMap(g -> g.getMembersList().stream()) |
| 657 | .map(ActionProfileGroup.Member::getMemberId) |
| 658 | // Prevent issuing many read requests for the same member. |
| 659 | .distinct() |
| 660 | .map(id -> ActionProfileMember.newBuilder() |
| 661 | .setActionProfileId(actionProfileId) |
| 662 | .setMemberId(id) |
| 663 | .build()) |
| 664 | .map(m -> Entity.newBuilder() |
| 665 | .setActionProfileMember(m) |
| 666 | .build()) |
| 667 | .collect(Collectors.toSet()); |
| 668 | final ReadRequest memberRequestMsg = ReadRequest.newBuilder().setDeviceId(p4DeviceId) |
| 669 | .addAllEntities(entityMsgs) |
| 670 | .build(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 671 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 672 | // Read members. |
| 673 | final Iterator<ReadResponse> memberResponses; |
| 674 | try { |
| 675 | memberResponses = blockingStub.read(memberRequestMsg); |
| 676 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 677 | log.warn("Unable to read members of action profile {} from {}: {}", |
| 678 | piActionProfileId, deviceId, e.getMessage()); |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 679 | return Collections.emptyList(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 682 | final Multimap<Integer, ActionProfileMember> groupIdToMembersMap = HashMultimap.create(); |
| 683 | Tools.stream(() -> memberResponses) |
| 684 | .map(ReadResponse::getEntitiesList) |
| 685 | .flatMap(List::stream) |
| 686 | .filter(e -> e.getEntityCase() == ACTION_PROFILE_MEMBER) |
| 687 | .map(Entity::getActionProfileMember) |
| 688 | .forEach(member -> groupIdToMemberIdsMap.asMap() |
| 689 | // Get all group IDs that contain this member. |
| 690 | .entrySet() |
| 691 | .stream() |
| 692 | .filter(entry -> entry.getValue().contains(member.getMemberId())) |
| 693 | .map(Map.Entry::getKey) |
| 694 | .forEach(gid -> groupIdToMembersMap.put(gid, member))); |
| 695 | |
| 696 | log.debug("Retrieved {} group members from action profile {} on {}...", |
| 697 | groupIdToMembersMap.size(), piActionProfileId.id(), deviceId); |
| 698 | |
| 699 | return groupMsgs.stream() |
| 700 | .map(groupMsg -> { |
| 701 | try { |
| 702 | return ActionProfileGroupEncoder.decode(groupMsg, |
| 703 | groupIdToMembersMap.get(groupMsg.getGroupId()), |
| 704 | pipeconf); |
| 705 | } catch (P4InfoBrowser.NotFoundException | EncodeException e) { |
| 706 | log.warn("Unable to decode group: {}\n {}", e.getMessage(), groupMsg); |
| 707 | return null; |
| 708 | } |
| 709 | }) |
| 710 | .filter(Objects::nonNull) |
| 711 | .collect(Collectors.toList()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | private boolean doWriteActionGroup(PiActionGroup group, WriteOperationType opType, PiPipeconf pipeconf) { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 715 | final ActionProfileGroup actionProfileGroup; |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 716 | try { |
| 717 | actionProfileGroup = ActionProfileGroupEncoder.encode(group, pipeconf); |
| 718 | } catch (EncodeException | P4InfoBrowser.NotFoundException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 719 | log.warn("Unable to encode group, aborting {} operation: {}", e.getMessage(), opType.name()); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 720 | return false; |
| 721 | } |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 722 | |
| 723 | final WriteRequest writeRequestMsg = WriteRequest.newBuilder() |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 724 | .setDeviceId(p4DeviceId) |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 725 | .setElectionId(p4RuntimeElectionId) |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 726 | .addUpdates(Update.newBuilder() |
| 727 | .setEntity(Entity.newBuilder() |
| 728 | .setActionProfileGroup(actionProfileGroup) |
| 729 | .build()) |
| 730 | .setType(UPDATE_TYPES.get(opType)) |
| 731 | .build()) |
| 732 | .build(); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 733 | try { |
Carmelo Cascone | 87b9b39 | 2017-10-02 18:33:20 +0200 | [diff] [blame] | 734 | blockingStub.write(writeRequestMsg); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 735 | return true; |
| 736 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 737 | logWriteErrors(Collections.singleton(group), e, opType, "group"); |
Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame] | 738 | return false; |
| 739 | } |
| 740 | } |
| 741 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 742 | private Collection<PiMeterCellConfig> doReadAllMeterCells( |
| 743 | Collection<PiMeterId> meterIds, PiPipeconf pipeconf) { |
| 744 | return doReadMeterEntities(MeterEntryCodec.readAllCellsEntities( |
| 745 | meterIds, pipeconf), pipeconf); |
| 746 | } |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 747 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 748 | private Collection<PiMeterCellConfig> doReadMeterCells( |
| 749 | Collection<PiMeterCellId> cellIds, PiPipeconf pipeconf) { |
| 750 | |
| 751 | final Collection<PiMeterCellConfig> piMeterCellConfigs = cellIds.stream() |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 752 | .map(cellId -> PiMeterCellConfig.builder() |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 753 | .withMeterCellId(cellId) |
| 754 | .build()) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 755 | .collect(Collectors.toList()); |
| 756 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 757 | return doReadMeterEntities(MeterEntryCodec.encodePiMeterCellConfigs( |
| 758 | piMeterCellConfigs, pipeconf), pipeconf); |
| 759 | } |
| 760 | |
| 761 | private Collection<PiMeterCellConfig> doReadMeterEntities( |
| 762 | Collection<Entity> entitiesToRead, PiPipeconf pipeconf) { |
| 763 | |
| 764 | if (entitiesToRead.size() == 0) { |
| 765 | return Collections.emptyList(); |
| 766 | } |
| 767 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 768 | final ReadRequest request = ReadRequest.newBuilder() |
| 769 | .setDeviceId(p4DeviceId) |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 770 | .addAllEntities(entitiesToRead) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 771 | .build(); |
| 772 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 773 | final Iterable<ReadResponse> responses; |
| 774 | try { |
| 775 | responses = () -> blockingStub.read(request); |
| 776 | } catch (StatusRuntimeException e) { |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 777 | log.warn("Unable to read meter cells: {}", e.getMessage()); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 778 | log.debug("exception", e); |
| 779 | return Collections.emptyList(); |
| 780 | } |
| 781 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 782 | List<Entity> responseEntities = StreamSupport |
| 783 | .stream(responses.spliterator(), false) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 784 | .map(ReadResponse::getEntitiesList) |
| 785 | .flatMap(List::stream) |
| 786 | .collect(Collectors.toList()); |
| 787 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 788 | return MeterEntryCodec.decodeMeterEntities(responseEntities, pipeconf); |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | private boolean doWriteMeterCells(Collection<PiMeterCellConfig> cellIds, PiPipeconf pipeconf) { |
| 792 | |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 793 | WriteRequest.Builder writeRequestBuilder = WriteRequest.newBuilder(); |
| 794 | |
Carmelo Cascone | 81929aa | 2018-04-07 01:38:55 -0700 | [diff] [blame^] | 795 | Collection<Update> updateMsgs = MeterEntryCodec.encodePiMeterCellConfigs(cellIds, pipeconf) |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 796 | .stream() |
| 797 | .map(meterEntryMsg -> |
| 798 | Update.newBuilder() |
| 799 | .setEntity(meterEntryMsg) |
| 800 | .setType(UPDATE_TYPES.get(WriteOperationType.MODIFY)) |
| 801 | .build()) |
| 802 | .collect(Collectors.toList()); |
| 803 | |
| 804 | if (updateMsgs.size() == 0) { |
| 805 | return true; |
| 806 | } |
| 807 | |
| 808 | writeRequestBuilder |
| 809 | .setDeviceId(p4DeviceId) |
| 810 | .setElectionId(p4RuntimeElectionId) |
| 811 | .addAllUpdates(updateMsgs) |
| 812 | .build(); |
| 813 | try { |
| 814 | blockingStub.write(writeRequestBuilder.build()); |
| 815 | return true; |
| 816 | } catch (StatusRuntimeException e) { |
| 817 | log.warn("Unable to write meter entries : {}", e.getMessage()); |
| 818 | log.debug("exception", e); |
| 819 | return false; |
| 820 | } |
| 821 | } |
| 822 | |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 823 | /** |
| 824 | * Returns the internal P4 device ID associated with this client. |
| 825 | * |
| 826 | * @return P4 device ID |
| 827 | */ |
Carmelo Cascone | f423bec | 2017-08-30 01:56:25 +0200 | [diff] [blame] | 828 | public long p4DeviceId() { |
Carmelo Cascone | b2e3dba | 2017-07-27 12:07:09 -0400 | [diff] [blame] | 829 | return p4DeviceId; |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * For testing purpose only. TODO: remove before release. |
| 834 | * |
| 835 | * @return blocking stub |
| 836 | */ |
| 837 | public P4RuntimeGrpc.P4RuntimeBlockingStub blockingStub() { |
| 838 | return this.blockingStub; |
| 839 | } |
| 840 | |
Andrea Campanella | fc1d34c | 2017-07-18 17:01:41 +0200 | [diff] [blame] | 841 | |
Andrea Campanella | 432f718 | 2017-07-14 18:43:27 +0200 | [diff] [blame] | 842 | @Override |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 843 | public void shutdown() { |
| 844 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 845 | log.info("Shutting down client for {}...", deviceId); |
| 846 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 847 | writeLock.lock(); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 848 | try { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 849 | if (streamRequestObserver != null) { |
| 850 | streamRequestObserver.onCompleted(); |
| 851 | cancellableContext.cancel(new InterruptedException("Requested client shutdown")); |
| 852 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 853 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 854 | this.executorService.shutdown(); |
| 855 | try { |
| 856 | executorService.awaitTermination(5, TimeUnit.SECONDS); |
| 857 | } catch (InterruptedException e) { |
| 858 | log.warn("Executor service didn't shutdown in time."); |
Ray Milkey | 5c7d488 | 2018-02-05 14:50:39 -0800 | [diff] [blame] | 859 | Thread.currentThread().interrupt(); |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 860 | } |
| 861 | } finally { |
| 862 | writeLock.unlock(); |
| 863 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 864 | } |
| 865 | |
Carmelo Cascone | 5bc7e10 | 2018-02-18 18:27:55 -0800 | [diff] [blame] | 866 | private <E extends PiEntity> void logWriteErrors(Collection<E> writeEntities, |
| 867 | StatusRuntimeException ex, |
| 868 | WriteOperationType opType, |
| 869 | String entryType) { |
| 870 | List<P4RuntimeOuterClass.Error> errors = null; |
| 871 | String description = null; |
| 872 | try { |
| 873 | errors = extractWriteErrorDetails(ex); |
| 874 | } catch (InvalidProtocolBufferException e) { |
| 875 | description = ex.getStatus().getDescription(); |
| 876 | } |
| 877 | |
| 878 | log.warn("Unable to {} {} {}(s) on {}: {}{} (detailed errors might be logged below)", |
| 879 | opType.name(), writeEntities.size(), entryType, deviceId, |
| 880 | ex.getStatus().getCode().name(), |
| 881 | description == null ? "" : " - " + description); |
| 882 | |
| 883 | if (errors == null || errors.isEmpty()) { |
| 884 | return; |
| 885 | } |
| 886 | |
| 887 | // FIXME: we are assuming entities is an ordered collection, e.g. a list, |
| 888 | // and that errors are reported in the same order as the corresponding |
| 889 | // written entity. Write RPC methods should be refactored to accept an |
| 890 | // order list of entities, instead of a collection. |
| 891 | if (errors.size() == writeEntities.size()) { |
| 892 | Iterator<E> entityIterator = writeEntities.iterator(); |
| 893 | errors.stream() |
| 894 | .map(e -> ImmutablePair.of(e, entityIterator.next())) |
| 895 | .filter(p -> p.left.getCanonicalCode() != Status.OK.getCode().value()) |
| 896 | .forEach(p -> log.warn("Unable to {} {}: {} [{}]", |
| 897 | opType.name(), entryType, parseP4Error(p.getLeft()), |
| 898 | p.getRight().toString())); |
| 899 | } else { |
| 900 | log.error("Unable to reconcile error details to updates " + |
| 901 | "(sent {} updates, but device returned {} errors)", |
| 902 | entryType, writeEntities.size(), errors.size()); |
| 903 | errors.stream() |
| 904 | .filter(err -> err.getCanonicalCode() != Status.OK.getCode().value()) |
| 905 | .forEach(err -> log.warn("Unable to {} {} (unknown): {}", |
| 906 | opType.name(), entryType, parseP4Error(err))); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | private List<P4RuntimeOuterClass.Error> extractWriteErrorDetails( |
| 911 | StatusRuntimeException ex) throws InvalidProtocolBufferException { |
| 912 | String statusString = ex.getStatus().getDescription(); |
| 913 | if (statusString == null) { |
| 914 | return Collections.emptyList(); |
| 915 | } |
| 916 | com.google.rpc.Status status = com.google.rpc.Status |
| 917 | .parseFrom(statusString.getBytes()); |
| 918 | return status.getDetailsList().stream() |
| 919 | .map(any -> { |
| 920 | try { |
| 921 | return any.unpack(P4RuntimeOuterClass.Error.class); |
| 922 | } catch (InvalidProtocolBufferException e) { |
| 923 | log.warn("Unable to unpack P4Runtime Error: {}", |
| 924 | any.toString()); |
| 925 | return null; |
| 926 | } |
| 927 | }) |
| 928 | .filter(Objects::nonNull) |
| 929 | .collect(Collectors.toList()); |
| 930 | |
| 931 | } |
| 932 | |
| 933 | private String parseP4Error(P4RuntimeOuterClass.Error err) { |
| 934 | return format("%s %s (%s code %d)%s", |
| 935 | Status.fromCodeValue(err.getCanonicalCode()), |
| 936 | err.getMessage(), |
| 937 | err.getSpace(), |
| 938 | err.getCode(), |
| 939 | err.hasDetails() ? "\n" + err.getDetails().toString() : ""); |
| 940 | } |
| 941 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 942 | /** |
| 943 | * Handles messages received from the device on the stream channel. |
| 944 | */ |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 945 | private class StreamChannelResponseObserver implements StreamObserver<StreamMessageResponse> { |
| 946 | |
| 947 | @Override |
| 948 | public void onNext(StreamMessageResponse message) { |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 949 | executorService.submit(() -> doNext(message)); |
| 950 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 951 | |
Carmelo Cascone | 8d99b17 | 2017-07-18 17:26:31 -0400 | [diff] [blame] | 952 | private void doNext(StreamMessageResponse message) { |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 953 | try { |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 954 | log.debug("Received message on stream channel from {}: {}", deviceId, message.getUpdateCase()); |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 955 | switch (message.getUpdateCase()) { |
| 956 | case PACKET: |
| 957 | // Packet-in |
| 958 | doPacketIn(message.getPacket()); |
Andrea Campanella | 288b273 | 2017-07-28 14:16:16 +0200 | [diff] [blame] | 959 | return; |
Carmelo Cascone | a966c34 | 2017-07-30 01:56:30 -0400 | [diff] [blame] | 960 | case ARBITRATION: |
| 961 | doArbitrationUpdateFromDevice(message.getArbitration()); |
| 962 | return; |
| 963 | default: |
| 964 | log.warn("Unrecognized stream message from {}: {}", deviceId, message.getUpdateCase()); |
| 965 | } |
| 966 | } catch (Throwable ex) { |
| 967 | log.error("Exception while processing stream channel message from {}", deviceId, ex); |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 968 | } |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | @Override |
| 972 | public void onError(Throwable throwable) { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 973 | log.warn("Error on stream channel for {}: {}", deviceId, Status.fromThrowable(throwable)); |
| 974 | // FIXME: we might want to recreate the channel. |
| 975 | // In general, we want to be robust against any transient error and, if the channel is open, make sure the |
| 976 | // stream channel is always on. |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | @Override |
| 980 | public void onCompleted() { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 981 | log.warn("Stream channel for {} has completed", deviceId); |
| 982 | // FIXME: same concern as before. |
Carmelo Cascone | f7aa3f9 | 2017-07-06 23:56:50 -0400 | [diff] [blame] | 983 | } |
| 984 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 985 | } |