blob: 4f050b0183242048ea41d270d656387f7b9eaf88 [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;
21import org.onosproject.net.PortNumber;
22import org.onosproject.net.behaviour.PortAdmin;
23
24import java.util.concurrent.CompletableFuture;
25
26import static java.util.concurrent.CompletableFuture.completedFuture;
27
28/**
29 * Implementation of PortAdmin for gNMI devices with OpenConfig support.
30 */
31public class OpenConfigGnmiPortAdminBehaviour
32 extends AbstractGnmiHandlerBehaviour
33 implements PortAdmin {
34
35 @Override
36 public CompletableFuture<Boolean> enable(PortNumber number) {
37 doEnable(number, true);
38 // Always returning true is OK assuming this is used only by the
39 // GeneralDeviceProvider, which ignores the return value and instead
40 // waits for a gNMI Update over the Subscribe RPC.
41 return completedFuture(true);
42 }
43
44 @Override
45 public CompletableFuture<Boolean> disable(PortNumber number) {
46 doEnable(number, false);
47 return completedFuture(true);
48 }
49
50 @Override
51 public CompletableFuture<Boolean> isEnabled(PortNumber number) {
52 throw new UnsupportedOperationException("Not implemented");
53 }
54
55 private void doEnable(PortNumber portNumber, boolean enabled) {
56 if (portNumber.isLogical()) {
57 log.warn("Cannot update port status for logical port {} on {}",
58 portNumber, deviceId);
59 return;
60 }
61 final GnmiClient client = getClientByKey();
62 if (client == null) {
63 log.warn("Cannot update ports on {}, missing gNMI client", deviceId);
64 return;
65 }
66 final Gnmi.Path path = Gnmi.Path.newBuilder()
67 .addElem(Gnmi.PathElem.newBuilder().setName("interfaces").build())
68 .addElem(Gnmi.PathElem.newBuilder().setName("interface")
69 .putKey("name", portNumber.name()).build())
70 .addElem(Gnmi.PathElem.newBuilder().setName("config").build())
71 .addElem(Gnmi.PathElem.newBuilder().setName("enabled").build())
72 .build();
73 final Gnmi.TypedValue value = Gnmi.TypedValue.newBuilder()
74 .setBoolVal(enabled)
75 .build();
76 final Gnmi.SetRequest request = Gnmi.SetRequest.newBuilder()
77 .addUpdate(Gnmi.Update.newBuilder()
78 .setPath(path)
79 .setVal(value)
80 .build())
81 .build();
82
83 // Async submit request and forget about it. In case of errors, the
84 // client will log them. In case of success, we should receive a gNMI
85 // Update over the Subscribe RPC with the new oper status.
86 client.set(request);
87 }
88}