Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [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 | |
Andrea Campanella | 0288c87 | 2017-08-07 18:32:51 +0200 | [diff] [blame] | 17 | package org.onosproject.drivers.p4runtime; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 18 | |
Andrea Campanella | 378e21a | 2017-06-07 12:09:59 +0200 | [diff] [blame] | 19 | import org.onosproject.net.DeviceId; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 20 | import org.onosproject.net.MastershipRole; |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 21 | import org.onosproject.net.device.ChannelListener; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 22 | import org.onosproject.net.device.DeviceHandshaker; |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 23 | import org.onosproject.p4runtime.api.P4RuntimeController; |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 24 | |
| 25 | import java.util.concurrent.CompletableFuture; |
| 26 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 27 | /** |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 28 | * Implementation of DeviceHandshaker for P4Runtime. |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 29 | */ |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 30 | public class P4RuntimeHandshaker extends AbstractP4RuntimeHandlerBehaviour implements DeviceHandshaker { |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 31 | |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 32 | // TODO: consider abstract class with empty connect method and implementation into a protected one for reusability. |
| 33 | |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 34 | @Override |
| 35 | public CompletableFuture<Boolean> connect() { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 36 | return CompletableFuture.supplyAsync(this::doConnect); |
| 37 | } |
| 38 | |
| 39 | private boolean doConnect() { |
Carmelo Cascone | e3a7c74 | 2017-09-01 01:25:52 +0200 | [diff] [blame] | 40 | return super.createClient(); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public CompletableFuture<Boolean> disconnect() { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 45 | return CompletableFuture.supplyAsync(() -> { |
| 46 | P4RuntimeController controller = handler().get(P4RuntimeController.class); |
| 47 | DeviceId deviceId = handler().data().deviceId(); |
| 48 | controller.removeClient(deviceId); |
| 49 | return true; |
| 50 | }); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public CompletableFuture<Boolean> isReachable() { |
Carmelo Cascone | 59f57de | 2017-07-11 19:55:09 -0400 | [diff] [blame] | 55 | return CompletableFuture.supplyAsync(() -> { |
| 56 | P4RuntimeController controller = handler().get(P4RuntimeController.class); |
| 57 | DeviceId deviceId = handler().data().deviceId(); |
| 58 | return controller.isReacheable(deviceId); |
| 59 | }); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public CompletableFuture<MastershipRole> roleChanged(MastershipRole newRole) { |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 64 | deviceId = handler().data().deviceId(); |
| 65 | controller = handler().get(P4RuntimeController.class); |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 66 | CompletableFuture<MastershipRole> result = new CompletableFuture<>(); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 67 | |
| 68 | client = controller.getClient(deviceId); |
| 69 | if (client == null || !controller.isReacheable(deviceId)) { |
Andrea Campanella | 14e196d | 2017-07-24 18:11:36 +0200 | [diff] [blame] | 70 | result.complete(MastershipRole.NONE); |
Yi Tseng | 3e7f145 | 2017-10-20 10:31:53 -0700 | [diff] [blame] | 71 | return result; |
| 72 | } |
| 73 | if (newRole.equals(MastershipRole.MASTER)) { |
| 74 | client.sendMasterArbitrationUpdate().thenAcceptAsync(success -> { |
| 75 | if (!success) { |
| 76 | log.warn("Device {} arbitration failed", deviceId); |
| 77 | result.complete(MastershipRole.STANDBY); |
| 78 | } else { |
| 79 | result.complete(MastershipRole.MASTER); |
| 80 | } |
| 81 | }); |
| 82 | } else { |
| 83 | // Since we don't need to do anything, we can complete it directly |
| 84 | // Spec: The client with the highest election id is referred to as the |
| 85 | // "master", while all other clients are referred to as "slaves". |
| 86 | result.complete(newRole); |
| 87 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 88 | return result; |
| 89 | } |
Andrea Campanella | 1e57344 | 2018-05-17 17:07:13 +0200 | [diff] [blame] | 90 | |
| 91 | @Override |
| 92 | public void addChannelListener(ChannelListener listener) { |
| 93 | controller.addChannelListener(deviceId, listener); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void removeChannelListener(ChannelListener listener) { |
| 98 | controller.removeChannelListener(deviceId, listener); |
| 99 | } |
Andrea Campanella | 241896c | 2017-05-10 13:11:04 -0700 | [diff] [blame] | 100 | } |