blob: 8ce68d7c730c9877f8fa2bb3224e85b15082164c [file] [log] [blame]
Yi Tsengd7716482018-10-31 15:34:30 -07001/*
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.stratum;
18
Yi Tsengd7716482018-10-31 15:34:30 -070019import org.onosproject.drivers.gnmi.GnmiHandshaker;
20import org.onosproject.drivers.p4runtime.P4RuntimeHandshaker;
Yi Tsengd7716482018-10-31 15:34:30 -070021import org.onosproject.net.MastershipRole;
22import org.onosproject.net.device.DeviceAgentListener;
23import org.onosproject.net.device.DeviceHandshaker;
Yi Tsengd7716482018-10-31 15:34:30 -070024import org.onosproject.net.provider.ProviderId;
Yi Tsengd7716482018-10-31 15:34:30 -070025
26import java.util.concurrent.CompletableFuture;
Yi Tsengd7716482018-10-31 15:34:30 -070027
28/**
29 * Implementation of DeviceHandshaker for Stratum device.
30 */
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080031public class StratumHandshaker
32 extends AbstractStratumBehaviour<DeviceHandshaker>
33 implements DeviceHandshaker {
Yi Tsengd7716482018-10-31 15:34:30 -070034
35 public StratumHandshaker() {
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080036 super(new P4RuntimeHandshaker(), new GnmiHandshaker());
Yi Tsengd7716482018-10-31 15:34:30 -070037 }
38
39 @Override
Carmelo Cascone3977ea42019-02-28 13:43:42 -080040 public boolean isReachable() {
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080041 // Reachability is mainly used for mastership contests and it's a
42 // prerequisite for availability. We can probably live without gNMI and
43 // gNOI, but we will always need P4Runtime.
44 return p4runtime.isReachable();
Carmelo Cascone3977ea42019-02-28 13:43:42 -080045 }
46
47 @Override
48 public CompletableFuture<Boolean> probeReachability() {
49 return p4runtime.probeReachability();
50 }
51
52 @Override
53 public boolean isAvailable() {
54 // Availability concerns packet forwarding, hence we consider only
55 // P4Runtime.
56 return p4runtime.isAvailable();
57 }
58
59 @Override
60 public CompletableFuture<Boolean> probeAvailability() {
61 return p4runtime.probeAvailability();
Yi Tsengd7716482018-10-31 15:34:30 -070062 }
63
64 @Override
65 public void roleChanged(MastershipRole newRole) {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080066 p4runtime.roleChanged(newRole);
67 }
68
69 @Override
70 public void roleChanged(int preference, long term) {
71 p4runtime.roleChanged(preference, term);
Yi Tsengd7716482018-10-31 15:34:30 -070072 }
73
74 @Override
75 public MastershipRole getRole() {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080076 return p4runtime.getRole();
Yi Tsengd7716482018-10-31 15:34:30 -070077 }
78
79 @Override
80 public void addDeviceAgentListener(ProviderId providerId, DeviceAgentListener listener) {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080081 p4runtime.addDeviceAgentListener(providerId, listener);
Yi Tsengd7716482018-10-31 15:34:30 -070082 }
83
84 @Override
85 public void removeDeviceAgentListener(ProviderId providerId) {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080086 p4runtime.removeDeviceAgentListener(providerId);
Yi Tsengd7716482018-10-31 15:34:30 -070087 }
88
89 @Override
90 public CompletableFuture<Boolean> connect() {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080091 // We should execute connections in parallel.
92 return p4runtime.connect().thenCombine(gnmi.connect(), Boolean::logicalAnd);
Yi Tsengd7716482018-10-31 15:34:30 -070093 }
94
95 @Override
96 public boolean isConnected() {
Carmelo Cascone3977ea42019-02-28 13:43:42 -080097 return p4runtime.isConnected() && gnmi.isConnected();
Yi Tsengd7716482018-10-31 15:34:30 -070098 }
99
100 @Override
Carmelo Cascone3977ea42019-02-28 13:43:42 -0800101 public void disconnect() {
102 p4runtime.disconnect();
103 gnmi.disconnect();
Yi Tsengd7716482018-10-31 15:34:30 -0700104 }
105}