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 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame^] | 19 | import io.grpc.ManagedChannelBuilder; |
| 20 | import io.grpc.netty.NettyChannelBuilder; |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 21 | import org.onosproject.net.DeviceId; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 22 | import org.onosproject.net.MastershipRole; |
| 23 | import org.onosproject.net.device.DeviceHandshaker; |
| 24 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 25 | import org.onosproject.net.driver.DriverData; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame^] | 26 | import org.onosproject.p4runtime.api.P4RuntimeController; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 27 | import org.slf4j.Logger; |
| 28 | |
| 29 | import java.util.concurrent.CompletableFuture; |
| 30 | |
| 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | |
| 33 | /** |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame^] | 34 | * Implementation of DeviceHandshaker for BMv2. |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 35 | */ |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 36 | public class Bmv2Handshaker extends AbstractHandlerBehaviour |
| 37 | implements DeviceHandshaker { |
| 38 | |
| 39 | private final Logger log = getLogger(getClass()); |
| 40 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame^] | 41 | // TODO: consider abstract class with empty connect method and implementation into a protected one for reusability. |
| 42 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 43 | @Override |
| 44 | public CompletableFuture<Boolean> connect() { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame^] | 45 | return CompletableFuture.supplyAsync(this::doConnect); |
| 46 | } |
| 47 | |
| 48 | private boolean doConnect() { |
| 49 | |
| 50 | P4RuntimeController controller = handler().get(P4RuntimeController.class); |
| 51 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 52 | DeviceId deviceId = handler().data().deviceId(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame^] | 53 | // DeviceKeyService deviceKeyService = handler().get(DeviceKeyService.class); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 54 | DriverData data = data(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 55 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame^] | 56 | String serverAddr = data.value("p4runtime_ip"); |
| 57 | int serverPort = Integer.valueOf(data.value("p4runtime_port")); |
| 58 | int p4DeviceId = Integer.valueOf(data.value("p4runtime_deviceId")); |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 59 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame^] | 60 | ManagedChannelBuilder channelBuilder = NettyChannelBuilder |
| 61 | .forAddress(serverAddr, serverPort) |
| 62 | .usePlaintext(true); |
| 63 | |
| 64 | if (!controller.createClient(deviceId, p4DeviceId, channelBuilder)) { |
| 65 | log.warn("Unable to create P4runtime client for {}", deviceId); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | // TODO: gNMI handling |
| 70 | |
| 71 | return true; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public CompletableFuture<Boolean> disconnect() { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame^] | 76 | return CompletableFuture.supplyAsync(() -> { |
| 77 | P4RuntimeController controller = handler().get(P4RuntimeController.class); |
| 78 | DeviceId deviceId = handler().data().deviceId(); |
| 79 | controller.removeClient(deviceId); |
| 80 | return true; |
| 81 | }); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public CompletableFuture<Boolean> isReachable() { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame^] | 86 | return CompletableFuture.supplyAsync(() -> { |
| 87 | P4RuntimeController controller = handler().get(P4RuntimeController.class); |
| 88 | DeviceId deviceId = handler().data().deviceId(); |
| 89 | return controller.isReacheable(deviceId); |
| 90 | }); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public CompletableFuture<MastershipRole> roleChanged(MastershipRole newRole) { |
| 95 | CompletableFuture<MastershipRole> result = new CompletableFuture<>(); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame^] | 96 | log.warn("roleChanged not implemented"); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 97 | result.complete(MastershipRole.MASTER); |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame^] | 98 | // TODO. |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 99 | return result; |
| 100 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 101 | } |