Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.bmv2; |
| 18 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame^] | 19 | import org.onosproject.grpc.api.GrpcChannelId; |
| 20 | import org.onosproject.grpc.api.GrpcController; |
| 21 | import org.onosproject.grpc.api.GrpcServiceId; |
| 22 | import org.onosproject.grpc.api.GrpcStreamObserverId; |
| 23 | import org.onosproject.net.DeviceId; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 24 | import org.onosproject.net.MastershipRole; |
| 25 | import org.onosproject.net.device.DeviceHandshaker; |
| 26 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 27 | import org.onosproject.net.driver.DriverData; |
| 28 | import org.onosproject.net.key.DeviceKeyId; |
| 29 | import org.onosproject.net.key.DeviceKeyService; |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import java.util.concurrent.CompletableFuture; |
| 33 | |
| 34 | import static org.slf4j.LoggerFactory.getLogger; |
| 35 | |
| 36 | /** |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame^] | 37 | * Implementation of the DeviceHandshaker for the BMv2 softswitch. |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 38 | */ |
| 39 | //TODO consider abstract class with empty connect method and |
| 40 | //the implementation into a protected one for reusability. |
| 41 | //FIXME fill method bodies, used for testing. |
| 42 | public class Bmv2Handshaker extends AbstractHandlerBehaviour |
| 43 | implements DeviceHandshaker { |
| 44 | |
| 45 | private final Logger log = getLogger(getClass()); |
| 46 | |
| 47 | @Override |
| 48 | public CompletableFuture<Boolean> connect() { |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame^] | 49 | GrpcController controller = handler().get(GrpcController.class); |
| 50 | DeviceId deviceId = handler().data().deviceId(); |
| 51 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 52 | CompletableFuture<Boolean> result = new CompletableFuture<>(); |
| 53 | DeviceKeyService deviceKeyService = handler().get(DeviceKeyService.class); |
| 54 | DriverData data = data(); |
| 55 | //Retrieving information from the driver data. |
| 56 | log.info("protocol bmv2, ip {}, port {}, key {}", data.value("p4runtime_ip"), |
| 57 | data.value("p4runtime_port"), |
| 58 | deviceKeyService.getDeviceKey(DeviceKeyId.deviceKeyId(data.value("p4runtime_key"))) |
| 59 | .asUsernamePassword().username()); |
| 60 | |
| 61 | log.info("protocol gnmi, ip {}, port {}, key {}", data.value("gnmi_ip"), data.value("gnmi_port"), |
| 62 | deviceKeyService.getDeviceKey(DeviceKeyId.deviceKeyId(data.value("gnmi_key"))) |
| 63 | .asUsernamePassword().username()); |
| 64 | result.complete(true); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame^] | 65 | |
| 66 | //we know we need packet in so we register the observer. |
| 67 | GrpcChannelId channelId = GrpcChannelId.of(deviceId, "bmv2"); |
| 68 | GrpcServiceId serviceId = GrpcServiceId.of(channelId, "p4runtime"); |
| 69 | GrpcStreamObserverId observerId = GrpcStreamObserverId.of(serviceId, |
| 70 | Bmv2PacketProgrammable.class.getSimpleName()); |
| 71 | controller.addObserver(observerId, new Bmv2PacketInObserverHandler()); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 72 | return result; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public CompletableFuture<Boolean> disconnect() { |
| 77 | CompletableFuture<Boolean> result = new CompletableFuture<>(); |
| 78 | result.complete(true); |
| 79 | return result; |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public CompletableFuture<Boolean> isReachable() { |
| 84 | CompletableFuture<Boolean> result = new CompletableFuture<>(); |
| 85 | result.complete(true); |
| 86 | return result; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public CompletableFuture<MastershipRole> roleChanged(MastershipRole newRole) { |
| 91 | CompletableFuture<MastershipRole> result = new CompletableFuture<>(); |
| 92 | result.complete(MastershipRole.MASTER); |
| 93 | return result; |
| 94 | } |
| 95 | |
| 96 | } |