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