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