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