blob: 4b7606709b202c321a3b6c96df780be0693883c1 [file] [log] [blame]
Andrea Campanella378e21a2017-06-07 12:09:59 +02001/*
2 * Copyright 2017-present Open Networking Laboratory
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
17package org.onosproject.drivers.bmv2;
18
19import io.grpc.stub.StreamObserver;
20import org.onosproject.grpc.api.GrpcChannelId;
21import org.onosproject.grpc.api.GrpcController;
22import org.onosproject.grpc.api.GrpcObserverHandler;
23import org.onosproject.grpc.api.GrpcServiceId;
24import org.onosproject.grpc.api.GrpcStreamObserverId;
25import org.onosproject.net.DeviceId;
26import org.onosproject.net.driver.AbstractHandlerBehaviour;
27import org.onosproject.net.driver.DriverHandler;
28import org.onosproject.net.packet.OutboundPacket;
29import org.onosproject.net.packet.PacketProgrammable;
30
31import java.util.Optional;
32
33/**
34 * Packet Programmable behaviour for BMv2 devices.
35 */
36public class Bmv2PacketProgrammable extends AbstractHandlerBehaviour implements PacketProgrammable {
37 @Override
38 public void emit(OutboundPacket packet) {
39 DriverHandler handler = handler();
40 GrpcController controller = handler.get(GrpcController.class);
41 DeviceId deviceId = handler.data().deviceId();
42 GrpcChannelId channelId = GrpcChannelId.of(deviceId, "bmv2");
43 GrpcServiceId serviceId = GrpcServiceId.of(channelId, "p4runtime");
44 GrpcStreamObserverId observerId = GrpcStreamObserverId.of(serviceId,
45 this.getClass().getSimpleName());
46 Optional<GrpcObserverHandler> manager = controller.getObserverManager(observerId);
47 if (!manager.isPresent()) {
48 //this is the first time the behaviour is called
49 controller.addObserver(observerId, new Bmv2PacketInObserverHandler());
50 }
51 //other already registered the observer for us.
52 Optional<StreamObserver> observer = manager.get().requestStreamObserver();
53 observer.ifPresent(objectStreamObserver -> objectStreamObserver.onNext(packet));
54 }
55}