Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.drivers.gnmi; |
| 18 | |
| 19 | import gnmi.Gnmi; |
| 20 | import org.onosproject.gnmi.api.GnmiClient; |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 21 | import org.onosproject.gnmi.api.GnmiController; |
| 22 | import org.onosproject.grpc.utils.AbstractGrpcHandlerBehaviour; |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 23 | import org.onosproject.net.PortNumber; |
| 24 | import org.onosproject.net.behaviour.PortAdmin; |
| 25 | |
| 26 | import java.util.concurrent.CompletableFuture; |
| 27 | |
| 28 | import static java.util.concurrent.CompletableFuture.completedFuture; |
| 29 | |
| 30 | /** |
| 31 | * Implementation of PortAdmin for gNMI devices with OpenConfig support. |
| 32 | */ |
| 33 | public class OpenConfigGnmiPortAdminBehaviour |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 34 | extends AbstractGrpcHandlerBehaviour<GnmiClient, GnmiController> |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 35 | implements PortAdmin { |
| 36 | |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 37 | public OpenConfigGnmiPortAdminBehaviour() { |
| 38 | super(GnmiController.class); |
| 39 | } |
| 40 | |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 41 | @Override |
| 42 | public CompletableFuture<Boolean> enable(PortNumber number) { |
Carmelo Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 43 | if (!setupBehaviour("enable()")) { |
| 44 | return completedFuture(false); |
| 45 | } |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 46 | 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 Cascone | c2be50a | 2019-04-10 00:15:39 -0700 | [diff] [blame] | 55 | if (!setupBehaviour("disable()")) { |
| 56 | return completedFuture(false); |
| 57 | } |
Carmelo Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 58 | 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 Cascone | ab5d41e | 2019-03-06 18:02:34 -0800 | [diff] [blame] | 73 | 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 | } |