blob: 8774c0991fd9c8eb96acb3053c8d35a8af35ac97 [file] [log] [blame]
Andrea Campanella241896c2017-05-10 13:11:04 -07001/*
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
17package org.onosproject.drivers.bmv2;
18
Andrea Campanella378e21a2017-06-07 12:09:59 +020019import org.onosproject.grpc.api.GrpcChannelId;
20import org.onosproject.grpc.api.GrpcController;
21import org.onosproject.grpc.api.GrpcServiceId;
22import org.onosproject.grpc.api.GrpcStreamObserverId;
23import org.onosproject.net.DeviceId;
Andrea Campanella241896c2017-05-10 13:11:04 -070024import org.onosproject.net.MastershipRole;
25import org.onosproject.net.device.DeviceHandshaker;
26import org.onosproject.net.driver.AbstractHandlerBehaviour;
27import org.onosproject.net.driver.DriverData;
28import org.onosproject.net.key.DeviceKeyId;
29import org.onosproject.net.key.DeviceKeyService;
30import org.slf4j.Logger;
31
32import java.util.concurrent.CompletableFuture;
33
34import static org.slf4j.LoggerFactory.getLogger;
35
36/**
Andrea Campanella378e21a2017-06-07 12:09:59 +020037 * Implementation of the DeviceHandshaker for the BMv2 softswitch.
Andrea Campanella241896c2017-05-10 13:11:04 -070038 */
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.
42public class Bmv2Handshaker extends AbstractHandlerBehaviour
43 implements DeviceHandshaker {
44
45 private final Logger log = getLogger(getClass());
46
47 @Override
48 public CompletableFuture<Boolean> connect() {
Andrea Campanella378e21a2017-06-07 12:09:59 +020049 GrpcController controller = handler().get(GrpcController.class);
50 DeviceId deviceId = handler().data().deviceId();
51
Andrea Campanella241896c2017-05-10 13:11:04 -070052 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 Campanella378e21a2017-06-07 12:09:59 +020065
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 Campanella241896c2017-05-10 13:11:04 -070072 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}