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