blob: 3a4a1b585ee027bc181e10b059d69a06640f149e [file] [log] [blame]
Carmelo Casconef7aa3f92017-07-06 23:56:50 -04001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Carmelo Casconef7aa3f92017-07-06 23:56:50 -04003 *
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
17package org.onosproject.p4runtime.ctl;
18
Carmelo Cascone8d99b172017-07-18 17:26:31 -040019import com.google.common.collect.ImmutableMap;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040020import com.google.protobuf.ByteString;
Carmelo Cascone59f57de2017-07-11 19:55:09 -040021import io.grpc.Context;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040022import io.grpc.ManagedChannel;
23import io.grpc.Status;
24import io.grpc.StatusRuntimeException;
25import io.grpc.stub.StreamObserver;
Andrea Campanella288b2732017-07-28 14:16:16 +020026import org.onlab.osgi.DefaultServiceDirectory;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040027import org.onosproject.net.DeviceId;
Andrea Campanella432f7182017-07-14 18:43:27 +020028import org.onosproject.net.pi.model.PiPipeconf;
29import org.onosproject.net.pi.runtime.PiPacketOperation;
Andrea Campanella288b2732017-07-28 14:16:16 +020030import org.onosproject.net.pi.runtime.PiPipeconfService;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040031import org.onosproject.net.pi.runtime.PiTableEntry;
32import org.onosproject.net.pi.runtime.PiTableId;
33import org.onosproject.p4runtime.api.P4RuntimeClient;
34import org.onosproject.p4runtime.api.P4RuntimeEvent;
35import org.slf4j.Logger;
36import p4.P4RuntimeGrpc;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040037import p4.P4RuntimeOuterClass.Entity;
38import p4.P4RuntimeOuterClass.ForwardingPipelineConfig;
39import p4.P4RuntimeOuterClass.MasterArbitrationUpdate;
40import p4.P4RuntimeOuterClass.PacketIn;
41import p4.P4RuntimeOuterClass.ReadRequest;
42import p4.P4RuntimeOuterClass.ReadResponse;
43import p4.P4RuntimeOuterClass.SetForwardingPipelineConfigRequest;
44import p4.P4RuntimeOuterClass.StreamMessageRequest;
45import p4.P4RuntimeOuterClass.StreamMessageResponse;
46import p4.P4RuntimeOuterClass.TableEntry;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040047import p4.P4RuntimeOuterClass.Update;
48import p4.P4RuntimeOuterClass.WriteRequest;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020049import p4.config.P4InfoOuterClass.P4Info;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040050import p4.tmp.P4Config;
51
52import java.io.IOException;
53import java.io.InputStream;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040054import java.util.Collection;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040055import java.util.Collections;
56import java.util.Iterator;
57import java.util.List;
58import java.util.Map;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040059import java.util.concurrent.CompletableFuture;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040060import java.util.concurrent.Executor;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040061import java.util.concurrent.ExecutorService;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040062import java.util.concurrent.Executors;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040063import java.util.concurrent.TimeUnit;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040064import java.util.concurrent.locks.Lock;
65import java.util.concurrent.locks.ReentrantLock;
66import java.util.function.Supplier;
67import java.util.stream.Collectors;
68import java.util.stream.StreamSupport;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040069
Carmelo Cascone8d99b172017-07-18 17:26:31 -040070import static org.onlab.util.Tools.groupedThreads;
71import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040072import static org.slf4j.LoggerFactory.getLogger;
Carmelo Cascone8d99b172017-07-18 17:26:31 -040073import static p4.P4RuntimeOuterClass.Entity.EntityCase.TABLE_ENTRY;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020074import static p4.P4RuntimeOuterClass.PacketOut;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040075import static p4.P4RuntimeOuterClass.SetForwardingPipelineConfigRequest.Action.VERIFY_AND_COMMIT;
76
77/**
78 * Implementation of a P4Runtime client.
79 */
Carmelo Cascone8d99b172017-07-18 17:26:31 -040080public final class P4RuntimeClientImpl implements P4RuntimeClient {
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040081
82 private static final int DEADLINE_SECONDS = 15;
83
Carmelo Cascone8d99b172017-07-18 17:26:31 -040084 // FIXME: use static election ID, since mastership arbitration is not yet support on BMv2 or Tofino.
85 private static final int ELECTION_ID = 1;
86
87 private static final Map<WriteOperationType, Update.Type> UPDATE_TYPES = ImmutableMap.of(
88 WriteOperationType.UNSPECIFIED, Update.Type.UNSPECIFIED,
89 WriteOperationType.INSERT, Update.Type.INSERT,
90 WriteOperationType.MODIFY, Update.Type.MODIFY,
91 WriteOperationType.DELETE, Update.Type.DELETE
92 );
93
Carmelo Casconef7aa3f92017-07-06 23:56:50 -040094 private final Logger log = getLogger(getClass());
95
96 private final DeviceId deviceId;
97 private final int p4DeviceId;
98 private final P4RuntimeControllerImpl controller;
99 private final P4RuntimeGrpc.P4RuntimeBlockingStub blockingStub;
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400100 private final Context.CancellableContext cancellableContext;
101 private final ExecutorService executorService;
102 private final Executor contextExecutor;
103 private final Lock writeLock = new ReentrantLock();
104 private final StreamObserver<StreamMessageRequest> streamRequestObserver;
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400105
106
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400107 P4RuntimeClientImpl(DeviceId deviceId, int p4DeviceId, ManagedChannel channel, P4RuntimeControllerImpl controller) {
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400108 this.deviceId = deviceId;
109 this.p4DeviceId = p4DeviceId;
110 this.controller = controller;
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400111 this.cancellableContext = Context.current().withCancellation();
Carmelo Casconea966c342017-07-30 01:56:30 -0400112 this.executorService = Executors.newFixedThreadPool(15, groupedThreads(
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400113 "onos/p4runtime-client-" + deviceId.toString(),
114 deviceId.toString() + "-%d"));
115 this.contextExecutor = this.cancellableContext.fixedContextExecutor(executorService);
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200116 //TODO Investigate deadline or timeout in supplyInContext Method
117 this.blockingStub = P4RuntimeGrpc.newBlockingStub(channel);
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400118 P4RuntimeGrpc.P4RuntimeStub asyncStub = P4RuntimeGrpc.newStub(channel);
119 this.streamRequestObserver = asyncStub.streamChannel(new StreamChannelResponseObserver());
120 }
121
122 /**
123 * Executes the given task (supplier) in the gRPC context executor of this client, such that if the context is
124 * cancelled (e.g. client shutdown) the RPC is automatically cancelled.
125 * <p>
126 * Important: Tasks submitted in parallel by different threads are forced executed sequentially.
127 * <p>
128 */
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200129 private <U> CompletableFuture<U> supplyInContext(Supplier<U> supplier, String opDescription) {
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400130 return CompletableFuture.supplyAsync(() -> {
131 // TODO: explore a more relaxed locking strategy.
132 writeLock.lock();
133 try {
134 return supplier.get();
Carmelo Casconea966c342017-07-30 01:56:30 -0400135 } catch (Throwable ex) {
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200136 log.error("Exception in P4Runtime client of {}, executing {}", deviceId, opDescription, ex);
Carmelo Casconea966c342017-07-30 01:56:30 -0400137 throw ex;
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400138 } finally {
139 writeLock.unlock();
140 }
141 }, contextExecutor);
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400142 }
143
144 @Override
145 public CompletableFuture<Boolean> initStreamChannel() {
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200146 return supplyInContext(this::doInitStreamChannel, "initStreamChannel");
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400147 }
148
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400149 @Override
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400150 public CompletableFuture<Boolean> setPipelineConfig(PiPipeconf pipeconf, ExtensionType targetConfigExtType) {
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200151 return supplyInContext(() -> doSetPipelineConfig(pipeconf, targetConfigExtType), "setPipelineConfig");
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400152 }
153
154 @Override
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400155 public CompletableFuture<Boolean> writeTableEntries(Collection<PiTableEntry> piTableEntries,
156 WriteOperationType opType, PiPipeconf pipeconf) {
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200157 return supplyInContext(() -> doWriteTableEntries(piTableEntries, opType, pipeconf),
158 "writeTableEntries-" + opType.name());
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400159 }
160
161 @Override
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400162 public CompletableFuture<Collection<PiTableEntry>> dumpTable(PiTableId piTableId, PiPipeconf pipeconf) {
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200163 return supplyInContext(() -> doDumpTable(piTableId, pipeconf), "dumpTable-" + piTableId);
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400164 }
165
166 @Override
Andrea Campanella432f7182017-07-14 18:43:27 +0200167 public CompletableFuture<Boolean> packetOut(PiPacketOperation packet, PiPipeconf pipeconf) {
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200168 return supplyInContext(() -> doPacketOut(packet, pipeconf), "packetOut");
Andrea Campanella432f7182017-07-14 18:43:27 +0200169 }
170
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400171 /* Blocking method implementations below */
172
173 private boolean doInitStreamChannel() {
174 // To listen for packets and other events, we need to start the RPC.
175 // Here we do it by sending a master arbitration update.
176 log.info("initializing stream chanel on {}...", deviceId);
177 if (!doArbitrationUpdate()) {
178 log.warn("Unable to initialize stream channel for {}", deviceId);
179 return false;
180 } else {
181 return true;
182 }
183 }
184
185 private boolean doArbitrationUpdate() {
186 log.info("Sending arbitration update to {}...", deviceId);
187 StreamMessageRequest requestMsg = StreamMessageRequest.newBuilder()
188 .setArbitration(MasterArbitrationUpdate.newBuilder()
189 .setDeviceId(p4DeviceId)
190 .build())
191 .build();
192 try {
193 streamRequestObserver.onNext(requestMsg);
194 return true;
195 } catch (StatusRuntimeException e) {
196 log.warn("Arbitration update failed for {}: {}", deviceId, e);
197 return false;
198 }
199 }
200
201 private boolean doSetPipelineConfig(PiPipeconf pipeconf, ExtensionType targetConfigExtType) {
202
203 log.info("Setting pipeline config for {} to {} using {}...", deviceId, pipeconf.id(), targetConfigExtType);
204
205 P4Info p4Info = PipeconfHelper.getP4Info(pipeconf);
206 if (p4Info == null) {
207 // Problem logged by PipeconfHelper.
208 return false;
209 }
210
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400211
Andrea Campanella0288c872017-08-07 18:32:51 +0200212 ForwardingPipelineConfig.Builder pipelineConfigBuilder = ForwardingPipelineConfig
213 .newBuilder()
214 .setDeviceId(p4DeviceId)
215 .setP4Info(p4Info);
216
217 //if the target config extension is null we don't want to add the config.
218 if (targetConfigExtType != null) {
219 if (!pipeconf.extension(targetConfigExtType).isPresent()) {
220 log.warn("Missing extension {} in pipeconf {}", targetConfigExtType, pipeconf.id());
221 return false;
222 }
223 InputStream targetConfig = pipeconf.extension(targetConfigExtType).get();
224 P4Config.P4DeviceConfig p4DeviceConfigMsg;
225 try {
226 p4DeviceConfigMsg = P4Config.P4DeviceConfig
227 .newBuilder()
228 .setExtras(P4Config.P4DeviceConfig.Extras.getDefaultInstance())
229 .setReassign(true)
230 .setDeviceData(ByteString.readFrom(targetConfig))
231 .build();
232
233 pipelineConfigBuilder.setP4DeviceConfig(p4DeviceConfigMsg.toByteString());
234
235 } catch (IOException ex) {
236 log.warn("Unable to load target-specific config for {}: {}", deviceId, ex.getMessage());
237 return false;
238 }
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400239 }
240
241 SetForwardingPipelineConfigRequest request = SetForwardingPipelineConfigRequest
242 .newBuilder()
243 .setAction(VERIFY_AND_COMMIT)
Andrea Campanella0288c872017-08-07 18:32:51 +0200244 .addConfigs(pipelineConfigBuilder.build())
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400245 .build();
246
247 try {
248 this.blockingStub.setForwardingPipelineConfig(request);
249
250 } catch (StatusRuntimeException ex) {
251 log.warn("Unable to set pipeline config for {}: {}", deviceId, ex.getMessage());
252 return false;
253 }
254
255 return true;
256 }
257
258 private boolean doWriteTableEntries(Collection<PiTableEntry> piTableEntries, WriteOperationType opType,
259 PiPipeconf pipeconf) {
260
261 WriteRequest.Builder writeRequestBuilder = WriteRequest.newBuilder();
262
263 Collection<Update> updateMsgs = TableEntryEncoder.encode(piTableEntries, pipeconf)
264 .stream()
265 .map(tableEntryMsg ->
266 Update.newBuilder()
267 .setEntity(Entity.newBuilder()
268 .setTableEntry(tableEntryMsg)
269 .build())
270 .setType(UPDATE_TYPES.get(opType))
271 .build())
272 .collect(Collectors.toList());
273
274 if (updateMsgs.size() == 0) {
275 return true;
276 }
277
278 writeRequestBuilder
279 .setDeviceId(p4DeviceId)
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200280 /* PI ignores this ElectionId, commenting out for now.
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400281 .setElectionId(Uint128.newBuilder()
282 .setHigh(0)
283 .setLow(ELECTION_ID)
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200284 .build()) */
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400285 .addAllUpdates(updateMsgs)
286 .build();
287
288 try {
289 blockingStub.write(writeRequestBuilder.build());
290 return true;
291 } catch (StatusRuntimeException e) {
292 log.warn("Unable to write table entries ({}): {}", opType, e.getMessage());
293 return false;
294 }
295 }
296
297 private Collection<PiTableEntry> doDumpTable(PiTableId piTableId, PiPipeconf pipeconf) {
298
Carmelo Cascone9f007702017-08-24 13:30:51 +0200299 log.debug("Dumping table {} from {} (pipeconf {})...", piTableId, deviceId, pipeconf.id());
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400300
301 P4InfoBrowser browser = PipeconfHelper.getP4InfoBrowser(pipeconf);
302 int tableId;
303 try {
304 tableId = browser.tables().getByName(piTableId.id()).getPreamble().getId();
305 } catch (P4InfoBrowser.NotFoundException e) {
306 log.warn("Unable to dump table: {}", e.getMessage());
307 return Collections.emptyList();
308 }
309
310 ReadRequest requestMsg = ReadRequest.newBuilder()
311 .setDeviceId(p4DeviceId)
312 .addEntities(Entity.newBuilder()
313 .setTableEntry(TableEntry.newBuilder()
314 .setTableId(tableId)
315 .build())
316 .build())
317 .build();
318
319 Iterator<ReadResponse> responses;
320 try {
321 responses = blockingStub.read(requestMsg);
322 } catch (StatusRuntimeException e) {
323 log.warn("Unable to dump table: {}", e.getMessage());
324 return Collections.emptyList();
325 }
326
327 Iterable<ReadResponse> responseIterable = () -> responses;
328 List<TableEntry> tableEntryMsgs = StreamSupport
329 .stream(responseIterable.spliterator(), false)
330 .map(ReadResponse::getEntitiesList)
331 .flatMap(List::stream)
332 .filter(entity -> entity.getEntityCase() == TABLE_ENTRY)
333 .map(Entity::getTableEntry)
334 .collect(Collectors.toList());
335
Carmelo Cascone9f007702017-08-24 13:30:51 +0200336 log.debug("Retrieved {} entries from table {} on {}...", tableEntryMsgs.size(), piTableId, deviceId);
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400337
338 return TableEntryEncoder.decode(tableEntryMsgs, pipeconf);
339 }
340
Andrea Campanellafc1d34c2017-07-18 17:01:41 +0200341 private boolean doPacketOut(PiPacketOperation packet, PiPipeconf pipeconf) {
342 try {
343 //encode the PiPacketOperation into a PacketOut
344 PacketOut packetOut = PacketIOCodec.encodePacketOut(packet, pipeconf);
345
346 //Build the request
347 StreamMessageRequest packetOutRequest = StreamMessageRequest
348 .newBuilder().setPacket(packetOut).build();
349
350 //Send the request
351 streamRequestObserver.onNext(packetOutRequest);
352
353 } catch (P4InfoBrowser.NotFoundException e) {
354 log.error("Cant find expected metadata in p4Info file. {}", e.getMessage());
355 log.debug("Exception", e);
356 return false;
357 }
358 return true;
359 }
360
Carmelo Casconea966c342017-07-30 01:56:30 -0400361 private void doPacketIn(PacketIn packetInMsg) {
362
363 // Retrieve the pipeconf for this client's device.
364 PiPipeconfService pipeconfService = DefaultServiceDirectory.getService(PiPipeconfService.class);
365 if (pipeconfService == null) {
366 throw new IllegalStateException("PiPipeconfService is null. Can't handle packet in.");
367 }
368 final PiPipeconf pipeconf;
369 if (pipeconfService.ofDevice(deviceId).isPresent() &&
370 pipeconfService.getPipeconf(pipeconfService.ofDevice(deviceId).get()).isPresent()) {
371 pipeconf = pipeconfService.getPipeconf(pipeconfService.ofDevice(deviceId).get()).get();
372 } else {
373 log.warn("Unable to get pipeconf of {}. Can't handle packet in", deviceId);
374 return;
375 }
376 // Decode packet message and post event.
Carmelo Cascone2cad9ef2017-08-01 21:52:07 +0200377 PiPacketOperation packetOperation = PacketIOCodec.decodePacketIn(packetInMsg, pipeconf);
378 DefaultPacketIn packetInEventSubject = new DefaultPacketIn(deviceId, packetOperation);
379 P4RuntimeEvent event = new P4RuntimeEvent(P4RuntimeEvent.Type.PACKET_IN, packetInEventSubject);
Carmelo Casconea966c342017-07-30 01:56:30 -0400380 log.debug("Received packet in: {}", event);
381 controller.postEvent(event);
382 }
383
384 private void doArbitrationUpdateFromDevice(MasterArbitrationUpdate arbitrationMsg) {
385
386 log.warn("Received arbitration update from {} (NOT IMPLEMENTED YET): {}", deviceId, arbitrationMsg);
387 }
388
Carmelo Casconeb2e3dba2017-07-27 12:07:09 -0400389 /**
390 * Returns the internal P4 device ID associated with this client.
391 *
392 * @return P4 device ID
393 */
394 public int p4DeviceId() {
395 return p4DeviceId;
396 }
397
398 /**
399 * For testing purpose only. TODO: remove before release.
400 *
401 * @return blocking stub
402 */
403 public P4RuntimeGrpc.P4RuntimeBlockingStub blockingStub() {
404 return this.blockingStub;
405 }
406
Andrea Campanellafc1d34c2017-07-18 17:01:41 +0200407
Andrea Campanella432f7182017-07-14 18:43:27 +0200408 @Override
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400409 public void shutdown() {
410
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400411 log.info("Shutting down client for {}...", deviceId);
412
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400413 writeLock.lock();
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400414 try {
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400415 if (streamRequestObserver != null) {
416 streamRequestObserver.onCompleted();
417 cancellableContext.cancel(new InterruptedException("Requested client shutdown"));
418 }
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400419
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400420 this.executorService.shutdown();
421 try {
422 executorService.awaitTermination(5, TimeUnit.SECONDS);
423 } catch (InterruptedException e) {
424 log.warn("Executor service didn't shutdown in time.");
425 }
426 } finally {
427 writeLock.unlock();
428 }
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400429 }
430
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400431 /**
432 * Handles messages received from the device on the stream channel.
433 */
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400434 private class StreamChannelResponseObserver implements StreamObserver<StreamMessageResponse> {
435
436 @Override
437 public void onNext(StreamMessageResponse message) {
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400438 executorService.submit(() -> doNext(message));
439 }
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400440
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400441 private void doNext(StreamMessageResponse message) {
Carmelo Casconea966c342017-07-30 01:56:30 -0400442 try {
Andrea Campanella0288c872017-08-07 18:32:51 +0200443 log.debug("Received message on stream channel from {}: {}", deviceId, message.getUpdateCase());
Carmelo Casconea966c342017-07-30 01:56:30 -0400444 switch (message.getUpdateCase()) {
445 case PACKET:
446 // Packet-in
447 doPacketIn(message.getPacket());
Andrea Campanella288b2732017-07-28 14:16:16 +0200448 return;
Carmelo Casconea966c342017-07-30 01:56:30 -0400449 case ARBITRATION:
450 doArbitrationUpdateFromDevice(message.getArbitration());
451 return;
452 default:
453 log.warn("Unrecognized stream message from {}: {}", deviceId, message.getUpdateCase());
454 }
455 } catch (Throwable ex) {
456 log.error("Exception while processing stream channel message from {}", deviceId, ex);
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400457 }
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400458 }
459
460 @Override
461 public void onError(Throwable throwable) {
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400462 log.warn("Error on stream channel for {}: {}", deviceId, Status.fromThrowable(throwable));
463 // FIXME: we might want to recreate the channel.
464 // In general, we want to be robust against any transient error and, if the channel is open, make sure the
465 // stream channel is always on.
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400466 }
467
468 @Override
469 public void onCompleted() {
Carmelo Cascone59f57de2017-07-11 19:55:09 -0400470 log.warn("Stream channel for {} has completed", deviceId);
471 // FIXME: same concern as before.
Carmelo Casconef7aa3f92017-07-06 23:56:50 -0400472 }
473 }
Carmelo Cascone8d99b172017-07-18 17:26:31 -0400474}