blob: efb7b77120a17c261d784fe0eae996293daac1fc [file] [log] [blame]
Esin Karaman971fb7f2017-12-28 13:44:52 +00001/*
2 * Copyright 2018-present Open Networking Foundation
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 org.onosproject.drivers.bmv2.api.Bmv2PreController;
20import org.onosproject.drivers.p4runtime.P4RuntimeHandshaker;
21import org.onosproject.net.DeviceId;
22import org.slf4j.Logger;
23import org.slf4j.LoggerFactory;
24
25import java.util.concurrent.CompletableFuture;
26
27public class Bmv2DeviceHandshaker extends P4RuntimeHandshaker {
28
29 public static final String THRIFT_SERVER_ADDRESS_KEY = "bmv2-thrift_ip";
30 public static final String THRIFT_SERVER_PORT_KEY = "bmv2-thrift_port";
31 protected final Logger log = LoggerFactory.getLogger(getClass());
32
33 @Override
34 public CompletableFuture<Boolean> connect() {
35 //connect to both GRPC and Thrift servers in parallel
36 CompletableFuture<Boolean> futureP4Runtime = super.connect();
37 CompletableFuture<Boolean> futureBmv2Pre = connectToBmv2Pre();
38 //combine futures and asses the overall result by using P4Runtime connection status
39 return futureP4Runtime.thenCombine(futureBmv2Pre,
40 (p4RuntimeConnected, bmv2PreConnected) -> p4RuntimeConnected);
41 }
42
43 @Override
44 public CompletableFuture<Boolean> disconnect() {
45 //disconnect from both GRPC and Thrift servers in parallel
46 CompletableFuture<Boolean> futureP4Runtime = super.disconnect();
47 CompletableFuture<Boolean> futureBmv2Pre = disconnectFromBmv2Pre();
48 //combine futures and asses the overall result by using P4Runtime disconnection status
49 return futureP4Runtime.thenCombine(futureBmv2Pre,
50 (p4RuntimeDisconnected, bmv2PreDisconnected) -> p4RuntimeDisconnected);
51 }
52
53 private CompletableFuture<Boolean> connectToBmv2Pre() {
54 return CompletableFuture.supplyAsync(this::createPreClient);
55 }
56
57 private CompletableFuture<Boolean> disconnectFromBmv2Pre() {
58 return CompletableFuture.supplyAsync(() -> {
59 removePreClient();
60 return true;
61 });
62 }
63
64 /**
65 * Creates a BMv2 PRE client for this device.
66 *
67 * @return true if successful; false otherwise
68 */
69 private boolean createPreClient() {
70 DeviceId deviceId = handler().data().deviceId();
71
72 String serverAddress = data().value(THRIFT_SERVER_ADDRESS_KEY);
73 String serverPortString = data().value(THRIFT_SERVER_PORT_KEY);
74
75 if (serverAddress == null || serverPortString == null) {
76 log.warn("Unable to create PRE client for {}, missing driver data key (required is {}, and {})",
77 deviceId, THRIFT_SERVER_ADDRESS_KEY, THRIFT_SERVER_PORT_KEY);
78 return false;
79 }
80 Bmv2PreController bmv2PreController = handler().get(Bmv2PreController.class);
81 try {
82 return bmv2PreController.createPreClient(deviceId,
83 serverAddress,
84 Integer.parseUnsignedInt(serverPortString));
85 } catch (RuntimeException e) {
86 log.warn("Unable to create BMv2 PRE client for {} due to {}", deviceId, e.toString());
87 return false;
88 }
89 }
90
91 private void removePreClient() {
92 DeviceId deviceId = handler().data().deviceId();
93 handler().get(Bmv2PreController.class).removePreClient(deviceId);
94 }
95}