blob: 7a01ff0f6cc5bcb9cf16ed9f3412394a741fdcb8 [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;
pierventrec8e8e452021-09-27 12:35:09 +020023import org.onosproject.net.Port;
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080024import org.onosproject.net.PortNumber;
25import org.onosproject.net.behaviour.PortAdmin;
26
27import java.util.concurrent.CompletableFuture;
28
29import static java.util.concurrent.CompletableFuture.completedFuture;
30
31/**
32 * Implementation of PortAdmin for gNMI devices with OpenConfig support.
33 */
34public class OpenConfigGnmiPortAdminBehaviour
Carmelo Casconec2be50a2019-04-10 00:15:39 -070035 extends AbstractGrpcHandlerBehaviour<GnmiClient, GnmiController>
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080036 implements PortAdmin {
37
Carmelo Casconec2be50a2019-04-10 00:15:39 -070038 public OpenConfigGnmiPortAdminBehaviour() {
39 super(GnmiController.class);
40 }
41
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080042 @Override
43 public CompletableFuture<Boolean> enable(PortNumber number) {
Carmelo Casconec2be50a2019-04-10 00:15:39 -070044 if (!setupBehaviour("enable()")) {
45 return completedFuture(false);
46 }
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080047 doEnable(number, true);
48 // Always returning true is OK assuming this is used only by the
49 // GeneralDeviceProvider, which ignores the return value and instead
50 // waits for a gNMI Update over the Subscribe RPC.
51 return completedFuture(true);
52 }
53
54 @Override
55 public CompletableFuture<Boolean> disable(PortNumber number) {
Carmelo Casconec2be50a2019-04-10 00:15:39 -070056 if (!setupBehaviour("disable()")) {
57 return completedFuture(false);
58 }
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080059 doEnable(number, false);
60 return completedFuture(true);
61 }
62
63 @Override
64 public CompletableFuture<Boolean> isEnabled(PortNumber number) {
65 throw new UnsupportedOperationException("Not implemented");
66 }
67
68 private void doEnable(PortNumber portNumber, boolean enabled) {
69 if (portNumber.isLogical()) {
70 log.warn("Cannot update port status for logical port {} on {}",
71 portNumber, deviceId);
72 return;
73 }
pierventrec8e8e452021-09-27 12:35:09 +020074
75 /* Requests coming from the north may come without name.
76 When this happens port name equals to port number */
77 if (!portNumber.hasName()) {
78 if (deviceService == null) {
79 log.warn("Cannot update port status of port {} on {} because the device " +
80 "service is null", portNumber, deviceId);
81 return;
82 }
83 Port devicePort = deviceService.getPort(deviceId, portNumber);
84 if (devicePort != null) {
85 // Some devices may reject the config, make sure we act on the reported port
86 portNumber = devicePort.number();
87 }
88 }
89
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080090 final Gnmi.Path path = Gnmi.Path.newBuilder()
91 .addElem(Gnmi.PathElem.newBuilder().setName("interfaces").build())
92 .addElem(Gnmi.PathElem.newBuilder().setName("interface")
93 .putKey("name", portNumber.name()).build())
94 .addElem(Gnmi.PathElem.newBuilder().setName("config").build())
95 .addElem(Gnmi.PathElem.newBuilder().setName("enabled").build())
96 .build();
97 final Gnmi.TypedValue value = Gnmi.TypedValue.newBuilder()
98 .setBoolVal(enabled)
99 .build();
100 final Gnmi.SetRequest request = Gnmi.SetRequest.newBuilder()
101 .addUpdate(Gnmi.Update.newBuilder()
102 .setPath(path)
103 .setVal(value)
104 .build())
105 .build();
106
107 // Async submit request and forget about it. In case of errors, the
108 // client will log them. In case of success, we should receive a gNMI
109 // Update over the Subscribe RPC with the new oper status.
110 client.set(request);
111 }
112}