blob: 6ae4c331e84f2b7dd2c9e0d5424ac77146e39923 [file] [log] [blame]
Yi Tseng27851e32018-11-01 18:30:04 -07001/*
2 * Copyright 2017-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 com.google.common.collect.Lists;
20import com.google.common.collect.Maps;
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080021import com.google.common.util.concurrent.Futures;
Yi Tseng27851e32018-11-01 18:30:04 -070022import gnmi.Gnmi;
23import gnmi.Gnmi.GetRequest;
24import gnmi.Gnmi.GetResponse;
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080025import org.onlab.packet.ChassisId;
Carmelo Casconec2be50a2019-04-10 00:15:39 -070026import org.onosproject.gnmi.api.GnmiClient;
27import org.onosproject.gnmi.api.GnmiController;
28import org.onosproject.grpc.utils.AbstractGrpcHandlerBehaviour;
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080029import org.onosproject.net.AnnotationKeys;
Yi Tseng27851e32018-11-01 18:30:04 -070030import org.onosproject.net.DefaultAnnotations;
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080031import org.onosproject.net.Device;
Yi Tseng27851e32018-11-01 18:30:04 -070032import org.onosproject.net.PortNumber;
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080033import org.onosproject.net.device.DefaultDeviceDescription;
Yi Tseng27851e32018-11-01 18:30:04 -070034import org.onosproject.net.device.DefaultPortDescription;
35import org.onosproject.net.device.DeviceDescription;
36import org.onosproject.net.device.DeviceDescriptionDiscovery;
37import org.onosproject.net.device.PortDescription;
38import org.slf4j.Logger;
39import org.slf4j.LoggerFactory;
40
Yi Tseng27851e32018-11-01 18:30:04 -070041import java.util.Collections;
42import java.util.List;
43import java.util.Map;
Yi Tseng27851e32018-11-01 18:30:04 -070044
45import static gnmi.Gnmi.Path;
46import static gnmi.Gnmi.PathElem;
47import static gnmi.Gnmi.Update;
48
49/**
50 * Class that discovers the device description and ports of a device that
51 * supports the gNMI protocol and Openconfig models.
52 */
53public class OpenConfigGnmiDeviceDescriptionDiscovery
Carmelo Casconec2be50a2019-04-10 00:15:39 -070054 extends AbstractGrpcHandlerBehaviour<GnmiClient, GnmiController>
Yi Tseng27851e32018-11-01 18:30:04 -070055 implements DeviceDescriptionDiscovery {
56
Yi Tseng27851e32018-11-01 18:30:04 -070057 private static final Logger log = LoggerFactory
58 .getLogger(OpenConfigGnmiDeviceDescriptionDiscovery.class);
59
pierventred03459c2021-06-01 12:26:36 +020060 private static final String LAST_CHANGE = "last-change";
Yi Tsengca5cfe72022-02-07 11:42:12 -080061 private static final String SDK_PORT = "sdk-port";
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080062
63 private static final String UNKNOWN = "unknown";
Yi Tseng59d5f3e2018-11-27 23:09:41 -080064
pierventrec19294d2022-02-02 22:33:41 +010065 // FIXME temporary solution will be removed when the
66 // transition to p4rt translation is completed
67 public static boolean readPortId = false;
68
Carmelo Casconec2be50a2019-04-10 00:15:39 -070069 public OpenConfigGnmiDeviceDescriptionDiscovery() {
70 super(GnmiController.class);
71 }
72
Yi Tseng27851e32018-11-01 18:30:04 -070073 @Override
74 public DeviceDescription discoverDeviceDetails() {
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080075 return new DefaultDeviceDescription(
76 data().deviceId().uri(),
77 Device.Type.SWITCH,
78 data().driver().manufacturer(),
79 data().driver().hwVersion(),
80 data().driver().swVersion(),
81 UNKNOWN,
82 new ChassisId(),
83 true,
84 DefaultAnnotations.builder()
85 .set(AnnotationKeys.PROTOCOL, "gNMI")
86 .build());
Yi Tseng27851e32018-11-01 18:30:04 -070087 }
88
89 @Override
90 public List<PortDescription> discoverPortDetails() {
Carmelo Casconec32976e2019-04-08 14:50:52 -070091 if (!setupBehaviour("discoverPortDetails()")) {
Yi Tseng27851e32018-11-01 18:30:04 -070092 return Collections.emptyList();
93 }
94 log.debug("Discovering port details on device {}", handler().data().deviceId());
95
Carmelo Casconeab5d41e2019-03-06 18:02:34 -080096 final GetResponse response = Futures.getUnchecked(client.get(buildPortStateRequest()));
Yi Tseng27851e32018-11-01 18:30:04 -070097
Yi Tsengd7716482018-10-31 15:34:30 -070098 final Map<String, DefaultPortDescription.Builder> ports = Maps.newHashMap();
99 final Map<String, DefaultAnnotations.Builder> annotations = Maps.newHashMap();
pierventre374e2612022-01-31 13:14:46 +0100100 final Map<String, PortNumber> portIds = Maps.newHashMap();
Yi Tseng27851e32018-11-01 18:30:04 -0700101
102 // Creates port descriptions with port name and port number
103 response.getNotificationList()
Yi Tseng59d5f3e2018-11-27 23:09:41 -0800104 .forEach(notification -> {
Yi Tseng59d5f3e2018-11-27 23:09:41 -0800105 notification.getUpdateList().forEach(update -> {
106 // /interfaces/interface[name=ifName]/state/...
107 final String ifName = update.getPath().getElem(1)
108 .getKeyMap().get("name");
109 if (!ports.containsKey(ifName)) {
110 ports.put(ifName, DefaultPortDescription.builder());
111 annotations.put(ifName, DefaultAnnotations.builder());
112 }
113 final DefaultPortDescription.Builder builder = ports.get(ifName);
114 final DefaultAnnotations.Builder annotationsBuilder = annotations.get(ifName);
pierventre374e2612022-01-31 13:14:46 +0100115 parseInterfaceInfo(update, ifName, builder, annotationsBuilder, portIds);
Yi Tseng59d5f3e2018-11-27 23:09:41 -0800116 });
Yi Tseng27851e32018-11-01 18:30:04 -0700117 });
118
Yi Tsengd7716482018-10-31 15:34:30 -0700119 final List<PortDescription> portDescriptionList = Lists.newArrayList();
Yi Tseng27851e32018-11-01 18:30:04 -0700120 ports.forEach((key, value) -> {
pierventred03459c2021-06-01 12:26:36 +0200121 // For devices not providing last-change, we set it to 0
122 final DefaultAnnotations.Builder annotationsBuilder = annotations.get(key);
123 if (!annotationsBuilder.build().keys().contains(LAST_CHANGE)) {
124 annotationsBuilder.set(LAST_CHANGE, String.valueOf(0));
125 }
pierventrec19294d2022-02-02 22:33:41 +0100126 /* Override port number if read port-id is enabled
127 and /interfaces/interface/state/id is available */
128 if (readPortId && portIds.containsKey(key)) {
pierventre374e2612022-01-31 13:14:46 +0100129 value.withPortNumber(portIds.get(key));
130 }
Yi Tseng27851e32018-11-01 18:30:04 -0700131 DefaultAnnotations annotation = annotations.get(key).build();
132 portDescriptionList.add(value.annotations(annotation).build());
133 });
pierventre374e2612022-01-31 13:14:46 +0100134
Yi Tseng27851e32018-11-01 18:30:04 -0700135 return portDescriptionList;
136 }
137
138 private GetRequest buildPortStateRequest() {
139 Path path = Path.newBuilder()
140 .addElem(PathElem.newBuilder().setName("interfaces").build())
141 .addElem(PathElem.newBuilder().setName("interface").putKey("name", "...").build())
142 .addElem(PathElem.newBuilder().setName("state").build())
143 .build();
144 return GetRequest.newBuilder()
145 .addPath(path)
146 .setType(GetRequest.DataType.ALL)
147 .setEncoding(Gnmi.Encoding.PROTO)
148 .build();
149 }
150
151 /**
152 * Parses the interface information.
153 *
Yi Tsengd7716482018-10-31 15:34:30 -0700154 * @param update the update received
Yi Tseng27851e32018-11-01 18:30:04 -0700155 */
156 private void parseInterfaceInfo(Update update,
157 String ifName,
158 DefaultPortDescription.Builder builder,
pierventre374e2612022-01-31 13:14:46 +0100159 DefaultAnnotations.Builder annotationsBuilder,
160 Map<String, PortNumber> portIds) {
Yi Tseng27851e32018-11-01 18:30:04 -0700161
Yi Tsengd7716482018-10-31 15:34:30 -0700162 final Path path = update.getPath();
163 final List<PathElem> elems = path.getElemList();
164 final Gnmi.TypedValue val = update.getVal();
Yi Tseng27851e32018-11-01 18:30:04 -0700165 if (elems.size() == 4) {
pierventre374e2612022-01-31 13:14:46 +0100166 /* /interfaces/interface/state/ifindex
167 /interfaces/interface/state/oper-status
168 /interfaces/interface/state/last-change
169 /interfaces/interface/state/id */
Yi Tsengd7716482018-10-31 15:34:30 -0700170 final String pathElemName = elems.get(3).getName();
Yi Tseng27851e32018-11-01 18:30:04 -0700171 switch (pathElemName) {
172 case "ifindex": // port number
173 builder.withPortNumber(PortNumber.portNumber(val.getUintVal(), ifName));
Yi Tsengca5cfe72022-02-07 11:42:12 -0800174 annotationsBuilder.set(SDK_PORT, String.valueOf(val.getUintVal()));
Yi Tsengd7716482018-10-31 15:34:30 -0700175 return;
Yi Tseng27851e32018-11-01 18:30:04 -0700176 case "oper-status":
177 builder.isEnabled(parseOperStatus(val.getStringVal()));
pierventred03459c2021-06-01 12:26:36 +0200178 return;
179 case "last-change":
180 annotationsBuilder.set(LAST_CHANGE, String.valueOf(val.getUintVal()));
Yi Tsengd7716482018-10-31 15:34:30 -0700181 return;
pierventre374e2612022-01-31 13:14:46 +0100182 case "id":
183 /* Temporary stored in portIds and eventually substituted
184 when all updates have been processed. This is done because
185 there is no guarantee about the order of the updates delivery */
186 portIds.put(ifName, PortNumber.portNumber(val.getUintVal(), ifName));
187 return;
Yi Tseng27851e32018-11-01 18:30:04 -0700188 default:
Yi Tseng27851e32018-11-01 18:30:04 -0700189 break;
190 }
Yi Tsengd7716482018-10-31 15:34:30 -0700191 } else if (elems.size() == 5) {
Yi Tseng27851e32018-11-01 18:30:04 -0700192 // /interfaces/interface/ethernet/config/port-speed
Yi Tsengd7716482018-10-31 15:34:30 -0700193 final String pathElemName = elems.get(4).getName();
194 if (pathElemName.equals("port-speed")) {
195 builder.portSpeed(parsePortSpeed(val.getStringVal()));
196 return;
Yi Tseng27851e32018-11-01 18:30:04 -0700197 }
198 }
Yi Tsengd7716482018-10-31 15:34:30 -0700199 log.debug("Unknown path when parsing interface info: {}", path);
Yi Tseng27851e32018-11-01 18:30:04 -0700200 }
201
202 private boolean parseOperStatus(String operStatus) {
203 switch (operStatus) {
204 case "UP":
205 return true;
206 case "DOWN":
207 default:
208 return false;
209 }
210 }
211
212 private long parsePortSpeed(String speed) {
213 log.debug("Speed from config {}", speed);
214 switch (speed) {
215 case "SPEED_10MB":
216 return 10;
217 case "SPEED_100MB":
218 return 100;
219 case "SPEED_1GB":
220 return 1000;
221 case "SPEED_10GB":
222 return 10000;
223 case "SPEED_25GB":
224 return 25000;
225 case "SPEED_40GB":
226 return 40000;
227 case "SPEED_50GB":
228 return 50000;
229 case "SPEED_100GB":
230 return 100000;
231 default:
Yi Tsengd7716482018-10-31 15:34:30 -0700232 log.warn("Unrecognized port speed string '{}'", speed);
Yi Tseng27851e32018-11-01 18:30:04 -0700233 return 1000;
234 }
235 }
236}