blob: 666d5b1e231e933a51e74df90aa007c4f4d80833 [file] [log] [blame]
Carmelo Casconeab5d41e2019-03-06 18:02:34 -08001/*
2 * Copyright 2019-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.gnmi;
18
19import gnmi.Gnmi;
20import org.onosproject.gnmi.api.GnmiClient;
Carmelo Casconec2be50a2019-04-10 00:15:39 -070021import org.onosproject.gnmi.api.GnmiController;
22import org.onosproject.grpc.utils.AbstractGrpcHandlerBehaviour;
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080023import org.onosproject.net.PortNumber;
24import org.onosproject.net.behaviour.PortAdmin;
25
26import java.util.concurrent.CompletableFuture;
27
28import static java.util.concurrent.CompletableFuture.completedFuture;
29
30/**
31 * Implementation of PortAdmin for gNMI devices with OpenConfig support.
32 */
33public class OpenConfigGnmiPortAdminBehaviour
Carmelo Casconec2be50a2019-04-10 00:15:39 -070034 extends AbstractGrpcHandlerBehaviour<GnmiClient, GnmiController>
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080035 implements PortAdmin {
36
Carmelo Casconec2be50a2019-04-10 00:15:39 -070037 public OpenConfigGnmiPortAdminBehaviour() {
38 super(GnmiController.class);
39 }
40
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080041 @Override
42 public CompletableFuture<Boolean> enable(PortNumber number) {
Carmelo Casconec2be50a2019-04-10 00:15:39 -070043 if (!setupBehaviour("enable()")) {
44 return completedFuture(false);
45 }
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080046 doEnable(number, true);
47 // Always returning true is OK assuming this is used only by the
48 // GeneralDeviceProvider, which ignores the return value and instead
49 // waits for a gNMI Update over the Subscribe RPC.
50 return completedFuture(true);
51 }
52
53 @Override
54 public CompletableFuture<Boolean> disable(PortNumber number) {
Carmelo Casconec2be50a2019-04-10 00:15:39 -070055 if (!setupBehaviour("disable()")) {
56 return completedFuture(false);
57 }
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080058 doEnable(number, false);
59 return completedFuture(true);
60 }
61
62 @Override
63 public CompletableFuture<Boolean> isEnabled(PortNumber number) {
64 throw new UnsupportedOperationException("Not implemented");
65 }
66
67 private void doEnable(PortNumber portNumber, boolean enabled) {
68 if (portNumber.isLogical()) {
69 log.warn("Cannot update port status for logical port {} on {}",
70 portNumber, deviceId);
71 return;
72 }
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080073 final Gnmi.Path path = Gnmi.Path.newBuilder()
74 .addElem(Gnmi.PathElem.newBuilder().setName("interfaces").build())
75 .addElem(Gnmi.PathElem.newBuilder().setName("interface")
76 .putKey("name", portNumber.name()).build())
77 .addElem(Gnmi.PathElem.newBuilder().setName("config").build())
78 .addElem(Gnmi.PathElem.newBuilder().setName("enabled").build())
79 .build();
80 final Gnmi.TypedValue value = Gnmi.TypedValue.newBuilder()
81 .setBoolVal(enabled)
82 .build();
83 final Gnmi.SetRequest request = Gnmi.SetRequest.newBuilder()
84 .addUpdate(Gnmi.Update.newBuilder()
85 .setPath(path)
86 .setVal(value)
87 .build())
88 .build();
89
90 // Async submit request and forget about it. In case of errors, the
91 // client will log them. In case of success, we should receive a gNMI
92 // Update over the Subscribe RPC with the new oper status.
93 client.set(request);
94 }
95}